* point. */
public void testOpenPriorSnapshot() throws IOException {
// Never deletes a commit
KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy();
Directory dir = newDirectory();
policy.dir = dir;
IndexWriter writer = new IndexWriter(
dir,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
setIndexDeletionPolicy(policy).
setMaxBufferedDocs(2).
setMergePolicy(newLogMergePolicy(10))
);
for(int i=0;i<10;i++) {
addDoc(writer);
if ((1+i)%2 == 0)
writer.commit();
}
writer.close();
Collection<IndexCommit> commits = IndexReader.listCommits(dir);
assertEquals(5, commits.size());
IndexCommit lastCommit = null;
for (final IndexCommit commit : commits) {
if (lastCommit == null || commit.getGeneration() > lastCommit.getGeneration())
lastCommit = commit;
}
assertTrue(lastCommit != null);
// Now add 1 doc and optimize
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT,
new MockAnalyzer(random)).setIndexDeletionPolicy(policy));
addDoc(writer);
assertEquals(11, writer.numDocs());
writer.optimize();
writer.close();
assertEquals(6, IndexReader.listCommits(dir).size());
// Now open writer on the commit just before optimize:
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setIndexDeletionPolicy(policy).setIndexCommit(lastCommit));
assertEquals(10, writer.numDocs());
// Should undo our rollback:
writer.rollback();
IndexReader r = IndexReader.open(dir, true);
// Still optimized, still 11 docs
assertTrue(r.isOptimized());
assertEquals(11, r.numDocs());
r.close();
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
.setIndexDeletionPolicy(policy).setIndexCommit(lastCommit));
assertEquals(10, writer.numDocs());
// Commits the rollback:
writer.close();
// Now 8 because we made another commit
assertEquals(7, IndexReader.listCommits(dir).size());
r = IndexReader.open(dir, true);
// Not optimized because we rolled it back, and now only
// 10 docs
assertTrue(!r.isOptimized());
assertEquals(10, r.numDocs());
r.close();
// Reoptimize
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexDeletionPolicy(policy));
writer.optimize();
writer.close();
r = IndexReader.open(dir, true);
assertTrue(r.isOptimized());
assertEquals(10, r.numDocs());
r.close();
// Now open writer on the commit just before optimize,
// but this time keeping only the last commit:
writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexCommit(lastCommit));
assertEquals(10, writer.numDocs());
// Reader still sees optimized index, because writer
// opened on the prior commit has not yet committed:
r = IndexReader.open(dir, true);
assertTrue(r.isOptimized());
assertEquals(10, r.numDocs());
r.close();
writer.close();
// Now reader sees unoptimized index:
r = IndexReader.open(dir, true);
assertTrue(!r.isOptimized());
assertEquals(10, r.numDocs());
r.close();
dir.close();
}