pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / index / TestTieredMergePolicy.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.lang.reflect.Method;
21
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;
28
29 public class TestTieredMergePolicy extends LuceneTestCase {
30
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);
39     try {
40       Class<?> clazz = Class.forName("org.apache.lucene.index.TieredMergePolicy");
41       Method m = clazz.getMethod("setForceMergeDeletesPctAllowed", double.class);
42       m.invoke(tmp, 30.0);
43     } catch (Exception e) {
44       throw new RuntimeException(e);
45     }
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));
52       w.addDocument(doc);
53     }
54     assertEquals(80, w.maxDoc());
55     assertEquals(80, w.numDocs());
56
57     if (VERBOSE) {
58       System.out.println("\nTEST: delete docs");
59     }
60     w.deleteDocuments(new Term("content", "0"));
61     w.expungeDeletes();
62
63     assertEquals(80, w.maxDoc());
64     assertEquals(60, w.numDocs());
65
66     if (VERBOSE) {
67       System.out.println("\nTEST: expunge2");
68     }
69     try {
70       Class<?> clazz = Class.forName("org.apache.lucene.index.TieredMergePolicy");
71       Method m = clazz.getMethod("setForceMergeDeletesPctAllowed", double.class);
72       m.invoke(tmp, 10.0);
73     } catch (Exception e) {
74       throw new RuntimeException(e);
75     }
76     //tmp.setExpungeDeletesPctAllowed(10.0);
77     w.expungeDeletes();
78     assertEquals(60, w.maxDoc());
79     assertEquals(60, w.numDocs());
80     w.close();
81     dir.close();
82   }
83
84   public void testPartialOptimize() throws Exception {
85     int num = atLeast(10);
86     for(int iter=0;iter<num;iter++) {
87       if (VERBOSE) {
88         System.out.println("TEST: iter=" + iter);
89       }
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);
98
99       IndexWriter w = new IndexWriter(dir, conf);
100       w.setInfoStream(VERBOSE ? System.out : null);
101       int maxCount = 0;
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));
106         w.addDocument(doc);
107         int count = w.getSegmentCount();
108         maxCount = Math.max(count, maxCount);
109         assertTrue("count=" + count + " maxCount=" + maxCount, count >= maxCount-3);
110       }
111
112       w.flush(true, true);
113
114       int segmentCount = w.getSegmentCount();
115       int targetCount = _TestUtil.nextInt(random, 1, segmentCount);
116       if (VERBOSE) {
117         System.out.println("TEST: optimize to " + targetCount + " segs (current count=" + segmentCount + ")");
118       }
119       w.optimize(targetCount);
120       assertEquals(targetCount, w.getSegmentCount());
121
122       w.close();
123       dir.close();
124     }
125   }
126 }