Search in sources :

Example 51 with RAMDirectory

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

the class IndexMigrationTest method testMigration.

public void testMigration() throws Exception {
    List<Document> docs = new ArrayList<Document>();
    docs.add(createDocument("ab", "a"));
    docs.add(createDocument("a", "b"));
    docs.add(createDocument("abcd", "c"));
    docs.add(createDocument("abc", "d"));
    DirectoryManager dirMgr = new RAMDirectoryManager();
    PersistentIndex idx = new PersistentIndex("index", new StandardAnalyzer(Version.LUCENE_36), Similarity.getDefault(), new DocNumberCache(100), new IndexingQueue(new IndexingQueueStore(new RAMDirectory())), dirMgr, 0);
    idx.addDocuments(docs.toArray(new Document[docs.size()]));
    idx.commit();
    IndexMigration.migrate(idx, dirMgr, SEP_CHAR);
}
Also used : RAMDirectoryManager(org.apache.jackrabbit.core.query.lucene.directory.RAMDirectoryManager) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ArrayList(java.util.ArrayList) DirectoryManager(org.apache.jackrabbit.core.query.lucene.directory.DirectoryManager) RAMDirectoryManager(org.apache.jackrabbit.core.query.lucene.directory.RAMDirectoryManager) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 52 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 53 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 54 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 55 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)

Aggregations

RAMDirectory (org.apache.lucene.store.RAMDirectory)183 Directory (org.apache.lucene.store.Directory)101 IndexWriter (org.apache.lucene.index.IndexWriter)82 Document (org.apache.lucene.document.Document)75 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)73 IndexSearcher (org.apache.lucene.search.IndexSearcher)43 IndexReader (org.apache.lucene.index.IndexReader)41 Test (org.junit.Test)35 TextField (org.apache.lucene.document.TextField)33 Field (org.apache.lucene.document.Field)29 Term (org.apache.lucene.index.Term)25 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)21 Before (org.junit.Before)21 IOException (java.io.IOException)19 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)19 Analyzer (org.apache.lucene.analysis.Analyzer)18 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