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.lang.reflect.Method;
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LuceneTestCase;
27 import org.apache.lucene.util._TestUtil;
29 public class TestTieredMergePolicy extends LuceneTestCase {
31 public void testExpungeDeletes() throws Exception {
32 Directory dir = newDirectory();
33 IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
34 TieredMergePolicy tmp = newTieredMergePolicy();
35 conf.setMergePolicy(tmp);
36 conf.setMaxBufferedDocs(4);
37 tmp.setMaxMergeAtOnce(100);
38 tmp.setSegmentsPerTier(100);
40 Class<?> clazz = Class.forName("org.apache.lucene.index.TieredMergePolicy");
41 Method m = clazz.getMethod("setForceMergeDeletesPctAllowed", double.class);
43 } catch (Exception e) {
44 throw new RuntimeException(e);
46 //tmp.setExpungeDeletesPctAllowed(30.0);
47 IndexWriter w = new IndexWriter(dir, conf);
48 w.setInfoStream(VERBOSE ? System.out : null);
49 for(int i=0;i<80;i++) {
50 Document doc = new Document();
51 doc.add(newField("content", "aaa " + (i%4), Field.Store.NO, Field.Index.ANALYZED));
54 assertEquals(80, w.maxDoc());
55 assertEquals(80, w.numDocs());
58 System.out.println("\nTEST: delete docs");
60 w.deleteDocuments(new Term("content", "0"));
63 assertEquals(80, w.maxDoc());
64 assertEquals(60, w.numDocs());
67 System.out.println("\nTEST: expunge2");
70 Class<?> clazz = Class.forName("org.apache.lucene.index.TieredMergePolicy");
71 Method m = clazz.getMethod("setForceMergeDeletesPctAllowed", double.class);
73 } catch (Exception e) {
74 throw new RuntimeException(e);
76 //tmp.setExpungeDeletesPctAllowed(10.0);
78 assertEquals(60, w.maxDoc());
79 assertEquals(60, w.numDocs());
84 public void testPartialOptimize() throws Exception {
85 int num = atLeast(10);
86 for(int iter=0;iter<num;iter++) {
88 System.out.println("TEST: iter=" + iter);
90 Directory dir = newDirectory();
91 IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
92 conf.setMergeScheduler(new SerialMergeScheduler());
93 TieredMergePolicy tmp = newTieredMergePolicy();
94 conf.setMergePolicy(tmp);
95 conf.setMaxBufferedDocs(2);
96 tmp.setMaxMergeAtOnce(3);
97 tmp.setSegmentsPerTier(6);
99 IndexWriter w = new IndexWriter(dir, conf);
100 w.setInfoStream(VERBOSE ? System.out : null);
102 final int numDocs = _TestUtil.nextInt(random, 20, 100);
103 for(int i=0;i<numDocs;i++) {
104 Document doc = new Document();
105 doc.add(newField("content", "aaa " + (i%4), Field.Store.NO, Field.Index.ANALYZED));
107 int count = w.getSegmentCount();
108 maxCount = Math.max(count, maxCount);
109 assertTrue("count=" + count + " maxCount=" + maxCount, count >= maxCount-3);
114 int segmentCount = w.getSegmentCount();
115 int targetCount = _TestUtil.nextInt(random, 1, segmentCount);
117 System.out.println("TEST: optimize to " + targetCount + " segs (current count=" + segmentCount + ")");
119 w.optimize(targetCount);
120 assertEquals(targetCount, w.getSegmentCount());