Apache Lucene open-source search software
LUCENE-2858, LUCENE-3716: Move all commit/version/indexExists/reopen/open stuff to DirectoryReader, IR/AIR/CIR is now only an abstract index to search on, nothing more. In the future we might create an interface Reopenable that has all methods. For now SearcherManager & Co. simply check for instanceof DirectoryReader
Uwe Schindler66f17c3bab76
diff --git a/lucene/src/java/org/apache/lucene/index/BaseMultiReader.java b/lucene/src/java/org/apache/lucene/index/BaseMultiReader.javaindex bb359dbb7da..8d5fbe1bf88 100644--- a/lucene/src/java/org/apache/lucene/index/BaseMultiReader.java+++ b/lucene/src/java/org/apache/lucene/index/BaseMultiReader.java@@ -52,9 +52,6 @@ abstract class BaseMultiReader<R extends IndexReader> extends CompositeIndexReadtopLevelContext = (CompositeReaderContext) ReaderUtil.buildReaderContext(this);}- @Override- protected abstract CompositeIndexReader doOpenIfChanged() throws CorruptIndexException, IOException;-@Overridepublic Fields getTermVectors(int docID) throws IOException {ensureOpen();diff --git a/lucene/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/src/java/org/apache/lucene/index/CheckIndex.javaindex efac32a9c5c..bb8eb7dc66e 100644--- a/lucene/src/java/org/apache/lucene/index/CheckIndex.java+++ b/lucene/src/java/org/apache/lucene/index/CheckIndex.java@@ -538,7 +538,7 @@ public class CheckIndex {}if (infoStream != null)infoStream.print(" test: open reader.........");- reader = new SegmentReader(info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR, IOContext.DEFAULT);+ reader = new SegmentReader(info, DirectoryReader.DEFAULT_TERMS_INDEX_DIVISOR, IOContext.DEFAULT);segInfoStat.openReaderPassed = true;diff --git a/lucene/src/java/org/apache/lucene/index/CompositeIndexReader.java b/lucene/src/java/org/apache/lucene/index/CompositeIndexReader.javaindex cdbc9a324ff..b208845194e 100644--- a/lucene/src/java/org/apache/lucene/index/CompositeIndexReader.java+++ b/lucene/src/java/org/apache/lucene/index/CompositeIndexReader.java@@ -92,161 +92,6 @@ public abstract class CompositeIndexReader extends IndexReader {@Overridepublic abstract CompositeReaderContext getTopReaderContext();- /**- * If the index has changed since it was opened, open and return a new reader;- * else, return {@code null}.- *- * @see #openIfChanged(IndexReader)- */- protected CompositeIndexReader doOpenIfChanged() throws CorruptIndexException, IOException {- throw new UnsupportedOperationException("This reader does not support reopen().");- }-- /**- * If the index has changed since it was opened, open and return a new reader;- * else, return {@code null}.- *- * @see #openIfChanged(IndexReader, IndexCommit)- */- protected CompositeIndexReader doOpenIfChanged(final IndexCommit commit) throws CorruptIndexException, IOException {- throw new UnsupportedOperationException("This reader does not support reopen(IndexCommit).");- }-- /**- * If the index has changed since it was opened, open and return a new reader;- * else, return {@code null}.- *- * @see #openIfChanged(IndexReader, IndexWriter, boolean)- */- protected CompositeIndexReader doOpenIfChanged(IndexWriter writer, boolean applyAllDeletes) throws CorruptIndexException, IOException {- return writer.getReader(applyAllDeletes);- }-- /**- * Version number when this IndexReader was opened. Not- * implemented in the IndexReader base class.- *- * <p>If this reader is based on a Directory (ie, was- * created by calling {@link #open}, or {@link #openIfChanged} on- * a reader based on a Directory), then this method- * returns the version recorded in the commit that the- * reader opened. This version is advanced every time- * {@link IndexWriter#commit} is called.</p>- *- * <p>If instead this reader is a near real-time reader- * (ie, obtained by a call to {@link- * IndexWriter#getReader}, or by calling {@link #openIfChanged}- * on a near real-time reader), then this method returns- * the version of the last commit done by the writer.- * Note that even as further changes are made with the- * writer, the version will not changed until a commit is- * completed. Thus, you should not rely on this method to- * determine when a near real-time reader should be- * opened. Use {@link #isCurrent} instead.</p>- */- public abstract long getVersion();-- /**- * Check whether any new changes have occurred to the- * index since this reader was opened.- *- * <p>If this reader is based on a Directory (ie, was- * created by calling {@link #open}, or {@link #openIfChanged} on- * a reader based on a Directory), then this method checks- * if any further commits (see {@link IndexWriter#commit}- * have occurred in that directory).</p>- *- * <p>If instead this reader is a near real-time reader- * (ie, obtained by a call to {@link- * IndexWriter#getReader}, or by calling {@link #openIfChanged}- * on a near real-time reader), then this method checks if- * either a new commit has occurred, or any new- * uncommitted changes have taken place via the writer.- * Note that even if the writer has only performed- * merging, this method will still return false.</p>- *- * <p>In any event, if this returns false, you should call- * {@link #openIfChanged} to get a new reader that sees the- * changes.</p>- *- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- * @throws UnsupportedOperationException unless overridden in subclass- */- public abstract boolean isCurrent() throws CorruptIndexException, IOException;-- /**- * Returns the time the index in the named directory was last modified.- * Do not use this to check whether the reader is still up-to-date, use- * {@link #isCurrent()} instead.- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- */- public static long lastModified(final Directory directory) throws CorruptIndexException, IOException {- return ((Long) new SegmentInfos.FindSegmentsFile(directory) {- @Override- public Object doBody(String segmentFileName) throws IOException {- return Long.valueOf(directory.fileModified(segmentFileName));- }- }.run()).longValue();- }-- /**- * Reads version number from segments files. The version number is- * initialized with a timestamp and then increased by one for each change of- * the index.- *- * @param directory where the index resides.- * @return version number.- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- */- public static long getCurrentVersion(Directory directory) throws CorruptIndexException, IOException {- return SegmentInfos.readCurrentVersion(directory);- }-- /**- * Reads commitUserData, previously passed to {@link- * IndexWriter#commit(Map)}, from current index- * segments file. This will return null if {@link- * IndexWriter#commit(Map)} has never been called for- * this index.- *- * @param directory where the index resides.- * @return commit userData.- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- *- * @see #getCommitUserData()- */- public static Map<String, String> getCommitUserData(Directory directory) throws CorruptIndexException, IOException {- return SegmentInfos.readCurrentUserData(directory);- }-- /**- * Retrieve the String userData optionally passed to- * IndexWriter#commit. This will return null if {@link- * IndexWriter#commit(Map)} has never been called for- * this index.- *- * @see #getCommitUserData(Directory)- */- public Map<String,String> getCommitUserData() {- throw new UnsupportedOperationException("This reader does not support this method.");- }-- /**- * Expert: return the IndexCommit that this reader has- * opened. This method is only implemented by those- * readers that correspond to a Directory with its own- * segments_N file.- *- * @lucene.experimental- */- public IndexCommit getIndexCommit() throws IOException {- throw new UnsupportedOperationException("This reader does not support this method.");- }-/** Expert: returns the sequential sub readers that this* reader is logically composed of. If this reader is not composed* of sequential child readers, it should return null.@@ -256,15 +101,6 @@ public abstract class CompositeIndexReader extends IndexReader {*/public abstract IndexReader[] getSequentialSubReaders();- /** For IndexReader implementations that use- * TermInfosReader to read terms, this returns the- * current indexDivisor as specified when the reader was- * opened.- */- public int getTermInfosIndexDivisor() {- throw new UnsupportedOperationException("This reader does not support this method.");- }-/*** {@link ReaderContext} for {@link CompositeIndexReader} instance.* @lucene.experimentaldiff --git a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java b/lucene/src/java/org/apache/lucene/index/DirectoryReader.javaindex 282c27ac236..25189e62f78 100644--- a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java+++ b/lucene/src/java/org/apache/lucene/index/DirectoryReader.java@@ -32,15 +32,101 @@ import org.apache.lucene.util.IOUtils;/*** An IndexReader which reads indexes with multiple segments.- * To get an instance of this reader use {@link IndexReader.open(Directory)}.+ * To get an instance of this reader use {@link #open(Directory)}.*/public final class DirectoryReader extends BaseMultiReader<SegmentReader> {+ static int DEFAULT_TERMS_INDEX_DIVISOR = 1;+protected final Directory directory;private final IndexWriter writer;private final SegmentInfos segmentInfos;private final int termInfosIndexDivisor;private final boolean applyAllDeletes;+ /** Returns a IndexReader reading the index in the given+ * Directory+ * @param directory the index directory+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ */+ public static DirectoryReader open(final Directory directory) throws CorruptIndexException, IOException {+ return open(directory, null, DEFAULT_TERMS_INDEX_DIVISOR);+ }++ /** Expert: Returns a IndexReader reading the index in the given+ * Directory with the given termInfosIndexDivisor.+ * @param directory the index directory+ * @param termInfosIndexDivisor Subsamples which indexed+ * terms are loaded into RAM. This has the same effect as {@link+ * IndexWriterConfig#setTermIndexInterval} except that setting+ * must be done at indexing time while this setting can be+ * set per reader. When set to N, then one in every+ * N*termIndexInterval terms in the index is loaded into+ * memory. By setting this to a value > 1 you can reduce+ * memory usage, at the expense of higher latency when+ * loading a TermInfo. The default value is 1. Set this+ * to -1 to skip loading the terms index entirely.+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ */+ public static DirectoryReader open(final Directory directory, int termInfosIndexDivisor) throws CorruptIndexException, IOException {+ return open(directory, null, termInfosIndexDivisor);+ }++ /**+ * Open a near real time IndexReader from the {@link org.apache.lucene.index.IndexWriter}.+ *+ * @param writer The IndexWriter to open from+ * @param applyAllDeletes If true, all buffered deletes will+ * be applied (made visible) in the returned reader. If+ * false, the deletes are not applied but remain buffered+ * (in IndexWriter) so that they will be applied in the+ * future. Applying deletes can be costly, so if your app+ * can tolerate deleted documents being returned you might+ * gain some performance by passing false.+ * @return The new IndexReader+ * @throws CorruptIndexException+ * @throws IOException if there is a low-level IO error+ *+ * @see #openIfChanged(IndexReader,IndexWriter,boolean)+ *+ * @lucene.experimental+ */+ public static DirectoryReader open(final IndexWriter writer, boolean applyAllDeletes) throws CorruptIndexException, IOException {+ return writer.getReader(applyAllDeletes);+ }++ /** Expert: returns an IndexReader reading the index in the given+ * {@link IndexCommit}.+ * @param commit the commit point to open+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ */+ public static DirectoryReader open(final IndexCommit commit) throws CorruptIndexException, IOException {+ return open(commit.getDirectory(), commit, DEFAULT_TERMS_INDEX_DIVISOR);+ }+++ /** Expert: returns an IndexReader reading the index in the given+ * {@link IndexCommit} and termInfosIndexDivisor.+ * @param commit the commit point to open+ * @param termInfosIndexDivisor Subsamples which indexed+ * terms are loaded into RAM. This has the same effect as {@link+ * IndexWriterConfig#setTermIndexInterval} except that setting+ * must be done at indexing time while this setting can be+ * set per reader. When set to N, then one in every+ * N*termIndexInterval terms in the index is loaded into+ * memory. By setting this to a value > 1 you can reduce+ * memory usage, at the expense of higher latency when+ * loading a TermInfo. The default value is 1. Set this+ * to -1 to skip loading the terms index entirely.+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ */+ public static DirectoryReader open(final IndexCommit commit, int termInfosIndexDivisor) throws CorruptIndexException, IOException {+ return open(commit.getDirectory(), commit, termInfosIndexDivisor);+ }+DirectoryReader(SegmentReader[] readers, Directory directory, IndexWriter writer,SegmentInfos sis, int termInfosIndexDivisor, boolean applyAllDeletes) throws IOException {super(readers);@@ -51,7 +137,7 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {this.applyAllDeletes = applyAllDeletes;}- static DirectoryReader open(final Directory directory, final IndexCommit commit,+ private static DirectoryReader open(final Directory directory, final IndexCommit commit,final int termInfosIndexDivisor) throws CorruptIndexException, IOException {return (DirectoryReader) new SegmentInfos.FindSegmentsFile(directory) {@Override@@ -117,7 +203,7 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {}/** This constructor is only used for {@link #doOpenIfChanged()} */- static DirectoryReader open(Directory directory, IndexWriter writer, SegmentInfos infos, SegmentReader[] oldReaders,+ private static DirectoryReader open(Directory directory, IndexWriter writer, SegmentInfos infos, SegmentReader[] oldReaders,int termInfosIndexDivisor) throws IOException {// we put the old SegmentReaders in a map, that allows us// to lookup a reader using its segment name@@ -201,6 +287,116 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {infos, termInfosIndexDivisor, false);}+ /**+ * If the index has changed since the provided reader was+ * opened, open and return a new reader; else, return+ * null. The new reader, if not null, will be the same+ * type of reader as the previous one, ie an NRT reader+ * will open a new NRT reader, a MultiReader will open a+ * new MultiReader, etc.+ *+ * <p>This method is typically far less costly than opening a+ * fully new <code>IndexReader</code> as it shares+ * resources (for example sub-readers) with the provided+ * <code>IndexReader</code>, when possible.+ *+ * <p>The provided reader is not closed (you are responsible+ * for doing so); if a new reader is returned you also+ * must eventually close it. Be sure to never close a+ * reader while other threads are still using it; see+ * {@link SearcherManager} to simplify managing this.+ *+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ * @return null if there are no changes; else, a new+ * IndexReader instance which you must eventually close+ */+ public static DirectoryReader openIfChanged(DirectoryReader oldReader) throws IOException {+ final DirectoryReader newReader = oldReader.doOpenIfChanged();+ assert newReader != oldReader;+ return newReader;+ }++ /**+ * If the IndexCommit differs from what the+ * provided reader is searching, open and return a new+ * reader; else, return null.+ *+ * @see #openIfChanged(IndexReader)+ */+ public static DirectoryReader openIfChanged(DirectoryReader oldReader, IndexCommit commit) throws IOException {+ final DirectoryReader newReader = oldReader.doOpenIfChanged(commit);+ assert newReader != oldReader;+ return newReader;+ }++ /**+ * Expert: If there changes (committed or not) in the+ * {@link IndexWriter} versus what the provided reader is+ * searching, then open and return a new+ * IndexReader searching both committed and uncommitted+ * changes from the writer; else, return null (though, the+ * current implementation never returns null).+ *+ * <p>This provides "near real-time" searching, in that+ * changes made during an {@link IndexWriter} session can be+ * quickly made available for searching without closing+ * the writer nor calling {@link IndexWriter#commit}.+ *+ * <p>It's <i>near</i> real-time because there is no hard+ * guarantee on how quickly you can get a new reader after+ * making changes with IndexWriter. You'll have to+ * experiment in your situation to determine if it's+ * fast enough. As this is a new and experimental+ * feature, please report back on your findings so we can+ * learn, improve and iterate.</p>+ *+ * <p>The very first time this method is called, this+ * writer instance will make every effort to pool the+ * readers that it opens for doing merges, applying+ * deletes, etc. This means additional resources (RAM,+ * file descriptors, CPU time) will be consumed.</p>+ *+ * <p>For lower latency on reopening a reader, you should+ * call {@link IndexWriterConfig#setMergedSegmentWarmer} to+ * pre-warm a newly merged segment before it's committed+ * to the index. This is important for minimizing+ * index-to-search delay after a large merge. </p>+ *+ * <p>If an addIndexes* call is running in another thread,+ * then this reader will only search those segments from+ * the foreign index that have been successfully copied+ * over, so far.</p>+ *+ * <p><b>NOTE</b>: Once the writer is closed, any+ * outstanding readers may continue to be used. However,+ * if you attempt to reopen any of those readers, you'll+ * hit an {@link AlreadyClosedException}.</p>+ *+ * @return IndexReader that covers entire index plus all+ * changes made so far by this IndexWriter instance, or+ * null if there are no new changes+ *+ * @param writer The IndexWriter to open from+ *+ * @param applyAllDeletes If true, all buffered deletes will+ * be applied (made visible) in the returned reader. If+ * false, the deletes are not applied but remain buffered+ * (in IndexWriter) so that they will be applied in the+ * future. Applying deletes can be costly, so if your app+ * can tolerate deleted documents being returned you might+ * gain some performance by passing false.+ *+ * @throws IOException+ *+ * @lucene.experimental+ */+ public static DirectoryReader openIfChanged(DirectoryReader oldReader, IndexWriter writer, boolean applyAllDeletes) throws IOException {+ final DirectoryReader newReader = oldReader.doOpenIfChanged(writer, applyAllDeletes);+ assert newReader != oldReader;+ return newReader;+ }+/** {@inheritDoc} */@Overridepublic String toString() {@@ -222,13 +418,11 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {return buffer.toString();}- @Override- protected final CompositeIndexReader doOpenIfChanged() throws CorruptIndexException, IOException {+ protected final DirectoryReader doOpenIfChanged() throws CorruptIndexException, IOException {return doOpenIfChanged(null);}- @Override- protected final CompositeIndexReader doOpenIfChanged(final IndexCommit commit) throws CorruptIndexException, IOException {+ protected final DirectoryReader doOpenIfChanged(final IndexCommit commit) throws CorruptIndexException, IOException {ensureOpen();// If we were obtained by writer.getReader(), re-ask the@@ -240,18 +434,17 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {}}- @Override- protected final CompositeIndexReader doOpenIfChanged(IndexWriter writer, boolean applyAllDeletes) throws CorruptIndexException, IOException {+ protected final DirectoryReader doOpenIfChanged(IndexWriter writer, boolean applyAllDeletes) throws CorruptIndexException, IOException {ensureOpen();if (writer == this.writer && applyAllDeletes == this.applyAllDeletes) {return doOpenFromWriter(null);} else {// fail by calling supers impl throwing UOE- return super.doOpenIfChanged(writer, applyAllDeletes);+ throw new UnsupportedOperationException("This reader cannot be reopened from the given IndexWriter, as they don't match.");}}- private final CompositeIndexReader doOpenFromWriter(IndexCommit commit) throws CorruptIndexException, IOException {+ private final DirectoryReader doOpenFromWriter(IndexCommit commit) throws CorruptIndexException, IOException {if (commit != null) {throw new IllegalArgumentException("a reader obtained from IndexWriter.getReader() cannot currently accept a commit");}@@ -260,7 +453,7 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {return null;}- CompositeIndexReader reader = writer.getReader(applyAllDeletes);+ DirectoryReader reader = writer.getReader(applyAllDeletes);// If in fact no changes took place, return null:if (reader.getVersion() == segmentInfos.getVersion()) {@@ -271,7 +464,7 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {return reader;}- private synchronized CompositeIndexReader doOpenNoWriter(IndexCommit commit) throws CorruptIndexException, IOException {+ private synchronized DirectoryReader doOpenNoWriter(IndexCommit commit) throws CorruptIndexException, IOException {if (commit == null) {if (isCurrent()) {@@ -286,7 +479,7 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {}}- return (CompositeIndexReader) new SegmentInfos.FindSegmentsFile(directory) {+ return (DirectoryReader) new SegmentInfos.FindSegmentsFile(directory) {@Overrideprotected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {final SegmentInfos infos = new SegmentInfos();@@ -301,19 +494,16 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {}/** Version number when this IndexReader was opened. */- @Overridepublic long getVersion() {ensureOpen();return segmentInfos.getVersion();}- @Overridepublic Map<String,String> getCommitUserData() {ensureOpen();return segmentInfos.getUserData();}- @Overridepublic boolean isCurrent() throws CorruptIndexException, IOException {ensureOpen();if (writer == null || writer.isClosed()) {@@ -347,7 +537,6 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {}/** Returns the directory this index resides in. */- @Overridepublic Directory directory() {// Don't ensureOpen here -- in certain cases, when a// cloned/reopened reader needs to commit, it may call@@ -355,7 +544,6 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {return directory;}- @Overridepublic int getTermInfosIndexDivisor() {ensureOpen();return termInfosIndexDivisor;@@ -366,7 +554,6 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {* <p/>* @lucene.experimental*/- @Overridepublic IndexCommit getIndexCommit() throws IOException {ensureOpen();return new ReaderCommit(segmentInfos, directory);@@ -419,6 +606,69 @@ public final class DirectoryReader extends BaseMultiReader<SegmentReader> {return commits;}+ /**+ * Returns the time the index in the named directory was last modified.+ * Do not use this to check whether the reader is still up-to-date, use+ * {@link #isCurrent()} instead.+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ */+ public static long lastModified(final Directory directory) throws CorruptIndexException, IOException {+ return ((Long) new SegmentInfos.FindSegmentsFile(directory) {+ @Override+ public Object doBody(String segmentFileName) throws IOException {+ return Long.valueOf(directory.fileModified(segmentFileName));+ }+ }.run()).longValue();+ }++ /**+ * Reads version number from segments files. The version number is+ * initialized with a timestamp and then increased by one for each change of+ * the index.+ *+ * @param directory where the index resides.+ * @return version number.+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ */+ public static long getCurrentVersion(Directory directory) throws CorruptIndexException, IOException {+ return SegmentInfos.readCurrentVersion(directory);+ }++ /**+ * Reads commitUserData, previously passed to {@link+ * IndexWriter#commit(Map)}, from current index+ * segments file. This will return null if {@link+ * IndexWriter#commit(Map)} has never been called for+ * this index.+ *+ * @param directory where the index resides.+ * @return commit userData.+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ *+ * @see #getCommitUserData()+ */+ public static Map<String, String> getCommitUserData(Directory directory) throws CorruptIndexException, IOException {+ return SegmentInfos.readCurrentUserData(directory);+ }++ /**+ * Returns <code>true</code> if an index exists at the specified directory.+ * @param directory the directory to check for an index+ * @return <code>true</code> if an index exists; <code>false</code> otherwise+ * @throws IOException if there is a problem with accessing the index+ */+ public static boolean indexExists(Directory directory) throws IOException {+ try {+ new SegmentInfos().read(directory);+ return true;+ } catch (IOException ioe) {+ return false;+ }+ }+private static final class ReaderCommit extends IndexCommit {private String segmentsFileName;Collection<String> files;diff --git a/lucene/src/java/org/apache/lucene/index/FilterIndexReader.java b/lucene/src/java/org/apache/lucene/index/FilterIndexReader.javaindex 8876ab3fdf7..d2c2d6bb5fb 100644--- a/lucene/src/java/org/apache/lucene/index/FilterIndexReader.java+++ b/lucene/src/java/org/apache/lucene/index/FilterIndexReader.java@@ -291,12 +291,6 @@ public class FilterIndexReader extends AtomicIndexReader {this.in = in;}- @Override- public Directory directory() {- ensureOpen();- return in.directory();- }-@Overridepublic Bits getLiveDocs() {ensureOpen();@@ -405,9 +399,4 @@ public class FilterIndexReader extends AtomicIndexReader {ensureOpen();return in.normValues(field);}-- @Override- public int getTermInfosIndexDivisor() {- return in.getTermInfosIndexDivisor();- }}diff --git a/lucene/src/java/org/apache/lucene/index/IndexReader.java b/lucene/src/java/org/apache/lucene/index/IndexReader.javaindex 72eda488d12..b458d419928 100644--- a/lucene/src/java/org/apache/lucene/index/IndexReader.java+++ b/lucene/src/java/org/apache/lucene/index/IndexReader.java@@ -112,8 +112,6 @@ public abstract class IndexReader implements Closeable {private final AtomicInteger refCount = new AtomicInteger();- static int DEFAULT_TERMS_INDEX_DIVISOR = 1;-/** Expert: returns the current refCount for this reader */public final int getRefCount() {// NOTE: don't ensureOpen, so that callers can see@@ -222,9 +220,11 @@ public abstract class IndexReader implements Closeable {* @param directory the index directory* @throws CorruptIndexException if the index is corrupt* @throws IOException if there is a low-level IO error+ * @deprecated Use {@link DirectoryReader#open(Directory)}*/+ @Deprecatedpublic static DirectoryReader open(final Directory directory) throws CorruptIndexException, IOException {- return DirectoryReader.open(directory, null, DEFAULT_TERMS_INDEX_DIVISOR);+ return DirectoryReader.open(directory);}/** Expert: Returns a IndexReader reading the index in the given@@ -242,9 +242,11 @@ public abstract class IndexReader implements Closeable {* to -1 to skip loading the terms index entirely.* @throws CorruptIndexException if the index is corrupt* @throws IOException if there is a low-level IO error+ * @deprecated Use {@link DirectoryReader#open(Directory,int)}*/+ @Deprecatedpublic static DirectoryReader open(final Directory directory, int termInfosIndexDivisor) throws CorruptIndexException, IOException {- return DirectoryReader.open(directory, null, termInfosIndexDivisor);+ return DirectoryReader.open(directory, termInfosIndexDivisor);}/**@@ -265,9 +267,11 @@ public abstract class IndexReader implements Closeable {* @see #openIfChanged(IndexReader,IndexWriter,boolean)** @lucene.experimental+ * @deprecated Use {@link DirectoryReader#open(IndexWriter,boolean)}*/+ @Deprecatedpublic static DirectoryReader open(final IndexWriter writer, boolean applyAllDeletes) throws CorruptIndexException, IOException {- return writer.getReader(applyAllDeletes);+ return DirectoryReader.open(writer, applyAllDeletes);}/** Expert: returns an IndexReader reading the index in the given@@ -275,9 +279,11 @@ public abstract class IndexReader implements Closeable {* @param commit the commit point to open* @throws CorruptIndexException if the index is corrupt* @throws IOException if there is a low-level IO error+ * @deprecated Use {@link DirectoryReader#open(IndexCommit)}*/+ @Deprecatedpublic static DirectoryReader open(final IndexCommit commit) throws CorruptIndexException, IOException {- return DirectoryReader.open(commit.getDirectory(), commit, DEFAULT_TERMS_INDEX_DIVISOR);+ return DirectoryReader.open(commit);}@@ -296,179 +302,11 @@ public abstract class IndexReader implements Closeable {* to -1 to skip loading the terms index entirely.* @throws CorruptIndexException if the index is corrupt* @throws IOException if there is a low-level IO error+ * @deprecated Use {@link DirectoryReader#open(IndexCommit,int)}*/+ @Deprecatedpublic static DirectoryReader open(final IndexCommit commit, int termInfosIndexDivisor) throws CorruptIndexException, IOException {- return DirectoryReader.open(commit.getDirectory(), commit, termInfosIndexDivisor);- }-- /**- * If the index has changed since the provided reader was- * opened, open and return a new reader; else, return- * null. The new reader, if not null, will be the same- * type of reader as the previous one, ie an NRT reader- * will open a new NRT reader, a MultiReader will open a- * new MultiReader, etc.- *- * <p>This method is typically far less costly than opening a- * fully new <code>IndexReader</code> as it shares- * resources (for example sub-readers) with the provided- * <code>IndexReader</code>, when possible.- *- * <p>The provided reader is not closed (you are responsible- * for doing so); if a new reader is returned you also- * must eventually close it. Be sure to never close a- * reader while other threads are still using it; see- * {@link SearcherManager} to simplify managing this.- *- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- * @return null if there are no changes; else, a new- * IndexReader instance which you must eventually close- */- public static CompositeIndexReader openIfChanged(CompositeIndexReader oldReader) throws IOException {- final CompositeIndexReader newReader = oldReader.doOpenIfChanged();- assert newReader != oldReader;- return newReader;- }-- /**- * If the IndexCommit differs from what the- * provided reader is searching, open and return a new- * reader; else, return null.- *- * @see #openIfChanged(IndexReader)- */- public static CompositeIndexReader openIfChanged(CompositeIndexReader oldReader, IndexCommit commit) throws IOException {- final CompositeIndexReader newReader = oldReader.doOpenIfChanged(commit);- assert newReader != oldReader;- return newReader;- }-- /**- * Expert: If there changes (committed or not) in the- * {@link IndexWriter} versus what the provided reader is- * searching, then open and return a new- * IndexReader searching both committed and uncommitted- * changes from the writer; else, return null (though, the- * current implementation never returns null).- *- * <p>This provides "near real-time" searching, in that- * changes made during an {@link IndexWriter} session can be- * quickly made available for searching without closing- * the writer nor calling {@link IndexWriter#commit}.- *- * <p>It's <i>near</i> real-time because there is no hard- * guarantee on how quickly you can get a new reader after- * making changes with IndexWriter. You'll have to- * experiment in your situation to determine if it's- * fast enough. As this is a new and experimental- * feature, please report back on your findings so we can- * learn, improve and iterate.</p>- *- * <p>The very first time this method is called, this- * writer instance will make every effort to pool the- * readers that it opens for doing merges, applying- * deletes, etc. This means additional resources (RAM,- * file descriptors, CPU time) will be consumed.</p>- *- * <p>For lower latency on reopening a reader, you should- * call {@link IndexWriterConfig#setMergedSegmentWarmer} to- * pre-warm a newly merged segment before it's committed- * to the index. This is important for minimizing- * index-to-search delay after a large merge. </p>- *- * <p>If an addIndexes* call is running in another thread,- * then this reader will only search those segments from- * the foreign index that have been successfully copied- * over, so far.</p>- *- * <p><b>NOTE</b>: Once the writer is closed, any- * outstanding readers may continue to be used. However,- * if you attempt to reopen any of those readers, you'll- * hit an {@link AlreadyClosedException}.</p>- *- * @return IndexReader that covers entire index plus all- * changes made so far by this IndexWriter instance, or- * null if there are no new changes- *- * @param writer The IndexWriter to open from- *- * @param applyAllDeletes If true, all buffered deletes will- * be applied (made visible) in the returned reader. If- * false, the deletes are not applied but remain buffered- * (in IndexWriter) so that they will be applied in the- * future. Applying deletes can be costly, so if your app- * can tolerate deleted documents being returned you might- * gain some performance by passing false.- *- * @throws IOException- *- * @lucene.experimental- */- public static CompositeIndexReader openIfChanged(CompositeIndexReader oldReader, IndexWriter writer, boolean applyAllDeletes) throws IOException {- final CompositeIndexReader newReader = oldReader.doOpenIfChanged(writer, applyAllDeletes);- assert newReader != oldReader;- return newReader;- }-- /**- * Returns the directory associated with this index. The Default- * implementation returns the directory specified by subclasses when- * delegating to the IndexReader(Directory) constructor, or throws an- * UnsupportedOperationException if one was not specified.- * @throws UnsupportedOperationException if no directory- */- public Directory directory() {- ensureOpen();- throw new UnsupportedOperationException("This reader does not support this method.");- }-- /**- * Returns the time the index in the named directory was last modified.- * Do not use this to check whether the reader is still up-to-date, use- * {@link #isCurrent()} instead.- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- */- public static long lastModified(final Directory directory2) throws CorruptIndexException, IOException {- return ((Long) new SegmentInfos.FindSegmentsFile(directory2) {- @Override- public Object doBody(String segmentFileName) throws IOException {- return Long.valueOf(directory2.fileModified(segmentFileName));- }- }.run()).longValue();- }-- /**- * Reads version number from segments files. The version number is- * initialized with a timestamp and then increased by one for each change of- * the index.- *- * @param directory where the index resides.- * @return version number.- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- */- public static long getCurrentVersion(Directory directory) throws CorruptIndexException, IOException {- return SegmentInfos.readCurrentVersion(directory);- }-- /**- * Reads commitUserData, previously passed to {@link- * IndexWriter#commit(Map)}, from current index- * segments file. This will return null if {@link- * IndexWriter#commit(Map)} has never been called for- * this index.- *- * @param directory where the index resides.- * @return commit userData.- * @throws CorruptIndexException if the index is corrupt- * @throws IOException if there is a low-level IO error- *- * @see #getCommitUserData()- */- public static Map<String, String> getCommitUserData(Directory directory) throws CorruptIndexException, IOException {- return SegmentInfos.readCurrentUserData(directory);+ return DirectoryReader.open(commit, termInfosIndexDivisor);}/** Retrieve term vectors for this document, or null if@@ -491,21 +329,6 @@ public abstract class IndexReader implements Closeable {return vectors.terms(field);}- /**- * Returns <code>true</code> if an index exists at the specified directory.- * @param directory the directory to check for an index- * @return <code>true</code> if an index exists; <code>false</code> otherwise- * @throws IOException if there is a problem with accessing the index- */- public static boolean indexExists(Directory directory) throws IOException {- try {- new SegmentInfos().read(directory);- return true;- } catch (IOException ioe) {- return false;- }- }-/** Returns the number of documents in this index. */public abstract int numDocs();@@ -586,25 +409,6 @@ public abstract class IndexReader implements Closeable {/** Implements close. */protected abstract void doClose() throws IOException;- /** Returns all commit points that exist in the Directory.- * Normally, because the default is {@link- * KeepOnlyLastCommitDeletionPolicy}, there would be only- * one commit point. But if you're using a custom {@link- * IndexDeletionPolicy} then there could be many commits.- * Once you have a given commit, you can open a reader on- * it by calling {@link IndexReader#open(IndexCommit)}- * There must be at least one commit in- * the Directory, else this method throws {@link- * IndexNotFoundException}. Note that if a commit is in- * progress while this method is running, that commit- * may or may not be returned.- *- * @return a sorted list of {@link IndexCommit}s, from oldest- * to latest. */- public static List<IndexCommit> listCommits(Directory dir) throws IOException {- return DirectoryReader.listCommits(dir);- }-/*** Expert: Returns a the root {@link ReaderContext} for this* {@link IndexReader}'s sub-reader tree. Iff this reader is composed of sub@@ -643,15 +447,6 @@ public abstract class IndexReader implements Closeable {// on closereturn this;}-- /** For IndexReader implementations that use- * TermInfosReader to read terms, this returns the- * current indexDivisor as specified when the reader was- * opened.- */- public int getTermInfosIndexDivisor() {- throw new UnsupportedOperationException("This reader does not support this method.");- }public final int docFreq(Term term) throws IOException {return docFreq(term.field(), term.bytes());diff --git a/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java b/lucene/src/java/org/apache/lucene/index/IndexUpgrader.javaindex 227d82f6c8e..4a43466fbee 100644--- a/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java+++ b/lucene/src/java/org/apache/lucene/index/IndexUpgrader.java@@ -134,7 +134,7 @@ public final class IndexUpgrader {}public void upgrade() throws IOException {- if (!IndexReader.indexExists(dir)) {+ if (!DirectoryReader.indexExists(dir)) {throw new IndexNotFoundException(dir.toString());}diff --git a/lucene/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/src/java/org/apache/lucene/index/IndexWriter.javaindex 72a1ddb3ff3..5ececcf2462 100644--- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java@@ -872,7 +872,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit {create = false;} else {// CREATE_OR_APPEND - create only if an index does not exist- create = !IndexReader.indexExists(directory);+ create = !DirectoryReader.indexExists(directory);}// If index is too old, reading the segments will throwdiff --git a/lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java b/lucene/src/java/org/apache/lucene/index/IndexWriterConfig.javaindex add2fe6924c..ccd6fb32e7a 100644--- a/lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java+++ b/lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java@@ -90,7 +90,7 @@ public final class IndexWriterConfig implements Cloneable {public final static boolean DEFAULT_READER_POOLING = false;/** Default value is 1. Change using {@link #setReaderTermsIndexDivisor(int)}. */- public static final int DEFAULT_READER_TERMS_INDEX_DIVISOR = IndexReader.DEFAULT_TERMS_INDEX_DIVISOR;+ public static final int DEFAULT_READER_TERMS_INDEX_DIVISOR = DirectoryReader.DEFAULT_TERMS_INDEX_DIVISOR;/** Default value is 1945. Change using {@link #setRAMPerThreadHardLimitMB(int)} */public static final int DEFAULT_RAM_PER_THREAD_HARD_LIMIT_MB = 1945;diff --git a/lucene/src/java/org/apache/lucene/index/MultiReader.java b/lucene/src/java/org/apache/lucene/index/MultiReader.javaindex be781b4fbc2..f85463f3ce2 100644--- a/lucene/src/java/org/apache/lucene/index/MultiReader.java+++ b/lucene/src/java/org/apache/lucene/index/MultiReader.java@@ -59,12 +59,6 @@ public class MultiReader extends BaseMultiReader<IndexReader> {this.decrefOnClose = decrefOnClose;}- @Override- protected synchronized CompositeIndexReader doOpenIfChanged() throws CorruptIndexException, IOException {- // nocommit: remove this method- return null;- }-@Overrideprotected synchronized void doClose() throws IOException {IOException ioe = null;@@ -82,26 +76,4 @@ public class MultiReader extends BaseMultiReader<IndexReader> {// throw the first exceptionif (ioe != null) throw ioe;}-- @Override- public boolean isCurrent() throws CorruptIndexException, IOException {- ensureOpen();- for (int i = 0; i < subReaders.length; i++) {- final IndexReader r = subReaders[i];- if (r instanceof CompositeIndexReader && !((CompositeIndexReader) r).isCurrent()) {- return false;- }- }-- // all subreaders are up to date- return true;- }-- /** Not implemented.- * @throws UnsupportedOperationException- */- @Override- public long getVersion() {- throw new UnsupportedOperationException("MultiReader does not support this method.");- }}diff --git a/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java b/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.javaindex 1d1c9761dae..d81d4094c3a 100644--- a/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java+++ b/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java@@ -62,7 +62,7 @@ public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy {* keeps a lock on the snapshots directory).*/public static Map<String, String> readSnapshotsInfo(Directory dir) throws IOException {- IndexReader r = IndexReader.open(dir);+ IndexReader r = DirectoryReader.open(dir);Map<String, String> snapshots = new HashMap<String, String>();try {int numDocs = r.numDocs();diff --git a/lucene/src/java/org/apache/lucene/index/SegmentMerger.java b/lucene/src/java/org/apache/lucene/index/SegmentMerger.javaindex ba6f9128ec9..7021268d56c 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentMerger.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentMerger.java@@ -329,7 +329,9 @@ final class SegmentMerger {}if (mergeState.payloadProcessorProvider != null) {- mergeState.dirPayloadProcessor[i] = mergeState.payloadProcessorProvider.getDirProcessor(reader.reader.directory());+ // nocommit: this was original, is the change correct:+ // mergeState.dirPayloadProcessor[i] = mergeState.payloadProcessorProvider.getDirProcessor(reader.reader.directory());+ mergeState.dirPayloadProcessor[i] = mergeState.payloadProcessorProvider.getDirProcessor(directory);}}diff --git a/lucene/src/java/org/apache/lucene/index/SegmentReader.java b/lucene/src/java/org/apache/lucene/index/SegmentReader.javaindex 32b42d4fe61..e74d1cc11e5 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentReader.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentReader.java@@ -250,7 +250,6 @@ public final class SegmentReader extends AtomicIndexReader {}/** Returns the directory this index resides in. */- @Overridepublic Directory directory() {// Don't ensureOpen here -- in certain cases, when a// cloned/reopened reader needs to commit, it may call@@ -271,7 +270,6 @@ public final class SegmentReader extends AtomicIndexReader {return this;}- @Overridepublic int getTermInfosIndexDivisor() {return core.termsIndexDivisor;}diff --git a/lucene/src/java/org/apache/lucene/search/SearcherLifetimeManager.java b/lucene/src/java/org/apache/lucene/search/SearcherLifetimeManager.javaindex 9f6d0d68daa..5c88675467a 100644--- a/lucene/src/java/org/apache/lucene/search/SearcherLifetimeManager.java+++ b/lucene/src/java/org/apache/lucene/search/SearcherLifetimeManager.java@@ -25,7 +25,7 @@ import java.util.List;import java.util.concurrent.ConcurrentHashMap;import org.apache.lucene.search.NRTManager; // javadocs-import org.apache.lucene.index.CompositeIndexReader; // javadocs+import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader; // javadocsimport org.apache.lucene.store.AlreadyClosedException;import org.apache.lucene.util.IOUtils;@@ -110,8 +110,8 @@ public class SearcherLifetimeManager implements Closeable {public SearcherTracker(IndexSearcher searcher) {this.searcher = searcher;- // nocommit: fix getVersion() usage for atomic readers- version = ((CompositeIndexReader) searcher.getIndexReader()).getVersion();+ // nocommit: what happens if this is no DirectoryReader?+ version = ((DirectoryReader) searcher.getIndexReader()).getVersion();searcher.getIndexReader().incRef();// Use nanoTime not currentTimeMillis since it [in// theory] reduces risk from clock shift@@ -171,7 +171,7 @@ public class SearcherLifetimeManager implements Closeable {// could be risky (if it's buggy); we could get better// bug isolation if we assign our own private ID:// nocommit: fix getVersion() usage for atomic readers- final long version = ((CompositeIndexReader) searcher.getIndexReader()).getVersion();+ final long version = ((DirectoryReader) searcher.getIndexReader()).getVersion();SearcherTracker tracker = searchers.get(version);if (tracker == null) {//System.out.println("RECORD version=" + version + " ms=" + System.currentTimeMillis());diff --git a/lucene/src/java/org/apache/lucene/search/SearcherManager.java b/lucene/src/java/org/apache/lucene/search/SearcherManager.javaindex 4071a60f663..1b1143dc73a 100644--- a/lucene/src/java/org/apache/lucene/search/SearcherManager.java+++ b/lucene/src/java/org/apache/lucene/search/SearcherManager.java@@ -23,7 +23,7 @@ import java.util.concurrent.Semaphore;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexReader;-import org.apache.lucene.index.CompositeIndexReader;+import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.search.NRTManager; // javadocsimport org.apache.lucene.search.IndexSearcher; // javadocs@@ -94,7 +94,7 @@ public final class SearcherManager implements Closeable {searcherFactory = new SearcherFactory();}this.searcherFactory = searcherFactory;- currentSearcher = searcherFactory.newSearcher(IndexReader.open(writer, applyAllDeletes));+ currentSearcher = searcherFactory.newSearcher(DirectoryReader.open(writer, applyAllDeletes));}/**@@ -111,7 +111,7 @@ public final class SearcherManager implements Closeable {searcherFactory = new SearcherFactory();}this.searcherFactory = searcherFactory;- currentSearcher = searcherFactory.newSearcher(IndexReader.open(dir));+ currentSearcher = searcherFactory.newSearcher(DirectoryReader.open(dir));}/**@@ -146,8 +146,8 @@ public final class SearcherManager implements Closeable {final IndexSearcher searcherToReopen = acquire();try {final IndexReader r = searcherToReopen.getIndexReader();- newReader = (r instanceof CompositeIndexReader) ?- IndexReader.openIfChanged((CompositeIndexReader) r) :+ newReader = (r instanceof DirectoryReader) ?+ DirectoryReader.openIfChanged((DirectoryReader) r) :null;} finally {release(searcherToReopen);@@ -176,15 +176,15 @@ public final class SearcherManager implements Closeable {/*** Returns <code>true</code> if no changes have occured since this searcher* ie. reader was opened, otherwise <code>false</code>.- * @see CompositeIndexReader#isCurrent()+ * @see DirectoryReader#isCurrent()*/public boolean isSearcherCurrent() throws CorruptIndexException,IOException {final IndexSearcher searcher = acquire();try {final IndexReader r = searcher.getIndexReader();- return r instanceof CompositeIndexReader ?- ((CompositeIndexReader ) r).isCurrent() :+ return r instanceof DirectoryReader ?+ ((DirectoryReader ) r).isCurrent() :true;} finally {release(searcher);