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);
78 verifyCommitOrder(commits);
84 * This is useful for adding to a big index when you know
85 * readers are not using it.
87 class KeepNoneOnInitDeletionPolicy implements IndexDeletionPolicy {
90 public void onInit(List<? extends IndexCommit> commits) throws IOException {
91 verifyCommitOrder(commits);
93 // On init, delete all commit points:
94 for (final IndexCommit commit : commits) {
96 assertTrue(commit.isDeleted());
99 public void onCommit(List<? extends IndexCommit> commits) throws IOException {
100 verifyCommitOrder(commits);
101 int size = commits.size();
102 // Delete all but last one:
103 for(int i=0;i<size-1;i++) {
104 ((IndexCommit) commits.get(i)).delete();
110 class KeepLastNDeletionPolicy implements IndexDeletionPolicy {
115 Set<String> seen = new HashSet<String>();
117 public KeepLastNDeletionPolicy(int numToKeep) {
118 this.numToKeep = numToKeep;
121 public void onInit(List<? extends IndexCommit> commits) throws IOException {
123 System.out.println("TEST: onInit");
125 verifyCommitOrder(commits);
127 // do no deletions on init
128 doDeletes(commits, false);
131 public void onCommit(List<? extends IndexCommit> commits) throws IOException {
133 System.out.println("TEST: onCommit");
135 verifyCommitOrder(commits);
136 doDeletes(commits, true);
139 private void doDeletes(List<? extends IndexCommit> commits, boolean isCommit) {
141 // Assert that we really are only called for each new
144 String fileName = ((IndexCommit) commits.get(commits.size()-1)).getSegmentsFileName();
145 if (seen.contains(fileName)) {
146 throw new RuntimeException("onCommit was called twice on the same commit point: " + fileName);
151 int size = commits.size();
152 for(int i=0;i<size-numToKeep;i++) {
153 ((IndexCommit) commits.get(i)).delete();
160 * Delete a commit only when it has been obsoleted by N
163 class ExpirationTimeDeletionPolicy implements IndexDeletionPolicy {
166 double expirationTimeSeconds;
169 public ExpirationTimeDeletionPolicy(Directory dir, double seconds) {
171 this.expirationTimeSeconds = seconds;
174 public void onInit(List<? extends IndexCommit> commits) throws IOException {
175 verifyCommitOrder(commits);
179 public void onCommit(List<? extends IndexCommit> commits) throws IOException {
180 verifyCommitOrder(commits);
182 IndexCommit lastCommit = commits.get(commits.size()-1);
184 // Any commit older than expireTime should be deleted:
185 double expireTime = dir.fileModified(lastCommit.getSegmentsFileName())/1000.0 - expirationTimeSeconds;
187 for (final IndexCommit commit : commits) {
188 double modTime = dir.fileModified(commit.getSegmentsFileName())/1000.0;
189 if (commit != lastCommit && modTime < expireTime) {
198 * Test "by time expiration" deletion policy:
200 public void testExpirationTimeDeletionPolicy() throws IOException, InterruptedException {
202 final double SECONDS = 2.0;
204 Directory dir = newDirectory();
205 ExpirationTimeDeletionPolicy policy = new ExpirationTimeDeletionPolicy(dir, SECONDS);
206 IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT,
207 new MockAnalyzer(random))
208 .setIndexDeletionPolicy(policy);
209 MergePolicy mp = conf.getMergePolicy();
210 if (mp instanceof LogMergePolicy) {
211 setUseCompoundFile(mp, true);
213 IndexWriter writer = new IndexWriter(dir, conf);
218 long lastDeleteTime = 0;
219 for(int i=0;i<ITER;i++) {
220 // Record last time when writer performed deletes of
222 lastDeleteTime = System.currentTimeMillis();
223 conf = newIndexWriterConfig(TEST_VERSION_CURRENT,
224 new MockAnalyzer(random)).setOpenMode(
225 OpenMode.APPEND).setIndexDeletionPolicy(policy);
226 mp = conf.getMergePolicy();
227 if (mp instanceof LogMergePolicy) {
228 setUseCompoundFile(mp, true);
230 writer = new IndexWriter(dir, conf);
231 for(int j=0;j<17;j++) {
237 // Make sure to sleep long enough so that some commit
238 // points will be deleted:
239 Thread.sleep((int) (1000.0*(SECONDS/5.0)));
243 // First, make sure the policy in fact deleted something:
244 assertTrue("no commits were deleted", policy.numDelete > 0);
246 // Then simplistic check: just verify that the
247 // segments_N's that still exist are in fact within SECONDS
248 // seconds of the last one's mod time, and, that I can
249 // open a reader on each:
250 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
252 String fileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
255 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
257 boolean oneSecondResolution = true;
261 IndexReader reader = IndexReader.open(dir, true);
263 fileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
267 // if we are on a filesystem that seems to have only
268 // 1 second resolution, allow +1 second in commit
270 long modTime = dir.fileModified(fileName);
271 oneSecondResolution &= (modTime % 1000) == 0;
272 final long leeway = (long) ((SECONDS + (oneSecondResolution ? 1.0:0.0))*1000);
274 assertTrue("commit point was older than " + SECONDS + " seconds (" + (lastDeleteTime - modTime) + " msec) but did not get deleted ", lastDeleteTime - modTime <= leeway);
275 } catch (IOException e) {
280 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
288 * Test a silly deletion policy that keeps all commits around.
290 public void testKeepAllDeletionPolicy() throws IOException {
291 for(int pass=0;pass<2;pass++) {
294 System.out.println("TEST: cycle pass=" + pass);
297 boolean useCompoundFile = (pass % 2) != 0;
299 // Never deletes a commit
300 KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy();
302 Directory dir = newDirectory();
305 IndexWriterConfig conf = newIndexWriterConfig(
306 TEST_VERSION_CURRENT, new MockAnalyzer(random))
307 .setIndexDeletionPolicy(policy).setMaxBufferedDocs(10)
308 .setMergeScheduler(new SerialMergeScheduler());
309 MergePolicy mp = conf.getMergePolicy();
310 if (mp instanceof LogMergePolicy) {
311 setUseCompoundFile(mp, useCompoundFile);
313 IndexWriter writer = new IndexWriter(dir, conf);
314 for(int i=0;i<107;i++) {
319 final boolean isOptimized;
321 IndexReader r = IndexReader.open(dir);
322 isOptimized = r.isOptimized();
326 conf = newIndexWriterConfig(TEST_VERSION_CURRENT,
327 new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setOpenMode(
328 OpenMode.APPEND).setIndexDeletionPolicy(policy);
329 mp = conf.getMergePolicy();
330 if (mp instanceof LogMergePolicy) {
331 setUseCompoundFile(mp, true);
334 System.out.println("TEST: open writer for optimize");
336 writer = new IndexWriter(dir, conf);
337 writer.setInfoStream(VERBOSE ? System.out : null);
341 assertEquals(isOptimized ? 0:1, policy.numOnInit);
343 // If we are not auto committing then there should
344 // be exactly 2 commits (one per close above):
345 assertEquals(1 + (isOptimized ? 0:1), policy.numOnCommit);
348 Collection<IndexCommit> commits = IndexReader.listCommits(dir);
349 // 2 from closing writer
350 assertEquals(1 + (isOptimized ? 0:1), commits.size());
352 // Make sure we can open a reader on each commit:
353 for (final IndexCommit commit : commits) {
354 IndexReader r = IndexReader.open(commit, null, false);
358 // Simplistic check: just verify all segments_N's still
359 // exist, and, I can open a reader on each:
360 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
361 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
363 IndexReader reader = IndexReader.open(dir, true);
365 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
369 // Now that we've removed a commit point, which
370 // should have orphan'd at least one index file.
371 // Open & close a writer and assert that it
372 // actually removed something:
373 int preCount = dir.listAll().length;
374 writer = new IndexWriter(dir, newIndexWriterConfig(
375 TEST_VERSION_CURRENT,
376 new MockAnalyzer(random)).setOpenMode(
377 OpenMode.APPEND).setIndexDeletionPolicy(policy));
379 int postCount = dir.listAll().length;
380 assertTrue(postCount < preCount);
388 /* Uses KeepAllDeletionPolicy to keep all commits around,
389 * then, opens a new IndexWriter on a previous commit
391 public void testOpenPriorSnapshot() throws IOException {
392 // Never deletes a commit
393 KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy();
395 Directory dir = newDirectory();
398 IndexWriter writer = new IndexWriter(
400 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
401 setIndexDeletionPolicy(policy).
402 setMaxBufferedDocs(2).
403 setMergePolicy(newLogMergePolicy(10))
405 for(int i=0;i<10;i++) {
412 Collection<IndexCommit> commits = IndexReader.listCommits(dir);
413 assertEquals(5, commits.size());
414 IndexCommit lastCommit = null;
415 for (final IndexCommit commit : commits) {
416 if (lastCommit == null || commit.getGeneration() > lastCommit.getGeneration())
419 assertTrue(lastCommit != null);
421 // Now add 1 doc and optimize
422 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT,
423 new MockAnalyzer(random)).setIndexDeletionPolicy(policy));
425 assertEquals(11, writer.numDocs());
429 assertEquals(6, IndexReader.listCommits(dir).size());
431 // Now open writer on the commit just before optimize:
432 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
433 .setIndexDeletionPolicy(policy).setIndexCommit(lastCommit));
434 assertEquals(10, writer.numDocs());
436 // Should undo our rollback:
439 IndexReader r = IndexReader.open(dir, true);
440 // Still optimized, still 11 docs
441 assertTrue(r.isOptimized());
442 assertEquals(11, r.numDocs());
445 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
446 .setIndexDeletionPolicy(policy).setIndexCommit(lastCommit));
447 assertEquals(10, writer.numDocs());
448 // Commits the rollback:
451 // Now 8 because we made another commit
452 assertEquals(7, IndexReader.listCommits(dir).size());
454 r = IndexReader.open(dir, true);
455 // Not optimized because we rolled it back, and now only
457 assertTrue(!r.isOptimized());
458 assertEquals(10, r.numDocs());
462 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexDeletionPolicy(policy));
466 r = IndexReader.open(dir, true);
467 assertTrue(r.isOptimized());
468 assertEquals(10, r.numDocs());
471 // Now open writer on the commit just before optimize,
472 // but this time keeping only the last commit:
473 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setIndexCommit(lastCommit));
474 assertEquals(10, writer.numDocs());
476 // Reader still sees optimized index, because writer
477 // opened on the prior commit has not yet committed:
478 r = IndexReader.open(dir, true);
479 assertTrue(r.isOptimized());
480 assertEquals(10, r.numDocs());
485 // Now reader sees unoptimized index:
486 r = IndexReader.open(dir, true);
487 assertTrue(!r.isOptimized());
488 assertEquals(10, r.numDocs());
495 /* Test keeping NO commit points. This is a viable and
496 * useful case eg where you want to build a big index and
497 * you know there are no readers.
499 public void testKeepNoneOnInitDeletionPolicy() throws IOException {
500 for(int pass=0;pass<2;pass++) {
502 boolean useCompoundFile = (pass % 2) != 0;
504 KeepNoneOnInitDeletionPolicy policy = new KeepNoneOnInitDeletionPolicy();
506 Directory dir = newDirectory();
508 IndexWriterConfig conf = newIndexWriterConfig(
509 TEST_VERSION_CURRENT, new MockAnalyzer(random))
510 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)
511 .setMaxBufferedDocs(10);
512 MergePolicy mp = conf.getMergePolicy();
513 if (mp instanceof LogMergePolicy) {
514 setUseCompoundFile(mp, useCompoundFile);
516 IndexWriter writer = new IndexWriter(dir, conf);
517 for(int i=0;i<107;i++) {
522 conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
523 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy);
524 mp = conf.getMergePolicy();
525 if (mp instanceof LogMergePolicy) {
526 setUseCompoundFile(mp, true);
528 writer = new IndexWriter(dir, conf);
532 assertEquals(1, policy.numOnInit);
533 // If we are not auto committing then there should
534 // be exactly 2 commits (one per close above):
535 assertEquals(2, policy.numOnCommit);
537 // Simplistic check: just verify the index is in fact
539 IndexReader reader = IndexReader.open(dir, true);
547 * Test a deletion policy that keeps last N commits.
549 public void testKeepLastNDeletionPolicy() throws IOException {
552 for(int pass=0;pass<2;pass++) {
554 boolean useCompoundFile = (pass % 2) != 0;
556 Directory dir = newDirectory();
558 KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);
560 for(int j=0;j<N+1;j++) {
561 IndexWriterConfig conf = newIndexWriterConfig(
562 TEST_VERSION_CURRENT, new MockAnalyzer(random))
563 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)
564 .setMaxBufferedDocs(10);
565 MergePolicy mp = conf.getMergePolicy();
566 if (mp instanceof LogMergePolicy) {
567 setUseCompoundFile(mp, useCompoundFile);
569 IndexWriter writer = new IndexWriter(dir, conf);
570 for(int i=0;i<17;i++) {
577 assertTrue(policy.numDelete > 0);
578 assertEquals(N, policy.numOnInit);
579 assertEquals(N+1, policy.numOnCommit);
581 // Simplistic check: just verify only the past N segments_N's still
582 // exist, and, I can open a reader on each:
583 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
584 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
585 for(int i=0;i<N+1;i++) {
587 IndexReader reader = IndexReader.open(dir, true);
590 fail("should have failed on commits prior to last " + N);
592 } catch (IOException e) {
598 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
608 * Test a deletion policy that keeps last N commits
609 * around, with reader doing deletes.
611 public void testKeepLastNDeletionPolicyWithReader() throws IOException {
614 for(int pass=0;pass<2;pass++) {
616 boolean useCompoundFile = (pass % 2) != 0;
618 KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);
620 Directory dir = newDirectory();
621 IndexWriterConfig conf = newIndexWriterConfig(
622 TEST_VERSION_CURRENT, new MockAnalyzer(random))
623 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy).setMergePolicy(newLogMergePolicy());
624 MergePolicy mp = conf.getMergePolicy();
625 if (mp instanceof LogMergePolicy) {
626 setUseCompoundFile(mp, useCompoundFile);
628 IndexWriter writer = new IndexWriter(dir, conf);
630 Term searchTerm = new Term("content", "aaa");
631 Query query = new TermQuery(searchTerm);
633 for(int i=0;i<N+1;i++) {
635 System.out.println("\nTEST: cycle i=" + i);
637 conf = newIndexWriterConfig(
638 TEST_VERSION_CURRENT, new MockAnalyzer(random))
639 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy).setMergePolicy(newLogMergePolicy());
640 mp = conf.getMergePolicy();
641 if (mp instanceof LogMergePolicy) {
642 setUseCompoundFile(mp, useCompoundFile);
644 writer = new IndexWriter(dir, conf);
645 writer.setInfoStream(VERBOSE ? System.out : null);
646 for(int j=0;j<17;j++) {
651 System.out.println("TEST: close writer");
654 IndexReader reader = IndexReader.open(dir, policy, false);
655 reader.deleteDocument(3*i+1);
656 reader.setNorm(4*i+1, "content", 2.0F);
657 IndexSearcher searcher = newSearcher(reader);
658 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
659 assertEquals(16*(1+i), hits.length);
662 System.out.println("TEST: close reader numOnCommit=" + policy.numOnCommit);
667 conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
668 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy);
669 mp = conf.getMergePolicy();
670 if (mp instanceof LogMergePolicy) {
671 setUseCompoundFile(mp, useCompoundFile);
673 IndexReader r = IndexReader.open(dir);
674 final boolean wasOptimized = r.isOptimized();
676 writer = new IndexWriter(dir, conf);
681 assertEquals(2*(N+1)+1, policy.numOnInit);
682 assertEquals(2*(N+2) - (wasOptimized ? 1:0), policy.numOnCommit);
684 IndexSearcher searcher = new IndexSearcher(dir, false);
685 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
686 assertEquals(176, hits.length);
688 // Simplistic check: just verify only the past N segments_N's still
689 // exist, and, I can open a reader on each:
690 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
692 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
693 int expectedCount = 176;
695 for(int i=0;i<N+1;i++) {
697 IndexReader reader = IndexReader.open(dir, true);
699 // Work backwards in commits on what the expected
701 searcher = newSearcher(reader);
702 hits = searcher.search(query, null, 1000).scoreDocs;
710 assertEquals(expectedCount, hits.length);
714 fail("should have failed on commits before last 5");
716 } catch (IOException e) {
722 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
731 * Test a deletion policy that keeps last N commits
732 * around, through creates.
734 public void testKeepLastNDeletionPolicyWithCreates() throws IOException {
738 for(int pass=0;pass<2;pass++) {
740 boolean useCompoundFile = (pass % 2) != 0;
742 KeepLastNDeletionPolicy policy = new KeepLastNDeletionPolicy(N);
744 Directory dir = newDirectory();
745 IndexWriterConfig conf = newIndexWriterConfig(
746 TEST_VERSION_CURRENT, new MockAnalyzer(random))
747 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy)
748 .setMaxBufferedDocs(10);
749 MergePolicy mp = conf.getMergePolicy();
750 if (mp instanceof LogMergePolicy) {
751 setUseCompoundFile(mp, useCompoundFile);
753 IndexWriter writer = new IndexWriter(dir, conf);
755 Term searchTerm = new Term("content", "aaa");
756 Query query = new TermQuery(searchTerm);
758 for(int i=0;i<N+1;i++) {
760 conf = newIndexWriterConfig(
761 TEST_VERSION_CURRENT, new MockAnalyzer(random))
762 .setOpenMode(OpenMode.APPEND).setIndexDeletionPolicy(policy)
763 .setMaxBufferedDocs(10);
764 mp = conf.getMergePolicy();
765 if (mp instanceof LogMergePolicy) {
766 setUseCompoundFile(mp, useCompoundFile);
768 writer = new IndexWriter(dir, conf);
769 for(int j=0;j<17;j++) {
774 IndexReader reader = IndexReader.open(dir, policy, false);
775 reader.deleteDocument(3);
776 reader.setNorm(5, "content", 2.0F);
777 IndexSearcher searcher = newSearcher(reader);
778 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
779 assertEquals(16, hits.length);
784 writer = new IndexWriter(dir, newIndexWriterConfig(
785 TEST_VERSION_CURRENT, new MockAnalyzer(random))
786 .setOpenMode(OpenMode.CREATE).setIndexDeletionPolicy(policy));
787 // This will not commit: there are no changes
788 // pending because we opened for "create":
792 assertEquals(3*(N+1), policy.numOnInit);
793 assertEquals(3*(N+1)+1, policy.numOnCommit);
795 IndexSearcher searcher = new IndexSearcher(dir, false);
796 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
797 assertEquals(0, hits.length);
799 // Simplistic check: just verify only the past N segments_N's still
800 // exist, and, I can open a reader on each:
801 long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
803 dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
804 int expectedCount = 0;
806 for(int i=0;i<N+1;i++) {
808 IndexReader reader = IndexReader.open(dir, true);
810 // Work backwards in commits on what the expected
812 searcher = newSearcher(reader);
813 hits = searcher.search(query, null, 1000).scoreDocs;
814 assertEquals(expectedCount, hits.length);
816 if (expectedCount == 0) {
818 } else if (expectedCount == 16) {
820 } else if (expectedCount == 17) {
825 fail("should have failed on commits before last " + N);
827 } catch (IOException e) {
833 dir.deleteFile(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
842 private void addDoc(IndexWriter writer) throws IOException
844 Document doc = new Document();
845 doc.add(newField("content", "aaa", Field.Store.NO, Field.Index.ANALYZED));
846 writer.addDocument(doc);