add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / index / TestDoc.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 import java.io.File;
20 import java.io.FileReader;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25
26 import java.util.LinkedList;
27 import java.util.Collection;
28
29 import junit.framework.TestSuite;
30 import junit.textui.TestRunner;
31
32 import org.apache.lucene.analysis.MockAnalyzer;
33 import org.apache.lucene.document.Document;
34 import org.apache.lucene.document.Field;
35 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
36 import org.apache.lucene.store.Directory;
37 import org.apache.lucene.util.LuceneTestCase;
38 import org.apache.lucene.util._TestUtil;
39
40 /** JUnit adaptation of an older test case DocTest. */
41 public class TestDoc extends LuceneTestCase {
42
43     /** Main for running test case by itself. */
44     public static void main(String args[]) {
45         TestRunner.run (new TestSuite(TestDoc.class));
46     }
47
48     private File workDir;
49     private File indexDir;
50     private LinkedList<File> files;
51
52     /** Set the test case. This test case needs
53      *  a few text files created in the current working directory.
54      */
55     @Override
56     public void setUp() throws Exception {
57         super.setUp();
58         workDir = _TestUtil.getTempDir("TestDoc");
59         workDir.mkdirs();
60
61         indexDir = _TestUtil.getTempDir("testIndex");
62         indexDir.mkdirs();
63
64         Directory directory = newFSDirectory(indexDir);
65         directory.close();
66
67         files = new LinkedList<File>();
68         files.add(createOutput("test.txt",
69             "This is the first test file"
70         ));
71
72         files.add(createOutput("test2.txt",
73             "This is the second test file"
74         ));
75     }
76
77     private File createOutput(String name, String text) throws IOException {
78         FileWriter fw = null;
79         PrintWriter pw = null;
80
81         try {
82             File f = new File(workDir, name);
83             if (f.exists()) f.delete();
84
85             fw = new FileWriter(f);
86             pw = new PrintWriter(fw);
87             pw.println(text);
88             return f;
89
90         } finally {
91             if (pw != null) pw.close();
92             if (fw != null) fw.close();
93         }
94     }
95
96
97     /** This test executes a number of merges and compares the contents of
98      *  the segments created when using compound file or not using one.
99      *
100      *  TODO: the original test used to print the segment contents to System.out
101      *        for visual validation. To have the same effect, a new method
102      *        checkSegment(String name, ...) should be created that would
103      *        assert various things about the segment.
104      */
105     public void testIndexAndMerge() throws Exception {
106       StringWriter sw = new StringWriter();
107       PrintWriter out = new PrintWriter(sw, true);
108       
109       Directory directory = newFSDirectory(indexDir);
110       IndexWriter writer = new IndexWriter(
111           directory,
112           newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
113               setOpenMode(OpenMode.CREATE).
114               setMaxBufferedDocs(-1).
115               setMergePolicy(newLogMergePolicy(10))
116       );
117
118       SegmentInfo si1 = indexDoc(writer, "test.txt");
119       printSegment(out, si1);
120
121       SegmentInfo si2 = indexDoc(writer, "test2.txt");
122       printSegment(out, si2);
123       writer.close();
124
125       SegmentInfo siMerge = merge(si1, si2, "merge", false);
126       printSegment(out, siMerge);
127
128       SegmentInfo siMerge2 = merge(si1, si2, "merge2", false);
129       printSegment(out, siMerge2);
130
131       SegmentInfo siMerge3 = merge(siMerge, siMerge2, "merge3", false);
132       printSegment(out, siMerge3);
133       
134       directory.close();
135       out.close();
136       sw.close();
137       String multiFileOutput = sw.getBuffer().toString();
138       //System.out.println(multiFileOutput);
139
140       sw = new StringWriter();
141       out = new PrintWriter(sw, true);
142
143       directory = newFSDirectory(indexDir);
144       writer = new IndexWriter(
145           directory,
146           newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
147               setOpenMode(OpenMode.CREATE).
148               setMaxBufferedDocs(-1).
149               setMergePolicy(newLogMergePolicy(10))
150       );
151
152       si1 = indexDoc(writer, "test.txt");
153       printSegment(out, si1);
154
155       si2 = indexDoc(writer, "test2.txt");
156       printSegment(out, si2);
157       writer.close();
158
159       siMerge = merge(si1, si2, "merge", true);
160       printSegment(out, siMerge);
161
162       siMerge2 = merge(si1, si2, "merge2", true);
163       printSegment(out, siMerge2);
164
165       siMerge3 = merge(siMerge, siMerge2, "merge3", true);
166       printSegment(out, siMerge3);
167       
168       directory.close();
169       out.close();
170       sw.close();
171       String singleFileOutput = sw.getBuffer().toString();
172
173       assertEquals(multiFileOutput, singleFileOutput);
174    }
175
176    private SegmentInfo indexDoc(IndexWriter writer, String fileName)
177    throws Exception
178    {
179       File file = new File(workDir, fileName);
180       Document doc = new Document();
181       doc.add(new Field("contents", new FileReader(file)));
182       writer.addDocument(doc);
183       writer.commit();
184       return writer.newestSegment();
185    }
186
187
188    private SegmentInfo merge(SegmentInfo si1, SegmentInfo si2, String merged, boolean useCompoundFile)
189    throws Exception {
190       SegmentReader r1 = SegmentReader.get(true, si1, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
191       SegmentReader r2 = SegmentReader.get(true, si2, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
192
193       SegmentMerger merger = new SegmentMerger(si1.dir, IndexWriterConfig.DEFAULT_TERM_INDEX_INTERVAL, merged, null, null, new FieldInfos());
194
195       merger.add(r1);
196       merger.add(r2);
197       merger.merge();
198       r1.close();
199       r2.close();
200       
201       final SegmentInfo info = new SegmentInfo(merged, si1.docCount + si2.docCount, si1.dir,
202                                                false, true,
203                                                merger.fieldInfos().hasProx(),
204                                                merger.fieldInfos().hasVectors());
205       
206       if (useCompoundFile) {
207         Collection<String> filesToDelete = merger.createCompoundFile(merged + ".cfs", info);
208         info.setUseCompoundFile(true);
209         for (final String fileToDelete : filesToDelete) 
210           si1.dir.deleteFile(fileToDelete);
211       }
212
213       return info;
214    }
215
216
217    private void printSegment(PrintWriter out, SegmentInfo si)
218    throws Exception {
219       SegmentReader reader = SegmentReader.get(true, si, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
220
221       for (int i = 0; i < reader.numDocs(); i++)
222         out.println(reader.document(i));
223
224       TermEnum tis = reader.terms();
225       while (tis.next()) {
226         out.print(tis.term());
227         out.println(" DF=" + tis.docFreq());
228
229         TermPositions positions = reader.termPositions(tis.term());
230         try {
231           while (positions.next()) {
232             out.print(" doc=" + positions.doc());
233             out.print(" TF=" + positions.freq());
234             out.print(" pos=");
235             out.print(positions.nextPosition());
236             for (int j = 1; j < positions.freq(); j++)
237               out.print("," + positions.nextPosition());
238             out.println("");
239           }
240         } finally {
241           positions.close();
242         }
243       }
244       tis.close();
245       reader.close();
246     }
247 }