1 package org.apache.lucene.store;
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.
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
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;
42 public class TestNRTCachingDirectory extends LuceneTestCase {
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);
55 System.out.println("TEST: numDocs=" + numDocs);
58 final List<String> ids = new ArrayList<String>();
60 for(int docCount=0;docCount<numDocs;docCount++) {
61 final Document doc = docs.nextDoc();
62 ids.add(new String(doc.get("docid")));
64 if (random.nextInt(20) == 17) {
66 r = IndexReader.open(w.w, false);
68 final IndexReader r2 = r.reopen();
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);
87 // Close should force cache to clear since all files are sync'd
90 final String[] cachedFiles = cachedDir.listCachedFiles();
91 for(String file : cachedFiles) {
92 System.out.println("FAIL: cached file " + file + " remains after sync");
94 assertEquals(0, cachedFiles.length);
96 r = IndexReader.open(dir);
97 for(String id : ids) {
98 assertEquals(1, r.docFreq(new Term("docid", id)));
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;
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);
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);
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);
128 IndexReader.open(dir, true);
129 fail("did not hit expected exception");
130 } catch (NoSuchDirectoryException nsde) {
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";
141 dir.createOutput(name).close();
142 assertTrue(dir.fileExists(name));
143 assertTrue(Arrays.asList(dir.listAll()).contains(name));
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.
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++) {