-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OAK-10149: Rebase may be expensive with many siblings
- Loading branch information
Showing
5 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...t/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/ReverseNodeStateDiff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.jackrabbit.oak.plugins.document.util; | ||
|
||
import org.apache.jackrabbit.oak.api.PropertyState; | ||
import org.apache.jackrabbit.oak.spi.state.NodeState; | ||
import org.apache.jackrabbit.oak.spi.state.NodeStateDiff; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Implementation of a {@code NodeStateDiff} that reports the inverse operation | ||
* to the wrapped {@code NodeStateDiff}. | ||
*/ | ||
public class ReverseNodeStateDiff implements NodeStateDiff { | ||
|
||
private final NodeStateDiff diff; | ||
|
||
public ReverseNodeStateDiff(NodeStateDiff diff) { | ||
this.diff = requireNonNull(diff); | ||
} | ||
|
||
@Override | ||
public boolean propertyAdded(PropertyState after) { | ||
return diff.propertyDeleted(after); | ||
} | ||
|
||
@Override | ||
public boolean propertyChanged(PropertyState before, | ||
PropertyState after) { | ||
return diff.propertyChanged(after, before); | ||
} | ||
|
||
@Override | ||
public boolean propertyDeleted(PropertyState before) { | ||
return diff.propertyAdded(before); | ||
} | ||
|
||
@Override | ||
public boolean childNodeAdded(String name, | ||
NodeState after) { | ||
return diff.childNodeDeleted(name, after); | ||
} | ||
|
||
@Override | ||
public boolean childNodeChanged(String name, | ||
NodeState before, | ||
NodeState after) { | ||
return diff.childNodeChanged(name, after, before); | ||
} | ||
|
||
@Override | ||
public boolean childNodeDeleted(String name, | ||
NodeState before) { | ||
return diff.childNodeAdded(name, before); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/RebaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.jackrabbit.oak.plugins.document; | ||
|
||
import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore; | ||
import org.apache.jackrabbit.oak.spi.state.NodeBuilder; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import static org.apache.jackrabbit.oak.plugins.document.TestUtils.merge; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.lessThan; | ||
|
||
public class RebaseTest { | ||
|
||
@Rule | ||
public DocumentMKBuilderProvider builderProvider = new DocumentMKBuilderProvider(); | ||
|
||
private CountingDocumentStore store; | ||
|
||
private DocumentNodeStore ns; | ||
|
||
@Before | ||
public void setup() { | ||
store = new CountingDocumentStore(new MemoryDocumentStore()); | ||
ns = builderProvider.newBuilder() | ||
.setDocumentStore(store) | ||
.setLeaseCheckMode(LeaseCheckMode.DISABLED) | ||
.setAsyncDelay(0) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void rebase() throws Exception { | ||
NodeBuilder builder = ns.getRoot().builder(); | ||
addNodes(builder, 10, 3); | ||
merge(ns, builder); | ||
|
||
DocumentNodeState root = ns.getRoot(); | ||
DocumentNodeStoreBranch b = ns.createBranch(root); | ||
builder = root.builder(); | ||
builder.child("node-4").child("node-2").child("x").child("y"); | ||
b.setRoot(builder.getNodeState()); | ||
|
||
builder.child("node-4").child("node-2").child("x").child("y").setProperty("foo", "bar"); | ||
|
||
ns.invalidateNodeChildrenCache(); | ||
ns.getNodeCache().invalidateAll(); | ||
|
||
store.resetCounters(); | ||
ns.rebase(builder); | ||
assertThat(store.getNumFindCalls(Collection.NODES), lessThan(3)); | ||
} | ||
|
||
private void addNodes(NodeBuilder builder, int num, int level) { | ||
if (level > 0) { | ||
for (int i = 0; i < num; i++) { | ||
addNodes(builder.child("node-" + i), num, level - 1); | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...c/test/java/org/apache/jackrabbit/oak/plugins/document/util/ReverseNodeStateDiffTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.jackrabbit.oak.plugins.document.util; | ||
|
||
import org.apache.jackrabbit.oak.json.JsopDiff; | ||
import org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState; | ||
import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder; | ||
import org.apache.jackrabbit.oak.spi.state.NodeBuilder; | ||
import org.apache.jackrabbit.oak.spi.state.NodeState; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ReverseNodeStateDiffTest { | ||
|
||
private NodeState base = EmptyNodeState.EMPTY_NODE; | ||
|
||
@Before | ||
public void initializeBase() { | ||
NodeBuilder builder = new MemoryNodeBuilder(base); | ||
builder.child("a"); | ||
builder.setProperty("p", "v"); | ||
base = builder.getNodeState(); | ||
} | ||
|
||
@Test | ||
public void compare() { | ||
// diff must report the reverse | ||
compareAgainstBase(b -> b.setProperty("q", "v"), "^\"/q\":null"); | ||
compareAgainstBase(b -> b.removeProperty("p"), "^\"/p\":\"v\""); | ||
compareAgainstBase(b -> b.setProperty("p", "w"), "^\"/p\":\"v\""); | ||
compareAgainstBase(b -> b.child("a").setProperty("p", "v"), "^\"/a/p\":null"); | ||
compareAgainstBase(b -> b.child("a").remove(), "+\"/a\":{}"); | ||
compareAgainstBase(b -> b.child("a").child("b"), "-\"/a/b\""); | ||
} | ||
|
||
private void compareAgainstBase(Modification modification, | ||
String jsopDiff) { | ||
NodeBuilder builder = new MemoryNodeBuilder(base); | ||
modification.apply(builder); | ||
NodeState modified = builder.getNodeState(); | ||
|
||
JsopDiff diff = new JsopDiff(); | ||
modified.compareAgainstBaseState(base, new ReverseNodeStateDiff(diff)); | ||
assertEquals(jsopDiff, diff.toString()); | ||
} | ||
|
||
private interface Modification { | ||
|
||
void apply(NodeBuilder builder); | ||
} | ||
} |