Search in sources :

Example 1 with DefaultNodeStateDiff

use of org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff in project jackrabbit-oak by apache.

the class CompositeCompareTest method reportedNodesAreWrapped.

@Test
public void reportedNodesAreWrapped() {
    Mounts.Builder mipBuilder = Mounts.newBuilder();
    mipBuilder.readOnlyMount("libs", "/libs");
    MountInfoProvider mip = mipBuilder.build();
    NodeStore globalStore = new MemoryNodeStore();
    CompositeNodeStore.Builder nsBuilder = new CompositeNodeStore.Builder(mip, globalStore);
    nsBuilder.addMount("libs", new MemoryNodeStore());
    CompositeNodeStore compositeNodeStore = nsBuilder.build();
    NodeBuilder builder = compositeNodeStore.getRoot().builder();
    builder.child("changed");
    builder.child("deleted");
    NodeState base = builder.getNodeState();
    builder.getChildNode("changed").setProperty("newProp", "xyz", Type.STRING);
    builder.getChildNode("deleted").remove();
    builder.child("added");
    final NodeState modified = builder.getNodeState();
    final Set<String> modifiedNodes = newHashSet();
    modified.compareAgainstBaseState(base, new DefaultNodeStateDiff() {

        @Override
        public boolean childNodeAdded(String name, NodeState after) {
            assertTrue(after instanceof CompositeNodeState);
            assertEquals(name, "added");
            modifiedNodes.add(name);
            return true;
        }

        @Override
        public boolean childNodeChanged(String name, NodeState before, NodeState after) {
            assertTrue(before instanceof CompositeNodeState);
            assertTrue(after instanceof CompositeNodeState);
            assertEquals(name, "changed");
            modifiedNodes.add(name);
            return true;
        }

        @Override
        public boolean childNodeDeleted(String name, NodeState before) {
            assertTrue(before instanceof CompositeNodeState);
            assertEquals(name, "deleted");
            modifiedNodes.add(name);
            return true;
        }
    });
    assertEquals(ImmutableSet.of("added", "changed", "deleted"), modifiedNodes);
}
Also used : Mounts(org.apache.jackrabbit.oak.spi.mount.Mounts) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) DefaultNodeStateDiff(org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff) MountInfoProvider(org.apache.jackrabbit.oak.spi.mount.MountInfoProvider) Test(org.junit.Test)

Example 2 with DefaultNodeStateDiff

use of org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff in project jackrabbit-oak by apache.

the class CompositeCompareTest method onlyPropertiesOnMainNodesAreCompared.

@Test
public void onlyPropertiesOnMainNodesAreCompared() throws CommitFailedException {
    MountInfoProvider mip = Mounts.newBuilder().mount("libs", "/libs").build();
    NodeStore globalStore = new MemoryNodeStore();
    NodeStore libsStore = new MemoryNodeStore();
    List<MountedNodeStore> mounts = Lists.newArrayList();
    mounts.add(new MountedNodeStore(mip.getMountByName("libs"), libsStore));
    CompositeNodeStore compositeNodeStore = new CompositeNodeStore(mip, globalStore, mounts);
    NodeState empty = compositeNodeStore.getRoot();
    NodeBuilder builder = globalStore.getRoot().builder();
    builder.setProperty("global-prop-1", "val");
    builder.setProperty("global-prop-2", "val");
    globalStore.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    NodeBuilder libsBuilder = libsStore.getRoot().builder();
    libsBuilder.setProperty("libs-prop-1", "val");
    libsBuilder.setProperty("libs-prop-2", "val");
    libsStore.merge(libsBuilder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    NodeState modified = compositeNodeStore.getRoot();
    final Set<String> addedProperties = newHashSet();
    modified.compareAgainstBaseState(empty, new DefaultNodeStateDiff() {

        @Override
        public boolean propertyAdded(PropertyState after) {
            addedProperties.add(after.getName());
            return true;
        }
    });
    assertEquals(ImmutableSet.of("global-prop-1", "global-prop-2"), addedProperties);
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) MemoryNodeStore(org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore) DefaultNodeStateDiff(org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff) MountInfoProvider(org.apache.jackrabbit.oak.spi.mount.MountInfoProvider) Test(org.junit.Test)

Example 3 with DefaultNodeStateDiff

use of org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff in project jackrabbit-oak by apache.

the class MapRecord method compare.

boolean compare(MapRecord before, final NodeStateDiff diff) {
    if (Record.fastEquals(this, before)) {
        return true;
    }
    Segment segment = getSegment();
    int head = segment.readInt(getRecordNumber());
    if (isDiff(head)) {
        int hash = segment.readInt(getRecordNumber(), 4);
        RecordId keyId = segment.readRecordId(getRecordNumber(), 8);
        final String key = reader.readString(keyId);
        final RecordId value = segment.readRecordId(getRecordNumber(), 8, 1);
        MapRecord base = reader.readMap(segment.readRecordId(getRecordNumber(), 8, 2));
        boolean rv = base.compare(before, new DefaultNodeStateDiff() {

            @Override
            public boolean childNodeAdded(String name, NodeState after) {
                return name.equals(key) || diff.childNodeAdded(name, after);
            }

            @Override
            public boolean childNodeChanged(String name, NodeState before, NodeState after) {
                return name.equals(key) || diff.childNodeChanged(name, before, after);
            }

            @Override
            public boolean childNodeDeleted(String name, NodeState before) {
                return diff.childNodeDeleted(name, before);
            }
        });
        if (rv) {
            MapEntry beforeEntry = before.getEntry(key);
            if (beforeEntry == null) {
                rv = diff.childNodeAdded(key, reader.readNode(value));
            } else if (!value.equals(beforeEntry.getValue())) {
                rv = diff.childNodeChanged(key, beforeEntry.getNodeState(), reader.readNode(value));
            }
        }
        return rv;
    }
    Segment beforeSegment = before.getSegment();
    int beforeHead = beforeSegment.readInt(before.getRecordNumber());
    if (isDiff(beforeHead)) {
        int hash = beforeSegment.readInt(before.getRecordNumber(), 4);
        RecordId keyId = beforeSegment.readRecordId(before.getRecordNumber(), 8);
        final String key = reader.readString(keyId);
        final RecordId value = beforeSegment.readRecordId(before.getRecordNumber(), 8, 1);
        MapRecord base = reader.readMap(beforeSegment.readRecordId(before.getRecordNumber(), 8, 2));
        boolean rv = this.compare(base, new DefaultNodeStateDiff() {

            @Override
            public boolean childNodeAdded(String name, NodeState after) {
                return diff.childNodeAdded(name, after);
            }

            @Override
            public boolean childNodeChanged(String name, NodeState before, NodeState after) {
                return name.equals(key) || diff.childNodeChanged(name, before, after);
            }

            @Override
            public boolean childNodeDeleted(String name, NodeState before) {
                return name.equals(key) || diff.childNodeDeleted(name, before);
            }
        });
        if (rv) {
            MapEntry afterEntry = this.getEntry(key);
            if (afterEntry == null) {
                rv = diff.childNodeDeleted(key, reader.readNode(value));
            } else if (!value.equals(afterEntry.getValue())) {
                rv = diff.childNodeChanged(key, reader.readNode(value), afterEntry.getNodeState());
            }
        }
        return rv;
    }
    if (isBranch(beforeHead) && isBranch(head)) {
        return compareBranch(before, this, diff);
    }
    Iterator<MapEntry> beforeEntries = before.getEntries().iterator();
    Iterator<MapEntry> afterEntries = this.getEntries().iterator();
    MapEntry beforeEntry = nextOrNull(beforeEntries);
    MapEntry afterEntry = nextOrNull(afterEntries);
    while (beforeEntry != null || afterEntry != null) {
        int d = compare(beforeEntry, afterEntry);
        if (d < 0) {
            assert beforeEntry != null;
            if (!diff.childNodeDeleted(beforeEntry.getName(), beforeEntry.getNodeState())) {
                return false;
            }
            beforeEntry = nextOrNull(beforeEntries);
        } else if (d == 0) {
            assert beforeEntry != null;
            assert afterEntry != null;
            if (!beforeEntry.getValue().equals(afterEntry.getValue()) && !diff.childNodeChanged(beforeEntry.getName(), beforeEntry.getNodeState(), afterEntry.getNodeState())) {
                return false;
            }
            beforeEntry = nextOrNull(beforeEntries);
            afterEntry = nextOrNull(afterEntries);
        } else {
            assert afterEntry != null;
            if (!diff.childNodeAdded(afterEntry.getName(), afterEntry.getNodeState())) {
                return false;
            }
            afterEntry = nextOrNull(afterEntries);
        }
    }
    return true;
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) MapEntry.newMapEntry(org.apache.jackrabbit.oak.segment.MapEntry.newMapEntry) DefaultNodeStateDiff(org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff)

Example 4 with DefaultNodeStateDiff

use of org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff in project jackrabbit-oak by apache.

the class DocumentBundlingTest method journalDiffAndBundling.

@Test
public void journalDiffAndBundling() 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());
    NodeState r1 = merge(builder);
    builder = store.getRoot().builder();
    childBuilder(builder, "/test/book.jpg/jcr:content").setProperty("fooContent", "bar");
    childBuilder(builder, "/test/book.jpg").setProperty("fooBook", "bar");
    NodeState r2 = merge(builder);
    final List<String> addedPropertyNames = Lists.newArrayList();
    r2.compareAgainstBaseState(r1, new DefaultNodeStateDiff() {

        @Override
        public boolean propertyAdded(PropertyState after) {
            addedPropertyNames.add(after.getName());
            return super.propertyAdded(after);
        }

        @Override
        public boolean childNodeChanged(String name, NodeState before, NodeState after) {
            return after.compareAgainstBaseState(before, this);
        }
    });
    assertTrue("No change reported for /test/book.jpg", addedPropertyNames.contains("fooBook"));
    assertTrue("No change reported for /test/book.jpg/jcr:content", addedPropertyNames.contains("fooContent"));
}
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) DefaultNodeStateDiff(org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) Test(org.junit.Test)

Example 5 with DefaultNodeStateDiff

use of org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff in project jackrabbit-oak by apache.

the class DocumentNodeStoreTest method diffCache.

@Test
public void diffCache() throws Exception {
    final AtomicInteger numQueries = new AtomicInteger();
    MemoryDocumentStore store = new MemoryDocumentStore() {

        @Nonnull
        @Override
        public <T extends Document> List<T> query(Collection<T> collection, String fromKey, String toKey, int limit) {
            numQueries.incrementAndGet();
            return super.query(collection, fromKey, toKey, limit);
        }
    };
    DocumentNodeStore ns = builderProvider.newBuilder().setDocumentStore(store).setAsyncDelay(0).getNodeStore();
    NodeBuilder builder = ns.getRoot().builder();
    builder.child("foo").child("child");
    merge(ns, builder);
    builder = ns.getRoot().builder();
    builder.child("bar");
    merge(ns, builder);
    DocumentNodeState before = ns.getRoot();
    builder = ns.getRoot().builder();
    builder.child("foo").child("child").child("node");
    merge(ns, builder);
    DocumentNodeState after = ns.getRoot();
    numQueries.set(0);
    final List<String> added = Lists.newArrayList();
    ns.compare(asDocumentNodeState(after.getChildNode("foo").getChildNode("child")), asDocumentNodeState(before.getChildNode("foo").getChildNode("child")), new DefaultNodeStateDiff() {

        @Override
        public boolean childNodeAdded(String name, NodeState after) {
            added.add(name);
            return super.childNodeAdded(name, after);
        }
    });
    assertEquals(1, added.size());
    assertEquals("node", added.get(0));
    assertEquals("must not run queries", 0, numQueries.get());
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) TRANSIENT(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.Type.TRANSIENT) CONSTRAINT(org.apache.jackrabbit.oak.api.CommitFailedException.CONSTRAINT) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultNodeStateDiff(org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff) Test(org.junit.Test)

Aggregations

DefaultNodeStateDiff (org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff)12 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)12 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)10 Test (org.junit.Test)10 MemoryDocumentStore (org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore)5 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)3 MemoryNodeStore (org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore)3 MountInfoProvider (org.apache.jackrabbit.oak.spi.mount.MountInfoProvider)3 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)3 Clock (org.apache.jackrabbit.oak.stats.Clock)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CONSTRAINT (org.apache.jackrabbit.oak.api.CommitFailedException.CONSTRAINT)2 TRANSIENT (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.Type.TRANSIENT)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 DocumentNodeState (org.apache.jackrabbit.oak.plugins.document.DocumentNodeState)1 MapEntry.newMapEntry (org.apache.jackrabbit.oak.segment.MapEntry.newMapEntry)1 Mounts (org.apache.jackrabbit.oak.spi.mount.Mounts)1 AbstractNodeState (org.apache.jackrabbit.oak.spi.state.AbstractNodeState)1 ChildNodeEntry (org.apache.jackrabbit.oak.spi.state.ChildNodeEntry)1