add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / misc / src / test / org / apache / lucene / index / TestMultiPassIndexSplitter.java
1 package org.apache.lucene.index;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.lucene.analysis.MockAnalyzer;
20
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
26 public class TestMultiPassIndexSplitter extends LuceneTestCase {
27   IndexReader input;
28   int NUM_DOCS = 11;
29   Directory dir;
30   
31   @Override
32   public void setUp() throws Exception {
33     super.setUp();
34     dir = newDirectory();
35     IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
36     Document doc;
37     for (int i = 0; i < NUM_DOCS; i++) {
38       doc = new Document();
39       doc.add(newField("id", i + "", Field.Store.YES, Field.Index.NOT_ANALYZED));
40       doc.add(newField("f", i + " " + i, Field.Store.YES, Field.Index.ANALYZED));
41       w.addDocument(doc);
42     }
43     w.close();
44     input = IndexReader.open(dir, false);
45     // delete the last doc
46     input.deleteDocument(input.maxDoc() - 1);
47     IndexReader inputOld = input;
48     input = input.reopen(true);
49     inputOld.close();
50   }
51   
52   @Override
53   public void tearDown() throws Exception {
54     input.close();
55     dir.close();
56     super.tearDown();
57   }
58   
59   /**
60    * Test round-robin splitting.
61    */
62   public void testSplitRR() throws Exception {
63     MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
64     Directory[] dirs = new Directory[]{
65             newDirectory(),
66             newDirectory(),
67             newDirectory()
68     };
69     splitter.split(TEST_VERSION_CURRENT, input, dirs, false);
70     IndexReader ir;
71     ir = IndexReader.open(dirs[0], true);
72     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1); // rounding error
73     Document doc = ir.document(0);
74     assertEquals("0", doc.get("id"));
75     Term t;
76     TermEnum te;
77     t = new Term("id", "1");
78     te = ir.terms(t);
79     assertNotSame(t, te.term());
80     ir.close();
81     ir = IndexReader.open(dirs[1], true);
82     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
83     doc = ir.document(0);
84     assertEquals("1", doc.get("id"));
85     t = new Term("id", "0");
86     te = ir.terms(t);
87     assertNotSame(t, te.term());
88     ir.close();
89     ir = IndexReader.open(dirs[2], true);
90     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
91     doc = ir.document(0);
92     assertEquals("2", doc.get("id"));
93     t = new Term("id", "1");
94     te = ir.terms(t);
95     assertNotSame(t, te.term());
96     t = new Term("id", "0");
97     te = ir.terms(t);
98     assertNotSame(t, te.term());    
99     ir.close();
100     for (Directory d : dirs)
101       d.close();
102   }
103   
104   /**
105    * Test sequential splitting.
106    */
107   public void testSplitSeq() throws Exception {
108     MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
109     Directory[] dirs = new Directory[]{
110             newDirectory(),
111             newDirectory(),
112             newDirectory()
113     };
114     splitter.split(TEST_VERSION_CURRENT, input, dirs, true);
115     IndexReader ir;
116     ir = IndexReader.open(dirs[0], true);
117     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
118     Document doc = ir.document(0);
119     assertEquals("0", doc.get("id"));
120     int start = ir.numDocs();
121     ir.close();
122     ir = IndexReader.open(dirs[1], true);
123     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
124     doc = ir.document(0);
125     assertEquals(start + "", doc.get("id"));
126     start += ir.numDocs();
127     ir.close();
128     ir = IndexReader.open(dirs[2], true);
129     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
130     doc = ir.document(0);
131     assertEquals(start + "", doc.get("id"));
132     // make sure the deleted doc is not here
133     Term t;
134     TermEnum te;
135     t = new Term("id", (NUM_DOCS - 1) + "");
136     te = ir.terms(t);
137     assertNotSame(t, te.term());    
138     ir.close();
139     for (Directory d : dirs)
140       d.close();
141   }
142 }