Author: jukka
Date: Tue Jul 1 17:54:54 2014
New Revision: 1607152
URL: http://svn.apache.org/r1607152
Log:
OAK-1934: Optimize MutableTree.orderBefore for the common case
Only scan the actual child node list when the target node is not found in
the child order list or when appending to the end of the list. Ohterwise
use the already existing child order property.
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/MutableTree.java
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/MutableTree.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/MutableTree.java?rev=1607152&r1=1607151&r2=1607152&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/MutableTree.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/MutableTree.java
Tue Jul 1 17:54:54 2014
@@ -21,7 +21,6 @@ package org.apache.jackrabbit.oak.core;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
-import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newArrayListWithCapacity;
import static org.apache.jackrabbit.oak.api.Type.NAMES;
import static org.apache.jackrabbit.oak.commons.PathUtils.elements;
@@ -227,31 +226,56 @@ class MutableTree extends AbstractTree {
public boolean orderBefore(final String name) {
beforeWrite();
if (parent == null) {
- // root does not have siblings
- return false;
- }
- if (name != null) {
- if (name.equals(this.name) || !parent.hasChild(name)) {
- // same node or no such sibling (not existing or not
accessible)
- return false;
- }
+ return false; // root does not have siblings
+ } else if (this.name.equals(name)) {
+ return false; // same node
}
+
// perform the reorder
- List<String> names = newArrayList();
- for (String n : parent.getChildNames()) {
- if (n.equals(name)) {
+ List<String> names = newArrayListWithCapacity(10000);
+ NodeBuilder builder = parent.nodeBuilder;
+ boolean found = false;
+
+ // first try reordering based on the (potentially out-of-sync)
+ // child order property in the parent node
+ for (String n : builder.getNames(OAK_CHILD_ORDER)) {
+ if (n.equals(name) && parent.hasChild(name)) {
names.add(this.name);
+ found = true;
}
if (!n.equals(this.name)) {
names.add(n);
}
}
+
+ // if the target node name was not found in the parent's child order
+ // property, we need to fall back to recreating the child order list
+ if (!found) {
+ names.clear();
+ for (String n : parent.getChildNames()) {
+ if (n.equals(name)) {
+ names.add(this.name);
+ found = true;
+ }
+ if (!n.equals(this.name)) {
+ names.add(n);
+ }
+ }
+ }
+
if (name == null) {
names.add(this.name);
+ found = true;
+ }
+
+ if (found) {
+ builder.setProperty(OAK_CHILD_ORDER, names, NAMES);
+ root.updated();
+ return true;
+ } else {
+ // no such sibling (not existing or not accessible)
+ return false;
}
- parent.nodeBuilder.setProperty(OAK_CHILD_ORDER, names, NAMES);
- root.updated();
- return true;
}
@Override