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 ch...@apache.org on 2016/07/04 15:25:57 UTC

svn commit: r1751311 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/multiplex/ main/java/org/apache/jackrabbit/oak/spi/mount/ test/java/org/apache/jackrabbit/oak/plugins/multiplex/

Author: chetanm
Date: Mon Jul  4 15:25:57 2016
New Revision: 1751311

URL: http://svn.apache.org/viewvc?rev=1751311&view=rev
Log:
OAK-3404 - [multiplex] Path to logical store mapping

Introducing MountInfoProvider which is responsible for mapping a path to mount. A simple naive implementation is provided which check for mounting based on path being ancestor

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfo.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProvider.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/Mount.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/MountInfoProvider.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/package-info.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfoTest.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProviderTest.java   (with props)

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfo.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfo.java?rev=1751311&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfo.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfo.java Mon Jul  4 15:25:57 2016
@@ -0,0 +1,55 @@
+/*
+ * 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.multiplex;
+
+import java.util.List;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.jackrabbit.oak.spi.mount.Mount;
+
+import static org.apache.jackrabbit.oak.commons.PathUtils.isAncestor;
+
+final class MountInfo {
+    private final Mount mount;
+    private final List<String> includedPaths;
+
+    public MountInfo(Mount mount, List<String> includedPaths){
+        this.mount = mount;
+        this.includedPaths = ImmutableList.copyOf(includedPaths);
+    }
+
+    public Mount getMount() {
+        return mount;
+    }
+
+    public boolean isMounted(String path){
+        if (path.contains(mount.getPathFragmentName())){
+            return true;
+        }
+
+        //TODO may be optimized via trie
+        for (String includedPath : includedPaths){
+            if (includedPath.equals(path) || isAncestor(includedPath, path)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProvider.java?rev=1751311&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProvider.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProvider.java Mon Jul  4 15:25:57 2016
@@ -0,0 +1,102 @@
+/*
+ * 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.multiplex;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.apache.jackrabbit.oak.spi.mount.Mount;
+import org.apache.jackrabbit.oak.spi.mount.MountInfoProvider;
+
+import static java.util.Arrays.asList;
+
+/**
+ * A simple and inefficient implementation to manage mountpoints
+ */
+public class SimpleMountInfoProvider implements MountInfoProvider {
+    private final List<MountInfo> mountInfos;
+    private final Map<String, Mount> mounts;
+
+    public SimpleMountInfoProvider(List<MountInfo> mountInfos){
+        this.mountInfos = ImmutableList.copyOf(mountInfos);
+        this.mounts = getMounts(mountInfos);
+        //TODO add validation of mountpoints
+    }
+
+    @Override
+    public Mount getMountInfo(String path) {
+        for (MountInfo md : mountInfos){
+            if (md.isMounted(path)){
+                return md.getMount();
+            }
+        }
+        return Mount.DEFAULT;
+    }
+
+    @Override
+    public Collection<Mount> getNonDefaultMounts() {
+        return mounts.values();
+    }
+
+    @Override
+    public Mount getMount(String name) {
+        return mounts.get(name);
+    }
+
+    //~----------------------------------------< builder >
+
+    public static Builder newBuilder(){
+        return new Builder();
+    }
+
+    public static final class Builder {
+        private final List<MountInfo> mounts = Lists.newArrayListWithCapacity(1);
+
+        public Builder mount(String name, String... paths) {
+            mounts.add(new MountInfo(new Mount(name), asList(paths)));
+            return this;
+        }
+
+        public Builder readOnlyMount(String name, String... paths) {
+            mounts.add(new MountInfo(new Mount(name, true), asList(paths)));
+            return this;
+        }
+
+        public SimpleMountInfoProvider build() {
+            return new SimpleMountInfoProvider(mounts);
+        }
+    }
+
+    //~----------------------------------------< private >
+
+    private static Map<String, Mount> getMounts(List<MountInfo> mountInfos) {
+        Map<String, Mount> mounts = Maps.newHashMap();
+        for (MountInfo mi : mountInfos){
+            mounts.put(mi.getMount().getName(), mi.getMount());
+        }
+        return ImmutableMap.copyOf(mounts);
+    }
+
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/Mount.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/Mount.java?rev=1751311&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/Mount.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/Mount.java Mon Jul  4 15:25:57 2016
@@ -0,0 +1,92 @@
+/*
+ * 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.spi.mount;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+public final class Mount {
+    /**
+     * Default Mount info which indicates that no explicit mount
+     * is created for given path
+     */
+    public static final Mount DEFAULT = new Mount("", false, true);
+
+    private final String name;
+    private final boolean readOnly;
+    private final boolean defaultMount;
+    private final String pathFragmentName;
+
+    public Mount(String name){
+        this(name, false);
+    }
+
+    public Mount(String name, boolean readOnly) {
+       this(name, readOnly, false);
+    }
+
+    private Mount(String name, boolean readOnly, boolean defaultMount){
+        this.name = checkNotNull(name, "Mount name must not be null");
+        this.readOnly = readOnly;
+        this.defaultMount = defaultMount;
+        this.pathFragmentName = "oak:" + name;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public boolean isReadOnly() {
+        return readOnly;
+    }
+
+    public boolean isDefault(){
+        return defaultMount;
+    }
+
+    /**
+     * Decorated mount name which is meant to be used for constructing path
+     * which should become part of given mount
+     */
+    public String getPathFragmentName() {
+        return pathFragmentName;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Mount mount = (Mount) o;
+
+        return name.equals(mount.name);
+    }
+
+    @Override
+    public int hashCode() {
+        return name.hashCode();
+    }
+
+    @Override
+    public String toString() {
+        String readAttr = readOnly ? "r" : "rw";
+        String displayName =  defaultMount ? "default" : name;
+        return displayName + "(" + readAttr + ")";
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/Mount.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/MountInfoProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/MountInfoProvider.java?rev=1751311&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/MountInfoProvider.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/MountInfoProvider.java Mon Jul  4 15:25:57 2016
@@ -0,0 +1,68 @@
+/*
+ * 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.spi.mount;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import javax.annotation.CheckForNull;
+
+public interface MountInfoProvider {
+    MountInfoProvider DEFAULT = new MountInfoProvider() {
+        @Override
+        public Mount getMountInfo(String path) {
+            return Mount.DEFAULT;
+        }
+
+        @Override
+        public Collection<Mount> getNonDefaultMounts() {
+            return Collections.emptySet();
+        }
+
+        @Override
+        public Mount getMount(String name) {
+            return null;
+        }
+    };
+
+    /**
+     * Maps a given path to logical store name.
+     *
+     * @param path node path for which backing store location is to be determined
+     * @return mountInfo for the given path. If no explicit mount configured then
+     * default mount would be returned
+     */
+    Mount getMountInfo(String path);
+
+    /**
+     * Set of non default mount points configured for the setup
+     */
+    Collection<Mount> getNonDefaultMounts();
+
+    /**
+     * Returns the mount instance for given mount name
+     *
+     * @param name name of the mount
+     * @return mount instance for given mount name. If no mount exists for given name
+     * null would be returned
+     */
+    @CheckForNull
+    Mount getMount(String name);
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/MountInfoProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/package-info.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/package-info.java?rev=1751311&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/package-info.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/package-info.java Mon Jul  4 15:25:57 2016
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+@Version("1.0.0")
+@Export(optional = "provide:=true")
+package org.apache.jackrabbit.oak.spi.mount;
+
+import aQute.bnd.annotation.Export;
+import aQute.bnd.annotation.Version;
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/mount/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfoTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfoTest.java?rev=1751311&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfoTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfoTest.java Mon Jul  4 15:25:57 2016
@@ -0,0 +1,41 @@
+/*
+ * 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.multiplex;
+
+import org.apache.jackrabbit.oak.spi.mount.Mount;
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableList.of;
+import static org.junit.Assert.*;
+
+public class MountInfoTest {
+
+    @Test
+    public void mountNameInPath() throws Exception{
+        MountInfo md = new MountInfo(new Mount("foo"), of("/a", "/b"));
+        assertTrue(md.isMounted("/a"));
+        assertTrue(md.isMounted("/b"));
+        assertTrue(md.isMounted("/b/c/d"));
+        assertTrue("dynamic mount path not recognized", md.isMounted("/x/y/oak:foo/a"));
+        assertFalse(md.isMounted("/x/y"));
+        assertFalse(md.isMounted("/x/y/foo"));
+    }
+
+}
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/MountInfoTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProviderTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProviderTest.java?rev=1751311&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProviderTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProviderTest.java Mon Jul  4 15:25:57 2016
@@ -0,0 +1,87 @@
+/*
+ * 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.multiplex;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.jackrabbit.oak.spi.mount.Mount;
+import org.apache.jackrabbit.oak.spi.mount.MountInfoProvider;
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableList.of;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class SimpleMountInfoProviderTest {
+    @Test
+    public void defaultMount() throws Exception {
+        MountInfoProvider mip = new SimpleMountInfoProvider(Collections.<MountInfo>emptyList());
+
+        assertNotNull(mip.getMountInfo("/a"));
+        assertTrue(mip.getMountInfo("/a").isDefault());
+    }
+
+    @Test
+    public void basicMounting() throws Exception {
+        MountInfoProvider mip = SimpleMountInfoProvider.newBuilder()
+                .mount("foo", "/a", "/b")
+                .mount("bar", "/x", "/y")
+                .build();
+
+        assertEquals("foo", mip.getMountInfo("/a").getName());
+        assertEquals("foo", mip.getMountInfo("/a/x").getName());
+        assertEquals("bar", mip.getMountInfo("/x").getName());
+        assertTrue(mip.getMountInfo("/z").isDefault());
+    }
+
+    @Test
+    public void nonDefaultMounts() throws Exception{
+        MountInfoProvider mip = SimpleMountInfoProvider.newBuilder()
+                .mount("foo", "/a", "/b")
+                .mount("bar", "/x", "/y")
+                .build();
+
+        Collection<Mount> mounts = mip.getNonDefaultMounts();
+        assertEquals(2, mounts.size());
+        assertFalse(mounts.contains(Mount.DEFAULT));
+
+        assertNotNull(mip.getMount("foo"));
+        assertNotNull(mip.getMount("bar"));
+        assertNull(mip.getMount("boom"));
+    }
+
+    @Test
+    public void readOnlyMounting() throws Exception{
+        MountInfoProvider mip = SimpleMountInfoProvider.newBuilder()
+                .mount("foo", "/a", "/b")
+                .readOnlyMount("bar", "/x", "/y")
+                .build();
+
+        assertTrue(mip.getMount("bar").isReadOnly());
+        assertFalse(mip.getMount("foo").isReadOnly());
+    }
+
+}
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/multiplex/SimpleMountInfoProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native