add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / misc / src / test / org / apache / lucene / store / TestNRTCachingDirectory.java
1 package org.apache.lucene.store;
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 java.io.File;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.apache.lucene.analysis.Analyzer;
27 import org.apache.lucene.analysis.MockAnalyzer;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.index.IndexReader;
30 import org.apache.lucene.index.IndexWriter;
31 import org.apache.lucene.index.IndexWriterConfig;
32 import org.apache.lucene.index.RandomIndexWriter;
33 import org.apache.lucene.index.Term;
34 import org.apache.lucene.search.IndexSearcher;
35 import org.apache.lucene.search.TermQuery;
36 import org.apache.lucene.search.TopDocs;
37 import org.apache.lucene.util.LineFileDocs;
38 import org.apache.lucene.util.LuceneTestCase;
39 import org.apache.lucene.util.Version;
40 import org.apache.lucene.util._TestUtil;
41
42 public class TestNRTCachingDirectory extends LuceneTestCase {
43
44   public void testNRTAndCommit() throws Exception {
45     Directory dir = newDirectory();
46     NRTCachingDirectory cachedDir = new NRTCachingDirectory(dir, 2.0, 25.0);
47     IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
48     conf.setMergeScheduler(cachedDir.getMergeScheduler());
49     RandomIndexWriter w = new RandomIndexWriter(random, cachedDir, conf);
50     w.w.setInfoStream(VERBOSE ? System.out : null);
51     final LineFileDocs docs = new LineFileDocs(random);    
52     final int numDocs = _TestUtil.nextInt(random, 100, 400);
53
54     if (VERBOSE) {
55       System.out.println("TEST: numDocs=" + numDocs);
56     }
57
58     final List<String> ids = new ArrayList<String>();
59     IndexReader r = null;
60     for(int docCount=0;docCount<numDocs;docCount++) {
61       final Document doc = docs.nextDoc();
62       ids.add(new String(doc.get("docid")));
63       w.addDocument(doc);
64       if (random.nextInt(20) == 17) {
65         if (r == null) {
66           r = IndexReader.open(w.w, false);
67         } else {
68           final IndexReader r2 = r.reopen();
69           if (r2 != r) {
70             r.close();
71             r = r2;
72           }
73         }
74         assertEquals(1+docCount, r.numDocs());
75         final IndexSearcher s = new IndexSearcher(r);
76         // Just make sure search can run; we can't assert
77         // totHits since it could be 0
78         TopDocs hits = s.search(new TermQuery(new Term("body", "the")), 10);
79         // System.out.println("tot hits " + hits.totalHits);
80       }
81     }
82
83     if (r != null) {
84       r.close();
85     }
86
87     // Close should force cache to clear since all files are sync'd
88     w.close();
89
90     final String[] cachedFiles = cachedDir.listCachedFiles();
91     for(String file : cachedFiles) {
92       System.out.println("FAIL: cached file " + file + " remains after sync");
93     }
94     assertEquals(0, cachedFiles.length);
95     
96     r = IndexReader.open(dir);
97     for(String id : ids) {
98       assertEquals(1, r.docFreq(new Term("docid", id)));
99     }
100     r.close();
101     cachedDir.close();
102   }
103
104   // NOTE: not a test; just here to make sure the code frag
105   // in the javadocs is correct!
106   public void verifyCompiles() throws Exception {
107     Analyzer analyzer = null;
108
109     Directory fsDir = FSDirectory.open(new File("/path/to/index"));
110     NRTCachingDirectory cachedFSDir = new NRTCachingDirectory(fsDir, 2.0, 25.0);
111     IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_32, analyzer);
112     conf.setMergeScheduler(cachedFSDir.getMergeScheduler());
113     IndexWriter writer = new IndexWriter(cachedFSDir, conf);
114   }
115
116   public void testDeleteFile() throws Exception {
117     Directory dir = new NRTCachingDirectory(newDirectory(), 2.0, 25.0);
118     dir.createOutput("foo.txt").close();
119     dir.deleteFile("foo.txt");
120     assertEquals(0, dir.listAll().length);
121     dir.close();
122   }
123   
124   // LUCENE-3382 -- make sure we get exception if the directory really does not exist.
125   public void testNoDir() throws Throwable {
126     Directory dir = new NRTCachingDirectory(newFSDirectory(_TestUtil.getTempDir("doesnotexist")), 2.0, 25.0);
127     try {
128       IndexReader.open(dir, true);
129       fail("did not hit expected exception");
130     } catch (NoSuchDirectoryException nsde) {
131       // expected
132     }
133     dir.close();
134   }
135   
136   // LUCENE-3382 test that we can add a file, and then when we call list() we get it back
137   public void testDirectoryFilter() throws IOException {
138     Directory dir = new NRTCachingDirectory(newFSDirectory(_TestUtil.getTempDir("foo")), 2.0, 25.0);
139     String name = "file";
140     try {
141       dir.createOutput(name).close();
142       assertTrue(dir.fileExists(name));
143       assertTrue(Arrays.asList(dir.listAll()).contains(name));
144     } finally {
145       dir.close();
146     }
147   }
148   
149   /** Creates a file of the specified size with sequential data. The first
150    *  byte is written as the start byte provided. All subsequent bytes are
151    *  computed as start + offset where offset is the number of the byte.
152    */
153   private void createSequenceFile(Directory dir, String name, byte start, int size) throws IOException {
154       IndexOutput os = dir.createOutput(name);
155       for (int i=0; i < size; i++) {
156           os.writeByte(start);
157           start ++;
158       }
159       os.close();
160   }
161 }