1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.Collection;
26 import org.apache.lucene.analysis.MockAnalyzer;
27 import org.apache.lucene.analysis.WhitespaceAnalyzer;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
31 import org.apache.lucene.search.IndexSearcher;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.search.ScoreDoc;
34 import org.apache.lucene.search.TermQuery;
35 import org.apache.lucene.store.Directory;
36 import org.apache.lucene.util.LuceneTestCase;
39 Verify we can read the pre-2.1 file format, do searches
40 against it, and add documents to it.
43 public class TestDeletionPolicy extends LuceneTestCase {
45 private void verifyCommitOrder(List<? extends IndexCommit> commits) throws IOException {
46 final IndexCommit firstCommit = commits.get(0);
47 long last = SegmentInfos.generationFromSegmentsFileName(firstCommit.getSegmentsFileName());
48 assertEquals(last, firstCommit.getGeneration());
49 long lastVersion = firstCommit.getVersion();
50 long lastTimestamp = firstCommit.getTimestamp();
51 for(int i=1;i<commits.size();i++) {
52 final IndexCommit commit = commits.get(i);
53 long now = SegmentInfos.generationFromSegmentsFileName(commit.getSegmentsFileName());
54 long nowVersion = commit.getVersion();
55 long nowTimestamp = commit.getTimestamp();
56 assertTrue("SegmentInfos commits are out-of-order", now > last);
57 assertTrue("SegmentInfos versions are out-of-order", nowVersion > lastVersion);
58 assertTrue("SegmentInfos timestamps are out-of-order: now=" + nowTimestamp + " vs last=" + lastTimestamp, nowTimestamp >= lastTimestamp);
59 assertEquals(now, commit.getGeneration());
61 lastVersion = nowVersion;
62 lastTimestamp = nowTimestamp;
66 class KeepAllDeletionPolicy implements IndexDeletionPolicy {
70 public void onInit(List<? extends IndexCommit> commits) throws IOException {
71 verifyCommitOrder(commits);
74 public void onCommit(List<? extends IndexCommit> commits) throws IOException {
75 IndexCommit lastCommit = commits.get(commits.size()-1);
76 IndexReader r = IndexReader.open(dir, true);
77 assertEquals("lastCommit.isOptimized()=" + lastCommit.isOptimized() + " vs IndexReader.isOptimized=" + r.isOptimized(), r.isOptimized(), lastCommit.isOptimized());
79 verifyCommitOrder(commits);
85 * This is useful for adding to a big index when you know
86 * readers are not using it.
88 class KeepNoneOnInitDeletionPolicy implements IndexDeletionPolicy {
91 public void onInit(List<? extends IndexCommit> commits) throws IOException {
92 verifyCommitOrder(commits);
94 // On init, delete all commit points:
95 for (final IndexCommit commit : commits) {
97 assertTrue(commit.isDeleted());
100 public void onCommit(List<? extends IndexCommit> commits) throws IOException {
101 verifyCommitOrder(commits);
102 int size = commits.size();
103 // Delete all but last one:
104 for(int i=0;i<size-1;i++) {
105 ((IndexCommit) commits.get(i)).delete();
111 class KeepLastNDeletionPolicy implements IndexDeletionPolicy {
116 Set<String> seen = new HashSet<String>();
118 public KeepLastNDeletionPolicy(int numToKeep) {
119 this.numToKeep = numToKeep;
122 public void onInit(List<? extends IndexCommit> commits) throws IOException {
124 System.out.println("TEST: onInit");
126 verifyCommitOrder(commits);
128 // do no deletions on init
129 doDeletes(commits, false);
132 public void onCommit(List<? extends IndexCommit> commits) throws IOException {
134 System.out.println("TEST: onCommit");
136 verifyCommitOrder(commits);
137 doDeletes(commits, true);
140 private void doDeletes(List<? extends IndexCommit> commits, boolean isCommit) {
142 // Assert that we really are only called for each new
145 String fileName = ((IndexCommit) commits.get(commits.size()-1)).getSegmentsFileName();
146 if (seen.contains(fileName)) {
147 throw new RuntimeException("onCommit was called twice on the same commit point: " + fileName);
152 int size = commits.size();
153 for(int i=0;i<size-numToKeep;i++) {
154 ((IndexCommit) commits.get(i)).delete();
161 * Delete a commit only when it has been obsoleted by N
164 class ExpirationTimeDeletionPolicy implements IndexDeletionPolicy {
167 double expirationTimeSeconds;
170 public ExpirationTimeDeletionPolicy(Directory dir, double seconds) {
172 this.expirationTimeSeconds = seconds;
175 public void onInit(List<? extends IndexCommit> commits) throws IOException {
176 verifyCommitOrder(commits);
180 public void onCommit(List<? extends IndexCommit> commits) throws IOException {
181 verifyCommitOrder(commits);
183 IndexCommit lastCommit = commits.get(commits.size()-1);
185 // Any commit older than expireTime should be deleted:
186 double expireTime = dir.fileModified(lastCommit.getSegmentsFileName())/1000.0 - expirationTimeSeconds;
188 for (final IndexCommit commit : commits) {
189 double modTime = dir.fileModified(commit.getSegmentsFileName())/1000.0;
190 if (commit != lastCommit && modTime < expireTime) {
199 * Test "by time expiration" deletion policy:
201 public void testExpirationTimeDeletionPolicy() throws IOException, InterruptedException {
203 final double SECONDS = 2.0;
205 Directory dir = newDirectory();
206 ExpirationTimeDeletionPolicy policy = new ExpirationTimeDeletionPolicy(dir, SECONDS);
207 IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT,
208 new MockAnalyzer(random))
209 .setIndexDeletionPolicy(policy);
210 MergePolicy mp = conf.getMergePolicy();
211 if (mp instanceof LogMergePolicy) {
212 setUseCompoundFile(mp, true);
214 IndexWriter writer = new IndexWriter(dir, conf);
219 long lastDeleteTime = 0;
220 for(int i=0;i<ITER;i++) {
221 // Record last time when writer performed deletes of
223 lastDeleteTime = System.currentTimeMillis();
224 conf = newIndexWriterConfig(TEST_VERSION_CURRENT,
225 new MockAnalyzer(random)).setOpenMode(
226 OpenMode.APPEND).setIndexDeletionPolicy(policy);
227 mp = conf.getMergePolicy();
228 if (mp instanceof LogMergePolicy) {
229 setUseCompoundFile(mp, true);
231 writer = new IndexWriter(dir, conf);
232 for(int j=0;j<17;j++) {
238 // Make sure to sleep long enough so that some commit
239 // points will be deleted:
240 Thread.sleep((int) (1000.0*(SECONDS/5.0)));
244 // First, make sure the policy in fact deleted something:
245 assertTrue("no commits were deleted", policy.numDelete > 0);
247 // Then simplistic check: just verify that the
248 // segments_N's that still exist are in fact within SECONDS
249 // seconds of the last one's mod time, and, that I can
250 // open a reader on each:
251 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
253 String fileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
256 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
258 boolean oneSecondResolution = true;
262 IndexReader reader = IndexReader.open(dir, true);
264 fileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
268 // if we are on a filesystem that seems to have only
269 // 1 second resolution, allow +1 second in commit
271 long modTime = dir.fileModified(fileName);
272 oneSecondResolution &= (modTime % 1000) == 0;
273 final long leeway = (long) ((SECONDS + (oneSecondResolution ? 1.0:0.0))*1000);
275 assertTrue("commit point was older than " + SECONDS + " seconds (" + (lastDeleteTime - modTime) + " msec) but did not get deleted ", lastDeleteTime - modTime <= leeway);
276 } catch (IOException e) {
281 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
289 * Test a silly deletion policy that keeps all commits around.
291 public void testKeepAllDeletionPolicy() throws IOException {
292 for(int pass=0;pass<2;pass++) {
295 System.out.println("TEST: cycle pass=" + pass);
298 boolean useCompoundFile = (pass % 2) != 0;
300 // Never deletes a commit
301 KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy();
303 Directory dir = newDirectory();
306 IndexWriterConfig conf = newIndexWriterConfig(
307 TEST_VERSION_CURRENT, new MockAnalyzer(random))
308 .setIndexDeletionPolicy(policy).setMaxBufferedDocs(10)
309 .setMergeScheduler(new SerialMergeScheduler());
310 MergePolicy mp = conf.getMergePolicy();
311 if (mp instanceof LogMergePolicy) {
312 setUseCompoundFile(mp, useCompoundFile);
314 IndexWriter writer = new IndexWriter(dir, conf);
315 for(int i=0;i<107;i++) {
320 final boolean isOptimized;
322 IndexReader r = IndexReader.open(dir);
323 isOptimized = r.isOptimized();
327 conf = newIndexWriterConfig(TEST_VERSION_CURRENT,
328 new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setOpenMode(
329 OpenMode.APPEND).setIndexDeletionPolicy(policy);
330 mp = conf.getMergePolicy();
331 if (mp instanceof LogMergePolicy) {
332 setUseCompoundFile(mp, true);
335 System.out.println("TEST: open writer for optimize");
337 writer = new IndexWriter(dir, conf);
338 writer.setInfoStream(VERBOSE ? System.out : null);
342 assertEquals(isOptimized ? 0:1, policy.numOnInit);
344 // If we are not auto committing then there should
345 // be exactly 2 commits (one per close above):
346 assertEquals(1 + (isOptimized ? 0:1), policy.numOnCommit);
349 Collection<IndexCommit> commits = IndexReader.listCommits(dir);
350 // 2 from closing writer
351 assertEquals(1 + (isOptimized ? 0:1), commits.size());
353 // Make sure we can open a reader on each commit:
354 for (final IndexCommit commit : commits) {
355 IndexReader r = IndexReader.open(commit, null, false);
359 // Simplistic check: just verify all segments_N's still
360 // exist, and, I can open a reader on each:
361 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
362 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
364 IndexReader reader = IndexReader.open(dir, true);
366 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
370 // Now that we've removed a commit point, which
371 // should have orphan'd at least one index file.
372 // Open & close a writer and assert that it
373 // actually removed something:
374 int preCount = dir.listAll().length;
375 writer = new IndexWriter(dir, newIndexWriterConfig(
376 TEST_VERSION_CURRENT,
377 new MockAnalyzer(random)).setOpenMode(
378 OpenMode.APPEND).setIndexDeletionPolicy(policy));
380 int postCount = dir.listAll().length;
381 assertTrue(postCount < preCount);
389 /* Uses KeepAllDeletionPolicy to keep all commits around,
390 * then, opens a new IndexWriter on a previous commit
392 public void testOpenPriorSnapshot() throws IOException {
393 // Never deletes a commit
394 KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy();
396 Directory dir = newDirectory();
399 IndexWriter writer = new IndexWriter(
401 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
402 setIndexDeletionPolicy(policy).
403 setMaxBufferedDocs(2).
404 setMergePolicy(newLogMergePolicy(10))
406 for(int i=0;i<10;i++) {
413 Collection<IndexCommit> commits = IndexReader.listCommits(dir);
414 assertEquals(5, commits.size());
415 IndexCommit lastCommit = null;
416 for (final IndexCommit commit : commits) {
417 if (lastCommit == null || commit.getGeneration() > lastCommit.getGeneration())
420 assertTrue(lastCommit != null);
422 // Now add 1 doc and optimize
423 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT,
424 new MockAnalyzer(random)).setIndexDeletionPolicy(policy));
426 assertEquals(11, writer.numDocs());
430 assertEquals(6, IndexReader.listCommits(dir).size());
432 // Now open writer on the commit just before optimize:
433 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
434 .setIndexDeletionPolicy(policy).setIndexCommit(lastCommit));
435 assertEquals(10, writer.numDocs());
437 // Should undo our rollback:
440 IndexReader r = IndexReader.open(dir, true);
441 // Still optimized, still 11 docs
442 assertTrue(r.isOptimized());
443 assertEquals(11, r.numDocs());
446 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
447 .setIndexDeletionPolicy(policy).setIndexCommit(lastCommit));
448 assertEquals(10, writer.numDocs());
449 // Commits the rollback:
452 // Now 8 because we made another commit
453 assertEquals(7, IndexReader.listCommits(dir).size());
455 r = IndexReader.open(dir, true);
456 // Not optimized because we rolled it back, and now only
458 assertTrue(!r.isOptimized());
459 assertEquals(10, r.numDocs());
463 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexDeletionPolicy(policy));
467 r = IndexReader.open(dir, true);
468 assertTrue(r.isOptimized());
469 assertEquals(10, r.numDocs());
472 // Now open writer on the commit just before optimize,
473 // but this time keeping only the last commit:
474 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexCommit(lastCommit));
475 assertEquals(10, writer.numDocs());
477 // Reader still sees optimized index, because writer
478 // opened on the prior commit has not yet committed:
479 r = IndexReader.open(dir, true);
480 assertTrue(r.isOptimized());
481 assertEquals(10, r.numDocs());
486 // Now reader sees unoptimized index:
487 r = IndexReader.open(dir, true);
488 assertTrue(!r.isOptimized());
489 assertEquals(10, r.numDocs());
496 /* Test keeping NO commit points. This is a viable and
497 * useful case eg where you want to build a big index and
498 * you know there are no readers.
500 public void testKeepNoneOnInitDeletionPolicy() throws IOException {
501 for(int pass=0;pass<2;pass++) {
503 boolean useCompoundFile = (pass % 2) != 0;
505 KeepNoneOnInitDeletionPolicy policy = new KeepNoneOnInitDeletionPolicy();
507 Directory dir = newDirectory();
509 IndexWriterConfig conf = newIndexWriterConfig(
510 TEST_VERSION_CURRENT, new MockAnalyzer(random))
511 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)
512 .setMaxBufferedDocs(10);
513 MergePolicy mp = conf.getMergePolicy();
514 if (mp instanceof LogMergePolicy) {
515 setUseCompoundFile(mp, useCompoundFile);
517 IndexWriter writer = new IndexWriter(dir, conf);
518 for(int i=0;i<107;i++) {
523 conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
524 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy);
525 mp = conf.getMergePolicy();
526 if (mp instanceof LogMergePolicy) {
527 setUseCompoundFile(mp, true);
529 writer = new IndexWriter(dir, conf);
533 assertEquals(1, policy.numOnInit);
534 // If we are not auto committing then there should
535 // be exactly 2 commits (one per close above):
536 assertEquals(2, policy.numOnCommit);
538 // Simplistic check: just verify the index is in fact
540 IndexReader reader = IndexReader.open(dir, true);
548 * Test a deletion policy that keeps last N commits.
550 public void testKeepLastNDeletionPolicy() throws IOException {
553 for(int pass=0;pass<2;pass++) {
555 boolean useCompoundFile = (pass % 2) != 0;
557 Directory dir = newDirectory();
559 KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);
561 for(int j=0;j<N+1;j++) {
562 IndexWriterConfig conf = newIndexWriterConfig(
563 TEST_VERSION_CURRENT, new MockAnalyzer(random))
564 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)
565 .setMaxBufferedDocs(10);
566 MergePolicy mp = conf.getMergePolicy();
567 if (mp instanceof LogMergePolicy) {
568 setUseCompoundFile(mp, useCompoundFile);
570 IndexWriter writer = new IndexWriter(dir, conf);
571 for(int i=0;i<17;i++) {
578 assertTrue(policy.numDelete > 0);
579 assertEquals(N, policy.numOnInit);
580 assertEquals(N+1, policy.numOnCommit);
582 // Simplistic check: just verify only the past N segments_N's still
583 // exist, and, I can open a reader on each:
584 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
585 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
586 for(int i=0;i<N+1;i++) {
588 IndexReader reader = IndexReader.open(dir, true);
591 fail("should have failed on commits prior to last " + N);
593 } catch (IOException e) {
599 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
609 * Test a deletion policy that keeps last N commits
610 * around, with reader doing deletes.
612 public void testKeepLastNDeletionPolicyWithReader() throws IOException {
615 for(int pass=0;pass<2;pass++) {
617 boolean useCompoundFile = (pass % 2) != 0;
619 KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);
621 Directory dir = newDirectory();
622 IndexWriterConfig conf = newIndexWriterConfig(
623 TEST_VERSION_CURRENT, new MockAnalyzer(random))
624 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy).setMergePolicy(newLogMergePolicy());
625 MergePolicy mp = conf.getMergePolicy();
626 if (mp instanceof LogMergePolicy) {
627 setUseCompoundFile(mp, useCompoundFile);
629 IndexWriter writer = new IndexWriter(dir, conf);
631 Term searchTerm = new Term("content", "aaa");
632 Query query = new TermQuery(searchTerm);
634 for(int i=0;i<N+1;i++) {
636 System.out.println("\nTEST: cycle i=" + i);
638 conf = newIndexWriterConfig(
639 TEST_VERSION_CURRENT, new MockAnalyzer(random))
640 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy).setMergePolicy(newLogMergePolicy());
641 mp = conf.getMergePolicy();
642 if (mp instanceof LogMergePolicy) {
643 setUseCompoundFile(mp, useCompoundFile);
645 writer = new IndexWriter(dir, conf);
646 writer.setInfoStream(VERBOSE ? System.out : null);
647 for(int j=0;j<17;j++) {
652 System.out.println("TEST: close writer");
655 IndexReader reader = IndexReader.open(dir, policy, false);
656 reader.deleteDocument(3*i+1);
657 reader.setNorm(4*i+1, "content", 2.0F);
658 IndexSearcher searcher = newSearcher(reader);
659 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
660 assertEquals(16*(1+i), hits.length);
663 System.out.println("TEST: close reader numOnCommit=" + policy.numOnCommit);
668 conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
669 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy);
670 mp = conf.getMergePolicy();
671 if (mp instanceof LogMergePolicy) {
672 setUseCompoundFile(mp, useCompoundFile);
674 IndexReader r = IndexReader.open(dir);
675 final boolean wasOptimized = r.isOptimized();
677 writer = new IndexWriter(dir, conf);
682 assertEquals(2*(N+1)+1, policy.numOnInit);
683 assertEquals(2*(N+2) - (wasOptimized ? 1:0), policy.numOnCommit);
685 IndexSearcher searcher = new IndexSearcher(dir, false);
686 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
687 assertEquals(176, hits.length);
689 // Simplistic check: just verify only the past N segments_N's still
690 // exist, and, I can open a reader on each:
691 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
693 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
694 int expectedCount = 176;
696 for(int i=0;i<N+1;i++) {
698 IndexReader reader = IndexReader.open(dir, true);
700 // Work backwards in commits on what the expected
702 searcher = newSearcher(reader);
703 hits = searcher.search(query, null, 1000).scoreDocs;
711 assertEquals(expectedCount, hits.length);
715 fail("should have failed on commits before last 5");
717 } catch (IOException e) {
723 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
732 * Test a deletion policy that keeps last N commits
733 * around, through creates.
735 public void testKeepLastNDeletionPolicyWithCreates() throws IOException {
739 for(int pass=0;pass<2;pass++) {
741 boolean useCompoundFile = (pass % 2) != 0;
743 KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);
745 Directory dir = newDirectory();
746 IndexWriterConfig conf = newIndexWriterConfig(
747 TEST_VERSION_CURRENT, new MockAnalyzer(random))
748 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)
749 .setMaxBufferedDocs(10);
750 MergePolicy mp = conf.getMergePolicy();
751 if (mp instanceof LogMergePolicy) {
752 setUseCompoundFile(mp, useCompoundFile);
754 IndexWriter writer = new IndexWriter(dir, conf);
756 Term searchTerm = new Term("content", "aaa");
757 Query query = new TermQuery(searchTerm);
759 for(int i=0;i<N+1;i++) {
761 conf = newIndexWriterConfig(
762 TEST_VERSION_CURRENT, new MockAnalyzer(random))
763 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy)
764 .setMaxBufferedDocs(10);
765 mp = conf.getMergePolicy();
766 if (mp instanceof LogMergePolicy) {
767 setUseCompoundFile(mp, useCompoundFile);
769 writer = new IndexWriter(dir, conf);
770 for(int j=0;j<17;j++) {
775 IndexReader reader = IndexReader.open(dir, policy, false);
776 reader.deleteDocument(3);
777 reader.setNorm(5, "content", 2.0F);
778 IndexSearcher searcher = newSearcher(reader);
779 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
780 assertEquals(16, hits.length);
785 writer = new IndexWriter(dir, newIndexWriterConfig(
786 TEST_VERSION_CURRENT, new MockAnalyzer(random))
787 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy));
788 // This will not commit: there are no changes
789 // pending because we opened for "create":
793 assertEquals(3*(N+1), policy.numOnInit);
794 assertEquals(3*(N+1)+1, policy.numOnCommit);
796 IndexSearcher searcher = new IndexSearcher(dir, false);
797 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
798 assertEquals(0, hits.length);
800 // Simplistic check: just verify only the past N segments_N's still
801 // exist, and, I can open a reader on each:
802 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
804 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
805 int expectedCount = 0;
807 for(int i=0;i<N+1;i++) {
809 IndexReader reader = IndexReader.open(dir, true);
811 // Work backwards in commits on what the expected
813 searcher = newSearcher(reader);
814 hits = searcher.search(query, null, 1000).scoreDocs;
815 assertEquals(expectedCount, hits.length);
817 if (expectedCount == 0) {
819 } else if (expectedCount == 16) {
821 } else if (expectedCount == 17) {
826 fail("should have failed on commits before last " + N);
828 } catch (IOException e) {
834 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
843 private void addDoc(IndexWriter writer) throws IOException
845 Document doc = new Document();
846 doc.add(newField("content", "aaa", Field.Store.NO, Field.Index.ANALYZED));
847 writer.addDocument(doc);