You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2013/12/02 16:37:36 UTC

svn commit: r1547061 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/observation/filter/RelativePathSelector.java test/java/org/apache/jackrabbit/oak/plugins/observation/filter/SelectorTest.java

Author: mduerig
Date: Mon Dec  2 15:37:36 2013
New Revision: 1547061

URL: http://svn.apache.org/r1547061
Log:
OAK-1133: Observation listener PLUS Replace NodeTypeFilter with UniversalFilter
Selector for relative paths

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/RelativePathSelector.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/SelectorTest.java

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/RelativePathSelector.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/RelativePathSelector.java?rev=1547061&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/RelativePathSelector.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/RelativePathSelector.java Mon Dec  2 15:37:36 2013
@@ -0,0 +1,82 @@
+/*
+ * 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.observation.filter;
+
+import static org.apache.jackrabbit.oak.commons.PathUtils.denotesCurrent;
+import static org.apache.jackrabbit.oak.commons.PathUtils.denotesParent;
+import static org.apache.jackrabbit.oak.commons.PathUtils.elements;
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.MISSING_NODE;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.core.ImmutableTree;
+import org.apache.jackrabbit.oak.plugins.observation.filter.UniversalFilter.Selector;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+
+/**
+ * A selector for selecting a node at a relative path from the node selected by
+ * an initial selector. Paths respect relative elements (i.e. {@code .} and {@code ..})
+ * <em>Note:</em> selecting the parent of the root node will return a non existing
+ * {@code Tree} instance.
+ */
+public class RelativePathSelector implements Selector {
+    private static final ImmutableTree MISSING_TREE = new ImmutableTree(MISSING_NODE);
+
+    private final String path;
+    private final Selector selector;
+
+    /**
+     * @param path      path to select from
+     * @param selector  selector to base {@code path} upon
+     */
+    public RelativePathSelector(String path, Selector selector) {
+        this.path = path;
+        this.selector = selector;
+    }
+
+    @Nonnull
+    @Override
+    public ImmutableTree select(@Nonnull UniversalFilter filter,
+            @CheckForNull PropertyState before, @CheckForNull PropertyState after) {
+        return select(selector.select(filter, before, after));
+    }
+
+    @Nonnull
+    @Override
+    public ImmutableTree select(@Nonnull UniversalFilter filter,
+            @Nonnull String name, @Nonnull NodeState before, @Nonnull NodeState after) {
+        return select(selector.select(filter, name, before, after));
+    }
+
+    private ImmutableTree select(ImmutableTree parent) {
+        ImmutableTree tree = parent;
+        for (String name : elements(path)) {
+            if (denotesParent(name)) {
+                tree = tree.isRoot() ? MISSING_TREE : tree.getParent();
+            } else if (!denotesCurrent(name)) {
+                tree = tree.getChild(name);
+            }
+        }
+        return tree;
+    }
+
+}

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/SelectorTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/SelectorTest.java?rev=1547061&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/SelectorTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/SelectorTest.java Mon Dec  2 15:37:36 2013
@@ -0,0 +1,78 @@
+/*
+ * 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.observation.filter;
+
+import static com.google.common.base.Predicates.alwaysTrue;
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import com.google.common.base.Predicate;
+import org.apache.jackrabbit.oak.core.ImmutableTree;
+import org.apache.jackrabbit.oak.plugins.observation.filter.UniversalFilter.Selector;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Test;
+
+public class SelectorTest {
+    public static final Predicate<ImmutableTree> ALL = alwaysTrue();
+
+    private final NodeState root; {
+        NodeBuilder builder = EMPTY_NODE.builder();
+        builder.setChildNode("a").setChildNode("b").setChildNode("c");
+        root = builder.getNodeState();
+    }
+
+    private final ImmutableTree rootTree = new ImmutableTree(root);
+
+    @Test
+    public void selectDescendant() {
+        String path = "/a/b/c";
+        Selector selector = new RelativePathSelector(path, Selectors.PARENT);
+        UniversalFilter filter = new UniversalFilter(rootTree, rootTree, selector, ALL);
+        assertEquals(path, selector.select(filter, null, null).getPath());
+    }
+
+    @Test
+    public void selectThis() {
+        String path = ".";
+        Selector selector = new RelativePathSelector(path, Selectors.PARENT);
+        UniversalFilter filter = new UniversalFilter(rootTree, rootTree, selector, ALL);
+        assertEquals(rootTree.getPath(), selector.select(filter, null, null).getPath());
+    }
+
+    @Test
+    public void selectAncestor() {
+        String path = "../..";
+        Selector selector = new RelativePathSelector(path, Selectors.PARENT);
+        UniversalFilter filter = new UniversalFilter(
+                rootTree.getChild("a").getChild("b").getChild("c"), rootTree, selector, ALL);
+        assertEquals("/a", selector.select(filter, null, null).getPath());
+    }
+
+    @Test
+    public void selectAncestorOfRoot() {
+        String path = "../..";
+        Selector selector = new RelativePathSelector(path, Selectors.PARENT);
+        UniversalFilter filter = new UniversalFilter(rootTree, rootTree, selector, ALL);
+        assertFalse("/a", selector.select(filter, null, null).exists());
+    }
+
+}