jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1309338 - in /jackrabbit/oak/trunk: oak-core/ oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/ oak-mk/src/main/java/org/apache/jackrabbit/mk/model/
Date Wed, 04 Apr 2012 11:39:59 GMT
Author: mduerig
Date: Wed Apr  4 11:39:58 2012
New Revision: 1309338

URL: http://svn.apache.org/viewvc?rev=1309338&view=rev
Log:
OAK-9: Internal tree builder
fixed implementation of getChildNodes and getProperties

Modified:
    jackrabbit/oak/trunk/oak-core/pom.xml
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateEditor.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientNodeState.java
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeStateEditor.java

Modified: jackrabbit/oak/trunk/oak-core/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/pom.xml?rev=1309338&r1=1309337&r2=1309338&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-core/pom.xml Wed Apr  4 11:39:58 2012
@@ -37,6 +37,12 @@
       <version>${project.version}</version>
     </dependency>
 
+    <dependency>
+        <groupId>commons-collections</groupId>
+        <artifactId>commons-collections</artifactId>
+        <version>3.2.1</version>
+    </dependency>
+
     <!-- Optional dependencies for different persistence backends -->
     <dependency>
       <groupId>org.mongodb</groupId>

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateEditor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateEditor.java?rev=1309338&r1=1309337&r2=1309338&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateEditor.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStateEditor.java
Wed Apr  4 11:39:58 2012
@@ -130,12 +130,6 @@ public class KernelNodeStateEditor imple
     }
 
     @Override
-    public NodeState getNodeState() {
-        // todo implement getNodeState
-        return null;
-    }
-
-    @Override
     public NodeState getBaseNodeState() {
         return base;
     }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientNodeState.java?rev=1309338&r1=1309337&r2=1309338&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TransientNodeState.java
Wed Apr  4 11:39:58 2012
@@ -1,5 +1,9 @@
 package org.apache.jackrabbit.oak.kernel;
 
+import org.apache.commons.collections.iterators.EmptyIterator;
+import org.apache.commons.collections.iterators.FilterIterator;
+import org.apache.commons.collections.iterators.IteratorChain;
+import org.apache.commons.collections.iterators.TransformIterator;
 import org.apache.jackrabbit.mk.model.ChildNodeEntry;
 import org.apache.jackrabbit.mk.model.NodeState;
 import org.apache.jackrabbit.mk.model.PropertyState;
@@ -9,9 +13,10 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.NoSuchElementException;
 import java.util.Set;
 
+import static org.apache.jackrabbit.oak.kernel.TransientNodeState.Iterators.*;
+
 public class TransientNodeState {
     private final KernelNodeStateEditor editor;
     private final NodeState persistentState;
@@ -115,111 +120,74 @@ public class TransientNodeState {
     }
 
     public Iterable<PropertyState> getProperties() {
+        final Iterable<? extends PropertyState> persisted = persistentState == null
+                ? null
+                : persistentState.getProperties();
+
         final Set<String> removed = new HashSet<String>();
         removed.addAll(removedProperties);
 
-        final Map<String, PropertyState> added = new HashMap<String, PropertyState>();
-        added.putAll(addedProperties);
-
-        final Iterable<? extends PropertyState>
-                persistedProperties = persistentState.getProperties(); // fixme check for
null
+        final Set<PropertyState> added = new HashSet<PropertyState>();
+        added.addAll(addedProperties.values());
 
+        // persisted - removed + added
         return new Iterable<PropertyState>() {
             @Override
             public Iterator<PropertyState> iterator() {
-                return new Iterator<PropertyState>() {
-                    private final Iterator<? extends PropertyState>
-                            properties = persistedProperties.iterator();
-                    private PropertyState next;
-
-                    @Override
-                    public boolean hasNext() {
-                        if (next == null) {
-                            while (properties.hasNext()) {
-                                PropertyState prop = properties.next();
-                                if (added.containsKey(prop.getName())) {
-                                    next = added.get(prop.getName());
-                                    break;
-                                }
-                                if (!removed.contains(prop.getName())) {
-                                    next = prop;
-                                    break;
-                                }
-                            }
+                Iterator<? extends PropertyState> properties = persisted == null
+                    ? Iterators.<PropertyState>empty()
+                    : persisted.iterator();
+
+                Iterator<PropertyState> persistedMinusRemoved =
+                    filter(properties, new Predicate<PropertyState>() {
+                        @Override
+                        public boolean evaluate(PropertyState state) {
+                            return !removed.contains(state.getName());
                         }
-                        return next != null;
-                    }
+                });
 
-                    @Override
-                    public PropertyState next() {
-                        if (!hasNext()) {
-                            throw new NoSuchElementException();
-                        }
-                        PropertyState e = next;
-                        next = null;
-                        return e;
-                    }
-
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException("remove");
-                    }
-                };
+                return add(persistedMinusRemoved, added.iterator());
             }
         };
     }
 
     public Iterable<TransientNodeState> getChildNodes(long offset, int count) {
+        final Iterable<? extends ChildNodeEntry> persisted = persistentState == null
+            ? null
+            : persistentState.getChildNodeEntries(offset, count);
+
         final Set<String> removed = new HashSet<String>();
         removed.addAll(removedNodes);
 
-        final Map<String, TransientNodeState> added = new HashMap<String, TransientNodeState>();
-        added.putAll(addedNodes);
-
-        final Iterable<? extends ChildNodeEntry>
-                persistedNodes = persistentState.getChildNodeEntries(offset, count); // fixme
check for null
+        final Set<TransientNodeState> added = new HashSet<TransientNodeState>();
+        added.addAll(addedNodes.values());
 
+        // persisted - removed + added
         return new Iterable<TransientNodeState>() {
             @Override
             public Iterator<TransientNodeState> iterator() {
-                return new Iterator<TransientNodeState>() {
-                    private final Iterator<? extends ChildNodeEntry>
-                            nodes = persistedNodes.iterator();
-                    private TransientNodeState next;
-
-                    @Override
-                    public boolean hasNext() {
-                        if (next == null) {
-                            while (nodes.hasNext()) {
-                                final ChildNodeEntry entry = nodes.next();
-                                if (added.containsKey(entry.getName())) {
-                                    next = added.get(entry.getName());
-                                    break;
-                                }
-                                if (!removed.contains(entry.getName())) {
-                                    next = getExistingChildNode(entry.getName());
-                                    break;
-                                }
-                            }
+                Iterator<? extends ChildNodeEntry> nodes = persisted == null
+                    ? Iterators.<ChildNodeEntry>empty()
+                    : persisted.iterator();
+
+                Iterator<ChildNodeEntry> persistedMinusRemovedEntries =
+                    filter(nodes, new Predicate<ChildNodeEntry>() {
+                        @Override
+                        public boolean evaluate(ChildNodeEntry entry) {
+                            return !removed.contains(entry.getName());
                         }
-                        return next != null;
-                    }
+                });
 
-                    @Override
-                    public TransientNodeState next() {
-                        if (!hasNext()) {
-                            throw new NoSuchElementException();
-                        }
-                        TransientNodeState e = next;
-                        next = null;
-                        return e;
-                    }
-
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException("remove");
-                    }
-                };
+                Iterator<TransientNodeState> persistedMinusRemoved =
+                    map(persistedMinusRemovedEntries,
+                        new Function1<ChildNodeEntry, TransientNodeState>() {
+                            @Override
+                            public TransientNodeState apply(ChildNodeEntry entry) {
+                                return getExistingChildNode(entry.getName());
+                            }
+                });
+
+                return add(persistedMinusRemoved, added.iterator());
             }
         };
     }
@@ -300,4 +268,98 @@ public class TransientNodeState {
         return persistentState != null && persistentState.getProperty(name) != null;
     }
 
+    // TODO: move to a more suitable location
+    static final class Iterators {
+        private Iterators() { }
+
+        /**
+         * Returns an empty iterator of type {@code T}.
+         *
+         * @param <T>
+         * @return
+         */
+        @SuppressWarnings("unchecked")
+        public static <T> Iterator<T> empty() {
+            return EmptyIterator.INSTANCE;
+        }
+
+        /**
+         * Returns an iterator for the concatenation of {@code iterator1} and
+         * {@code iterator2}.
+         *
+         * @param <T>
+         * @param iterator1
+         * @param iterator2
+         * @return
+         */
+        @SuppressWarnings("unchecked")
+        public static <T> Iterator<T> add(Iterator<? extends T> iterator1,
+                Iterator<? extends T> iterator2) {
+
+            return new IteratorChain(iterator1, iterator2);
+        }
+
+        /**
+         * Returns an iterator containing only the elements from an original
+         * {@code iterator} where the given {@code predicate} matches.
+         *
+         * @param <T>
+         * @param iterator
+         * @param predicate
+         * @return
+         */
+        @SuppressWarnings("unchecked")
+        public static <T> Iterator<T> filter(Iterator<? extends T> iterator,
+                final Predicate<? super T> predicate) {
+
+            return new FilterIterator(iterator, new org.apache.commons.collections.Predicate()
{
+                @Override
+                public boolean evaluate(Object object) {
+                    return predicate.evaluate((T) object);
+                }
+            });
+        }
+
+        /**
+         * Returns an iterator with elements of an original  {@code iterator} mapped by
+         * a function {@code f}.
+         *
+         * @param <T>
+         * @param <R>
+         * @param <S>
+         * @param iterator
+         * @param f
+         * @return
+         */
+        @SuppressWarnings("unchecked")
+        public static <T, R, S extends T> Iterator<R> map(Iterator<? extends
T> iterator,
+                final Function1<S, ? super R> f) {
+
+            return new TransformIterator(iterator, new org.apache.commons.collections.Transformer()
{
+                @Override
+                public Object transform(Object input) {
+                    return f.apply((S) input);
+                }
+            });
+        }
+
+        /**
+         * Type safe counter part of {@link org.apache.commons.collections.Predicate}.
+         *
+         * @param <T> type of values this predicate is defined on
+         */
+        interface Predicate<T> {
+            boolean evaluate(T arg);
+        }
+
+        /**
+         * Type safe counter part of {@link org.apache.commons.collections.Transformer}.
+         *
+         * @param <S>  argument type to transform from
+         * @param <T>  result type to transform to
+         */
+        public interface Function1<S, T> {
+            T apply(S argument);
+        }
+    }
 }

Modified: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeStateEditor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeStateEditor.java?rev=1309338&r1=1309337&r2=1309338&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeStateEditor.java
(original)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/model/NodeStateEditor.java
Wed Apr  4 11:39:58 2012
@@ -75,14 +75,6 @@ public interface NodeStateEditor {
     NodeStateEditor edit(String name);
 
     /**
-     * Returns an immutable node state that matches the current state of
-     * the editor.
-     *
-     * @return immutable node state
-     */
-    NodeState getNodeState();
-
-    /**
      * Return the base node state of this private branch
      * @return base node state
      */



Mime
View raw message