Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 633421) +++ CHANGES.txt (working copy) @@ -105,6 +105,9 @@ close/re-open of IndexWriter while still protecting an open snapshot (Tim Brennan via Mike McCandless) + 9. LUCENE-1201: Add IndexReader.getIndexCommit() method. (Tim Brennan + via Mike McCandless) + Optimizations 1. LUCENE-705: When building a compound file, use Index: src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java =================================================================== --- src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java (revision 633421) +++ src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java (working copy) @@ -31,7 +31,7 @@ import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.MockRAMDirectory; import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.apache.lucene.index.IndexCommitPoint; +import org.apache.lucene.index.IndexCommit; import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.TestIndexWriter; @@ -75,7 +75,7 @@ doc.add(new Field("content", "aaa", Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); for(int i=0;i<7;i++) writer.addDocument(doc); - IndexCommitPoint cp = dp.snapshot(); + IndexCommit cp = (IndexCommit) dp.snapshot(); copyFiles(dir, dp, cp); writer.close(); copyFiles(dir, dp, cp); @@ -177,7 +177,7 @@ public void backupIndex(Directory dir, SnapshotDeletionPolicy dp) throws IOException { // To backup an index we first take a snapshot: try { - copyFiles(dir, dp, dp.snapshot()); + copyFiles(dir, dp, (IndexCommit) dp.snapshot()); } finally { // Make sure to release the snapshot, otherwise these // files will never be deleted during this IndexWriter @@ -186,7 +186,7 @@ } } - private void copyFiles(Directory dir, SnapshotDeletionPolicy dp, IndexCommitPoint cp) throws IOException { + private void copyFiles(Directory dir, SnapshotDeletionPolicy dp, IndexCommit cp) throws IOException { // While we hold the snapshot, and nomatter how long // we take to do the backup, the IndexWriter will Index: src/test/org/apache/lucene/index/TestIndexReader.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexReader.java (revision 633421) +++ src/test/org/apache/lucene/index/TestIndexReader.java (working copy) @@ -1276,4 +1276,37 @@ } } + public void testGetIndexCommit() throws IOException { + + RAMDirectory d = new MockRAMDirectory(); + + // set up writer + IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); + writer.setMaxBufferedDocs(2); + for(int i=0;i<27;i++) + addDocumentWithFields(writer); + writer.close(); + + SegmentInfos sis = new SegmentInfos(); + sis.read(d); + IndexReader r = IndexReader.open(d); + IndexCommit c = r.getIndexCommit(); + + assertEquals(sis.getCurrentSegmentFileName(), c.getSegmentsFileName()); + + assertTrue(c.equals(r.getIndexCommit())); + + // Change the index + writer = new IndexWriter(d, new StandardAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED); + writer.setMaxBufferedDocs(2); + for(int i=0;i<7;i++) + addDocumentWithFields(writer); + writer.close(); + + IndexReader r2 = r.reopen(); + assertFalse(c.equals(r2.getIndexCommit())); + r.close(); + r2.close(); + d.close(); + } } Index: src/test/org/apache/lucene/index/TestDeletionPolicy.java =================================================================== --- src/test/org/apache/lucene/index/TestDeletionPolicy.java (revision 633421) +++ src/test/org/apache/lucene/index/TestDeletionPolicy.java (working copy) @@ -43,9 +43,9 @@ public class TestDeletionPolicy extends LuceneTestCase { private void verifyCommitOrder(List commits) { - long last = SegmentInfos.generationFromSegmentsFileName(((IndexCommitPoint) commits.get(0)).getSegmentsFileName()); + long last = SegmentInfos.generationFromSegmentsFileName(((IndexCommit) commits.get(0)).getSegmentsFileName()); for(int i=1;i last); last = now; } @@ -77,7 +77,7 @@ // On init, delete all commit points: Iterator it = commits.iterator(); while(it.hasNext()) { - ((IndexCommitPoint) it.next()).delete(); + ((IndexCommit) it.next()).delete(); } } public void onCommit(List commits) { @@ -85,7 +85,7 @@ int size = commits.size(); // Delete all but last one: for(int i=0;iExpert: policy for deletion of stale {@link IndexCommitPoint index commits}. + *

Expert: policy for deletion of stale {@link IndexCommit index commits}. * *

Implement this interface, and pass it to one * of the {@link IndexWriter} or {@link IndexReader} * constructors, to customize when older - * {@link IndexCommitPoint point-in-time commits} + * {@link IndexCommit point-in-time commits} * are deleted from the index directory. The default deletion policy * is {@link KeepOnlyLastCommitDeletionPolicy}, which always * removes old commits as soon as a new commit is done (this @@ -58,8 +58,8 @@ *

The writer locates all index commits present in the * index directory and calls this method. The policy may * choose to delete some of the commit points, doing so by - * calling method {@link IndexCommitPoint#delete delete()} - * of {@link IndexCommitPoint}.

+ * calling method {@link IndexCommit#delete delete()} + * of {@link IndexCommit}.

* *

Note: the last CommitPoint is the most recent one, * i.e. the "front index state". Be careful not to delete it, @@ -67,7 +67,7 @@ * you can afford to lose the index content while doing that. * * @param commits List of current - * {@link IndexCommitPoint point-in-time commits}, + * {@link IndexCommit point-in-time commits}, * sorted by age (the 0th one is the oldest commit). */ public void onInit(List commits) throws IOException; @@ -78,8 +78,8 @@ * with each commit.

* *

The policy may now choose to delete old commit points - * by calling method {@link IndexCommitPoint#delete delete()} - * of {@link IndexCommitPoint}.

+ * by calling method {@link IndexCommit#delete delete()} + * of {@link IndexCommit}.

* *

If writer has autoCommit = true then * this method will in general be called many times during @@ -94,7 +94,7 @@ * unless you know for sure what you are doing, and unless * you can afford to lose the index content while doing that. * - * @param commits List of {@link IndexCommitPoint}, + * @param commits List of {@link IndexCommit}, * sorted by age (the 0th one is the oldest commit). */ public void onCommit(List commits) throws IOException; Index: src/java/org/apache/lucene/index/DirectoryIndexReader.java =================================================================== --- src/java/org/apache/lucene/index/DirectoryIndexReader.java (revision 633421) +++ src/java/org/apache/lucene/index/DirectoryIndexReader.java (working copy) @@ -20,6 +20,8 @@ import java.io.IOException; import java.util.HashSet; +import java.util.Collection; +import java.util.ArrayList; import java.util.List; import org.apache.lucene.store.Directory; @@ -96,7 +98,6 @@ }.run(); } - public final synchronized IndexReader reopen() throws CorruptIndexException, IOException { ensureOpen(); @@ -337,4 +338,42 @@ } } + private static class ReaderCommit extends IndexCommit { + private String segmentsFileName; + Collection files; + Directory dir; + + ReaderCommit(SegmentInfos infos, Directory dir) throws IOException { + segmentsFileName = infos.getCurrentSegmentFileName(); + this.dir = dir; + final int size = infos.size(); + files = new ArrayList(size); + files.add(segmentsFileName); + for(int i=0;iWARNING: this API is new and experimental and + * may suddenly change.

+ */ + public IndexCommit getIndexCommit() throws IOException { + return new ReaderCommit(segmentInfos, directory); + } } Index: src/java/org/apache/lucene/index/IndexCommitPoint.java =================================================================== --- src/java/org/apache/lucene/index/IndexCommitPoint.java (revision 633421) +++ src/java/org/apache/lucene/index/IndexCommitPoint.java (working copy) @@ -17,24 +17,13 @@ * limitations under the License. */ -/** - *

Expert: represents a single commit into an index as seen by the - * {@link IndexDeletionPolicy}. - *

- * Changes to the content of an index are made visible only - * after the writer who made that change had written to the - * directory a new segments file (segments_N). This point in - * time, when the action of writing of a new segments file to the - * directory is completed, is therefore an index commit point. - *

- * Each index commit point has a unique segments file associated - * with it. The segments file associated with a later - * index commit point would have a larger N. - */ - import java.util.Collection; import java.io.IOException; +/** + * @deprecated Please subclass IndexCommit class instead + */ + public interface IndexCommitPoint { /** Index: src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java =================================================================== --- src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java (revision 633421) +++ src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java (working copy) @@ -21,6 +21,7 @@ import java.util.List; import java.util.ArrayList; import java.io.IOException; +import org.apache.lucene.store.Directory; /** A {@link IndexDeletionPolicy} that wraps around any other * {@link IndexDeletionPolicy} and adds the ability to hold and @@ -35,11 +36,14 @@ * SnapshotDeletionPolicy across multiple writers as long * as they are against the same index Directory. Any * snapshot held when a writer is closed will "survive" - * when the next writer is opened. */ + * when the next writer is opened. + * + *

WARNING: This API is a new and experimental and + * may suddenly change.

*/ public class SnapshotDeletionPolicy implements IndexDeletionPolicy { - private IndexCommitPoint lastCommit; + private IndexCommit lastCommit; private IndexDeletionPolicy primary; private String snapshot; @@ -49,12 +53,12 @@ public synchronized void onInit(List commits) throws IOException { primary.onInit(wrapCommits(commits)); - lastCommit = (IndexCommitPoint) commits.get(commits.size()-1); + lastCommit = (IndexCommit) commits.get(commits.size()-1); } public synchronized void onCommit(List commits) throws IOException { primary.onCommit(wrapCommits(commits)); - lastCommit = (IndexCommitPoint) commits.get(commits.size()-1); + lastCommit = (IndexCommit) commits.get(commits.size()-1); } /** Take a snapshot of the most recent commit to the @@ -66,6 +70,7 @@ * you call optimize()) then in the worst case this could * consume an extra 1X of your total index size, until * you release the snapshot. */ + // for 3.0: change this to return IndexCommit instead public synchronized IndexCommitPoint snapshot() { if (snapshot == null) snapshot = lastCommit.getSegmentsFileName(); @@ -82,9 +87,9 @@ throw new IllegalStateException("snapshot was not set; please call snapshot() first"); } - private class MyCommitPoint implements IndexCommitPoint { - IndexCommitPoint cp; - MyCommitPoint(IndexCommitPoint cp) { + private class MyCommitPoint extends IndexCommit { + IndexCommit cp; + MyCommitPoint(IndexCommit cp) { this.cp = cp; } public String getSegmentsFileName() { @@ -93,6 +98,9 @@ public Collection getFileNames() throws IOException { return cp.getFileNames(); } + public Directory getDirectory() { + return cp.getDirectory(); + } public void delete() { synchronized(SnapshotDeletionPolicy.this) { // Suppress the delete request if this commit point is @@ -107,7 +115,7 @@ final int count = commits.size(); List myCommits = new ArrayList(count); for(int i=0;iWARNING: this API is new and experimental and + * may suddenly change.

+ */ + public IndexCommit getIndexCommit() throws IOException { + throw new UnsupportedOperationException("This reader does not support this method."); + } + + /** * Prints the filename and size of each file within a given compound file. * Add the -extract flag to extract files to the current working directory. * In order to make the extracted version of the index work, you have to copy Index: src/java/org/apache/lucene/index/IndexWriter.java =================================================================== --- src/java/org/apache/lucene/index/IndexWriter.java (revision 633421) +++ src/java/org/apache/lucene/index/IndexWriter.java (working copy) @@ -184,7 +184,7 @@ * modified) directory files, we have a new "check point". * If the modified/new SegmentInfos is written to disk - as a new * (generation of) segments_N file - this check point is also an - * IndexCommitPoint. + * IndexCommit. * * With autoCommit=true, every checkPoint is also a CommitPoint. * With autoCommit=false, some checkPoints may not be commits. Index: src/java/org/apache/lucene/index/IndexFileDeleter.java =================================================================== --- src/java/org/apache/lucene/index/IndexFileDeleter.java (revision 633421) +++ src/java/org/apache/lucene/index/IndexFileDeleter.java (working copy) @@ -48,7 +48,7 @@ * force a blocking commit. * * The same directory file may be referenced by more than - * one IndexCommitPoints, i.e. more than one SegmentInfos. + * one IndexCommit, i.e. more than one SegmentInfos. * Therefore we count how many commits reference each file. * When all the commits referencing a certain file have been * deleted, the refcount for that file becomes zero, and the @@ -569,7 +569,7 @@ * equals. */ - final private static class CommitPoint implements Comparable, IndexCommitPoint { + final private static class CommitPoint extends IndexCommit implements Comparable { long gen; List files; @@ -594,9 +594,6 @@ } } - /** - * Get the segments_N file for this commit point. - */ public String getSegmentsFileName() { return segmentsFileName; } @@ -605,6 +602,10 @@ return Collections.unmodifiableCollection(files); } + public Directory getDirectory() { + return directory; + } + /** * Called only be the deletion policy, to remove this * commit point from the index. Index: src/java/org/apache/lucene/index/KeepOnlyLastCommitDeletionPolicy.java =================================================================== --- src/java/org/apache/lucene/index/KeepOnlyLastCommitDeletionPolicy.java (revision 633421) +++ src/java/org/apache/lucene/index/KeepOnlyLastCommitDeletionPolicy.java (working copy) @@ -44,7 +44,7 @@ // called by onInit above): int size = commits.size(); for(int i=0;i