📄 indexwriter.java
字号:
/** * <p>Determines the minimal number of delete terms required before the buffered * in-memory delete terms are applied and flushed. If there are documents * buffered in memory at the time, they are merged and a new segment is * created.</p> * <p>Disabled by default (writer flushes by RAM usage).</p> * * @throws IllegalArgumentException if maxBufferedDeleteTerms * is enabled but smaller than 1 * @see #setRAMBufferSizeMB */ public void setMaxBufferedDeleteTerms(int maxBufferedDeleteTerms) { ensureOpen(); if (maxBufferedDeleteTerms != DISABLE_AUTO_FLUSH && maxBufferedDeleteTerms < 1) throw new IllegalArgumentException( "maxBufferedDeleteTerms must at least be 1 when enabled"); docWriter.setMaxBufferedDeleteTerms(maxBufferedDeleteTerms); if (infoStream != null) message("setMaxBufferedDeleteTerms " + maxBufferedDeleteTerms); } /** * Returns the number of buffered deleted terms that will * trigger a flush if enabled. * @see #setMaxBufferedDeleteTerms */ public int getMaxBufferedDeleteTerms() { ensureOpen(); return docWriter.getMaxBufferedDeleteTerms(); } /** Determines how often segment indices are merged by addDocument(). With * smaller values, less RAM is used while indexing, and searches on * unoptimized indices are faster, but indexing speed is slower. With larger * values, more RAM is used during indexing, and while searches on unoptimized * indices are slower, indexing is faster. Thus larger values (> 10) are best * for batch index creation, and smaller values (< 10) for indices that are * interactively maintained. * * <p>Note that this method is a convenience method: it * just calls mergePolicy.setMergeFactor as long as * mergePolicy is an instance of {@link LogMergePolicy}. * Otherwise an IllegalArgumentException is thrown.</p> * * <p>This must never be less than 2. The default value is 10. */ public void setMergeFactor(int mergeFactor) { getLogMergePolicy().setMergeFactor(mergeFactor); } /** * <p>Returns the number of segments that are merged at * once and also controls the total number of segments * allowed to accumulate in the index.</p> * * <p>Note that this method is a convenience method: it * just calls mergePolicy.getMergeFactor as long as * mergePolicy is an instance of {@link LogMergePolicy}. * Otherwise an IllegalArgumentException is thrown.</p> * * @see #setMergeFactor */ public int getMergeFactor() { return getLogMergePolicy().getMergeFactor(); } /** * Expert: returns max delay inserted before syncing a * commit point. On Windows, at least, pausing before * syncing can increase net indexing throughput. The * delay is variable based on size of the segment's files, * and is only inserted when using * ConcurrentMergeScheduler for merges. * @deprecated This will be removed in 3.0, when * autoCommit=true is removed from IndexWriter. */ public double getMaxSyncPauseSeconds() { return maxSyncPauseSeconds; } /** * Expert: sets the max delay before syncing a commit * point. * @see #getMaxSyncPauseSeconds * @deprecated This will be removed in 3.0, when * autoCommit=true is removed from IndexWriter. */ public void setMaxSyncPauseSeconds(double seconds) { maxSyncPauseSeconds = seconds; } /** If non-null, this will be the default infoStream used * by a newly instantiated IndexWriter. * @see #setInfoStream */ public static void setDefaultInfoStream(PrintStream infoStream) { IndexWriter.defaultInfoStream = infoStream; } /** * Returns the current default infoStream for newly * instantiated IndexWriters. * @see #setDefaultInfoStream */ public static PrintStream getDefaultInfoStream() { return IndexWriter.defaultInfoStream; } /** If non-null, information about merges, deletes and a * message when maxFieldLength is reached will be printed * to this. */ public void setInfoStream(PrintStream infoStream) { ensureOpen(); setMessageID(infoStream); docWriter.setInfoStream(infoStream); deleter.setInfoStream(infoStream); if (infoStream != null) messageState(); } private void messageState() { message("setInfoStream: dir=" + directory + " autoCommit=" + autoCommit + " mergePolicy=" + mergePolicy + " mergeScheduler=" + mergeScheduler + " ramBufferSizeMB=" + docWriter.getRAMBufferSizeMB() + " maxBufferedDocs=" + docWriter.getMaxBufferedDocs() + " maxBuffereDeleteTerms=" + docWriter.getMaxBufferedDeleteTerms() + " maxFieldLength=" + maxFieldLength + " index=" + segString()); } /** * Returns the current infoStream in use by this writer. * @see #setInfoStream */ public PrintStream getInfoStream() { ensureOpen(); return infoStream; } /** * Sets the maximum time to wait for a write lock (in milliseconds) for this instance of IndexWriter. @see * @see #setDefaultWriteLockTimeout to change the default value for all instances of IndexWriter. */ public void setWriteLockTimeout(long writeLockTimeout) { ensureOpen(); this.writeLockTimeout = writeLockTimeout; } /** * Returns allowed timeout when acquiring the write lock. * @see #setWriteLockTimeout */ public long getWriteLockTimeout() { ensureOpen(); return writeLockTimeout; } /** * Sets the default (for any instance of IndexWriter) maximum time to wait for a write lock (in * milliseconds). */ public static void setDefaultWriteLockTimeout(long writeLockTimeout) { IndexWriter.WRITE_LOCK_TIMEOUT = writeLockTimeout; } /** * Returns default write lock timeout for newly * instantiated IndexWriters. * @see #setDefaultWriteLockTimeout */ public static long getDefaultWriteLockTimeout() { return IndexWriter.WRITE_LOCK_TIMEOUT; } /** * Commits all changes to an index and closes all * associated files. Note that this may be a costly * operation, so, try to re-use a single writer instead of * closing and opening a new one. See {@link #commit()} for * caveats about write caching done by some IO devices. * * <p> If an Exception is hit during close, eg due to disk * full or some other reason, then both the on-disk index * and the internal state of the IndexWriter instance will * be consistent. However, the close will not be complete * even though part of it (flushing buffered documents) * may have succeeded, so the write lock will still be * held.</p> * * <p> If you can correct the underlying cause (eg free up * some disk space) then you can call close() again. * Failing that, if you want to force the write lock to be * released (dangerous, because you may then lose buffered * docs in the IndexWriter instance) then you can do * something like this:</p> * * <pre> * try { * writer.close(); * } finally { * if (IndexWriter.isLocked(directory)) { * IndexWriter.unlock(directory); * } * } * </pre> * * after which, you must be certain not to use the writer * instance anymore.</p> * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public void close() throws CorruptIndexException, IOException { close(true); } /** * Closes the index with or without waiting for currently * running merges to finish. This is only meaningful when * using a MergeScheduler that runs merges in background * threads. * @param waitForMerges if true, this call will block * until all merges complete; else, it will ask all * running merges to abort, wait until those merges have * finished (which should be at most a few seconds), and * then return. */ public void close(boolean waitForMerges) throws CorruptIndexException, IOException { // If any methods have hit OutOfMemoryError, then abort // on close, in case the internal state of IndexWriter // or DocumentsWriter is corrupt if (hitOOM) { rollback(); return; } // Ensure that only one thread actually gets to do the closing: if (shouldClose()) closeInternal(waitForMerges); } // Returns true if this thread should attempt to close, or // false if IndexWriter is now closed; else, waits until // another thread finishes closing synchronized private boolean shouldClose() { while(true) { if (!closed) { if (!closing) { closing = true; return true; } else { // Another thread is presently trying to close; // wait until it finishes one way (closes // successfully) or another (fails to close) doWait(); } } else return false; } } private void closeInternal(boolean waitForMerges) throws CorruptIndexException, IOException { docWriter.pauseAllThreads(); try { if (infoStream != null) message("now flush at close"); docWriter.close(); // Only allow a new merge to be triggered if we are // going to wait for merges: flush(waitForMerges, true, true); if (waitForMerges) // Give merge scheduler last chance to run, in case // any pending merges are waiting: mergeScheduler.merge(this); mergePolicy.close(); finishMerges(waitForMerges); mergeScheduler.close(); if (infoStream != null) message("now call final commit()"); commit(0); if (infoStream != null) message("at close: " + segString()); synchronized(this) { docWriter = null; deleter.close(); } if (closeDir) directory.close(); if (writeLock != null) { writeLock.release(); // release write lock writeLock = null; } synchronized(this) { closed = true; } } catch (OutOfMemoryError oom) { hitOOM = true; throw oom; } finally { synchronized(this) { closing = false; notifyAll(); if (!closed) { if (docWriter != null) docWriter.resumeAllThreads(); if (infoStream != null) message("hit exception while closing"); } } } } /** Tells the docWriter to close its currently open shared * doc stores (stored fields & vectors files). * Return value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -