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/11/27 15:30:26 UTC
svn commit: r1546047 - in /jackrabbit/oak/trunk/oak-core/src:
main/java/org/apache/jackrabbit/oak/plugins/observation/filter/
test/java/org/apache/jackrabbit/oak/plugins/observation/
test/java/org/apache/jackrabbit/oak/plugins/observation/filter/
Author: mduerig
Date: Wed Nov 27 14:30:25 2013
New Revision: 1546047
URL: http://svn.apache.org/r1546047
Log:
OAK-1133: Observation listener PLUS
Path filter with simple globbing support
Added:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilter.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilterTest.java
Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilter.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilter.java?rev=1546047&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilter.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilter.java Wed Nov 27 14:30:25 2013
@@ -0,0 +1,159 @@
+package org.apache.jackrabbit.oak.plugins.observation.filter;
+
+import static com.google.common.base.Objects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.jackrabbit.oak.commons.PathUtils.elements;
+import static org.apache.jackrabbit.oak.commons.PathUtils.getName;
+
+import javax.annotation.Nonnull;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
+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;
+
+/**
+ * This {@code Filter} implementation supports filtering on paths using
+ * simple glob patterns. Such a pattern is a string denoting a path. Each
+ * element of the pattern is matched against the corresponding element of
+ * a path. Elements of the pattern are matched literally except for the special
+ * elements {@code *} and {@code **} where the former matches an arbitrary
+ * path element and the latter matches any number of path elements (including none).
+ * <p>
+ * Note: an empty path pattern matches no path.
+ * <p>
+ * Note: path patterns only match against the corresponding elements of the path
+ * and <em>do not</em> distinguish between absolute and relative paths.
+ * <p>
+ * Note: there is no way to escape {@code *} and {@code **}.
+ * <p>
+ * Examples:
+ * <pre>
+ * q matches q only
+ * * matches every path containing a single element
+ * ** matches every path
+ * a/b/c matches a/b/c only
+ * a/*/c matches a/x/c for every element x
+ * **/y/z match every path ending in y/z
+ * r/s/t/** matches r/s/t and all its descendants
+ * </pre>
+ */
+public class GlobbingPathFilter implements Filter {
+ public static final String STAR = "*";
+ public static final String STAR_STAR = "**";
+
+ private final ImmutableTree beforeTree;
+ private final ImmutableTree afterTree;
+ private final ImmutableList<String> pattern;
+
+ private GlobbingPathFilter(
+ @Nonnull ImmutableTree beforeTree,
+ @Nonnull ImmutableTree afterTree,
+ @Nonnull Iterable<String> pattern) {
+ this.beforeTree = checkNotNull(beforeTree);
+ this.afterTree = checkNotNull(afterTree);
+ this.pattern = ImmutableList.copyOf(checkNotNull(pattern));
+ }
+
+ public GlobbingPathFilter(
+ @Nonnull ImmutableTree beforeTree,
+ @Nonnull ImmutableTree afterTree,
+ @Nonnull String pattern) {
+ this(beforeTree, afterTree, elements(pattern));
+ }
+
+ @Override
+ public boolean includeAdd(PropertyState after) {
+ return includeItem(after.getName());
+ }
+
+ @Override
+ public boolean includeChange(PropertyState before, PropertyState after) {
+ return includeItem(after.getName());
+ }
+
+ @Override
+ public boolean includeDelete(PropertyState before) {
+ return includeItem(before.getName());
+ }
+
+ @Override
+ public boolean includeAdd(String name, NodeState after) {
+ return includeItem(name);
+ }
+
+ @Override
+ public boolean includeChange(String name, NodeState before, NodeState after) {
+ return includeItem(name);
+ }
+
+ @Override
+ public boolean includeDelete(String name, NodeState before) {
+ return includeItem(name);
+ }
+
+ @Override
+ public boolean includeMove(String sourcePath, String destPath, NodeState moved) {
+ return includeItem(getName(destPath));
+ }
+
+ @Override
+ public Filter create(String name, NodeState before, NodeState after) {
+ if (pattern.isEmpty()) {
+ return null;
+ }
+
+ String head = pattern.get(0);
+ if (pattern.size() == 1 && !STAR_STAR.equals(head)) {
+ // shortcut when no further matches are possible
+ return null;
+ }
+
+ if (STAR.equals(head) || head.equals(name)) {
+ return new GlobbingPathFilter(beforeTree.getChild(name), afterTree.getChild(name),
+ pattern.subList(1, pattern.size()));
+ } else if (STAR_STAR.equals(head)) {
+ if (pattern.size() >= 2 && pattern.get(1).equals(name)) {
+ // ** matches empty list of elements and pattern.get(1) matches name
+ // match the rest of the pattern against the rest of the path and
+ // match the whole pattern against the rest of the path
+ return Filters.any(
+ new GlobbingPathFilter(beforeTree.getChild(name), afterTree.getChild(name),
+ pattern.subList(2, pattern.size())),
+ new GlobbingPathFilter(beforeTree.getChild(name), afterTree.getChild(name),
+ pattern)
+ );
+ } else {
+ // ** matches name, match the whole pattern against the rest of the path
+ return new GlobbingPathFilter(beforeTree.getChild(name), afterTree.getChild(name),
+ pattern);
+ }
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .add("path", Joiner.on('/').join(pattern))
+ .toString();
+ }
+
+ //------------------------------------------------------------< private >---
+
+ private boolean includeItem(String name) {
+ if (!pattern.isEmpty() && pattern.size() <= 2) {
+ String head = pattern.get(0);
+ boolean headMatches = STAR.equals(head) || STAR_STAR.equals(head) || head.equals(name);
+ return pattern.size() == 1
+ ? headMatches
+ : headMatches && STAR_STAR.equals(pattern.get(1));
+ } else {
+ return false;
+ }
+ }
+
+}
Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilterTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilterTest.java?rev=1546047&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilterTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/filter/GlobbingPathFilterTest.java Wed Nov 27 14:30:25 2013
@@ -0,0 +1,275 @@
+/*
+ * 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.elements;
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static org.apache.jackrabbit.oak.plugins.observation.filter.GlobbingPathFilter.STAR;
+import static org.apache.jackrabbit.oak.plugins.observation.filter.GlobbingPathFilter.STAR_STAR;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.jackrabbit.oak.core.ImmutableTree;
+import org.apache.jackrabbit.oak.plugins.observation.filter.EventGenerator.Filter;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Before;
+import org.junit.Test;
+
+public class GlobbingPathFilterTest {
+
+ private ImmutableTree tree;
+
+ @Before
+ public void setup() {
+ NodeBuilder root = EMPTY_NODE.builder();
+ createPath(root, "a/b/c/d");
+ createPath(root, "q");
+ createPath(root, "x/y/x/y/z");
+ createPath(root, "r/s/t/u/v/r/s/t/u/v/r/s/t/u/v/w");
+ tree = new ImmutableTree(root.getNodeState());
+ }
+
+ private static void createPath(NodeBuilder root, String path) {
+ NodeBuilder builder = root;
+ for (String name : elements(path)) {
+ builder = builder.setChildNode(name);
+ }
+ }
+
+ /**
+ * An empty path pattern should match no path
+ */
+ @Test
+ public void emptyMatchesNothing() {
+ Filter rootFilter = new GlobbingPathFilter(tree, tree, "");
+ NodeState a = tree.getChild("a").getNodeState();
+ assertFalse(rootFilter.includeAdd("a", a));
+ assertNull(rootFilter.create("a", a, a));
+ }
+
+ /**
+ * q should match q
+ */
+ @Test
+ public void singleMatchesSingle() {
+ Filter filter = new GlobbingPathFilter(tree, tree, "q");
+ ImmutableTree t = tree;
+
+ assertTrue(filter.includeAdd("q", t.getNodeState()));
+ }
+
+ /**
+ * * should match q
+ */
+ @Test
+ public void starMatchesSingle() {
+ Filter filter = new GlobbingPathFilter(tree, tree, STAR);
+ ImmutableTree t = tree;
+
+ assertTrue(filter.includeAdd("q", t.getNodeState()));
+ }
+
+ /**
+ * ** should match every path
+ */
+ @Test
+ public void all() {
+ Filter filter = new GlobbingPathFilter(tree, tree, STAR_STAR);
+ ImmutableTree t = tree;
+
+ for(String name : elements("a/b/c/d")) {
+ assertTrue(filter.includeAdd(name, t.getNodeState()));
+ t = t.getChild(name);
+ filter = filter.create(name, t.getNodeState(), t.getNodeState());
+ assertNotNull(filter);
+ }
+ }
+
+ /**
+ * a/b/c should match a/b/c
+ */
+ @Test
+ public void literal() {
+ Filter rootFilter = new GlobbingPathFilter(tree, tree, "a/b/c");
+ NodeState a = tree.getChild("a").getNodeState();
+ assertFalse(rootFilter.includeAdd("a", a));
+
+ Filter aFilter = rootFilter.create("a", a, a);
+ assertNotNull(aFilter);
+ NodeState b = a.getChildNode("b");
+ assertFalse(aFilter.includeAdd("b", b));
+
+ Filter bFilter = aFilter.create("b", b, b);
+ assertNotNull(bFilter);
+ NodeState c = b.getChildNode("c");
+ assertTrue(bFilter.includeAdd("c", b));
+ assertFalse(bFilter.includeAdd("x", b));
+
+ assertNull(bFilter.create("c", c, c));
+ }
+
+ /**
+ * a/*/c should match a/b/c
+ */
+ @Test
+ public void starGlob() {
+ Filter rootFilter = new GlobbingPathFilter(tree, tree, "a/*/c");
+ NodeState a = tree.getChild("a").getNodeState();
+ assertFalse(rootFilter.includeAdd("a", a));
+
+ Filter aFilter = rootFilter.create("a", a, a);
+ assertNotNull(aFilter);
+ NodeState b = a.getChildNode("b");
+ assertFalse(aFilter.includeAdd("b", b));
+
+ Filter bFilter = aFilter.create("b", b, b);
+ assertNotNull(bFilter);
+ NodeState c = b.getChildNode("c");
+ assertTrue(bFilter.includeAdd("c", b));
+ assertFalse(bFilter.includeAdd("x", b));
+
+ assertNull(bFilter.create("c", c, c));
+ }
+
+ /**
+ * **//y/z should match x/y/x/y/z
+ */
+ @Test
+ public void starStarGlob() {
+ Filter rootFilter = new GlobbingPathFilter(tree, tree, "**/y/z");
+ NodeState x1 = tree.getChild("x").getNodeState();
+ assertFalse(rootFilter.includeAdd("x", x1));
+
+ Filter x1Filter = rootFilter.create("x", x1, x1);
+ assertNotNull(x1Filter);
+ NodeState y1 = x1.getChildNode("y");
+ assertFalse(x1Filter.includeAdd("y", y1));
+
+ Filter y1Filter = x1Filter.create("y", y1, y1);
+ assertNotNull(y1Filter);
+ NodeState x2 = y1.getChildNode("x");
+ assertFalse(y1Filter.includeAdd("x", x2));
+
+ Filter x2Filter = y1Filter.create("x", x2, x2);
+ assertNotNull(x2Filter);
+ NodeState y2 = x2.getChildNode("y");
+ assertFalse(x2Filter.includeAdd("y", y2));
+
+ Filter y2Filter = x2Filter.create("y", y2, y2);
+ assertNotNull(y2Filter);
+ NodeState z = y2.getChildNode("z");
+ assertTrue(y2Filter.includeAdd("z", z));
+
+ Filter zFilter = (y2Filter.create("z", z, z));
+ assertFalse(zFilter.includeAdd("x", EMPTY_NODE));
+ }
+
+ /**
+ * **/a/b/c should match a/b/c
+ */
+ @Test
+ public void matchAtStart() {
+ Filter rootFilter = new GlobbingPathFilter(tree, tree, "**/a/b/c");
+ NodeState a = tree.getChild("a").getNodeState();
+ assertFalse(rootFilter.includeAdd("a", a));
+
+ Filter aFilter = rootFilter.create("a", a, a);
+ assertNotNull(aFilter);
+ NodeState b = a.getChildNode("b");
+ assertFalse(aFilter.includeAdd("b", b));
+
+ Filter bFilter = aFilter.create("b", b, b);
+ assertNotNull(bFilter);
+ NodeState c = b.getChildNode("c");
+ assertTrue(bFilter.includeAdd("c", b));
+ assertFalse(bFilter.includeAdd("x", b));
+ }
+
+ /**
+ * **/r/s/t/u/v should match r/s/t/u/v and r/s/t/u/v/r/s/t/u/v and r/s/t/u/v/r/s/t/u/v/r/s/t/u/v
+ */
+ @Test
+ public void multipleMatches() {
+ Filter filter = new GlobbingPathFilter(tree, tree, "**/r/s/t/u/v");
+ ImmutableTree t = tree;
+
+ for(int c = 0; c < 2; c++) {
+ for(String name : elements("r/s/t/u")) {
+ assertFalse(filter.includeAdd(name, t.getNodeState()));
+ t = t.getChild(name);
+ filter = filter.create(name, t.getNodeState(), t.getNodeState());
+ assertNotNull(filter);
+ }
+
+ assertTrue(filter.includeAdd("v", t.getNodeState()));
+ t = t.getChild("v");
+ filter = filter.create("v", t.getNodeState(), t.getNodeState());
+ assertNotNull(filter);
+ }
+ }
+
+ /**
+ * **/r/s/t/u/v/w should match r/s/t/u/v/r/s/t/u/v/r/s/t/u/v/w
+ */
+ @Test
+ public void matchAtEnd() {
+ Filter filter = new GlobbingPathFilter(tree, tree, "**/r/s/t/u/v/w");
+ ImmutableTree t = tree;
+
+ for(String name : elements("r/s/t/u/v/r/s/t/u/v/r/s/t/u/v")) {
+ assertFalse(filter.includeAdd(name, t.getNodeState()));
+ t = t.getChild(name);
+ filter = filter.create(name, t.getNodeState(), t.getNodeState());
+ assertNotNull(filter);
+ }
+
+ assertTrue(filter.includeAdd("w", t.getNodeState()));
+ t = t.getChild("w");
+ filter = filter.create("w", t.getNodeState(), t.getNodeState());
+ assertNotNull(filter);
+ }
+
+ /**
+ * r/s/t/** should match r/s/t and all its descendants
+ */
+ @Test
+ public void matchSuffix() {
+ Filter filter = new GlobbingPathFilter(tree, tree, "r/s/t/**");
+ ImmutableTree t = tree;
+
+ for(String name : elements("r/s")) {
+ assertFalse(filter.includeAdd(name, t.getNodeState()));
+ t = t.getChild(name);
+ filter = filter.create(name, t.getNodeState(), t.getNodeState());
+ assertNotNull(filter);
+ }
+
+ for (String name: elements("t/u/v/r/s/t/u/v/r/s/t/u/v/w")) {
+ assertTrue(filter.includeAdd(name, t.getNodeState()));
+ t = t.getChild(name);
+ filter = filter.create(name, t.getNodeState(), t.getNodeState());
+ assertNotNull(filter);
+ }
+ }
+
+}