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
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. 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,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.util.LuceneTestCase;
21 import org.apache.lucene.store.Directory;
22 import org.apache.lucene.store.IndexInput;
23 import org.apache.lucene.store.IndexOutput;
24 import org.apache.lucene.store.MockDirectoryWrapper;
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.analysis.WhitespaceAnalyzer;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
35 Verify we can read the pre-2.1 file format, do searches
36 against it, and add documents to it.
39 public class TestIndexFileDeleter extends LuceneTestCase {
41 public void testDeleteLeftoverFiles() throws IOException {
42 MockDirectoryWrapper dir = newDirectory();
43 dir.setPreventDoubleWrite(false);
44 IndexWriterConfig conf = newIndexWriterConfig(
45 TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT))
46 .setMaxBufferedDocs(10);
47 LogMergePolicy mergePolicy = newLogMergePolicy(true, 10);
48 mergePolicy.setNoCFSRatio(1); // This test expects all of its segments to be in CFS
49 conf.setMergePolicy(mergePolicy);
51 IndexWriter writer = new IndexWriter(
53 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
54 setMaxBufferedDocs(10).
55 setMergePolicy(mergePolicy)
62 mergePolicy.setUseCompoundFile(false);
68 // Delete one doc so we get a .del file:
69 IndexReader reader = IndexReader.open(dir, false);
70 Term searchTerm = new Term("id", "7");
71 int delCount = reader.deleteDocuments(searchTerm);
72 assertEquals("didn't delete the right number of documents", 1, delCount);
74 // Set one norm so we get a .s0 file:
75 reader.setNorm(21, "content", (float) 1.5);
78 // Now, artificially create an extra .del file & extra
80 String[] files = dir.listAll();
83 for(int j=0;j<files.length;j++) {
84 System.out.println(j + ": " + files[j]);
88 // The numbering of fields can vary depending on which
89 // JRE is in use. On some JREs we see content bound to
90 // field 0; on others, field 1. So, here we have to
91 // figure out which field number corresponds to
92 // "content", and then set our expected file names below
94 CompoundFileReader cfsReader = new CompoundFileReader(dir, "_2.cfs");
95 FieldInfos fieldInfos = new FieldInfos(cfsReader, "_2.fnm");
96 int contentFieldIndex = -1;
97 for(i=0;i<fieldInfos.size();i++) {
98 FieldInfo fi = fieldInfos.fieldInfo(i);
99 if (fi.name.equals("content")) {
100 contentFieldIndex = i;
105 assertTrue("could not locate the 'content' field number in the _2.cfs segment", contentFieldIndex != -1);
107 String normSuffix = "s" + contentFieldIndex;
109 // Create a bogus separate norms file for a
110 // segment/field that actually has a separate norms file
112 copyFile(dir, "_2_1." + normSuffix, "_2_2." + normSuffix);
114 // Create a bogus separate norms file for a
115 // segment/field that actually has a separate norms file
116 // already, using the "not compound file" extension:
117 copyFile(dir, "_2_1." + normSuffix, "_2_2.f" + contentFieldIndex);
119 // Create a bogus separate norms file for a
120 // segment/field that does not have a separate norms
122 copyFile(dir, "_2_1." + normSuffix, "_1_1." + normSuffix);
124 // Create a bogus separate norms file for a
125 // segment/field that does not have a separate norms
126 // file already using the "not compound file" extension:
127 copyFile(dir, "_2_1." + normSuffix, "_1_1.f" + contentFieldIndex);
129 // Create a bogus separate del file for a
130 // segment that already has a separate del file:
131 copyFile(dir, "_0_1.del", "_0_2.del");
133 // Create a bogus separate del file for a
134 // segment that does not yet have a separate del file:
135 copyFile(dir, "_0_1.del", "_1_1.del");
137 // Create a bogus separate del file for a
138 // non-existent segment:
139 copyFile(dir, "_0_1.del", "_188_1.del");
141 // Create a bogus segment file:
142 copyFile(dir, "_0.cfs", "_188.cfs");
144 // Create a bogus fnm file when the CFS already exists:
145 copyFile(dir, "_0.cfs", "_0.fnm");
147 // Create a deletable file:
148 copyFile(dir, "_0.cfs", "deletable");
150 // Create some old segments file:
151 copyFile(dir, "segments_2", "segments");
152 copyFile(dir, "segments_2", "segments_1");
154 // Create a bogus cfs file shadowing a non-cfs segment:
155 assertTrue(dir.fileExists("_3.fdt"));
156 assertTrue(!dir.fileExists("_3.cfs"));
157 copyFile(dir, "_1.cfs", "_3.cfs");
159 String[] filesPre = dir.listAll();
161 // Open & close a writer: it should delete the above 4
162 // files and nothing more:
163 writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
166 String[] files2 = dir.listAll();
172 Set<String> dif = difFiles(files, files2);
174 if (!Arrays.equals(files, files2)) {
175 fail("IndexFileDeleter failed to delete unreferenced extra files: should have deleted " + (filesPre.length-files.length) + " files but only deleted " + (filesPre.length - files2.length) + "; expected files:\n " + asString(files) + "\n actual files:\n " + asString(files2)+"\ndif: "+dif);
179 private static Set<String> difFiles(String[] files1, String[] files2) {
180 Set<String> set1 = new HashSet<String>();
181 Set<String> set2 = new HashSet<String>();
182 Set<String> extra = new HashSet<String>();
184 for (int x=0; x < files1.length; x++) {
187 for (int x=0; x < files2.length; x++) {
190 Iterator<String> i1 = set1.iterator();
191 while (i1.hasNext()) {
192 String o = i1.next();
193 if (!set2.contains(o)) {
197 Iterator<String> i2 = set2.iterator();
198 while (i2.hasNext()) {
199 String o = i2.next();
200 if (!set1.contains(o)) {
207 private String asString(String[] l) {
209 for(int i=0;i<l.length;i++) {
218 public void copyFile(Directory dir, String src, String dest) throws IOException {
219 IndexInput in = dir.openInput(src);
220 IndexOutput out = dir.createOutput(dest);
221 byte[] b = new byte[1024];
222 long remainder = in.length();
223 while(remainder > 0) {
224 int len = (int) Math.min(b.length, remainder);
225 in.readBytes(b, 0, len);
226 out.writeBytes(b, len);
233 private void addDoc(IndexWriter writer, int id) throws IOException
235 Document doc = new Document();
236 doc.add(newField("content", "aaa", Field.Store.NO, Field.Index.ANALYZED));
237 doc.add(newField("id", Integer.toString(id), Field.Store.YES, Field.Index.NOT_ANALYZED));
238 writer.addDocument(doc);