📄 indexwriter.java
字号:
// merges private HashSet mergingSegments = new HashSet(); private MergePolicy mergePolicy = new LogByteSizeMergePolicy(); private MergeScheduler mergeScheduler = new ConcurrentMergeScheduler(); private LinkedList pendingMerges = new LinkedList(); private Set runningMerges = new HashSet(); private List mergeExceptions = new ArrayList(); private long mergeGen; private boolean stopMerges; private int flushCount; private int flushDeletesCount; private double maxSyncPauseSeconds = DEFAULT_MAX_SYNC_PAUSE_SECONDS; // Used to only allow one addIndexes to proceed at once // TODO: use ReadWriteLock once we are on 5.0 private int readCount; // count of how many threads are holding read lock private Thread writeThread; // non-null if any thread holds write lock synchronized void acquireWrite() { while(writeThread != null || readCount > 0) doWait(); // We could have been closed while we were waiting: ensureOpen(); writeThread = Thread.currentThread(); } synchronized void releaseWrite() { assert Thread.currentThread() == writeThread; writeThread = null; notifyAll(); } synchronized void acquireRead() { final Thread current = Thread.currentThread(); while(writeThread != null && writeThread != current) doWait(); readCount++; } synchronized void releaseRead() { readCount--; assert readCount >= 0; if (0 == readCount) notifyAll(); } /** * Used internally to throw an {@link * AlreadyClosedException} if this IndexWriter has been * closed. * @throws AlreadyClosedException if this IndexWriter is */ protected synchronized final void ensureOpen(boolean includePendingClose) throws AlreadyClosedException { if (closed || (includePendingClose && closing)) { throw new AlreadyClosedException("this IndexWriter is closed"); } } protected synchronized final void ensureOpen() throws AlreadyClosedException { ensureOpen(true); } /** * Prints a message to the infoStream (if non-null), * prefixed with the identifying information for this * writer and the thread that's calling it. */ public void message(String message) { if (infoStream != null) infoStream.println("IW " + messageID + " [" + Thread.currentThread().getName() + "]: " + message); } private synchronized void setMessageID(PrintStream infoStream) { if (infoStream != null && messageID == -1) { synchronized(MESSAGE_ID_LOCK) { messageID = MESSAGE_ID++; } } this.infoStream = infoStream; } /** * Casts current mergePolicy to LogMergePolicy, and throws * an exception if the mergePolicy is not a LogMergePolicy. */ private LogMergePolicy getLogMergePolicy() { if (mergePolicy instanceof LogMergePolicy) return (LogMergePolicy) mergePolicy; else throw new IllegalArgumentException("this method can only be called when the merge policy is the default LogMergePolicy"); } /** <p>Get the current setting of whether newly flushed * segments will use the compound file format. Note that * this just returns the value previously set with * setUseCompoundFile(boolean), or the default value * (true). You cannot use this to query the status of * previously flushed segments.</p> * * <p>Note that this method is a convenience method: it * just calls mergePolicy.getUseCompoundFile as long as * mergePolicy is an instance of {@link LogMergePolicy}. * Otherwise an IllegalArgumentException is thrown.</p> * * @see #setUseCompoundFile(boolean) */ public boolean getUseCompoundFile() { return getLogMergePolicy().getUseCompoundFile(); } /** <p>Setting to turn on usage of a compound file. When on, * multiple files for each segment are merged into a * single file when a new segment is flushed.</p> * * <p>Note that this method is a convenience method: it * just calls mergePolicy.setUseCompoundFile as long as * mergePolicy is an instance of {@link LogMergePolicy}. * Otherwise an IllegalArgumentException is thrown.</p> */ public void setUseCompoundFile(boolean value) { getLogMergePolicy().setUseCompoundFile(value); getLogMergePolicy().setUseCompoundDocStore(value); } /** Expert: Set the Similarity implementation used by this IndexWriter. * * @see Similarity#setDefault(Similarity) */ public void setSimilarity(Similarity similarity) { ensureOpen(); this.similarity = similarity; docWriter.setSimilarity(similarity); } /** Expert: Return the Similarity implementation used by this IndexWriter. * * <p>This defaults to the current value of {@link Similarity#getDefault()}. */ public Similarity getSimilarity() { ensureOpen(); return this.similarity; } /** Expert: Set the interval between indexed terms. Large values cause less * memory to be used by IndexReader, but slow random-access to terms. Small * values cause more memory to be used by an IndexReader, and speed * random-access to terms. * * This parameter determines the amount of computation required per query * term, regardless of the number of documents that contain that term. In * particular, it is the maximum number of other terms that must be * scanned before a term is located and its frequency and position information * may be processed. In a large index with user-entered query terms, query * processing time is likely to be dominated not by term lookup but rather * by the processing of frequency and positional data. In a small index * or when many uncommon query terms are generated (e.g., by wildcard * queries) term lookup may become a dominant cost. * * In particular, <code>numUniqueTerms/interval</code> terms are read into * memory by an IndexReader, and, on average, <code>interval/2</code> terms * must be scanned for each random term access. * * @see #DEFAULT_TERM_INDEX_INTERVAL */ public void setTermIndexInterval(int interval) { ensureOpen(); this.termIndexInterval = interval; } /** Expert: Return the interval between indexed terms. * * @see #setTermIndexInterval(int) */ public int getTermIndexInterval() { // We pass false because this method is called by SegmentMerger while we are in the process of closing ensureOpen(false); return termIndexInterval; } /** * Constructs an IndexWriter for the index in <code>path</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>path</code>, replacing the index already there, * if any. * * <p><b>NOTE</b>: autoCommit (see <a * href="#autoCommit">above</a>) is set to false with this * constructor. * * @param path the path to the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified * via the MaxFieldLength constructor. * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(String path, Analyzer a, boolean create, MaxFieldLength mfl) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, create, true, null, false, mfl.getLimit()); } /** * Constructs an IndexWriter for the index in <code>path</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>path</code>, replacing the index already there, if any. * * @param path the path to the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error * @deprecated This constructor will be removed in the 3.0 release. * Use {@link * #IndexWriter(String,Analyzer,boolean,MaxFieldLength)} * instead, and call {@link #commit()} when needed. */ public IndexWriter(String path, Analyzer a, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, create, true, null, true, DEFAULT_MAX_FIELD_LENGTH); } /** * Constructs an IndexWriter for the index in <code>path</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>path</code>, replacing the index already there, if any. * * <p><b>NOTE</b>: autoCommit (see <a * href="#autoCommit">above</a>) is set to false with this * constructor. * * @param path the path to the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified * via the MaxFieldLength constructor. * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(File path, Analyzer a, boolean create, MaxFieldLength mfl) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, create, true, null, false, mfl.getLimit()); } /** * Constructs an IndexWriter for the index in <code>path</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>path</code>, replacing the index already there, if any. * * @param path the path to the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error * @deprecated This constructor will be removed in the 3.0 release. * Use {@link * #IndexWriter(File,Analyzer,boolean,MaxFieldLength)} * instead, and call {@link #commit()} when needed. */ public IndexWriter(File path, Analyzer a, boolean create) throws CorruptIndexException, LockObtainFailedException, IOException { init(FSDirectory.getDirectory(path), a, create, true, null, true, DEFAULT_MAX_FIELD_LENGTH); } /** * Constructs an IndexWriter for the index in <code>d</code>. * Text will be analyzed with <code>a</code>. If <code>create</code> * is true, then a new, empty index will be created in * <code>d</code>, replacing the index already there, if any. * * <p><b>NOTE</b>: autoCommit (see <a * href="#autoCommit">above</a>) is set to false with this * constructor. * * @param d the index directory * @param a the analyzer to use * @param create <code>true</code> to create the index or overwrite * the existing one; <code>false</code> to append to the existing * index * @param mfl Maximum field length: LIMITED, UNLIMITED, or user-specified * via the MaxFieldLength constructor. * @throws CorruptIndexException if the index is corrupt * @throws LockObtainFailedException if another writer * has this index open (<code>write.lock</code> could not * be obtained) * @throws IOException if the directory cannot be read/written to, or * if it does not exist and <code>create</code> is * <code>false</code> or if there is any other low-level * IO error */ public IndexWriter(Directory d, Analyzer a, boolean create, MaxFieldLength mfl) throws CorruptIndexException, LockObtainFailedException, IOException { init(d, a, create, false, null, false, mfl.getLimit()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -