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 org.apache.lucene.analysis.MockAnalyzer;
21 import org.apache.lucene.document.Document;
22 import org.apache.lucene.document.Field;
23 import org.apache.lucene.store.Directory;
24 import org.apache.lucene.util.LuceneTestCase;
25 import org.apache.lucene.util._TestUtil;
27 public class TestTieredMergePolicy extends LuceneTestCase {
29 public void testExpungeDeletes() throws Exception {
30 Directory dir = newDirectory();
31 IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
32 TieredMergePolicy tmp = newTieredMergePolicy();
33 conf.setMergePolicy(tmp);
34 conf.setMaxBufferedDocs(4);
35 tmp.setMaxMergeAtOnce(100);
36 tmp.setSegmentsPerTier(100);
37 tmp.setExpungeDeletesPctAllowed(30.0);
38 IndexWriter w = new IndexWriter(dir, conf);
39 w.setInfoStream(VERBOSE ? System.out : null);
40 for(int i=0;i<80;i++) {
41 Document doc = new Document();
42 doc.add(newField("content", "aaa " + (i%4), Field.Store.NO, Field.Index.ANALYZED));
45 assertEquals(80, w.maxDoc());
46 assertEquals(80, w.numDocs());
49 System.out.println("\nTEST: delete docs");
51 w.deleteDocuments(new Term("content", "0"));
54 assertEquals(80, w.maxDoc());
55 assertEquals(60, w.numDocs());
58 System.out.println("\nTEST: expunge2");
60 tmp.setExpungeDeletesPctAllowed(10.0);
62 assertEquals(60, w.maxDoc());
63 assertEquals(60, w.numDocs());
68 public void testPartialOptimize() throws Exception {
69 int num = atLeast(10);
70 for(int iter=0;iter<num;iter++) {
72 System.out.println("TEST: iter=" + iter);
74 Directory dir = newDirectory();
75 IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
76 conf.setMergeScheduler(new SerialMergeScheduler());
77 TieredMergePolicy tmp = newTieredMergePolicy();
78 conf.setMergePolicy(tmp);
79 conf.setMaxBufferedDocs(2);
80 tmp.setMaxMergeAtOnce(3);
81 tmp.setSegmentsPerTier(6);
83 IndexWriter w = new IndexWriter(dir, conf);
84 w.setInfoStream(VERBOSE ? System.out : null);
86 final int numDocs = _TestUtil.nextInt(random, 20, 100);
87 for(int i=0;i<numDocs;i++) {
88 Document doc = new Document();
89 doc.add(newField("content", "aaa " + (i%4), Field.Store.NO, Field.Index.ANALYZED));
91 int count = w.getSegmentCount();
92 maxCount = Math.max(count, maxCount);
93 assertTrue("count=" + count + " maxCount=" + maxCount, count >= maxCount-3);
98 int segmentCount = w.getSegmentCount();
99 int targetCount = _TestUtil.nextInt(random, 1, segmentCount);
101 System.out.println("TEST: optimize to " + targetCount + " segs (current count=" + segmentCount + ")");
103 w.optimize(targetCount);
104 assertEquals(targetCount, w.getSegmentCount());