pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.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   }
48   
49   @Override
50   public void tearDown() throws Exception {
51     input.close();
52     dir.close();
53     super.tearDown();
54   }
55   
56   /**
57    * Test round-robin splitting.
58    */
59   public void testSplitRR() throws Exception {
60     MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
61     Directory[] dirs = new Directory[]{
62             newDirectory(),
63             newDirectory(),
64             newDirectory()
65     };
66     splitter.split(TEST_VERSION_CURRENT, input, dirs, false);
67     IndexReader ir;
68     ir = IndexReader.open(dirs[0], true);
69     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1); // rounding error
70     Document doc = ir.document(0);
71     assertEquals("0", doc.get("id"));
72     Term t;
73     TermEnum te;
74     t = new Term("id", "1");
75     te = ir.terms(t);
76     assertNotSame(t, te.term());
77     ir.close();
78     ir = IndexReader.open(dirs[1], true);
79     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
80     doc = ir.document(0);
81     assertEquals("1", doc.get("id"));
82     t = new Term("id", "0");
83     te = ir.terms(t);
84     assertNotSame(t, te.term());
85     ir.close();
86     ir = IndexReader.open(dirs[2], true);
87     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
88     doc = ir.document(0);
89     assertEquals("2", doc.get("id"));
90     t = new Term("id", "1");
91     te = ir.terms(t);
92     assertNotSame(t, te.term());
93     t = new Term("id", "0");
94     te = ir.terms(t);
95     assertNotSame(t, te.term());    
96     ir.close();
97     for (Directory d : dirs)
98       d.close();
99   }
100   
101   /**
102    * Test sequential splitting.
103    */
104   public void testSplitSeq() throws Exception {
105     MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
106     Directory[] dirs = new Directory[]{
107             newDirectory(),
108             newDirectory(),
109             newDirectory()
110     };
111     splitter.split(TEST_VERSION_CURRENT, input, dirs, true);
112     IndexReader ir;
113     ir = IndexReader.open(dirs[0], true);
114     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
115     Document doc = ir.document(0);
116     assertEquals("0", doc.get("id"));
117     int start = ir.numDocs();
118     ir.close();
119     ir = IndexReader.open(dirs[1], true);
120     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
121     doc = ir.document(0);
122     assertEquals(start + "", doc.get("id"));
123     start += ir.numDocs();
124     ir.close();
125     ir = IndexReader.open(dirs[2], true);
126     assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
127     doc = ir.document(0);
128     assertEquals(start + "", doc.get("id"));
129     // make sure the deleted doc is not here
130     Term t;
131     TermEnum te;
132     t = new Term("id", (NUM_DOCS - 1) + "");
133     te = ir.terms(t);
134     assertNotSame(t, te.term());    
135     ir.close();
136     for (Directory d : dirs)
137       d.close();
138   }
139 }