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;
}
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"));
}
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"));
}
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);
}
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());
}
Aggregations