Search in sources :

Example 56 with EditorHook

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

the class DocumentNodeStoreTest method commitRootSameAsModifiedPathWithConflicts.

@Test
public void commitRootSameAsModifiedPathWithConflicts() throws Exception {
    MemoryDocumentStore store = new MemoryDocumentStore(true);
    final DocumentNodeStore ns = builderProvider.newBuilder().setAsyncDelay(0).setDocumentStore(store).getNodeStore();
    NodeBuilder builder = ns.getRoot().builder();
    builder.child("a").child("b").setProperty("p", 0L);
    merge(ns, builder);
    final List<Throwable> exceptions = synchronizedList(Lists.newArrayList());
    Runnable task = new Runnable() {

        CommitHook hook = new CompositeHook(new ConflictHook(new AnnotatingConflictHandler()), new EditorHook(new ConflictValidatorProvider()));

        @Override
        public void run() {
            try {
                for (int i = 0; i < 100; i++) {
                    NodeBuilder builder = ns.getRoot().builder();
                    NodeBuilder b = builder.child("a").child("b");
                    PropertyState p = b.getProperty("p");
                    assertNotNull(p);
                    long value = p.getValue(Type.LONG) + 1;
                    b.setProperty(p.getName(), value);
                    try {
                        ns.merge(builder, hook, CommitInfo.EMPTY);
                    } catch (CommitFailedException e) {
                        if (e.asRepositoryException() instanceof InvalidItemStateException) {
                        // this is fine and may happen from time to
                        // time because the test updates the same
                        // property concurrently
                        } else {
                            // anything else is unexpected
                            exceptions.add(e);
                        }
                    }
                }
            } catch (AssertionError e) {
                exceptions.add(e);
            }
        }
    };
    List<Thread> threads = Lists.newArrayList();
    for (int i = 0; i < 4; i++) {
        threads.add(new Thread(task));
    }
    for (Thread t : threads) {
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    // check updates are consecutive
    NodeDocument doc = store.find(NODES, Utils.getIdFromPath("/a/b"));
    assertNotNull(doc);
    long previousValue = -1;
    List<String> values = Lists.newArrayList(doc.getLocalMap("p").values());
    for (String v : Lists.reverse(values)) {
        long currentValue = Long.parseLong(v);
        assertEquals(previousValue + 1, currentValue);
        previousValue = currentValue;
    }
    for (Throwable e : exceptions) {
        fail(e.toString());
    }
}
Also used : MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) InvalidItemStateException(javax.jcr.InvalidItemStateException) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) ConflictHook(org.apache.jackrabbit.oak.plugins.commit.ConflictHook) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) ConflictValidatorProvider(org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) CompositeHook(org.apache.jackrabbit.oak.spi.commit.CompositeHook) AnnotatingConflictHandler(org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Test(org.junit.Test)

Example 57 with EditorHook

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

the class DocumentBundlingTest method recreatedBundledNode2.

@Test
public void recreatedBundledNode2() throws Exception {
    NodeBuilder builder = store.getRoot().builder();
    NodeBuilder fileNode = newNode("nt:file");
    fileNode.child("jcr:content").setProperty("jcr:data", "foo");
    builder.child("test").setChildNode("book.jpg", fileNode.getNodeState());
    merge(builder);
    builder = store.getRoot().builder();
    builder.child("a");
    // In this case we recreate the node in CommitHook
    store.merge(builder, new EditorHook(new EditorProvider() {

        @Override
        public Editor getRootEditor(NodeState before, NodeState after, NodeBuilder builder, CommitInfo info) throws CommitFailedException {
            return new BookRecreatingEditor(builder);
        }
    }), CommitInfo.EMPTY);
}
Also used : DocumentNodeState(org.apache.jackrabbit.oak.plugins.document.DocumentNodeState) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) AbstractNodeState(org.apache.jackrabbit.oak.spi.state.AbstractNodeState) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) EditorProvider(org.apache.jackrabbit.oak.spi.commit.EditorProvider) Test(org.junit.Test)

Example 58 with EditorHook

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

the class NodeStoreUtils method mergeWithConcurrentCheck.

static void mergeWithConcurrentCheck(NodeStore nodeStore, NodeBuilder builder, IndexEditorProvider indexEditorProvider) throws CommitFailedException {
    CompositeHook hooks = new CompositeHook(ResetCommitAttributeHook.INSTANCE, new EditorHook(new IndexUpdateProvider(indexEditorProvider, null, true)), new ConflictHook(new AnnotatingConflictHandler()), new EditorHook(CompositeEditorProvider.compose(singletonList(new ConflictValidatorProvider()))));
    nodeStore.merge(builder, hooks, createCommitInfo());
}
Also used : CompositeHook(org.apache.jackrabbit.oak.spi.commit.CompositeHook) IndexUpdateProvider(org.apache.jackrabbit.oak.plugins.index.IndexUpdateProvider) AnnotatingConflictHandler(org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) ConflictHook(org.apache.jackrabbit.oak.plugins.commit.ConflictHook) ConflictValidatorProvider(org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider)

Example 59 with EditorHook

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

the class LuceneIndexEditor2Test method simplePropertyUpdateCallback.

@Test
public void simplePropertyUpdateCallback() throws Exception {
    IndexDefinitionBuilder defnb = new IndexDefinitionBuilder();
    defnb.indexRule("nt:base").property("foo").propertyIndex();
    NodeState defnState = defnb.build();
    IndexDefinition defn = new IndexDefinition(root, defnState, indexPath);
    LuceneIndexEditorContext ctx = newContext(defnState.builder(), defn, true);
    ctx.setPropertyUpdateCallback(propCallback);
    EditorHook hook = createHook(ctx);
    updateBefore(defnb);
    // Property added
    NodeBuilder builder = before.builder();
    builder.child("a").setProperty("foo", "bar");
    builder.child("a").setProperty("foo2", "bar");
    builder.child("a").child("b");
    before = hook.processCommit(root, builder.getNodeState(), CommitInfo.EMPTY);
    propCallback.state.assertState("/a", "foo", UpdateState.ADDED);
    assertEquals(1, propCallback.invocationCount);
    assertEquals(1, propCallback.doneInvocationCount);
    propCallback.reset();
    // Property updated
    builder = before.builder();
    builder.child("a").setProperty("foo", "bar2");
    builder.child("a").setProperty("foo2", "bar2");
    before = hook.processCommit(before, builder.getNodeState(), CommitInfo.EMPTY);
    propCallback.state.assertState("/a", "foo", UpdateState.UPDATED);
    assertEquals(1, propCallback.invocationCount);
    propCallback.reset();
    // Property deleted
    builder = before.builder();
    builder.child("a").removeProperty("foo");
    builder.child("a").removeProperty("foo2");
    before = hook.processCommit(before, builder.getNodeState(), CommitInfo.EMPTY);
    propCallback.state.assertState("/a", "foo", UpdateState.DELETED);
    assertEquals(1, propCallback.invocationCount);
    propCallback.reset();
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) IndexDefinitionBuilder(org.apache.jackrabbit.oak.plugins.index.lucene.util.IndexDefinitionBuilder) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 60 with EditorHook

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

the class LuceneIndexEditor2Test method basics.

@Test
public void basics() throws Exception {
    IndexDefinitionBuilder defnb = new IndexDefinitionBuilder();
    defnb.indexRule("nt:base").property("foo").propertyIndex();
    NodeState defnState = defnb.build();
    IndexDefinition defn = new IndexDefinition(root, defnState, indexPath);
    LuceneIndexEditorContext ctx = newContext(defnState.builder(), defn, true);
    EditorHook hook = createHook(ctx);
    updateBefore(defnb);
    NodeBuilder builder = before.builder();
    builder.child("a").setProperty("foo", "bar");
    hook.processCommit(root, builder.getNodeState(), CommitInfo.EMPTY);
    assertThat(writer.docs.keySet(), containsInAnyOrder("/a"));
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) IndexDefinitionBuilder(org.apache.jackrabbit.oak.plugins.index.lucene.util.IndexDefinitionBuilder) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Aggregations

EditorHook (org.apache.jackrabbit.oak.spi.commit.EditorHook)69 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)46 Test (org.junit.Test)44 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)43 IndexUpdateProvider (org.apache.jackrabbit.oak.plugins.index.IndexUpdateProvider)24 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)14 EmptyNodeState (org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState)12 ConflictValidatorProvider (org.apache.jackrabbit.oak.plugins.commit.ConflictValidatorProvider)11 PropertyIndexEditorProvider (org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider)11 CommitHook (org.apache.jackrabbit.oak.spi.commit.CommitHook)11 CompositeHook (org.apache.jackrabbit.oak.spi.commit.CompositeHook)11 CompositeEditorProvider (org.apache.jackrabbit.oak.spi.commit.CompositeEditorProvider)8 AnnotatingConflictHandler (org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler)7 ConflictHook (org.apache.jackrabbit.oak.plugins.commit.ConflictHook)7 MemoryNodeStore (org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore)7 EditorProvider (org.apache.jackrabbit.oak.spi.commit.EditorProvider)7 CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)6 MountInfoProvider (org.apache.jackrabbit.oak.spi.mount.MountInfoProvider)6 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)6 Before (org.junit.Before)6