1 package org.apache.lucene.index;
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 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;
26 public class TestMultiPassIndexSplitter extends LuceneTestCase {
32 public void setUp() throws Exception {
35 IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
37 for (int i = 0; i < NUM_DOCS; i++) {
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));
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);
53 public void tearDown() throws Exception {
60 * Test round-robin splitting.
62 public void testSplitRR() throws Exception {
63 MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
64 Directory[] dirs = new Directory[]{
69 splitter.split(TEST_VERSION_CURRENT, input, dirs, false);
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"));
77 t = new Term("id", "1");
79 assertNotSame(t, te.term());
81 ir = IndexReader.open(dirs[1], true);
82 assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
84 assertEquals("1", doc.get("id"));
85 t = new Term("id", "0");
87 assertNotSame(t, te.term());
89 ir = IndexReader.open(dirs[2], true);
90 assertTrue(ir.numDocs() - NUM_DOCS / 3 <= 1);
92 assertEquals("2", doc.get("id"));
93 t = new Term("id", "1");
95 assertNotSame(t, te.term());
96 t = new Term("id", "0");
98 assertNotSame(t, te.term());
100 for (Directory d : dirs)
105 * Test sequential splitting.
107 public void testSplitSeq() throws Exception {
108 MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
109 Directory[] dirs = new Directory[]{
114 splitter.split(TEST_VERSION_CURRENT, input, dirs, true);
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();
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();
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
135 t = new Term("id", (NUM_DOCS - 1) + "");
137 assertNotSame(t, te.term());
139 for (Directory d : dirs)