add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / misc / src / test / org / apache / lucene / index / TestIndexSplitter.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.lucene.index;
18
19 import java.io.File;
20
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LuceneTestCase;
27 import org.apache.lucene.util._TestUtil;
28
29 public class TestIndexSplitter extends LuceneTestCase {
30   public void test() throws Exception {
31     File dir = new File(TEMP_DIR, "testfilesplitter");
32     _TestUtil.rmDir(dir);
33     dir.mkdirs();
34     File destDir = new File(TEMP_DIR, "testfilesplitterdest");
35     _TestUtil.rmDir(destDir);
36     destDir.mkdirs();
37     Directory fsDir = newFSDirectory(dir);
38     LogMergePolicy mergePolicy = new LogByteSizeMergePolicy();
39     mergePolicy.setNoCFSRatio(1);
40     IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT,
41         new MockAnalyzer(random))
42         .setOpenMode(OpenMode.CREATE)
43         .setMergePolicy(mergePolicy);
44
45     IndexWriter iw = new IndexWriter(fsDir, conf);
46
47     for (int x=0; x < 100; x++) {
48       Document doc = DocHelper.createDocument(x, "index", 5);
49       iw.addDocument(doc);
50     }
51     iw.commit();
52     for (int x=100; x < 150; x++) {
53       Document doc = DocHelper.createDocument(x, "index2", 5);
54       iw.addDocument(doc);
55     }
56     iw.commit();
57     for (int x=150; x < 200; x++) {
58       Document doc = DocHelper.createDocument(x, "index3", 5);
59       iw.addDocument(doc);
60     }
61     iw.commit();
62     IndexReader iwReader = iw.getReader();
63     assertEquals(3, iwReader.getSequentialSubReaders().length);
64     iwReader.close();
65     iw.close();
66     
67     // we should have 2 segments now
68     IndexSplitter is = new IndexSplitter(dir);
69     String splitSegName = is.infos.info(1).name;
70     is.split(destDir, new String[] {splitSegName});
71     Directory fsDirDest = newFSDirectory(destDir);
72     IndexReader r = IndexReader.open(fsDirDest, true);
73     assertEquals(50, r.maxDoc());
74     r.close();
75     fsDirDest.close();
76     
77     // now test cmdline
78     File destDir2 = new File(TEMP_DIR, "testfilesplitterdest2");
79     _TestUtil.rmDir(destDir2);
80     destDir2.mkdirs();
81     IndexSplitter.main(new String[] {dir.getAbsolutePath(), destDir2.getAbsolutePath(), splitSegName});
82     assertEquals(3, destDir2.listFiles().length);
83     Directory fsDirDest2 = newFSDirectory(destDir2);
84     r = IndexReader.open(fsDirDest2, true);
85     assertEquals(50, r.maxDoc());
86     r.close();
87     fsDirDest2.close();
88     
89     // now remove the copied segment from src
90     IndexSplitter.main(new String[] {dir.getAbsolutePath(), "-d", splitSegName});
91     r = IndexReader.open(fsDir, true);
92     assertEquals(2, r.getSequentialSubReaders().length);
93     r.close();
94     fsDir.close();
95   }
96
97   public void testDeleteThenOptimize() throws Exception {
98     // Create directories where the indexes will reside
99     File indexPath = new File(TEMP_DIR, "testfilesplitter");
100     _TestUtil.rmDir(indexPath);
101     indexPath.mkdirs();
102     File indexSplitPath = new File(TEMP_DIR, "testfilesplitterdest");
103     _TestUtil.rmDir(indexSplitPath);
104     indexSplitPath.mkdirs();
105     
106     // Create the original index
107     LogMergePolicy mergePolicy = new LogByteSizeMergePolicy();
108     mergePolicy.setNoCFSRatio(1);
109     IndexWriterConfig iwConfig
110         = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
111               .setOpenMode(OpenMode.CREATE)
112               .setMergePolicy(mergePolicy);
113     Directory fsDir = newFSDirectory(indexPath);
114     IndexWriter indexWriter = new IndexWriter(fsDir, iwConfig);
115     Document doc = new Document();
116     doc.add(new Field("content", "doc 1", Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
117     indexWriter.addDocument(doc);
118     doc = new Document();
119     doc.add(new Field("content", "doc 2", Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
120     indexWriter.addDocument(doc);
121     indexWriter.close();
122     fsDir.close();
123     
124     // Create the split index
125     IndexSplitter indexSplitter = new IndexSplitter(indexPath);
126     String splitSegName = indexSplitter.infos.info(0).name;
127     indexSplitter.split(indexSplitPath, new String[] {splitSegName});
128
129     // Delete the first document in the split index
130     Directory fsDirDest = newFSDirectory(indexSplitPath);
131     IndexReader indexReader = IndexReader.open(fsDirDest, false);
132     indexReader.deleteDocument(0);
133     assertEquals(1, indexReader.numDocs());
134     indexReader.close();
135     fsDirDest.close();
136
137     // Optimize the split index
138     mergePolicy = new LogByteSizeMergePolicy();
139     mergePolicy.setNoCFSRatio(1);
140     iwConfig = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
141                    .setOpenMode(OpenMode.APPEND)
142                    .setMergePolicy(mergePolicy);
143     fsDirDest = newFSDirectory(indexSplitPath);
144     indexWriter = new IndexWriter(fsDirDest, iwConfig);
145     indexWriter.optimize();
146     indexWriter.close();
147     fsDirDest.close();
148
149     // Read the number of docs in the index
150     fsDirDest = newFSDirectory(indexSplitPath);
151     indexReader = IndexReader.open(fsDirDest);
152           assertEquals(1, indexReader.numDocs());
153     indexReader.close();
154     fsDirDest.close();
155   }
156 }