Methods Summary |
---|
protected synchronized void | addDocument(IndexDocument indexable)
if (!indexable.isInsert())
throw new GdataIndexerException(
"Index action must be set to insert");
setAction(indexable);
doWrite(indexable);
this.docsAdded.incrementAndGet();
|
public void | addIndexableDocumentTask(java.util.concurrent.Future task)Adds the given future task to the queue, and waits if the queue is full.
The queue size is set to 100 by default.
if (this.isDestroyed.get())
throw new IllegalStateException(
"Indexer has already been destroyed");
this.futurQueue.put(task);
|
protected void | closeSearcher()
try {
if (this.searcher != null)
this.searcher.close();
} finally {
this.searcher = null;
}
|
protected void | closeWriter()
try {
if (this.writer != null)
this.writer.close();
} finally {
this.writer = null;
}
|
protected synchronized void | commit(boolean optimize)This method commits all changes to the index and closes all open
resources (e.g. IndexWriter and IndexReader). This method notifies all
registered Commit listeners if invoked.
if (LOG.isInfoEnabled())
LOG.info("Commit called with optimize = " + optimize);
int changes = this.docsAdded.intValue() + this.docsDeleted.intValue()
+ this.docsUpdated.intValue();
/*
* don't call listeners to prevent unnecessary close / open of searchers
*/
if (changes == 0)
return;
this.committed.incrementAndGet();
if(optimize)
this.optimized.incrementAndGet();
doDeltete();
if (optimize) {
closeSearcher();
openWriter();
this.writer.optimize();
}
closeSearcher();
closeWriter();
this.docsAdded.set(0);
this.docsDeleted.set(0);
this.docsUpdated.set(0);
notifyCommitListeners(this.serviceConfiguration.getName());
|
public static synchronized org.apache.lucene.gdata.search.index.GDataIndexer | createGdataIndexer(org.apache.lucene.gdata.search.config.IndexSchema config, org.apache.lucene.store.Directory dir, boolean create)This factory method creates a new GDataIndexer using a instance of
{@link IndexTask}
GDataIndexer retVal = new GDataIndexer(config, dir, create);
retVal.setIndexTask(new IndexTask(retVal, retVal.futurQueue));
retVal.init();
return retVal;
|
public static synchronized org.apache.lucene.gdata.search.index.GDataIndexer | createTimedGdataIndexer(org.apache.lucene.gdata.search.config.IndexSchema config, org.apache.lucene.store.Directory dir, boolean create, long commitTimeout)This factory method creates a new GDataIndexer using a instance of
{@link TimedIndexTask}. This indexer will automatically commit the index
if no modification to the index occur for the given time. The used time
unit is {@link TimeUnit#SECONDS}. Values less than the default value
will be ignored. For the default value see {@link TimedIndexTask}.
GDataIndexer retVal = new GDataIndexer(config, dir, create);
retVal.setIndexTask(new TimedIndexTask(retVal, retVal.futurQueue,
commitTimeout));
retVal.init();
return retVal;
|
protected synchronized void | deleteDocument(IndexDocument indexable)
if (!indexable.isDelete())
throw new GdataIndexerException(
"Index action must be set to delete");
setAction(indexable);
this.docsDeleted.incrementAndGet();
|
protected synchronized void | destroy()
this.isDestroyed.set(true);
if (!this.indexTask.isStopped())
this.indexTask.stop();
this.futurQueue.add(new FinishingFuture());
this.indexTaskExecutor.shutdown();
closeWriter();
closeSearcher();
if (LOG.isInfoEnabled())
LOG.info("Destroying GdataIndexer for service -- "
+ this.serviceConfiguration.getName());
|
protected void | doDeltete()
if (this.action.size() == 0)
return;
if (LOG.isInfoEnabled())
LOG
.info("Deleting documents and duplicates from index, size of IndexDocuments "
+ this.action.size());
closeWriter();
openSearcher();
IndexReader reader = this.searcher.getIndexReader();
TermDocs termDocs = reader.termDocs();
for (Map.Entry<IndexDocument, Integer> entry : this.action.entrySet()) {
IndexDocument indexDocument = entry.getKey();
Integer docToKeep = entry.getValue();
// extend the array if needed
if (this.documentNumber == null
|| docToKeep > this.documentNumber.length)
this.documentNumber = new int[docToKeep];
for (int i = 0; i < this.documentNumber.length; i++) {
this.documentNumber[i] = -1;
}
/*
* get the term to find the document from the document itself
*/
termDocs.seek(indexDocument.getDeletealbe());
int pos = 0;
while (termDocs.next()) {
/*
* if this is a pure delete just delete it an continue
*/
if (docToKeep == 0) {
reader.deleteDocument(termDocs.doc());
continue;
}
int prev = this.documentNumber[pos];
this.documentNumber[pos] = termDocs.doc();
if (prev != -1) {
reader.deleteDocument(prev);
}
if (++pos >= docToKeep)
pos = 0;
}
}
/*
* clear the map after all documents are processed
*/
this.action.clear();
closeSearcher();
|
protected void | doWrite(IndexDocument document)
closeSearcher();
openWriter();
this.writer.addDocument(document.getWriteable());
|
protected void | init()
if (this.indexTask == null)
this.indexTask = new IndexTask(this, this.futurQueue);
this.indexTaskExecutor.execute(this.indexTask);
|
protected void | notifyCommitListeners(java.lang.String serviceId)
if (LOG.isInfoEnabled())
LOG.info("notify commit event listeners for service id: "
+ serviceId + " -- current size of registered listeners: "
+ this.listeners.size());
for (IndexEventListener listener : this.listeners) {
listener.commitCallBack(serviceId);
}
|
protected void | openSearcher()
if (this.searcher == null)
this.searcher = new IndexSearcher(this.dir);
|
protected void | openWriter()
openWriter(false);
|
private void | openWriter(boolean create)
if (this.writer == null)
this.writer = new GDataIndexWriter(this.dir, create,
this.serviceConfiguration);
|
public void | registerIndexEventListener(IndexEventListener listener)Registers a new IndexEventListener. All registered listeners will be
notified if the index has been committed.
if (listener == null || this.listeners.contains(listener))
return;
this.listeners.add(listener);
|
public void | removeIndexEventListener(IndexEventListener listener)Removes a registered IndexEventListener
if (listener == null || !this.listeners.contains(listener))
return;
this.listeners.remove(listener);
|
private void | setAction(IndexDocument doc)
Integer docCountToKeep = this.action.get(doc);
if (!doc.isDelete() && (docCountToKeep == null || docCountToKeep == 0)) {
/*
* add a ONE for ONE documents to keep for this IndexDocument when
* doDelete. doDelete will keep the latest added document and
* deletes all other documents for this IndexDocument e.g. all
* duplicates
*/
this.action.put(doc, ONE);
} else if (doc.isDelete()
&& (docCountToKeep == null || docCountToKeep > 0)) {
/*
* add a zero for zero documents to keep for this IndexDocument when
* doDelete
*/
this.action.put(doc, ZERO);
}
|
protected void | setIndexTask(IndexTask task)
if (task != null && this.indexTask == null)
this.indexTask = task;
|
protected synchronized void | updateDocument(IndexDocument indexable)
if (!indexable.isUpdate())
throw new GdataIndexerException(
"Index action must be set to update");
setAction(indexable);
doWrite(indexable);
this.docsUpdated.incrementAndGet();
|