📄 gdataindexer.java
字号:
} /** * Removes a registered IndexEventListener * * @param listener - * the listener to remove */ public void removeIndexEventListener(IndexEventListener listener) { if (listener == null || !this.listeners.contains(listener)) return; this.listeners.remove(listener); } protected void notifyCommitListeners(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 closeWriter() throws IOException { try { if (this.writer != null) this.writer.close(); } finally { this.writer = null; } } protected void closeSearcher() throws IOException { try { if (this.searcher != null) this.searcher.close(); } finally { this.searcher = null; } } protected void openSearcher() throws IOException { if (this.searcher == null) this.searcher = new IndexSearcher(this.dir); } protected void openWriter() throws IOException { openWriter(false); } private void openWriter(boolean create) throws IOException { if (this.writer == null) this.writer = new GDataIndexWriter(this.dir, create, this.serviceConfiguration); } /* * This should only be called in a synchronized block */ protected void doWrite(IndexDocument document) throws IOException { closeSearcher(); openWriter(); this.writer.addDocument(document.getWriteable()); } // only access synchronized int[] documentNumber; /* * This should only be called in a synchronized block */ protected void doDeltete() throws IOException { 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 synchronized void destroy() throws IOException { 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()); } /** * This factory method creates a new GDataIndexer using a instance of * {@link IndexTask} * * @param config - * the config to be used to configure the indexer * @param dir - * the directory to index to * @param create - * <code>true</code> to create a new index, <code>false</code> * to use the existing one. * @return - a new GDataIndexer instance * @throws IOException - * if an IOException occurs while initializing the indexer */ public static synchronized GDataIndexer createGdataIndexer( final IndexSchema config, Directory dir, boolean create) throws IOException { GDataIndexer retVal = new GDataIndexer(config, dir, create); retVal.setIndexTask(new IndexTask(retVal, retVal.futurQueue)); retVal.init(); return retVal; } /** * 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}. * * @param config - * the config to be used to configure the indexer * @param dir - * the directory to index to * @param create - * <code>true</code> to create a new index, <code>false</code> * to use the existing one. * @param commitTimeout - * the amount of seconds to wait until a commit should be * scheduled * @return - a new GDataIndexer instance * @throws IOException - * if an IOException occurs while initializing the indexer */ public static synchronized GDataIndexer createTimedGdataIndexer( final IndexSchema config, Directory dir, boolean create, long commitTimeout) throws IOException { GDataIndexer retVal = new GDataIndexer(config, dir, create); retVal.setIndexTask(new TimedIndexTask(retVal, retVal.futurQueue, commitTimeout)); retVal.init(); return retVal; } @SuppressWarnings("unused") static final class FinishingFuture implements Future<IndexDocument> { /** * @see java.util.concurrent.Future#cancel(boolean) */ public boolean cancel(boolean arg0) { return false; } /** * @see java.util.concurrent.Future#isCancelled() */ public boolean isCancelled() { return false; } /** * @see java.util.concurrent.Future#isDone() */ public boolean isDone() { return true; } /** * @see java.util.concurrent.Future#get() */ @SuppressWarnings("unused") public IndexDocument get() throws InterruptedException, ExecutionException { return null; } /** * @see java.util.concurrent.Future#get(long, * java.util.concurrent.TimeUnit) */ @SuppressWarnings("unused") public IndexDocument get(long arg0, TimeUnit arg1) throws InterruptedException, ExecutionException, TimeoutException { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -