add --shared
[pylucene.git] / lucene-java-3.4.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 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;
26
27 public class TestTieredMergePolicy extends LuceneTestCase {
28
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));
43       w.addDocument(doc);
44     }
45     assertEquals(80, w.maxDoc());
46     assertEquals(80, w.numDocs());
47
48     if (VERBOSE) {
49       System.out.println("\nTEST: delete docs");
50     }
51     w.deleteDocuments(new Term("content", "0"));
52     w.expungeDeletes();
53
54     assertEquals(80, w.maxDoc());
55     assertEquals(60, w.numDocs());
56
57     if (VERBOSE) {
58       System.out.println("\nTEST: expunge2");
59     }
60     tmp.setExpungeDeletesPctAllowed(10.0);
61     w.expungeDeletes();
62     assertEquals(60, w.maxDoc());
63     assertEquals(60, w.numDocs());
64     w.close();
65     dir.close();
66   }
67
68   public void testPartialOptimize() throws Exception {
69     int num = atLeast(10);
70     for(int iter=0;iter<num;iter++) {
71       if (VERBOSE) {
72         System.out.println("TEST: iter=" + iter);
73       }
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);
82
83       IndexWriter w = new IndexWriter(dir, conf);
84       w.setInfoStream(VERBOSE ? System.out : null);
85       int maxCount = 0;
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));
90         w.addDocument(doc);
91         int count = w.getSegmentCount();
92         maxCount = Math.max(count, maxCount);
93         assertTrue("count=" + count + " maxCount=" + maxCount, count >= maxCount-3);
94       }
95
96       w.flush(true, true);
97
98       int segmentCount = w.getSegmentCount();
99       int targetCount = _TestUtil.nextInt(random, 1, segmentCount);
100       if (VERBOSE) {
101         System.out.println("TEST: optimize to " + targetCount + " segs (current count=" + segmentCount + ")");
102       }
103       w.optimize(targetCount);
104       assertEquals(targetCount, w.getSegmentCount());
105
106       w.close();
107       dir.close();
108     }
109   }
110 }