Search in sources :

Example 1 with CommitContext

use of org.apache.jackrabbit.oak.spi.commit.CommitContext in project jackrabbit-oak by apache.

the class LuceneIndexEditorProvider method getIndexEditor.

@Override
public Editor getIndexEditor(@Nonnull String type, @Nonnull NodeBuilder definition, @Nonnull NodeState root, @Nonnull IndexUpdateCallback callback) throws CommitFailedException {
    if (TYPE_LUCENE.equals(type)) {
        checkArgument(callback instanceof ContextAwareCallback, "callback instance not of type " + "ContextAwareCallback [%s]", callback);
        IndexingContext indexingContext = ((ContextAwareCallback) callback).getIndexingContext();
        BlobDeletionCallback blobDeletionCallback = activeDeletedBlobCollector.getBlobDeletionCallback();
        indexingContext.registerIndexCommitCallback(blobDeletionCallback);
        LuceneIndexWriterFactory writerFactory = null;
        IndexDefinition indexDefinition = null;
        boolean asyncIndexing = true;
        String indexPath = indexingContext.getIndexPath();
        PropertyIndexUpdateCallback propertyUpdateCallback = null;
        if (nrtIndexingEnabled() && !indexingContext.isAsync() && IndexDefinition.supportsSyncOrNRTIndexing(definition)) {
            // incremental indexing
            if (indexingContext.isReindexing()) {
                return null;
            }
            CommitContext commitContext = getCommitContext(indexingContext);
            if (commitContext == null) {
                // Logically there should not be any commit without commit context. But
                // some initializer code does the commit with out it. So ignore such calls with
                // warning now
                // TODO Revisit use of warn level once all such cases are analyzed
                log.warn("No CommitContext found for commit", new Exception());
                return null;
            }
            // TODO Also check if index has been done once
            writerFactory = new LocalIndexWriterFactory(getDocumentHolder(commitContext), indexPath);
            // creating definition instance for each commit as this gets executed for each commit
            if (indexTracker != null) {
                indexDefinition = indexTracker.getIndexDefinition(indexPath);
                if (indexDefinition != null && !indexDefinition.hasMatchingNodeTypeReg(root)) {
                    log.debug("Detected change in NodeType registry for index {}. Would not use " + "existing index definition", indexDefinition.getIndexPath());
                    indexDefinition = null;
                }
            }
            if (indexDefinition == null) {
                indexDefinition = IndexDefinition.newBuilder(root, definition.getNodeState(), indexPath).build();
            }
            if (indexDefinition.hasSyncPropertyDefinitions()) {
                propertyUpdateCallback = new PropertyIndexUpdateCallback(indexPath, definition, root);
                if (indexTracker != null) {
                    PropertyQuery query = new LuceneIndexPropertyQuery(indexTracker, indexPath);
                    propertyUpdateCallback.getUniquenessConstraintValidator().setSecondStore(query);
                }
            }
            // Pass on a read only builder to ensure that nothing gets written
            // at all to NodeStore for local indexing.
            // TODO [hybrid] This would cause issue with Facets as for faceted fields
            // some stuff gets written to NodeBuilder. That logic should be refactored
            // to be moved to LuceneIndexWriter
            definition = new ReadOnlyBuilder(definition.getNodeState());
            asyncIndexing = false;
        }
        if (writerFactory == null) {
            writerFactory = new DefaultIndexWriterFactory(mountInfoProvider, newDirectoryFactory(blobDeletionCallback), writerConfig);
        }
        LuceneIndexEditorContext context = new LuceneIndexEditorContext(root, definition, indexDefinition, callback, writerFactory, extractedTextCache, augmentorFactory, indexingContext, asyncIndexing);
        context.setPropertyUpdateCallback(propertyUpdateCallback);
        return new LuceneIndexEditor(context);
    }
    return null;
}
Also used : PropertyIndexUpdateCallback(org.apache.jackrabbit.oak.plugins.index.lucene.property.PropertyIndexUpdateCallback) PropertyQuery(org.apache.jackrabbit.oak.plugins.index.lucene.property.PropertyQuery) LuceneIndexPropertyQuery(org.apache.jackrabbit.oak.plugins.index.lucene.property.LuceneIndexPropertyQuery) ReadOnlyBuilder(org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder) ContextAwareCallback(org.apache.jackrabbit.oak.plugins.index.ContextAwareCallback) BlobDeletionCallback(org.apache.jackrabbit.oak.plugins.index.lucene.directory.ActiveDeletedBlobCollectorFactory.BlobDeletionCallback) LuceneIndexPropertyQuery(org.apache.jackrabbit.oak.plugins.index.lucene.property.LuceneIndexPropertyQuery) LuceneIndexWriterFactory(org.apache.jackrabbit.oak.plugins.index.lucene.writer.LuceneIndexWriterFactory) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) CommitContext(org.apache.jackrabbit.oak.spi.commit.CommitContext) IndexingContext(org.apache.jackrabbit.oak.plugins.index.IndexingContext) LocalIndexWriterFactory(org.apache.jackrabbit.oak.plugins.index.lucene.hybrid.LocalIndexWriterFactory) DefaultIndexWriterFactory(org.apache.jackrabbit.oak.plugins.index.lucene.writer.DefaultIndexWriterFactory)

Example 2 with CommitContext

use of org.apache.jackrabbit.oak.spi.commit.CommitContext in project jackrabbit-oak by apache.

the class ExternalIndexObserver method excludes.

@Override
public boolean excludes(@Nonnull NodeState root, @Nonnull CommitInfo info) {
    // Only interested in external changes
    if (!info.isExternal()) {
        return true;
    }
    CommitContext commitContext = (CommitContext) info.getInfo().get(CommitContext.NAME);
    // Commit done internally i.e. one not using Root/Tree API
    if (commitContext == null) {
        return true;
    }
    IndexedPaths indexedPaths = (IndexedPaths) commitContext.get(LuceneDocumentHolder.NAME);
    // Nothing to be indexed
    if (indexedPaths == null) {
        log.debug("IndexPaths not found. Journal support missing");
        return true;
    }
    if (indexedPaths.isEmpty()) {
        return true;
    }
    return false;
}
Also used : CommitContext(org.apache.jackrabbit.oak.spi.commit.CommitContext)

Example 3 with CommitContext

use of org.apache.jackrabbit.oak.spi.commit.CommitContext 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 4 with CommitContext

use of org.apache.jackrabbit.oak.spi.commit.CommitContext in project jackrabbit-oak by apache.

the class LocalIndexObserverTest method docsAddedToQueue.

@Test
public void docsAddedToQueue() throws Exception {
    CommitInfo info = newCommitInfo();
    CommitContext cc = (CommitContext) info.getInfo().get(CommitContext.NAME);
    LuceneDocumentHolder holder = new LuceneDocumentHolder(collectingQueue, 500);
    holder.add(false, LuceneDoc.forDelete("foo", "bar"));
    cc.set(LuceneDocumentHolder.NAME, holder);
    observer.contentChanged(EMPTY_NODE, info);
    assertEquals(1, collectingQueue.getQueuedDocs().size());
    assertNull(cc.get(LuceneDocumentHolder.NAME));
}
Also used : CommitContext(org.apache.jackrabbit.oak.spi.commit.CommitContext) SimpleCommitContext(org.apache.jackrabbit.oak.spi.commit.SimpleCommitContext) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) Test(org.junit.Test)

Example 5 with CommitContext

use of org.apache.jackrabbit.oak.spi.commit.CommitContext in project jackrabbit-oak by apache.

the class ExternalChangesTest method changeSetForBranchCommit.

@Test
public void changeSetForBranchCommit() throws Exception {
    final int NUM_NODES = DocumentMK.UPDATE_LIMIT / 2;
    final int NUM_PROPS = 10;
    Set<String> propNames = Sets.newHashSet();
    NodeBuilder b1 = ns1.getRoot().builder();
    for (int i = 0; i < NUM_NODES; i++) {
        NodeBuilder c = b1.child("n" + i);
        for (int j = 0; j < NUM_PROPS; j++) {
            c.setProperty("q" + j, "value");
            c.setProperty("p" + j, "value");
            propNames.add("q" + j);
            propNames.add("p" + j);
        }
    }
    ns1.merge(b1, newCollectingHook(), newCommitInfo());
    ns1.runBackgroundUpdateOperations();
    c2.reset();
    ns2.runBackgroundReadOperations();
    CommitInfo ci = c2.getExternalChange();
    CommitContext cc = (CommitContext) ci.getInfo().get(CommitContext.NAME);
    assertNotNull(cc);
    ChangeSet cs = (ChangeSet) cc.get(ChangeSet.COMMIT_CONTEXT_OBSERVATION_CHANGESET);
    assertNotNull(cs);
    assertTrue(cs.getPropertyNames().containsAll(propNames));
}
Also used : CommitContext(org.apache.jackrabbit.oak.spi.commit.CommitContext) SimpleCommitContext(org.apache.jackrabbit.oak.spi.commit.SimpleCommitContext) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) ChangeSet(org.apache.jackrabbit.oak.spi.observation.ChangeSet) Test(org.junit.Test)

Aggregations

CommitContext (org.apache.jackrabbit.oak.spi.commit.CommitContext)13 SimpleCommitContext (org.apache.jackrabbit.oak.spi.commit.SimpleCommitContext)7 CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)6 Test (org.junit.Test)6 ChangeSet (org.apache.jackrabbit.oak.spi.observation.ChangeSet)5 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)5 Map (java.util.Map)1 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)1 JournalProperty (org.apache.jackrabbit.oak.plugins.document.spi.JournalProperty)1 JournalPropertyBuilder (org.apache.jackrabbit.oak.plugins.document.spi.JournalPropertyBuilder)1 CommitInfoCollector (org.apache.jackrabbit.oak.plugins.index.AsyncIndexUpdateTest.CommitInfoCollector)1 ContextAwareCallback (org.apache.jackrabbit.oak.plugins.index.ContextAwareCallback)1 IndexingContext (org.apache.jackrabbit.oak.plugins.index.IndexingContext)1 IndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition)1 LuceneDocumentMaker (org.apache.jackrabbit.oak.plugins.index.lucene.LuceneDocumentMaker)1 BlobDeletionCallback (org.apache.jackrabbit.oak.plugins.index.lucene.directory.ActiveDeletedBlobCollectorFactory.BlobDeletionCallback)1 LocalIndexWriterFactory (org.apache.jackrabbit.oak.plugins.index.lucene.hybrid.LocalIndexWriterFactory)1 LuceneIndexPropertyQuery (org.apache.jackrabbit.oak.plugins.index.lucene.property.LuceneIndexPropertyQuery)1 PropertyIndexUpdateCallback (org.apache.jackrabbit.oak.plugins.index.lucene.property.PropertyIndexUpdateCallback)1 PropertyQuery (org.apache.jackrabbit.oak.plugins.index.lucene.property.PropertyQuery)1