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);
}
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);
}
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;
}
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"));
}
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());
}
Aggregations