Revision 2730
Added by sgarg about 19 years ago
src/edu/ucsb/nceas/metacat/DBSAXHandler.java | ||
---|---|---|
54 | 54 |
* call when processing the XML stream and generating events. |
55 | 55 |
*/ |
56 | 56 |
public class DBSAXHandler extends DefaultHandler implements LexicalHandler, |
57 |
DeclHandler, Runnable
|
|
57 |
DeclHandler |
|
58 | 58 |
{ |
59 | 59 |
|
60 | 60 |
protected boolean atFirstElement; |
... | ... | |
97 | 97 |
|
98 | 98 |
protected String pub = null; |
99 | 99 |
|
100 |
protected Thread xmlIndex; |
|
101 |
|
|
102 | 100 |
private boolean endDocument = false; |
103 | 101 |
|
104 | 102 |
protected int serverCode = 1; |
... | ... | |
116 | 114 |
protected static final int MAXDATACHARS = 4000; |
117 | 115 |
|
118 | 116 |
//protected static final int MAXDATACHARS = 50; |
119 |
protected static final long INDEXDELAY = 10000; |
|
120 | 117 |
|
121 | 118 |
// methods writeChildNodeToDB, setAttribute, setNamespace, |
122 | 119 |
// writeTextForDBSAXNode will increase endNodeId. |
... | ... | |
215 | 212 |
this.groups = groups; |
216 | 213 |
this.pub = pub; |
217 | 214 |
this.serverCode = serverCode; |
218 |
this.xmlIndex = new Thread(this); |
|
219 | 215 |
} |
220 | 216 |
|
221 | 217 |
/** SAX Handler that receives notification of beginning of the document */ |
... | ... | |
440 | 436 |
hasTriple = true; |
441 | 437 |
currentTriple = new Triple(); |
442 | 438 |
} |
443 |
} |
|
439 |
}
|
|
444 | 440 |
|
445 |
public void runIndexingThread(){ |
|
446 |
|
|
447 |
boolean useXMLIndex = |
|
448 |
(new Boolean(MetaCatUtil.getOption("usexmlindex"))).booleanValue(); |
|
449 |
if (useXMLIndex && !isRevisionDoc) { |
|
450 |
try { |
|
451 |
xmlIndex.start(); |
|
452 |
} catch (NullPointerException e) { |
|
453 |
xmlIndex = null; |
|
454 |
logMetacat.error("Error in DBSAXHandler.runIndexingThread() " |
|
455 |
+ e.getMessage()); |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
|
|
460 |
/* |
|
461 |
* Run a separate thread to build the XML index for this document. This |
|
462 |
* thread is run asynchronously in order to more quickly return control to |
|
463 |
* the submitting user. The run method checks to see if the document has |
|
464 |
* been fully inserted before trying to update the xml_index table. |
|
465 |
*/ |
|
466 |
public void run() |
|
467 |
{ |
|
468 |
try { |
|
469 |
if (!isRevisionDoc) |
|
470 |
{ |
|
471 |
// stop 5 second |
|
472 |
Thread.sleep(5000); |
|
473 |
//make sure record is done |
|
474 |
checkDocumentTable(); |
|
475 |
// Build the index for this document |
|
476 |
currentDocument.buildIndex(); |
|
477 |
} |
|
478 |
} catch (Exception e) { |
|
479 |
logMetacat.error("Error in DBSAXHandler.run " |
|
480 |
+ e.getMessage()); |
|
481 |
} |
|
482 |
} |
|
483 |
|
|
484 |
/* |
|
485 |
* method to make sure insert is finished before create index table If new |
|
486 |
* version of record is in xml_documents every thing will be fine |
|
487 |
*/ |
|
488 |
private void checkDocumentTable() throws Exception |
|
489 |
{ |
|
490 |
|
|
491 |
DBConnection dbConn = null; |
|
492 |
int serialNumber = -1; |
|
493 |
|
|
494 |
try { |
|
495 |
// Opening separate db connection for writing XML Index |
|
496 |
dbConn = DBConnectionPool |
|
497 |
.getDBConnection("DBSAXHandler.checkDocumentTable"); |
|
498 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
|
499 |
|
|
500 |
// the following while loop construct checks to make sure that |
|
501 |
// the docid of the document that we are trying to index is already |
|
502 |
// in the xml_documents table. if this is not the case, the foreign |
|
503 |
// key relationship between xml_documents and xml_index is |
|
504 |
// temporarily broken causing multiple problems. |
|
505 |
boolean inxmldoc = false; |
|
506 |
long startTime = System.currentTimeMillis(); |
|
507 |
while (!inxmldoc) { |
|
508 |
String xmlDocumentsCheck = "select distinct docid from xml_documents" |
|
509 |
+ " where docid ='" |
|
510 |
+ docid |
|
511 |
+ "' and " |
|
512 |
+ " rev ='" |
|
513 |
+ revision + "'"; |
|
514 |
|
|
515 |
PreparedStatement xmlDocCheck = dbConn |
|
516 |
.prepareStatement(xmlDocumentsCheck); |
|
517 |
// Increase usage count |
|
518 |
dbConn.increaseUsageCount(1); |
|
519 |
xmlDocCheck.execute(); |
|
520 |
ResultSet doccheckRS = xmlDocCheck.getResultSet(); |
|
521 |
boolean tableHasRows = doccheckRS.next(); |
|
522 |
if (tableHasRows) { |
|
523 |
logMetacat.info( |
|
524 |
"=========== found the correct document"); |
|
525 |
inxmldoc = true; |
|
526 |
} |
|
527 |
doccheckRS.close(); |
|
528 |
xmlDocCheck.close(); |
|
529 |
// make sure the while loop will be ended in reseaonable time |
|
530 |
long stopTime = System.currentTimeMillis(); |
|
531 |
if ((stopTime - startTime) > INDEXDELAY) { |
|
532 |
logMetacat.warn("Couldn't find the docid for index build in " |
|
533 |
+ "reseaonable time!"); |
|
534 |
throw new Exception( |
|
535 |
"Couldn't find the docid for index build in " |
|
536 |
+ "reseaonable time!"); |
|
537 |
} |
|
538 |
}//while |
|
539 |
} catch (Exception e) { |
|
540 |
try { |
|
541 |
dbConn.rollback(); |
|
542 |
//dbconn.close(); |
|
543 |
} catch (SQLException sqle) { |
|
544 |
} |
|
545 |
logMetacat.error("Error in DBSAXHandler.checkDocumentTable " |
|
546 |
+ e.getMessage()); |
|
547 |
|
|
548 |
} finally { |
|
549 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
550 |
}//finally |
|
551 |
|
|
552 |
} |
|
553 |
|
|
554 | 441 |
/** SAX Handler that is called for each XML text node */ |
555 | 442 |
public void characters(char[] cbuf, int start, int len) throws SAXException |
556 | 443 |
{ |
Also available in: Unified diff
removed the indexing code from DBSAXHandler