add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / index / TestIndexFileDeleter.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
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
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,
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.
18  */
19
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;
30
31 import java.io.*;
32 import java.util.*;
33
34 /*
35   Verify we can read the pre-2.1 file format, do searches
36   against it, and add documents to it.
37 */
38
39 public class TestIndexFileDeleter extends LuceneTestCase {
40   
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);
50
51     IndexWriter writer = new IndexWriter(
52         dir,
53         newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
54             setMaxBufferedDocs(10).
55             setMergePolicy(mergePolicy)
56     );
57
58     int i;
59     for(i=0;i<35;i++) {
60       addDoc(writer, i);
61     }
62     mergePolicy.setUseCompoundFile(false);
63     for(;i<45;i++) {
64       addDoc(writer, i);
65     }
66     writer.close();
67
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);
73
74     // Set one norm so we get a .s0 file:
75     reader.setNorm(21, "content", (float) 1.5);
76     reader.close();
77
78     // Now, artificially create an extra .del file & extra
79     // .s0 file:
80     String[] files = dir.listAll();
81
82     /*
83     for(int j=0;j<files.length;j++) {
84       System.out.println(j + ": " + files[j]);
85     }
86     */
87
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
93     // accordingly:
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;
101         break;
102       }
103     }
104     cfsReader.close();
105     assertTrue("could not locate the 'content' field number in the _2.cfs segment", contentFieldIndex != -1);
106
107     String normSuffix = "s" + contentFieldIndex;
108
109     // Create a bogus separate norms file for a
110     // segment/field that actually has a separate norms file
111     // already:
112     copyFile(dir, "_2_1." + normSuffix, "_2_2." + normSuffix);
113
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);
118
119     // Create a bogus separate norms file for a
120     // segment/field that does not have a separate norms
121     // file already:
122     copyFile(dir, "_2_1." + normSuffix, "_1_1." + normSuffix);
123
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);
128
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");
132
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");
136
137     // Create a bogus separate del file for a
138     // non-existent segment:
139     copyFile(dir, "_0_1.del", "_188_1.del");
140
141     // Create a bogus segment file:
142     copyFile(dir, "_0.cfs", "_188.cfs");
143
144     // Create a bogus fnm file when the CFS already exists:
145     copyFile(dir, "_0.cfs", "_0.fnm");
146     
147     // Create a deletable file:
148     copyFile(dir, "_0.cfs", "deletable");
149
150     // Create some old segments file:
151     copyFile(dir, "segments_2", "segments");
152     copyFile(dir, "segments_2", "segments_1");
153
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");
158     
159     String[] filesPre = dir.listAll();
160
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));
164     writer.close();
165
166     String[] files2 = dir.listAll();
167     dir.close();
168
169     Arrays.sort(files);
170     Arrays.sort(files2);
171     
172     Set<String> dif = difFiles(files, files2);
173     
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);
176     }
177   }
178
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>();
183     
184     for (int x=0; x < files1.length; x++) {
185       set1.add(files1[x]);
186     }
187     for (int x=0; x < files2.length; x++) {
188       set2.add(files2[x]);
189     }
190     Iterator<String> i1 = set1.iterator();
191     while (i1.hasNext()) {
192       String o = i1.next();
193       if (!set2.contains(o)) {
194         extra.add(o);
195       }
196     }
197     Iterator<String> i2 = set2.iterator();
198     while (i2.hasNext()) {
199       String o = i2.next();
200       if (!set1.contains(o)) {
201         extra.add(o);
202       }
203     }
204     return extra;
205   }
206   
207   private String asString(String[] l) {
208     String s = "";
209     for(int i=0;i<l.length;i++) {
210       if (i > 0) {
211         s += "\n    ";
212       }
213       s += l[i];
214     }
215     return s;
216   }
217
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);
227       remainder -= len;
228     }
229     in.close();
230     out.close();
231   }
232
233   private void addDoc(IndexWriter writer, int id) throws IOException
234   {
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);
239   }
240 }