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.
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.HashSet;
26 import org.apache.lucene.analysis.MockAnalyzer;
27 import org.apache.lucene.index.IndexFileNames;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.IndexWriter;
30 import org.apache.lucene.index.IndexWriterConfig;
31 import org.apache.lucene.index.TestIndexWriterReader;
32 import org.apache.lucene.util.LuceneTestCase;
33 import org.apache.lucene.util._TestUtil;
35 public class TestFileSwitchDirectory extends LuceneTestCase {
37 * Test if writing doc stores to disk and everything else to ram works.
40 public void testBasic() throws IOException {
41 Set<String> fileExtensions = new HashSet<String>();
42 fileExtensions.add(IndexFileNames.FIELDS_EXTENSION);
43 fileExtensions.add(IndexFileNames.FIELDS_INDEX_EXTENSION);
45 MockDirectoryWrapper primaryDir = new MockDirectoryWrapper(random, new RAMDirectory());
46 primaryDir.setCheckIndexOnClose(false); // only part of an index
47 MockDirectoryWrapper secondaryDir = new MockDirectoryWrapper(random, new RAMDirectory());
48 secondaryDir.setCheckIndexOnClose(false); // only part of an index
50 FileSwitchDirectory fsd = new FileSwitchDirectory(fileExtensions, primaryDir, secondaryDir, true);
51 IndexWriter writer = new IndexWriter(
53 new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
54 setMergePolicy(newLogMergePolicy(false))
56 TestIndexWriterReader.createIndexNoClose(true, "ram", writer);
57 IndexReader reader = IndexReader.open(writer, true);
58 assertEquals(100, reader.maxDoc());
60 // we should see only fdx,fdt files here
61 String[] files = primaryDir.listAll();
62 assertTrue(files.length > 0);
63 for (int x=0; x < files.length; x++) {
64 String ext = FileSwitchDirectory.getExtension(files[x]);
65 assertTrue(fileExtensions.contains(ext));
67 files = secondaryDir.listAll();
68 assertTrue(files.length > 0);
69 // we should not see fdx,fdt files here
70 for (int x=0; x < files.length; x++) {
71 String ext = FileSwitchDirectory.getExtension(files[x]);
72 assertFalse(fileExtensions.contains(ext));
77 files = fsd.listAll();
78 for(int i=0;i<files.length;i++) {
79 assertNotNull(files[i]);
84 private Directory newFSSwitchDirectory(Set<String> primaryExtensions) throws IOException {
85 Directory a = new SimpleFSDirectory(_TestUtil.getTempDir("foo"));
86 Directory b = new SimpleFSDirectory(_TestUtil.getTempDir("bar"));
87 FileSwitchDirectory switchDir = new FileSwitchDirectory(primaryExtensions, a, b, true);
88 return new MockDirectoryWrapper(random, switchDir);
91 // LUCENE-3380 -- make sure we get exception if the directory really does not exist.
92 public void testNoDir() throws Throwable {
93 Directory dir = newFSSwitchDirectory(Collections.<String>emptySet());
95 IndexReader.open(dir, true);
96 fail("did not hit expected exception");
97 } catch (NoSuchDirectoryException nsde) {
103 // LUCENE-3380 test that we can add a file, and then when we call list() we get it back
104 public void testDirectoryFilter() throws IOException {
105 Directory dir = newFSSwitchDirectory(Collections.<String>emptySet());
106 String name = "file";
108 dir.createOutput(name).close();
109 assertTrue(dir.fileExists(name));
110 assertTrue(Arrays.asList(dir.listAll()).contains(name));