add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / misc / src / test / org / apache / lucene / index / TestPKIndexSplitter.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 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
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, 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
17  * the License.
18  */
19
20 import java.text.DecimalFormat;
21 import java.text.NumberFormat;
22
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;
31
32 public class TestPKIndexSplitter extends LuceneTestCase {
33
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);
42       w.addDocument(doc);
43     }
44     for (int x = 11; x < 20; x++) {
45       Document doc = createDocument(x, "2", 3, format);
46       w.addDocument(doc);
47     }
48     w.close();
49     
50     final Term midTerm = new Term("id", format.format(11));
51     
52     checkSplitting(dir, midTerm, 11, 9);
53     
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)));
60     w.close();
61     
62     checkSplitting(dir, midTerm, 10, 8);
63     
64     dir.close();
65   }
66   
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)));
73     splitter.split();
74     
75     IndexReader ir1 = IndexReader.open(dir1);
76     IndexReader ir2 = IndexReader.open(dir2);
77     assertEquals(leftCount, ir1.numDocs());
78     assertEquals(rightCount, ir2.numDocs());
79     
80     checkContents(ir1, "1");
81     checkContents(ir2, "2");
82     
83     ir1.close();
84     ir2.close();
85     
86     dir1.close();
87     dir2.close();
88   }
89   
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"));
94       }
95     }
96   }
97   
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));
105     sb.append("a");
106     sb.append(n);
107     doc.add(newField("field1", sb.toString(), Store.YES, Index.ANALYZED));
108     sb.append(" b");
109     sb.append(n);
110     for (int i = 1; i < numFields; i++) {
111       doc.add(newField("field" + (i + 1), sb.toString(), Store.YES, Index.ANALYZED));
112     }
113     return doc;
114   }
115 }