Author: chetanm
Date: Wed Oct 19 15:07:18 2016
New Revision: 1765622

URL: http://svn.apache.org/viewvc?rev=1765622&view=rev
Log:
OAK-1312 -  [bundling] Bundle nodes into a document

Utility methods to extract bundled node data based on relative property map

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtils.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/PropertyStateWrapper.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtilsTest.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtils.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtils.java?rev=1765622&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtils.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtils.java
 Wed Oct 19 15:07:18 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.document.bundlor;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+
+import static 
org.apache.jackrabbit.oak.plugins.document.bundlor.DocumentBundlor.META_PROP_NODE;
+
+public final class BundlorUtils {
+
+    public static Map<String, PropertyState> getMatchingProperties(Map<String, 
PropertyState> props, Matcher matcher){
+        if (!matcher.isMatch()){
+            return Collections.emptyMap();
+        }
+
+        Map<String, PropertyState> result = Maps.newHashMap();
+        for (Map.Entry<String, PropertyState> e : props.entrySet()){
+            String propertyPath = e.getKey();
+
+            //PathUtils.depth include depth for property name. So
+            //reduce 1 to get node depth
+            int depth = PathUtils.getDepth(propertyPath) - 1;
+
+            if (!propertyPath.startsWith(matcher.getMatchedPath())){
+                continue;
+            }
+
+            if (depth != matcher.depth()){
+                continue;
+            }
+
+            if (propertyPath.endsWith(META_PROP_NODE)){
+                continue;
+            }
+
+            //Extract property name from relative property path
+            final String newKey = PathUtils.getName(propertyPath);
+            PropertyState value = e.getValue();
+
+            if (depth > 0){
+                value = new PropertyStateWrapper(value){
+                    @Nonnull
+                    @Override
+                    public String getName() {
+                        return newKey;
+                    }
+                };
+            }
+
+            result.put(newKey, value);
+        }
+        return result;
+    }
+
+    public static List<String> getChildNodeNames(Collection<String> keys, 
Matcher matcher){
+        List<String> childNodeNames = Lists.newArrayList();
+
+        //Immediate child should have depth 1 more than matcher depth
+        int expectedDepth = matcher.depth() + 1;
+
+        for (String key : keys){
+            List<String> elements = 
ImmutableList.copyOf(PathUtils.elements(key));
+            int depth = elements.size() - 1;
+
+            if (depth == expectedDepth
+                    && elements.get(elements.size() - 
1).equals(META_PROP_NODE)){
+                //Child node name is the second last element
+                //[jcr:content/:self -> [jcr:content, :self]
+                childNodeNames.add(elements.get(elements.size() - 2));
+            }
+        }
+        return childNodeNames;
+    }
+}

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

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/PropertyStateWrapper.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/PropertyStateWrapper.java?rev=1765622&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/PropertyStateWrapper.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/bundlor/PropertyStateWrapper.java
 Wed Oct 19 15:07:18 2016
@@ -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.document.bundlor;
+
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.plugins.memory.AbstractPropertyState;
+
+//TODO Move this to org.apache.jackrabbit.oak.plugins.memory
+class PropertyStateWrapper extends AbstractPropertyState implements 
PropertyState {
+    private final PropertyState delegate;
+
+    public PropertyStateWrapper(PropertyState delegate) {
+        this.delegate = delegate;
+    }
+
+    @Nonnull
+    @Override
+    public String getName() {
+        return delegate.getName();
+    }
+
+    @Override
+    public boolean isArray() {
+        return delegate.isArray();
+    }
+
+    @Override
+    public Type<?> getType() {
+        return delegate.getType();
+    }
+
+    @Nonnull
+    @Override
+    public <T> T getValue(Type<T> type) {
+        return delegate.getValue(type);
+    }
+
+    @Nonnull
+    @Override
+    public <T> T getValue(Type<T> type, int index) {
+        return delegate.getValue(type, index);
+    }
+
+    @Override
+    public long size() {
+        return delegate.size();
+    }
+
+    @Override
+    public long size(int index) {
+        return delegate.size(index);
+    }
+
+    @Override
+    public int count() {
+        return delegate.count();
+    }
+}

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

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtilsTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtilsTest.java?rev=1765622&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtilsTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/bundlor/BundlorUtilsTest.java
 Wed Oct 19 15:07:18 2016
@@ -0,0 +1,107 @@
+/*
+ * 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.document.bundlor;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.junit.Test;
+
+import static java.util.Arrays.asList;
+import static org.apache.jackrabbit.oak.commons.PathUtils.concat;
+import static 
org.apache.jackrabbit.oak.plugins.document.bundlor.DocumentBundlor.META_PROP_NODE;
+import static 
org.apache.jackrabbit.oak.plugins.document.bundlor.DocumentBundlor.META_PROP_PATTERN;
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.hasItems;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class BundlorUtilsTest {
+    @Test
+    public void matchingProperties_Simple() throws Exception{
+        Map<String, PropertyState> result = BundlorUtils.getMatchingProperties(
+            create("a", "b", "c"), Matcher.NON_MATCHING
+        );
+
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void matchingProperties_BaseLevel() throws Exception{
+        Matcher m = new Include("jcr:content").createMatcher();
+        Map<String, PropertyState> result = BundlorUtils.getMatchingProperties(
+                create("a",
+                        concat("jcr:content", META_PROP_NODE),
+                        "jcr:content/jcr:data",
+                        "jcr:primaryType",
+                        META_PROP_PATTERN
+                ), m
+        );
+        assertThat(result.keySet(), hasItems("a", "jcr:primaryType", 
META_PROP_PATTERN));
+    }
+
+    @Test
+    public void matchingProperties_FirstLevel() throws Exception{
+        Matcher m = new 
Include("jcr:content").createMatcher().next("jcr:content");
+        Map<String, PropertyState> result = BundlorUtils.getMatchingProperties(
+                create("a",
+                        concat("jcr:content", META_PROP_NODE),
+                        "jcr:content/jcr:data",
+                        "jcr:content/metadata/format",
+                        "jcr:primaryType",
+                        META_PROP_PATTERN
+                ), m
+        );
+        assertThat(result.keySet(), hasItems("jcr:data"));
+        assertEquals("jcr:data", result.get("jcr:data").getName());
+    }
+
+    @Test
+    public void childNodeNames() throws Exception{
+        List<String> testData = asList("x",
+                concat("jcr:content", META_PROP_NODE),
+                "jcr:content/jcr:data",
+                concat("jcr:content/metadata", META_PROP_NODE),
+                "jcr:content/metadata/format",
+                concat("jcr:content/comments", META_PROP_NODE),
+                concat("jcr:content/renditions/original", META_PROP_NODE)
+        );
+
+        Matcher m = new Include("jcr:content/*").createMatcher();
+        List<String> names = BundlorUtils.getChildNodeNames(testData, m);
+        assertThat(names, hasItem("jcr:content"));
+
+        names = BundlorUtils.getChildNodeNames(testData, 
m.next("jcr:content"));
+        assertThat(names, hasItems("metadata", "comments"));
+    }
+
+    private Map<String, PropertyState> create(String ... keyNames){
+        Map<String, PropertyState> map = new HashMap<>();
+        for (String key : keyNames){
+            map.put(key, PropertyStates.createProperty(key, Boolean.TRUE));
+        }
+        return map;
+    }
+
+}
\ No newline at end of file

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


Reply via email to