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 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}.
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;i
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;i