Search in sources :

Example 1 with FilterPath

use of org.apache.lucene.mockfile.FilterPath in project lucene-solr by apache.

the class TestIndexWriter method testDeleteUnusedFiles.

public void testDeleteUnusedFiles() throws Exception {
    assumeFalse("test relies on exact filenames", Codec.getDefault() instanceof SimpleTextCodec);
    assumeWorkingMMapOnWindows();
    for (int iter = 0; iter < 2; iter++) {
        // relies on windows semantics
        Path path = createTempDir();
        FileSystem fs = new WindowsFS(path.getFileSystem()).getFileSystem(URI.create("file:///"));
        Path indexPath = new FilterPath(path, fs);
        // NOTE: on Unix, we cannot use MMapDir, because WindowsFS doesn't see/think it keeps file handles open.  Yet, on Windows, we MUST use
        // MMapDir because the windows OS will in fact prevent file deletion for us, and fails otherwise:
        FSDirectory dir;
        if (Constants.WINDOWS) {
            dir = new MMapDirectory(indexPath);
        } else {
            dir = new NIOFSDirectory(indexPath);
        }
        MergePolicy mergePolicy = newLogMergePolicy(true);
        // This test expects all of its segments to be in CFS
        mergePolicy.setNoCFSRatio(1.0);
        mergePolicy.setMaxCFSSegmentSizeMB(Double.POSITIVE_INFINITY);
        IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(mergePolicy).setUseCompoundFile(true));
        Document doc = new Document();
        doc.add(newTextField("field", "go", Field.Store.NO));
        w.addDocument(doc);
        DirectoryReader r;
        if (iter == 0) {
            // use NRT
            r = w.getReader();
        } else {
            // don't use NRT
            w.commit();
            r = DirectoryReader.open(dir);
        }
        assertTrue(Files.exists(indexPath.resolve("_0.cfs")));
        assertTrue(Files.exists(indexPath.resolve("_0.cfe")));
        assertTrue(Files.exists(indexPath.resolve("_0.si")));
        if (iter == 1) {
            // we run a full commit so there should be a segments file etc.
            assertTrue(Files.exists(indexPath.resolve("segments_1")));
        } else {
            // this is an NRT reopen - no segments files yet
            assertFalse(Files.exists(indexPath.resolve("segments_1")));
        }
        w.addDocument(doc);
        w.forceMerge(1);
        if (iter == 1) {
            w.commit();
        }
        IndexReader r2 = DirectoryReader.openIfChanged(r);
        assertNotNull(r2);
        assertTrue(r != r2);
        // NOTE: here we rely on "Windows" behavior, ie, even
        // though IW wanted to delete _0.cfs since it was
        // merged away, because we have a reader open
        // against this file, it should still be here:
        assertTrue(Files.exists(indexPath.resolve("_0.cfs")));
        // forceMerge created this
        //assertTrue(files.contains("_2.cfs"));
        w.deleteUnusedFiles();
        // r still holds this file open
        assertTrue(Files.exists(indexPath.resolve("_0.cfs")));
        //assertTrue(files.contains("_2.cfs"));
        r.close();
        if (iter == 0) {
            // on closing NRT reader, it calls writer.deleteUnusedFiles
            assertFalse(Files.exists(indexPath.resolve("_0.cfs")));
        } else {
            // now FSDir can remove it
            dir.deletePendingFiles();
            assertFalse(Files.exists(indexPath.resolve("_0.cfs")));
        }
        w.close();
        r2.close();
        dir.close();
    }
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) FilterPath(org.apache.lucene.mockfile.FilterPath) SimpleTextCodec(org.apache.lucene.codecs.simpletext.SimpleTextCodec) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) Document(org.apache.lucene.document.Document) MMapDirectory(org.apache.lucene.store.MMapDirectory) WindowsFS(org.apache.lucene.mockfile.WindowsFS) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) FileSystem(java.nio.file.FileSystem)

Example 2 with FilterPath

use of org.apache.lucene.mockfile.FilterPath in project lucene-solr by apache.

the class TestIndexWriter method testWithPendingDeletions.

public void testWithPendingDeletions() throws Exception {
    // irony: currently we don't emulate windows well enough to work on windows!
    assumeFalse("windows is not supported", Constants.WINDOWS);
    Path path = createTempDir();
    // Use WindowsFS to prevent open files from being deleted:
    FileSystem fs = new WindowsFS(path.getFileSystem()).getFileSystem(URI.create("file:///"));
    Path root = new FilterPath(path, fs);
    // MMapDirectory doesn't work because it closes its file handles after mapping!
    try (FSDirectory dir = new SimpleFSDirectory(root)) {
        IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
        IndexWriter w = new IndexWriter(dir, iwc);
        w.commit();
        IndexInput in = dir.openInput("segments_1", IOContext.DEFAULT);
        w.addDocument(new Document());
        w.close();
        assertTrue(dir.checkPendingDeletions());
        // make sure we get NFSF if we try to delete and already-pending-delete file:
        expectThrows(NoSuchFileException.class, () -> {
            dir.deleteFile("segments_1");
        });
        IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
            new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));
        });
        assertTrue(expected.getMessage().contains("still has pending deleted files; cannot initialize IndexWriter"));
        in.close();
    }
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) WindowsFS(org.apache.lucene.mockfile.WindowsFS) FilterPath(org.apache.lucene.mockfile.FilterPath) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) FileSystem(java.nio.file.FileSystem) IndexInput(org.apache.lucene.store.IndexInput) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) Document(org.apache.lucene.document.Document) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory)

Example 3 with FilterPath

use of org.apache.lucene.mockfile.FilterPath in project lucene-solr by apache.

the class LuceneTestCase method addVirusChecker.

public static Path addVirusChecker(Path path) {
    if (TestUtil.hasVirusChecker(path) == false) {
        VirusCheckingFS fs = new VirusCheckingFS(path.getFileSystem(), random().nextLong());
        FileSystem filesystem = fs.getFileSystem(URI.create("file:///"));
        path = new FilterPath(path, filesystem);
    }
    return path;
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) FileSystem(java.nio.file.FileSystem) VirusCheckingFS(org.apache.lucene.mockfile.VirusCheckingFS)

Aggregations

FileSystem (java.nio.file.FileSystem)3 FilterPath (org.apache.lucene.mockfile.FilterPath)3 Path (java.nio.file.Path)2 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 Document (org.apache.lucene.document.Document)2 WindowsFS (org.apache.lucene.mockfile.WindowsFS)2 FSDirectory (org.apache.lucene.store.FSDirectory)2 NIOFSDirectory (org.apache.lucene.store.NIOFSDirectory)2 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)2 SimpleTextCodec (org.apache.lucene.codecs.simpletext.SimpleTextCodec)1 VirusCheckingFS (org.apache.lucene.mockfile.VirusCheckingFS)1 IndexInput (org.apache.lucene.store.IndexInput)1 MMapDirectory (org.apache.lucene.store.MMapDirectory)1