jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1299781 - in /jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit: state/TransientNodeState.java utils/Iterators.java
Date Mon, 12 Mar 2012 18:50:28 GMT
Author: mduerig
Date: Mon Mar 12 18:50:27 2012
New Revision: 1299781

URL: http://svn.apache.org/viewvc?rev=1299781&view=rev
Log:
Microkernel based prototype of JCR implementation (WIP)
- avoid concurrent modification exception on transient modifications from within item iterators

Modified:
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/TransientNodeState.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/utils/Iterators.java

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/TransientNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/TransientNodeState.java?rev=1299781&r1=1299780&r2=1299781&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/TransientNodeState.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/TransientNodeState.java
Mon Mar 12 18:50:27 2012
@@ -27,8 +27,8 @@ import org.apache.jackrabbit.oak.model.C
 import org.apache.jackrabbit.oak.model.NodeState;
 import org.apache.jackrabbit.oak.model.PropertyState;
 import org.apache.jackrabbit.state.ChangeTree.NodeDelta;
-import org.apache.jackrabbit.utils.PagedIterator;
 import org.apache.jackrabbit.utils.Function1;
+import org.apache.jackrabbit.utils.PagedIterator;
 import org.apache.jackrabbit.utils.Predicate;
 
 import javax.jcr.ItemExistsException;
@@ -40,6 +40,7 @@ import static org.apache.jackrabbit.util
 import static org.apache.jackrabbit.utils.Iterators.filter;
 import static org.apache.jackrabbit.utils.Iterators.flatten;
 import static org.apache.jackrabbit.utils.Iterators.map;
+import static org.apache.jackrabbit.utils.Iterators.toIterable;
 
 /**
  * A {@code TransientNodeState} instance uses a {@code TransientSpace} to record changes
@@ -161,30 +162,29 @@ public class TransientNodeState {
 
         final NodeDelta delta = getNodeDelta();
 
-        // fixme: this is not safe against concurrent modifications. Either copy delta or
make NodeDelta immutable
         Iterator<ChildNodeEntry> unmodifiedEntries = filter(persistedEntries,
-                new Predicate<ChildNodeEntry>() {
-                    @Override
-                    public boolean evaluate(ChildNodeEntry entry) {
-                        return !delta.isNodeModified(entry.getName());
-                    }
-                });
+            new Predicate<ChildNodeEntry>() {
+                @Override
+                public boolean evaluate(ChildNodeEntry entry) {
+                    return !delta.isNodeModified(entry.getName());
+                }
+            });
         
         Iterator<TransientNodeState> unmodifiedStates = map(unmodifiedEntries,
-                new Function1<ChildNodeEntry, TransientNodeState>() {
-                    @Override
-                    public TransientNodeState apply(ChildNodeEntry entry) {
-                        return getNodeState(delta.getNode(entry.getName()));
-                    }
-                });
+            new Function1<ChildNodeEntry, TransientNodeState>() {
+                @Override
+                public TransientNodeState apply(ChildNodeEntry entry) {
+                    return getNodeState(delta.getNode(entry.getName()));
+                }
+            });
 
-        Iterator<TransientNodeState> modifiedStates = map(delta.getNodes(),
-                new Function1<NodeDelta, TransientNodeState>() {
-                    @Override
-                    public TransientNodeState apply(NodeDelta delta) {
-                        return getNodeState(delta);
-                    }
-                });
+        Iterator<TransientNodeState> modifiedStates = map(toIterable(delta.getNodes()).iterator(),
+            new Function1<NodeDelta, TransientNodeState>() {
+                @Override
+                public TransientNodeState apply(NodeDelta delta) {
+                    return getNodeState(delta);
+                }
+            });
 
         return chain(unmodifiedStates, modifiedStates);
     }
@@ -203,17 +203,17 @@ public class TransientNodeState {
         Iterable<? extends PropertyState> propertyStates = getPersistentNodeState().getProperties();
         final NodeDelta delta = getNodeDelta();
 
-        // fixme: this is not safe against concurrent modifications. Either copy delta or
make NodeDelta immutable
         Iterator<PropertyState> propertyEntries =
             filter(propertyStates.iterator(),
-                    new Predicate<PropertyState>() {
-                        @Override
-                        public boolean evaluate(PropertyState state) {
-                            return !state.getName().startsWith(":") && !delta.hasProperty(state.getName());
-                        }
-                    });
+                new Predicate<PropertyState>() {
+                    @Override
+                    public boolean evaluate(PropertyState state) {
+                        return !state.getName().startsWith(":") && !delta.hasProperty(state.getName());
+                    }
+                });
 
-        return chain(propertyEntries, delta.getPropertyStates());
+        Iterator<PropertyState> modifiedProperties = delta.getPropertyStates();
+        return chain(propertyEntries, toIterable(modifiedProperties).iterator());
     }
 
     /**

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/utils/Iterators.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/utils/Iterators.java?rev=1299781&r1=1299780&r2=1299781&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/utils/Iterators.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/utils/Iterators.java
Mon Mar 12 18:50:27 2012
@@ -23,8 +23,10 @@ import org.apache.commons.collections.it
 import org.apache.commons.collections.iterators.SingletonIterator;
 import org.apache.commons.collections.iterators.TransformIterator;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 import java.util.NoSuchElementException;
 
 /**
@@ -156,6 +158,34 @@ public final class Iterators {
     }
 
     /**
+     * Spool an iterator into an iterable
+     * @param iterator
+     * @param <T>
+     * @return iterable containing the values from {@code iterator}
+     */
+    public static <T> Iterable<T> toIterable(final Iterator<T> iterator)
{
+        return new Iterable<T>() {
+            private List<T> copy;
+
+            @Override
+            public Iterator<T> iterator() {
+                if (copy == null) {
+                    copy = spool(iterator);
+                }
+                return copy.iterator();
+            }
+
+            private List<T> spool(Iterator<T> iterator) {
+                List<T> list = new ArrayList<T>();
+                while (iterator.hasNext()) {
+                    list.add(iterator.next());
+                }
+                return list;
+            }
+        };
+    }
+
+    /**
      * Flattens an iterator of iterators into a single iterator.
      * @param iterators
      * @param <T>



Mime
View raw message