Index: src/test/org/apache/lucene/index/TestIndexReader.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexReader.java (revision 747340) +++ src/test/org/apache/lucene/index/TestIndexReader.java (working copy) @@ -68,7 +68,57 @@ public TestIndexReader(String name) { super(name); } + + public void testUserCommitData() throws Exception { + RAMDirectory d = new MockRAMDirectory(); + + String cmpUserCommitData = "foo fighters"; + + // 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(); + + IndexReader r = IndexReader.open(d); + r.deleteDocument(5); + r.flush(cmpUserCommitData); + r.close(); + + SegmentInfos sis = new SegmentInfos(); + sis.read(d); + IndexReader r2 = IndexReader.open(d); + IndexCommit c = r.getIndexCommit(); + assertEquals(c.getUserData(), cmpUserCommitData); + 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 r3 = r2.reopen(); + assertFalse(c.equals(r3.getIndexCommit())); + assertFalse(r2.getIndexCommit().isOptimized()); + r3.close(); + + writer = new IndexWriter(d, new StandardAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED); + writer.optimize(); + writer.close(); + + r3 = r2.reopen(); + assertTrue(r3.getIndexCommit().isOptimized()); + r2.close(); + r3.close(); + d.close(); + } + public void testIsCurrent() throws Exception { RAMDirectory d = new MockRAMDirectory(); Index: src/java/org/apache/lucene/index/DirectoryIndexReader.java =================================================================== --- src/java/org/apache/lucene/index/DirectoryIndexReader.java (revision 747340) +++ src/java/org/apache/lucene/index/DirectoryIndexReader.java (working copy) @@ -327,6 +327,10 @@ directory.close(); } + protected void doCommit() throws IOException { + doCommit(null); + } + /** * Commit changes resulting from delete, undeleteAll, or * setNorm operations @@ -336,10 +340,10 @@ * (transactional semantics). * @throws IOException if there is a low-level IO error */ - protected void doCommit() throws IOException { + protected void doCommit(String userCommitData) throws IOException { if (hasChanges) { if (segmentInfos != null) { - + segmentInfos.setUserData(userCommitData); // Default deleter (for backwards compatibility) is // KeepOnlyLastCommitDeleter: IndexFileDeleter deleter = new IndexFileDeleter(directory, Index: src/java/org/apache/lucene/index/ParallelReader.java =================================================================== --- src/java/org/apache/lucene/index/ParallelReader.java (revision 747340) +++ src/java/org/apache/lucene/index/ParallelReader.java (working copy) @@ -437,8 +437,12 @@ } protected void doCommit() throws IOException { + doCommit(null); + } + + protected void doCommit(String userCommitData) throws IOException { for (int i = 0; i < readers.size(); i++) - ((IndexReader)readers.get(i)).commit(); + ((IndexReader)readers.get(i)).commit(userCommitData); } protected synchronized void doClose() throws IOException { Index: src/java/org/apache/lucene/index/MultiReader.java =================================================================== --- src/java/org/apache/lucene/index/MultiReader.java (revision 747340) +++ src/java/org/apache/lucene/index/MultiReader.java (working copy) @@ -349,8 +349,12 @@ } protected void doCommit() throws IOException { + doCommit(null); + } + + protected void doCommit(String userCommitData) throws IOException { for (int i = 0; i < subReaders.length; i++) - subReaders[i].commit(); + subReaders[i].commit(userCommitData); } protected synchronized void doClose() throws IOException { Index: src/java/org/apache/lucene/index/FilterIndexReader.java =================================================================== --- src/java/org/apache/lucene/index/FilterIndexReader.java (revision 747340) +++ src/java/org/apache/lucene/index/FilterIndexReader.java (working copy) @@ -209,7 +209,8 @@ } protected void doDelete(int n) throws CorruptIndexException, IOException { in.deleteDocument(n); } - protected void doCommit() throws IOException { in.commit(); } + protected void doCommit() throws IOException { doCommit(null); } + protected void doCommit(String userCommitData) throws IOException { in.commit(userCommitData); } protected void doClose() throws IOException { in.close(); } Index: src/java/org/apache/lucene/index/IndexReader.java =================================================================== --- src/java/org/apache/lucene/index/IndexReader.java (revision 747340) +++ src/java/org/apache/lucene/index/IndexReader.java (working copy) @@ -993,8 +993,19 @@ ensureOpen(); commit(); } - + /** + * @param userCommitData Opaque String that's recorded + * into the segments file in the index, and retrievable + * by {@link IndexReader#getCommitUserData}. + * @throws IOException + */ + public final synchronized void flush(String userCommitData) throws IOException { + ensureOpen(); + commit(userCommitData); + } + + /** * Commit changes resulting from delete, undeleteAll, or * setNorm operations * @@ -1004,15 +1015,38 @@ * @throws IOException if there is a low-level IO error */ protected final synchronized void commit() throws IOException { - if(hasChanges){ - doCommit(); + commit(null); + } + + /** + * Commit changes resulting from delete, undeleteAll, or + * setNorm operations + * + * If an exception is hit, then either no changes or all + * changes will have been committed to the index + * (transactional semantics). + * @throws IOException if there is a low-level IO error + */ + protected final synchronized void commit(String userCommitData) throws IOException { + if (hasChanges) { + doCommit(userCommitData); } hasChanges = false; } - /** Implements commit. */ + /** Implements commit. + * @deprecated Please implement {@link #doCommit(String) + * instead}. */ protected abstract void doCommit() throws IOException; + /** Implements commit. NOTE: subclasses should override + * this. In 3.0 this will become an abstract method. */ + void doCommit(String userCommitData) throws IOException { + // Default impl discards userCommitData; all Lucene + // subclasses override this (do not discard it). + doCommit(); + } + /** * Closes files associated with this index. * Also saves any new deletions to disk.