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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.lucene.index;
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;
29 public class TestIndexSplitter extends LuceneTestCase {
30 public void test() throws Exception {
31 File dir = new File(TEMP_DIR, "testfilesplitter");
34 File destDir = new File(TEMP_DIR, "testfilesplitterdest");
35 _TestUtil.rmDir(destDir);
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);
45 IndexWriter iw = new IndexWriter(fsDir, conf);
47 for (int x=0; x < 100; x++) {
48 Document doc = DocHelper.createDocument(x, "index", 5);
52 for (int x=100; x < 150; x++) {
53 Document doc = DocHelper.createDocument(x, "index2", 5);
57 for (int x=150; x < 200; x++) {
58 Document doc = DocHelper.createDocument(x, "index3", 5);
62 IndexReader iwReader = iw.getReader();
63 assertEquals(3, iwReader.getSequentialSubReaders().length);
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());
78 File destDir2 = new File(TEMP_DIR, "testfilesplitterdest2");
79 _TestUtil.rmDir(destDir2);
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());
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);
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);
102 File indexSplitPath = new File(TEMP_DIR, "testfilesplitterdest");
103 _TestUtil.rmDir(indexSplitPath);
104 indexSplitPath.mkdirs();
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);
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});
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());
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();
149 // Read the number of docs in the index
150 fsDirDest = newFSDirectory(indexSplitPath);
151 indexReader = IndexReader.open(fsDirDest);
152 assertEquals(1, indexReader.numDocs());