1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with this
6 * work for additional information regarding copyright ownership. The ASF
7 * licenses this file to You under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
20 import java.text.DecimalFormat;
21 import java.text.NumberFormat;
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.analysis.MockTokenizer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field.Index;
27 import org.apache.lucene.document.Field.Store;
28 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
32 public class TestPKIndexSplitter extends LuceneTestCase {
34 public void testSplit() throws Exception {
35 NumberFormat format = new DecimalFormat("000000000");
36 Directory dir = newDirectory();
37 IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
38 TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false))
39 .setOpenMode(OpenMode.CREATE));
40 for (int x = 0; x < 11; x++) {
41 Document doc = createDocument(x, "1", 3, format);
44 for (int x = 11; x < 20; x++) {
45 Document doc = createDocument(x, "2", 3, format);
50 final Term midTerm = new Term("id", format.format(11));
52 checkSplitting(dir, midTerm, 11, 9);
54 // delete some documents
55 w = new IndexWriter(dir, newIndexWriterConfig(
56 TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false))
57 .setOpenMode(OpenMode.APPEND));
58 w.deleteDocuments(midTerm);
59 w.deleteDocuments(new Term("id", format.format(2)));
62 checkSplitting(dir, midTerm, 10, 8);
67 private void checkSplitting(Directory dir, Term splitTerm, int leftCount, int rightCount) throws Exception {
68 Directory dir1 = newDirectory();
69 Directory dir2 = newDirectory();
70 PKIndexSplitter splitter = new PKIndexSplitter(dir, dir1, dir2, splitTerm,
71 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)),
72 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
75 IndexReader ir1 = IndexReader.open(dir1);
76 IndexReader ir2 = IndexReader.open(dir2);
77 assertEquals(leftCount, ir1.numDocs());
78 assertEquals(rightCount, ir2.numDocs());
80 checkContents(ir1, "1");
81 checkContents(ir2, "2");
90 private void checkContents(IndexReader ir, String indexname) throws Exception {
91 for (int i = 0; i < ir.maxDoc(); i++) {
92 if (!ir.isDeleted(i)) {
93 assertEquals(indexname, ir.document(i).get("indexname"));
98 private Document createDocument(int n, String indexName,
99 int numFields, NumberFormat format) {
100 StringBuilder sb = new StringBuilder();
101 Document doc = new Document();
102 String id = format.format(n);
103 doc.add(newField("id", id, Store.YES, Index.NOT_ANALYZED));
104 doc.add(newField("indexname", indexName, Store.YES, Index.NOT_ANALYZED));
107 doc.add(newField("field1", sb.toString(), Store.YES, Index.ANALYZED));
110 for (int i = 1; i < numFields; i++) {
111 doc.add(newField("field" + (i + 1), sb.toString(), Store.YES, Index.ANALYZED));