Search in sources :

Example 1 with IndexDefinition

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

the class ExternalIndexObserver method contentChanged.

@Override
public void contentChanged(@Nonnull NodeState after, @Nonnull CommitInfo info) {
    //Only interested in external changes
    if (excludes(after, info)) {
        return;
    }
    CommitContext commitContext = (CommitContext) info.getInfo().get(CommitContext.NAME);
    IndexedPaths indexedPaths = (IndexedPaths) commitContext.get(LuceneDocumentHolder.NAME);
    commitContext.remove(LuceneDocumentHolder.NAME);
    log.trace("Received indexed paths {}", indexedPaths);
    int droppedCount = 0;
    int indexedCount = 0;
    TimerStats.Context ctx = timer.time();
    Set<String> indexPaths = Sets.newHashSet();
    for (IndexedPathInfo indexData : indexedPaths) {
        String path = indexData.getPath();
        NodeState indexedNode = null;
        for (String indexPath : indexData.getIndexPaths()) {
            IndexDefinition defn = indexTracker.getIndexDefinition(indexPath);
            //but not used locally
            if (defn == null) {
                continue;
            }
            //Lazily initialize indexedNode
            if (indexedNode == null) {
                indexedNode = NodeStateUtils.getNode(after, path);
            }
            if (!indexedNode.exists()) {
                continue;
            }
            IndexDefinition.IndexingRule indexingRule = defn.getApplicableIndexingRule(indexedNode);
            if (indexingRule == null) {
                log.debug("No indexingRule found for path {} for index {}", path, indexPath);
                continue;
            }
            indexPaths.add(indexPath);
            try {
                Document doc = new LuceneDocumentMaker(defn, indexingRule, path).makeDocument(indexedNode);
                if (doc != null) {
                    if (indexingQueue.add(LuceneDoc.forUpdate(indexPath, path, doc))) {
                        indexedCount++;
                    } else {
                        droppedCount++;
                    }
                }
            } catch (Exception e) {
                log.warn("Ignoring making LuceneDocument for path {} for index {} due to exception", path, indexPath, e);
            }
        }
    }
    if (droppedCount > 0) {
        log.warn("Dropped [{}] docs from indexing as queue is full", droppedCount);
    }
    added.mark(indexedCount);
    ctx.stop();
    log.debug("Added {} documents for {} indexes from external changes", indexedCount, indexPaths.size());
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) LuceneDocumentMaker(org.apache.jackrabbit.oak.plugins.index.lucene.LuceneDocumentMaker) TimerStats(org.apache.jackrabbit.oak.stats.TimerStats) Document(org.apache.lucene.document.Document) IndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition) CommitContext(org.apache.jackrabbit.oak.spi.commit.CommitContext)

Example 2 with IndexDefinition

use of org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition 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 3 with IndexDefinition

use of org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition 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 4 with IndexDefinition

use of org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition 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)

Example 5 with IndexDefinition

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

the class DefaultIndexWriterTest method indexUpdated.

@Test
public void indexUpdated() throws Exception {
    IndexDefinition defn = new IndexDefinition(root, builder.getNodeState(), "/foo");
    DefaultIndexWriter writer = new DefaultIndexWriter(defn, builder, new DefaultDirectoryFactory(null, null), INDEX_DATA_CHILD_NAME, SUGGEST_DATA_CHILD_NAME, false);
    Document document = new Document();
    document.add(newPathField("/a/b"));
    writer.updateDocument("/a/b", document);
    assertTrue(writer.close(0));
}
Also used : IndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition) Document(org.apache.lucene.document.Document) DefaultDirectoryFactory(org.apache.jackrabbit.oak.plugins.index.lucene.directory.DefaultDirectoryFactory) Test(org.junit.Test)

Aggregations

IndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition)28 Test (org.junit.Test)24 LuceneIndexWriter (org.apache.jackrabbit.oak.plugins.index.lucene.writer.LuceneIndexWriter)7 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)7 Document (org.apache.lucene.document.Document)7 Directory (org.apache.lucene.store.Directory)7 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)5 File (java.io.File)4 OakDirectory (org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory)4 Result (org.apache.jackrabbit.oak.plugins.index.lucene.directory.IndexConsistencyChecker.Result)3 LuceneIndexReader (org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader)3 DefaultDirectoryFactory (org.apache.jackrabbit.oak.plugins.index.lucene.directory.DefaultDirectoryFactory)2 DefaultIndexWriterFactory (org.apache.jackrabbit.oak.plugins.index.lucene.writer.DefaultIndexWriterFactory)2 LuceneIndexWriterFactory (org.apache.jackrabbit.oak.plugins.index.lucene.writer.LuceneIndexWriterFactory)2 StringField (org.apache.lucene.document.StringField)2 FSDirectory (org.apache.lucene.store.FSDirectory)2 Closer (com.google.common.io.Closer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Blob (org.apache.jackrabbit.oak.api.Blob)1