jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1546982 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/observation/filter/ test/java/org/apache/jackrabbit/oak/plugins/observation/filter/
Date Mon, 02 Dec 2013 12:41:22 GMT
Author: mduerig
Date: Mon Dec  2 12:41:21 2013
New Revision: 1546982

URL: http://svn.apache.org/r1546982
Log:
OAK-1133: Observation listener PLUS
Add an universal filter implementation

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/Selectors.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/UniversalFilter.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/NodeTypePredicateTest.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/UuidPredicateTest.java

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/Selectors.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/Selectors.java?rev=1546982&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/Selectors.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/Selectors.java
Mon Dec  2 12:41:21 2013
@@ -0,0 +1,85 @@
+/*
+ * 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.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;
+
+/**
+ * Common {@code Selector} implementations
+ */
+public final class Selectors {
+
+    /**
+     * A selector returning the node for which a call back on {@link EventGenerator.Filter}
was
+     * called or a non existing node in the case of a property call back.
+     */
+    public static final Selector THIS = new ThisSelector();
+
+    /**
+     * A selector returning the parent node of the item for which a call back
+     * on {@link EventGenerator.Filter} was called.
+     */
+    public static final Selector PARENT = new ParentSelector();
+
+    private Selectors() {
+    }
+
+    private static class ThisSelector implements Selector {
+        @Override
+        public ImmutableTree select(@Nonnull UniversalFilter filter,
+                @CheckForNull PropertyState before, @CheckForNull PropertyState after) {
+            return new ImmutableTree(MISSING_NODE);
+        }
+
+        @Override
+        public ImmutableTree select(@Nonnull UniversalFilter filter, @Nonnull String name,
+                @Nonnull NodeState before, @Nonnull NodeState after) {
+            return after.exists()
+                    ? filter.getAfterTree().getChild(name)
+                    : filter.getBeforeTree().getChild(name);
+        }
+    }
+
+    private static class ParentSelector implements Selector {
+        @Override
+        public ImmutableTree select(@Nonnull UniversalFilter filter,
+                @CheckForNull PropertyState before, @CheckForNull PropertyState after) {
+            return after != null
+                    ? filter.getAfterTree()
+                    : filter.getBeforeTree();
+        }
+
+        @Override
+        public ImmutableTree select(@Nonnull UniversalFilter filter, @Nonnull String name,
+                @Nonnull NodeState before, @Nonnull NodeState after) {
+            return after.exists()
+                    ? filter.getAfterTree()
+                    : filter.getBeforeTree();
+        }
+    }
+}

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/UniversalFilter.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/UniversalFilter.java?rev=1546982&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/UniversalFilter.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/UniversalFilter.java
Mon Dec  2 12:41:21 2013
@@ -0,0 +1,153 @@
+/*
+ * 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.Preconditions.checkNotNull;
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.MISSING_NODE;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import com.google.common.base.Predicate;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.core.ImmutableTree;
+import org.apache.jackrabbit.oak.plugins.observation.filter.EventGenerator.Filter;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+
+/**
+ * An universal {@code Filter} implementation, which can be parametrised by
+ * a {@link Selector} and a {@code Predicate}. The selector maps a call back
+ * on this filter to a tree. That tree is in turn passed to the predicate for
+ * determining whether to include or to exclude the respective event.
+ */
+public class UniversalFilter implements Filter {
+    private final ImmutableTree beforeTree;
+    private final ImmutableTree afterTree;
+
+    private final Selector selector;
+    private final Predicate<ImmutableTree> predicate;
+
+    /**
+     * Create a new instance of an universal filter rooted at the passed trees.
+     *
+     * @param beforeRootTree  root of the before tree
+     * @param afterRootTree   root of the after tree
+     * @param selector        selector for selecting the tree to match the predicate against
+     * @param predicate       predicate for determining whether to include or to exclude
an event
+     */
+    public UniversalFilter(
+            @Nonnull ImmutableTree beforeRootTree,
+            @Nonnull ImmutableTree afterRootTree,
+            @Nonnull Selector selector,
+            @Nonnull Predicate<ImmutableTree> predicate) {
+        this.predicate = checkNotNull(predicate);
+        this.beforeTree = checkNotNull(beforeRootTree);
+        this.afterTree = checkNotNull(afterRootTree);
+        this.selector = checkNotNull(selector);
+    }
+
+    /**
+     * A selector instance maps call backs on {@code Filters} to tree instances,
+     * which should be used for determining inclusion or exclusion of the associated event.
+     */
+    public interface Selector {
+
+        /**
+         * Map a property event.
+         * @param filter  filter instance on which respective call back occurred.
+         * @param before  before state or {@code null} for
+         *                {@link Filter#propertyAdded(PropertyState)}
+         * @param after   after state or {@code null} for
+         *                {@link Filter#propertyDeleted(PropertyState)}
+         * @return a tree instance for basing the filtering criterion (predicate) upon
+         */
+        @Nonnull
+        ImmutableTree select(@Nonnull UniversalFilter filter,
+                @CheckForNull PropertyState before, @CheckForNull PropertyState after);
+
+        /**
+         * Map a node event.
+         * @param filter  filter instance on which respective call back occurred.
+         * @param before  before state or {@code null} for
+         *                {@link Filter#childNodeAdded(String, NodeState)}
+         * @param after   after state or {@code null} for
+         *                {@link Filter#childNodeDeleted(String, NodeState)}
+         * @return a tree instance for basing the filtering criterion (predicate) upon
+         */
+        @Nonnull
+        ImmutableTree select(@Nonnull UniversalFilter filter, @Nonnull String name,
+                @Nonnull NodeState before, @Nonnull NodeState after);
+    }
+
+    /**
+     * @return  before tree this filter acts upon
+     */
+    public ImmutableTree getBeforeTree() {
+        return beforeTree;
+    }
+
+    /**
+     * @return  after tree this filter acts upon
+     */
+    public ImmutableTree getAfterTree() {
+        return afterTree;
+    }
+
+    @Override
+    public boolean includeAdd(PropertyState after) {
+        return predicate.apply(selector.select(this, null, after));
+    }
+
+    @Override
+    public boolean includeChange(PropertyState before, PropertyState after) {
+        return predicate.apply(selector.select(this, before, after));
+    }
+
+    @Override
+    public boolean includeDelete(PropertyState before) {
+        return predicate.apply(selector.select(this, before, null));
+    }
+
+    @Override
+    public boolean includeAdd(String name, NodeState after) {
+        return predicate.apply(selector.select(this, name, MISSING_NODE, after));
+    }
+
+    @Override
+    public boolean includeChange(String name, NodeState before, NodeState after) {
+        return predicate.apply(selector.select(this, name, before, after));
+    }
+
+    @Override
+    public boolean includeDelete(String name, NodeState before) {
+        return predicate.apply(selector.select(this, name, before, MISSING_NODE));
+    }
+
+    @Override
+    public boolean includeMove(String sourcePath, String name, NodeState moved) {
+        return predicate.apply(selector.select(this, name, MISSING_NODE, moved));
+    }
+
+    @Override
+    public Filter create(String name, NodeState before, NodeState after) {
+        return new UniversalFilter(beforeTree.getChild(name), afterTree.getChild(name),
+                selector, predicate);
+    }
+}

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/NodeTypePredicateTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/NodeTypePredicateTest.java?rev=1546982&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/NodeTypePredicateTest.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/NodeTypePredicateTest.java
Mon Dec  2 12:41:21 2013
@@ -0,0 +1,84 @@
+/*
+ * 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.JcrConstants.JCR_CONTENT;
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+import static org.apache.jackrabbit.JcrConstants.NT_BASE;
+import static org.apache.jackrabbit.JcrConstants.NT_FILE;
+import static org.apache.jackrabbit.JcrConstants.NT_FOLDER;
+import static org.apache.jackrabbit.JcrConstants.NT_RESOURCE;
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.core.ImmutableTree;
+import org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager;
+import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent;
+import org.junit.Test;
+
+public class NodeTypePredicateTest {
+    private final ReadOnlyNodeTypeManager ntManager =
+            ReadOnlyNodeTypeManager.getInstance(InitialContent.INITIAL_CONTENT);
+
+    @Test
+    public void emptyNodeTypeList() {
+        NodeTypePredicate p = new NodeTypePredicate(ntManager, new String[] {});
+        ImmutableTree tree = createTreeOfType(NT_BASE);
+        assertFalse(p.apply(tree));
+    }
+
+    @Test
+    public void singleNodeTypeMatch() {
+        NodeTypePredicate p = new NodeTypePredicate(ntManager, new String[] {NT_BASE});
+        ImmutableTree tree = createTreeOfType(NT_BASE);
+        assertTrue(p.apply(tree));
+    }
+
+    @Test
+    public void singleNodeTypeMiss() {
+        NodeTypePredicate p = new NodeTypePredicate(ntManager, new String[] {NT_FILE});
+        ImmutableTree tree = createTreeOfType(NT_BASE);
+        assertFalse(p.apply(tree));
+    }
+
+    @Test
+    public void multipleNodeTypesMatch() {
+        NodeTypePredicate p = new NodeTypePredicate(ntManager,
+                new String[] { NT_FOLDER, NT_RESOURCE, NT_FILE });
+        ImmutableTree tree = createTreeOfType(NT_FILE);
+        assertTrue(p.apply(tree));
+    }
+
+    @Test
+    public void multipleNodeTypesMiss() {
+        NodeTypePredicate p = new NodeTypePredicate(ntManager,
+                new String[] { NT_FOLDER, NT_RESOURCE, JCR_CONTENT });
+        ImmutableTree tree = createTreeOfType(NT_FILE);
+        assertFalse(p.apply(tree));
+    }
+
+    private static ImmutableTree createTreeOfType(String ntName) {
+        return new ImmutableTree(EMPTY_NODE.builder()
+                .setProperty(JCR_PRIMARYTYPE, ntName, Type.NAME)
+                .getNodeState());
+    }
+}

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/UuidPredicateTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/UuidPredicateTest.java?rev=1546982&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/UuidPredicateTest.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/UuidPredicateTest.java
Mon Dec  2 12:41:21 2013
@@ -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.observation.filter;
+
+import static org.apache.jackrabbit.JcrConstants.JCR_UUID;
+import static org.apache.jackrabbit.oak.plugins.identifier.IdentifierManager.generateUUID;
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.jackrabbit.oak.core.ImmutableTree;
+import org.junit.Test;
+
+public class UuidPredicateTest {
+
+    @Test
+    public void emptyUuidList() {
+        UuidPredicate p = new UuidPredicate(new String[] {});
+        ImmutableTree tree = createTreeWithUuid(generateUUID());
+        assertFalse(p.apply(tree));
+    }
+
+    @Test
+    public void singleUuidMatch() {
+        String uuid = generateUUID();
+        UuidPredicate p = new UuidPredicate(new String[] {uuid});
+        ImmutableTree tree = createTreeWithUuid(uuid);
+        assertTrue(p.apply(tree));
+    }
+
+    @Test
+    public void singleUuidMiss() {
+        UuidPredicate p = new UuidPredicate(new String[] {generateUUID()});
+        ImmutableTree tree = createTreeWithUuid(generateUUID());
+        assertFalse(p.apply(tree));
+    }
+
+    @Test
+    public void multipleUuidsMatch() {
+        String uuid = generateUUID();
+        UuidPredicate p = new UuidPredicate(
+                new String[] {generateUUID(), generateUUID(), uuid});
+        ImmutableTree tree = createTreeWithUuid(uuid);
+        assertTrue(p.apply(tree));
+    }
+
+    @Test
+    public void multipleUuidsMiss() {
+        UuidPredicate p = new UuidPredicate(
+                new String[] {generateUUID(), generateUUID(), generateUUID()});
+        ImmutableTree tree = createTreeWithUuid(generateUUID());
+        assertFalse(p.apply(tree));
+    }
+
+    private static ImmutableTree createTreeWithUuid(String uuid) {
+        return new ImmutableTree(EMPTY_NODE.builder()
+                .setProperty(JCR_UUID, uuid)
+                .getNodeState());
+    }
+}



Mime
View raw message