Search in sources :

Example 31 with EditorHook

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

the class AsyncIndexUpdateTest method disableSupersededIndex.

// OAK-6864
@Test
public void disableSupersededIndex() throws Exception {
    IndexEditorProvider propIdxEditorProvider = new PropertyIndexEditorProvider();
    EditorHook propIdxHook = new EditorHook(new IndexUpdateProvider(propIdxEditorProvider));
    MemoryNodeStore store = new MemoryNodeStore();
    String supersededIndexName = "supersededIndex";
    String supersedingIndexName = "supersedingIndex";
    AsyncIndexUpdate async = new AsyncIndexUpdate("async", store, propIdxEditorProvider);
    // Create superseded index def and merge it
    NodeBuilder builder = store.getRoot().builder();
    NodeBuilder oakIndex = builder.child(INDEX_DEFINITIONS_NAME);
    createIndexDefinition(oakIndex, supersededIndexName, true, false, ImmutableSet.of("foo"), null);
    store.merge(builder, propIdxHook, CommitInfo.EMPTY);
    // Create superseding index def and merge it
    builder = store.getRoot().builder();
    oakIndex = builder.child(INDEX_DEFINITIONS_NAME);
    createIndexDefinition(oakIndex, supersedingIndexName, true, false, ImmutableSet.of("foo"), null).setProperty(ASYNC_PROPERTY_NAME, ImmutableSet.of("async", "nrt"), Type.STRINGS).setProperty(SUPERSEDED_INDEX_PATHS, INDEX_DEFINITIONS_NAME + "/" + supersededIndexName);
    store.merge(builder, propIdxHook, CommitInfo.EMPTY);
    // add a change and index it thought superseded index
    builder = store.getRoot().builder();
    builder.child("testNode1").setProperty("foo", "bar");
    store.merge(builder, propIdxHook, CommitInfo.EMPTY);
    // verify state
    NodeState supersededIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersededIndexName);
    assertEquals("Index disabled too early", "property", supersededIndex.getString("type"));
    assertFalse("Don't set :disableIndexesOnNextCycle on superseded index", supersededIndex.hasProperty(DISABLE_INDEXES_ON_NEXT_CYCLE));
    NodeState supersedingIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersedingIndexName);
    assertFalse("Don't set :disableIndexesOnNextCycle on superseding index just yet", supersedingIndex.hasProperty(DISABLE_INDEXES_ON_NEXT_CYCLE));
    // do an async run - this should reindex the superseding index
    async.run();
    // verify state
    supersededIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersededIndexName);
    assertEquals("Index disabled too early", "property", supersededIndex.getString("type"));
    assertFalse("Don't set :disableIndexesOnNextCycle on superseded index", supersededIndex.hasProperty(DISABLE_INDEXES_ON_NEXT_CYCLE));
    supersedingIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersedingIndexName);
    assertTrue(":disableIndexesOnNextCycle not set on superseding index after reindexing run", supersedingIndex.hasProperty(DISABLE_INDEXES_ON_NEXT_CYCLE));
    // add another change and index it thought superseded index
    builder = store.getRoot().builder();
    store.getRoot().builder().child("testNode2").setProperty("foo", "bar");
    store.merge(builder, propIdxHook, CommitInfo.EMPTY);
    // verify state
    supersededIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersededIndexName);
    assertEquals("Index disabled too early", "property", supersededIndex.getString("type"));
    assertFalse("Don't set :disableIndexesOnNextCycle on superseded index", supersededIndex.hasProperty(DISABLE_INDEXES_ON_NEXT_CYCLE));
    supersedingIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersedingIndexName);
    assertTrue(":disableIndexesOnNextCycle not set on superseding index after reindexing run", supersedingIndex.getBoolean(DISABLE_INDEXES_ON_NEXT_CYCLE));
    // do another async run - indexes should get disabled now
    async.run();
    // verify state
    supersededIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersededIndexName);
    assertEquals("Index yet not disabled", "disabled", supersededIndex.getString("type"));
    assertFalse("Don't set :disableIndexesOnNextCycle on superseded index", supersededIndex.hasProperty(DISABLE_INDEXES_ON_NEXT_CYCLE));
    supersedingIndex = store.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(supersedingIndexName);
    assertFalse("Don't keep :disableIndexesOnNextCycle on superseding index after disabling", supersedingIndex.hasProperty(DISABLE_INDEXES_ON_NEXT_CYCLE));
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) PropertyIndexEditorProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 32 with EditorHook

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

the class PropertyIndexTest method singleMount.

@Test
public void singleMount() throws Exception {
    NodeState root = INITIAL_CONTENT;
    // Add index definition
    NodeBuilder builder = root.builder();
    NodeBuilder index = createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "foo", true, false, ImmutableSet.of("foo"), null);
    index.setProperty("entryCount", -1);
    NodeState before = builder.getNodeState();
    // Add some content and process it through the property index hook
    builder.child("a").setProperty("foo", "abc");
    builder.child("b").child("x").setProperty("foo", "abc");
    builder.child("a").child("x").setProperty("foo", "abc");
    builder.child("m").child("n").setProperty("foo", "abc");
    builder.child("m").child("n").child("o").setProperty("foo", "abc");
    builder.child("m").setProperty("foo", "abc");
    NodeState after = builder.getNodeState();
    MountInfoProvider mip = Mounts.newBuilder().mount("foo", "/a", "/m/n").build();
    Mount fooMount = mip.getMountByName("foo");
    Mount defMount = mip.getDefaultMount();
    EditorHook hook = new EditorHook(new IndexUpdateProvider(new PropertyIndexEditorProvider().with(mip)));
    NodeState indexed = hook.processCommit(before, after, CommitInfo.EMPTY);
    FilterImpl f = createFilter(indexed, NT_BASE);
    // Query the index
    PropertyIndexLookup lookup = new PropertyIndexLookup(indexed, mip);
    assertEquals(ImmutableSet.of("a", "b/x", "a/x", "m", "m/n", "m/n/o"), find(lookup, "foo", "abc", f));
    assertEquals(ImmutableSet.of(), find(lookup, "foo", "ghi", f));
    assertTrue(getNode(indexed, "/oak:index/foo/:index").exists());
    // Separate node for mount
    assertTrue(getNode(indexed, "/oak:index/foo/" + getNodeForMount(fooMount)).exists());
    // Index entries for paths in foo mount should go to :oak:foo-index
    assertTrue(getNode(indexed, pathInIndex(fooMount, "/oak:index/foo", "/a", "abc")).exists());
    assertTrue(getNode(indexed, pathInIndex(fooMount, "/oak:index/foo", "/a/x", "abc")).exists());
    assertTrue(getNode(indexed, pathInIndex(fooMount, "/oak:index/foo", "/m/n", "abc")).exists());
    assertTrue(getNode(indexed, pathInIndex(fooMount, "/oak:index/foo", "/m/n/o", "abc")).exists());
    assertFalse(getNode(indexed, pathInIndex(defMount, "/oak:index/foo", "/a", "abc")).exists());
    assertFalse(getNode(indexed, pathInIndex(defMount, "/oak:index/foo", "/a/x", "abc")).exists());
    assertFalse(getNode(indexed, pathInIndex(defMount, "/oak:index/foo", "/m/n", "abc")).exists());
    assertFalse(getNode(indexed, pathInIndex(defMount, "/oak:index/foo", "/m/n/o", "abc")).exists());
    // All other index entries should go to :index
    assertTrue(getNode(indexed, pathInIndex(defMount, "/oak:index/foo", "/b", "abc")).exists());
    assertTrue(getNode(indexed, pathInIndex(defMount, "/oak:index/foo", "/b/x", "abc")).exists());
    assertTrue(getNode(indexed, pathInIndex(defMount, "/oak:index/foo", "/m", "abc")).exists());
    assertFalse(getNode(indexed, pathInIndex(fooMount, "/oak:index/foo", "/b", "abc")).exists());
    assertFalse(getNode(indexed, pathInIndex(fooMount, "/oak:index/foo", "/b/x", "abc")).exists());
// System.out.println(NodeStateUtils.toString(getNode(indexed, "/oak:index/foo")));
}
Also used : IndexUpdateProvider(org.apache.jackrabbit.oak.plugins.index.IndexUpdateProvider) EmptyNodeState(org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) Mount(org.apache.jackrabbit.oak.spi.mount.Mount) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) MountInfoProvider(org.apache.jackrabbit.oak.spi.mount.MountInfoProvider) Test(org.junit.Test)

Example 33 with EditorHook

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

the class TestUtil method registerNodeType.

public static void registerNodeType(NodeBuilder builder, String nodeTypeDefn) {
    // Taken from org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent
    NodeState base = ModifiedNodeState.squeeze(builder.getNodeState());
    NodeStore store = new MemoryNodeStore(base);
    Root root = RootFactory.createSystemRoot(store, new EditorHook(new CompositeEditorProvider(new NamespaceEditorProvider(), new TypeEditorProvider())), null, null, null);
    NodeTypeRegistry.register(root, IOUtils.toInputStream(nodeTypeDefn), "test node types");
    NodeState target = store.getRoot();
    target.compareAgainstBaseState(base, new ApplyDiff(builder));
}
Also used : NamespaceEditorProvider(org.apache.jackrabbit.oak.plugins.name.NamespaceEditorProvider) ApplyDiff(org.apache.jackrabbit.oak.spi.state.ApplyDiff) CompositeEditorProvider(org.apache.jackrabbit.oak.spi.commit.CompositeEditorProvider) ModifiedNodeState(org.apache.jackrabbit.oak.plugins.memory.ModifiedNodeState) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) Root(org.apache.jackrabbit.oak.api.Root) TypeEditorProvider(org.apache.jackrabbit.oak.plugins.nodetype.TypeEditorProvider) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook)

Example 34 with EditorHook

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

the class LuceneIndexAggregation2Test method createRepository.

@Override
protected ContentRepository createRepository() {
    LuceneIndexProvider provider = new LuceneIndexProvider();
    return new Oak().with(new InitialContent() {

        @Override
        public void initialize(@Nonnull NodeBuilder builder) {
            super.initialize(builder);
            // registering additional node types for wider testing
            InputStream stream = null;
            try {
                stream = LuceneIndexAggregation2Test.class.getResourceAsStream("test_nodetypes.cnd");
                NodeState base = builder.getNodeState();
                NodeStore store = new MemoryNodeStore(base);
                Root root = RootFactory.createSystemRoot(store, new EditorHook(new CompositeEditorProvider(new NamespaceEditorProvider(), new TypeEditorProvider())), null, null, null);
                NodeTypeRegistry.register(root, stream, "testing node types");
                NodeState target = store.getRoot();
                target.compareAgainstBaseState(base, new ApplyDiff(builder));
            } catch (Exception e) {
                LOG.error("Error while registering required node types. Failing here", e);
                fail("Error while registering required node types");
            } finally {
                printNodeTypes(builder);
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (IOException e) {
                        LOG.debug("Ignoring exception on stream closing.", e);
                    }
                }
            }
        }
    }).with(new OpenSecurityProvider()).with(((QueryIndexProvider) provider.with(getNodeAggregator()))).with((Observer) provider).with(new LuceneIndexEditorProvider()).createContentRepository();
}
Also used : NamespaceEditorProvider(org.apache.jackrabbit.oak.plugins.name.NamespaceEditorProvider) ApplyDiff(org.apache.jackrabbit.oak.spi.state.ApplyDiff) CompositeEditorProvider(org.apache.jackrabbit.oak.spi.commit.CompositeEditorProvider) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Root(org.apache.jackrabbit.oak.api.Root) Nonnull(javax.annotation.Nonnull) InputStream(java.io.InputStream) OpenSecurityProvider(org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider) IOException(java.io.IOException) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) IOException(java.io.IOException) InitialContent(org.apache.jackrabbit.oak.InitialContent) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) TypeEditorProvider(org.apache.jackrabbit.oak.plugins.nodetype.TypeEditorProvider) EditorHook(org.apache.jackrabbit.oak.spi.commit.EditorHook) Oak(org.apache.jackrabbit.oak.Oak)

Example 35 with EditorHook

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

the class LuceneIndexEditor2Test method relativeProperties.

@Test
public void relativeProperties() throws Exception {
    IndexDefinitionBuilder defnb = new IndexDefinitionBuilder();
    defnb.indexRule("nt:base").property("jcr:content/metadata/foo").propertyIndex();
    defnb.aggregateRule("nt:base").include("*");
    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").child("jcr:content").child("metadata").setProperty("foo", "bar");
    builder.child("a").setProperty("foo2", "bar");
    before = hook.processCommit(root, builder.getNodeState(), CommitInfo.EMPTY);
    propCallback.state.assertState("/a", "jcr:content/metadata/foo", UpdateState.ADDED);
    assertEquals(1, propCallback.invocationCount);
    propCallback.reset();
    // Property updated
    builder = before.builder();
    builder.child("a").child("jcr:content").child("metadata").setProperty("foo", "bar2");
    builder.child("a").setProperty("foo2", "bar2");
    before = hook.processCommit(before, builder.getNodeState(), CommitInfo.EMPTY);
    propCallback.state.assertState("/a", "jcr:content/metadata/foo", UpdateState.UPDATED);
    assertEquals(1, propCallback.invocationCount);
    propCallback.reset();
    // Property deleted
    builder = before.builder();
    builder.child("a").child("jcr:content").child("metadata").removeProperty("foo");
    builder.child("a").removeProperty("foo2");
    before = hook.processCommit(before, builder.getNodeState(), CommitInfo.EMPTY);
    propCallback.state.assertState("/a", "jcr:content/metadata/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)

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