Search in sources :

Example 1 with OakDirectory

use of org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory in project jackrabbit-oak by apache.

the class LuceneIndexDumper method copyContent.

private void copyContent(NodeState idx, IndexDefinition defn, File dir, String dirName, Closer closer) throws IOException {
    File idxDir = DirectoryUtils.createSubDir(dir, dirName);
    Directory sourceDir = new OakDirectory(new ReadOnlyBuilder(idx), dirName, defn, true);
    Directory targetDir = FSDirectory.open(idxDir);
    closer.register(sourceDir);
    closer.register(targetDir);
    for (String file : sourceDir.listAll()) {
        sourceDir.copy(targetDir, file, file, IOContext.DEFAULT);
        size += sourceDir.fileLength(file);
    }
}
Also used : ReadOnlyBuilder(org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) File(java.io.File) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 2 with OakDirectory

use of org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory in project jackrabbit-oak by apache.

the class DefaultIndexReaderFactory method createReader.

@CheckForNull
private LuceneIndexReader createReader(IndexDefinition definition, NodeState defnNodeState, String indexPath, String indexDataNodeName, String suggestDataNodeName) throws IOException {
    Directory directory = null;
    NodeState data = defnNodeState.getChildNode(indexDataNodeName);
    if (data.exists()) {
        directory = new OakDirectory(new ReadOnlyBuilder(defnNodeState), indexDataNodeName, definition, true);
        if (cloner != null) {
            directory = cloner.wrapForRead(indexPath, definition, directory, indexDataNodeName);
        }
    } else if (PERSISTENCE_FILE.equalsIgnoreCase(defnNodeState.getString(PERSISTENCE_NAME))) {
        String path = defnNodeState.getString(PERSISTENCE_PATH);
        if (path != null && new File(path).exists()) {
            directory = FSDirectory.open(new File(path));
        }
    }
    if (directory != null) {
        OakDirectory suggestDirectory = null;
        if (definition.isSuggestEnabled()) {
            suggestDirectory = new OakDirectory(new ReadOnlyBuilder(defnNodeState), suggestDataNodeName, definition, true);
        }
        try {
            LuceneIndexReader reader = new DefaultIndexReader(directory, suggestDirectory, definition.getAnalyzer());
            // closed in LuceneIndexReader.close()
            directory = null;
            return reader;
        } finally {
            if (directory != null) {
                directory.close();
            }
        }
    }
    return null;
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) ReadOnlyBuilder(org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) File(java.io.File) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) CheckForNull(javax.annotation.CheckForNull)

Example 3 with OakDirectory

use of org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory in project jackrabbit-oak by apache.

the class LuceneIndexDumperTest method directoryDump.

@Test
public void directoryDump() throws Exception {
    IndexDefinition defn = IndexDefinition.newBuilder(rootState, idx.getNodeState(), "/fooIndex").build();
    long size = 0;
    Directory dir = new OakDirectory(idx, ":data", defn, false);
    createFile(dir, "foo.txt", "Test content");
    size += DirectoryUtils.dirSize(dir);
    Directory dir2 = new OakDirectory(idx, ":data2" + MultiplexersLucene.INDEX_DIR_SUFFIX, defn, false);
    createFile(dir2, "foo.txt", "Test content");
    size += DirectoryUtils.dirSize(dir2);
    NodeBuilder builder = rootState.builder();
    builder.setChildNode("fooIndex", idx.getNodeState());
    NodeState indexState = builder.getNodeState();
    File out = temporaryFolder.newFolder();
    LuceneIndexDumper dumper = new LuceneIndexDumper(indexState, "/fooIndex", out);
    dumper.dump();
    File indexDir = dumper.getIndexDir();
    assertNotNull(indexDir);
    // 2 dir + 1 meta
    assertEquals(3, indexDir.listFiles().length);
    assertEquals(dumper.getSize(), size);
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) IndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) File(java.io.File) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 4 with OakDirectory

use of org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory in project jackrabbit-oak by apache.

the class IndexConsistencyCheckerTest method missingFile.

@Test
public void missingFile() throws Exception {
    IndexDefinition defn = IndexDefinition.newBuilder(rootState, idx.getNodeState(), "/fooIndex").build();
    Directory dir = new OakDirectory(idx, ":data", defn, false);
    createIndex(dir, 10);
    NodeBuilder builder = rootState.builder();
    idx.getChildNode(":data").getChildNode("segments.gen").remove();
    builder.setChildNode("fooIndex", idx.getNodeState());
    NodeState indexState = builder.getNodeState();
    IndexConsistencyChecker checker = new IndexConsistencyChecker(indexState, "/fooIndex", temporaryFolder.getRoot());
    Result result = checker.check(Level.FULL);
    assertFalse(result.clean);
    assertEquals(1, result.dirStatus.get(0).missingFiles.size());
    assertNull(result.dirStatus.get(0).status);
    dumpResult(result);
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) IndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) Directory(org.apache.lucene.store.Directory) Result(org.apache.jackrabbit.oak.plugins.index.lucene.directory.IndexConsistencyChecker.Result) Test(org.junit.Test)

Example 5 with OakDirectory

use of org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory in project jackrabbit-oak by apache.

the class IndexConsistencyCheckerTest method badFile.

@Test
public void badFile() throws Exception {
    IndexDefinition defn = IndexDefinition.newBuilder(rootState, idx.getNodeState(), "/fooIndex").build();
    Directory dir = new OakDirectory(idx, ":data", defn, false);
    createIndex(dir, 10);
    NodeBuilder builder = rootState.builder();
    NodeBuilder file = idx.getChildNode(":data").getChildNode("_0.cfe");
    List<Blob> blobs = Lists.newArrayList(file.getProperty("jcr:data").getValue(Type.BINARIES));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(blobs.get(0).getNewStream(), baos);
    byte[] bytes = baos.toByteArray();
    //Flip the 3rd bit to make it corrupt
    bytes[0] = (byte) (bytes[0] ^ (1 << 3));
    blobs.set(0, new ArrayBasedBlob(bytes));
    file.setProperty("jcr:data", blobs, Type.BINARIES);
    builder.setChildNode("fooIndex", idx.getNodeState());
    NodeState indexState = builder.getNodeState();
    IndexConsistencyChecker checker = new IndexConsistencyChecker(indexState, "/fooIndex", temporaryFolder.getRoot());
    Result result = checker.check(Level.FULL);
    assertFalse(result.clean);
    assertEquals(0, result.dirStatus.get(0).missingFiles.size());
    assertFalse(result.dirStatus.get(0).status.clean);
}
Also used : Blob(org.apache.jackrabbit.oak.api.Blob) ArrayBasedBlob(org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) IndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition) ArrayBasedBlob(org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) Directory(org.apache.lucene.store.Directory) Result(org.apache.jackrabbit.oak.plugins.index.lucene.directory.IndexConsistencyChecker.Result) Test(org.junit.Test)

Aggregations

OakDirectory (org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory)9 Directory (org.apache.lucene.store.Directory)8 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)5 File (java.io.File)4 IndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition)4 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)4 Test (org.junit.Test)4 Result (org.apache.jackrabbit.oak.plugins.index.lucene.directory.IndexConsistencyChecker.Result)3 ReadOnlyBuilder (org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder)3 FSDirectory (org.apache.lucene.store.FSDirectory)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 CheckForNull (javax.annotation.CheckForNull)1 Blob (org.apache.jackrabbit.oak.api.Blob)1 ArrayBasedBlob (org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob)1 CheckIndex (org.apache.lucene.index.CheckIndex)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1