Search in sources :

Example 66 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project jackrabbit-oak by apache.

the class IndexPlannerTest method createSampleDirectory.

private static Directory createSampleDirectory(long numOfDocs) throws IOException {
    Directory dir = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(VERSION, LuceneIndexConstants.ANALYZER);
    IndexWriter writer = new IndexWriter(dir, config);
    for (int i = 0; i < numOfDocs; i++) {
        Document doc = new Document();
        doc.add(new StringField("foo", "bar" + i, Field.Store.NO));
        writer.addDocument(doc);
    }
    writer.close();
    return dir;
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 67 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project jackrabbit-oak by apache.

the class IndexCopierTest method copyOnWriteBasics.

@Test
public void copyOnWriteBasics() throws Exception {
    Directory baseDir = new CloseSafeDir();
    IndexDefinition defn = new IndexDefinition(root, builder.getNodeState(), "/foo");
    IndexCopier copier = new RAMIndexCopier(baseDir, sameThreadExecutor(), getWorkDir());
    Directory remote = new RAMDirectory();
    byte[] t1 = writeFile(remote, "t1");
    //State of remote directory should set before wrapping as later
    //additions would not be picked up given COW assume remote directory
    //to be read only
    Directory local = copier.wrapForWrite(defn, remote, false, INDEX_DATA_CHILD_NAME);
    assertEquals(newHashSet("t1"), newHashSet(local.listAll()));
    assertEquals(t1.length, local.fileLength("t1"));
    byte[] t2 = writeFile(local, "t2");
    assertEquals(newHashSet("t1", "t2"), newHashSet(local.listAll()));
    assertEquals(t2.length, local.fileLength("t2"));
    assertTrue(local.fileExists("t1"));
    assertTrue(local.fileExists("t2"));
    assertTrue("t2 should be copied to remote", remote.fileExists("t2"));
    readAndAssert(local, "t1", t1);
    readAndAssert(local, "t2", t2);
    local.deleteFile("t1");
    assertEquals(newHashSet("t2"), newHashSet(local.listAll()));
    local.deleteFile("t2");
    assertEquals(newHashSet(), newHashSet(local.listAll()));
    try {
        local.fileLength("nonExistentFile");
        fail();
    } catch (FileNotFoundException ignore) {
    }
    try {
        local.openInput("nonExistentFile", IOContext.DEFAULT);
        fail();
    } catch (FileNotFoundException ignore) {
    }
    local.close();
    assertFalse(baseDir.fileExists("t2"));
}
Also used : FileNotFoundException(java.io.FileNotFoundException) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) Test(org.junit.Test)

Example 68 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project jackrabbit-oak by apache.

the class IndexCopierTest method deletesOnClose.

@Test
public void deletesOnClose() throws Exception {
    //Use a close safe dir. In actual case the FSDir would
    //be opened on same file system hence it can retain memory
    //but RAMDirectory does not retain memory hence we simulate
    //that by not closing the RAMDir and reuse it
    Directory baseDir = new CloseSafeDir();
    IndexDefinition defn = new IndexDefinition(root, builder.getNodeState(), "/foo");
    IndexCopier c1 = new RAMIndexCopier(baseDir, sameThreadExecutor(), getWorkDir());
    Directory r1 = new RAMDirectory();
    byte[] t1 = writeFile(r1, "t1");
    byte[] t2 = writeFile(r1, "t2");
    Directory w1 = c1.wrapForRead("/foo", defn, r1, INDEX_DATA_CHILD_NAME);
    readAndAssert(w1, "t1", t1);
    readAndAssert(w1, "t2", t2);
    // t1 and t2 should now be present in local (base dir which back local)
    assertTrue(baseDir.fileExists("t1"));
    assertTrue(baseDir.fileExists("t2"));
    Directory r2 = new RAMDirectory();
    copy(r1, r2);
    r2.deleteFile("t1");
    Directory w2 = c1.wrapForRead("/foo", defn, r2, INDEX_DATA_CHILD_NAME);
    //Close would trigger removal of file which are not present in remote
    w2.close();
    assertFalse("t1 should have been deleted", baseDir.fileExists("t1"));
    assertTrue(baseDir.fileExists("t2"));
}
Also used : RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) Test(org.junit.Test)

Example 69 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project jackrabbit-oak by apache.

the class IndexCopierTest method deletedOnlyFilesForOlderVersion.

@Test
public void deletedOnlyFilesForOlderVersion() throws Exception {
    Directory baseDir = new CloseSafeDir();
    IndexDefinition defn = new IndexDefinition(root, builder.getNodeState(), "/foo");
    IndexCopier copier = new RAMIndexCopier(baseDir, sameThreadExecutor(), getWorkDir());
    //1. Open a local and read t1 from remote
    Directory remote1 = new RAMDirectory();
    byte[] t1 = writeFile(remote1, "t1");
    Directory local1 = copier.wrapForRead("/foo", defn, remote1, INDEX_DATA_CHILD_NAME);
    readAndAssert(local1, "t1", t1);
    //While local1 is open , open another local2 and read t2
    Directory remote2 = new RAMDirectory();
    byte[] t2 = writeFile(remote2, "t2");
    Directory local2 = copier.wrapForRead("/foo", defn, remote2, INDEX_DATA_CHILD_NAME);
    readAndAssert(local2, "t2", t2);
    //Close local1
    local1.close();
    //t2 should still be readable
    readAndAssert(local2, "t2", t2);
}
Also used : RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) Test(org.junit.Test)

Example 70 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project jackrabbit-oak by apache.

the class IndexCopierTest method reuseLocalDir.

/**
     * Test for the case where local directory is opened already contains
     * the index files and in such a case file should not be read from remote
     */
@Test
public void reuseLocalDir() throws Exception {
    Directory baseDir = new RAMDirectory();
    IndexDefinition defn = new IndexDefinition(root, builder.getNodeState(), "/foo");
    IndexCopier c1 = new RAMIndexCopier(baseDir, sameThreadExecutor(), getWorkDir());
    TestRAMDirectory remote = new TestRAMDirectory();
    Directory wrapped = c1.wrapForRead("/foo", defn, remote, INDEX_DATA_CHILD_NAME);
    byte[] t1 = writeFile(remote, "t1");
    //1. Read for the first time should be served from remote
    readAndAssert(wrapped, "t1", t1);
    assertEquals(1, remote.openedFiles.size());
    //2. Reuse the testDir and read again
    Directory wrapped2 = c1.wrapForRead("/foo", defn, remote, INDEX_DATA_CHILD_NAME);
    remote.reset();
    //3. Now read should be served from local
    readAndAssert(wrapped2, "t1", t1);
    assertEquals(0, remote.openedFiles.size());
    //Now check if local file gets corrupted then read from remote
    Directory wrapped3 = c1.wrapForRead("/foo", defn, remote, INDEX_DATA_CHILD_NAME);
    remote.reset();
    //4. Corrupt the local copy
    writeFile(baseDir, "t1");
    //Now read would be done from remote
    readAndAssert(wrapped3, "t1", t1);
    assertEquals(1, remote.openedFiles.size());
}
Also used : RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) Test(org.junit.Test)

Aggregations

RAMDirectory (org.apache.lucene.store.RAMDirectory)185 Directory (org.apache.lucene.store.Directory)101 IndexWriter (org.apache.lucene.index.IndexWriter)84 Document (org.apache.lucene.document.Document)76 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)75 IndexSearcher (org.apache.lucene.search.IndexSearcher)44 IndexReader (org.apache.lucene.index.IndexReader)41 Test (org.junit.Test)35 TextField (org.apache.lucene.document.TextField)34 Field (org.apache.lucene.document.Field)30 Term (org.apache.lucene.index.Term)25 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)23 Before (org.junit.Before)23 IOException (java.io.IOException)19 Analyzer (org.apache.lucene.analysis.Analyzer)19 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)19 TopDocs (org.apache.lucene.search.TopDocs)16 DirectoryReader (org.apache.lucene.index.DirectoryReader)15 FilterDirectory (org.apache.lucene.store.FilterDirectory)15 FieldType (org.apache.lucene.document.FieldType)13