src/main/java/ch/uzh/ifi/seal/ase/cscc/index/ParallelizedInvertedIndex.java
@Override
    public void indexDocument(IndexDocument doc) {
        // open a new db connection for each index task
        try {
            Connection dbConn = openSQLConnection();
            dbConn.setAutoCommit(false);
            if (ParallelizedInvertedIndex.this.isIndexed(dbConn, doc)) {
                // do not put identical documents in index twice
//            System.out.println("doc " + doc.getId() + " is already indexed");
                dbConn.close(); // doc is already in index, we're done here. close db connection.
                return;
            }
            serializeIndexDocument(dbConn, doc);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        executorService.execute(new Runnable() {
            private int attempts = 0;

            @Override
            public void run() {
                attempts++;
                if (attempts > 50) {
                    System.out.println("attempt: " + attempts + "\n" + this.toString());
                }
                try {

                    Directory indexDirectory = ParallelizedInvertedIndex.this.getIndexDirectory(doc);
                    IndexWriterConfig config = new IndexWriterConfig();
                    // CREATE_OR_APPEND creates a new index if one does not exist, otherwise it opens the index and documents will be appended
                    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
                    IndexWriter w;
                    if (filePathToIndexWriter.containsKey(indexDirectory)) {
                        w = filePathToIndexWriter.get(indexDirectory);
                        w.flush();
                        ParallelizedInvertedIndex.this.addDocToLuceneIndex(w, doc);
                        w.close();
                    } else {
                        w = new IndexWriter(indexDirectory, config);
                        filePathToIndexWriter.put(indexDirectory, w);
                        w.flush();
                        ParallelizedInvertedIndex.this.addDocToLuceneIndex(w, doc);
                        w.close();
                    }
                    System.out.println(attempts + ": successfully indexed " + doc.getId());
                } catch (LockObtainFailedException lockObtainFailedException) {
                    // there's a lock on the Lucene index
                    // Resubmit this task
                    executorService.execute(this);
                    System.out.println(attempts + ": LockObtainFailedException encountered. Keeping task in queue. docID = " + doc.getId());
                } catch (IOException e) {
                    e.printStackTrace();
                    executorService.shutdown();
                    System.exit(1); // exit on IOException
                } catch (Throwable t) {
                    LOGGER.severe("UNCAUGHT EXCEPTION");
                    t.printStackTrace();
                }
            }
        });
    }
/**
     * Puts an IndexDocument in the index.
     *
     * @param doc document to store in index
     */
Puts an IndexDocument in the index.
@param doc document to store in index
Puts an IndexDocument in the index . @param doc document to store in index
@Override
    public void indexDocument(IndexDocument doc) {
        try {
            Connection dbConn = openSQLConnection();
            dbConn.setAutoCommit(false);
            if (ParallelizedInvertedIndex.this.isIndexed(dbConn, doc)) {
                dbConn.close(); 
                return;
            }
            serializeIndexDocument(dbConn, doc);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        executorService.execute(new Runnable() {
            private int attempts = 0;
            @Override
            public void run() {
                attempts++;
                if (attempts > 50) {
                    System.out.println("attempt: " + attempts + "\n" + this.toString());
                }
                try {
                    Directory indexDirectory = ParallelizedInvertedIndex.this.getIndexDirectory(doc);
                    IndexWriterConfig config = new IndexWriterConfig();
                    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
                    IndexWriter w;
                    if (filePathToIndexWriter.containsKey(indexDirectory)) {
                        w = filePathToIndexWriter.get(indexDirectory);
                        w.flush();
                        ParallelizedInvertedIndex.this.addDocToLuceneIndex(w, doc);
                        w.close();
                    } else {
                        w = new IndexWriter(indexDirectory, config);
                        filePathToIndexWriter.put(indexDirectory, w);
                        w.flush();
                        ParallelizedInvertedIndex.this.addDocToLuceneIndex(w, doc);
                        w.close();
                    }
                    System.out.println(attempts + ": successfully indexed " + doc.getId());
                } catch (LockObtainFailedException lockObtainFailedException) {
                    executorService.execute(this);
                    System.out.println(attempts + ": LockObtainFailedException encountered. Keeping task in queue. docID = " + doc.getId());
                } catch (IOException e) {
                    e.printStackTrace();
                    executorService.shutdown();
                    System.exit(1); 
                } catch (Throwable t) {
                    LOGGER.severe("UNCAUGHT EXCEPTION");
                    t.printStackTrace();
                }
            }
        });
    }
@ Override public void indexDocument ( IndexDocument doc ) { try { Connection dbConn = openSQLConnection ( ) ; dbConn . setAutoCommit ( false ) ; if ( ParallelizedInvertedIndex . this . isIndexed ( dbConn , doc ) ) { dbConn . close ( ) ; return ; } serializeIndexDocument ( dbConn , doc ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( SQLException e ) { e . printStackTrace ( ) ; } executorService . execute ( new Runnable ( ) { private int attempts = 0 ; @ Override public void run ( ) { attempts ++ ; if ( attempts > 50 ) { System . out . println ( "attempt: " + attempts + "\n" + this . toString ( ) ) ; } try { Directory indexDirectory = ParallelizedInvertedIndex . this . getIndexDirectory ( doc ) ; IndexWriterConfig config = new IndexWriterConfig ( ) ; config . setOpenMode ( IndexWriterConfig . OpenMode . CREATE_OR_APPEND ) ; IndexWriter w ; if ( filePathToIndexWriter . containsKey ( indexDirectory ) ) { w = filePathToIndexWriter . get ( indexDirectory ) ; w . flush ( ) ; ParallelizedInvertedIndex . this . addDocToLuceneIndex ( w , doc ) ; w . close ( ) ; } else { w = new IndexWriter ( indexDirectory , config ) ; filePathToIndexWriter . put ( indexDirectory , w ) ; w . flush ( ) ; ParallelizedInvertedIndex . this . addDocToLuceneIndex ( w , doc ) ; w . close ( ) ; } System . out . println ( attempts + ": successfully indexed " + doc . getId ( ) ) ; } catch ( LockObtainFailedException lockObtainFailedException ) { executorService . execute ( this ) ; System . out . println ( attempts + ": LockObtainFailedException encountered. Keeping task in queue. docID = " + doc . getId ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; executorService . shutdown ( ) ; System . exit ( 1 ) ; } catch ( Throwable t ) { LOGGER . severe ( "UNCAUGHT EXCEPTION" ) ; t . printStackTrace ( ) ; } } } ) ; }
Puts an IndexDocument in the index.
Puts an IndexDocument in the index .
// open a new db connection for each index task // do not put identical documents in index twice //            System.out.println("doc " + doc.getId() + " is already indexed"); // doc is already in index, we're done here. close db connection. // CREATE_OR_APPEND creates a new index if one does not exist, otherwise it opens the index and documents will be appended // there's a lock on the Lucene index // Resubmit this task // exit on IOException