Author: chetanm
Date: Fri Dec  8 07:25:44 2017
New Revision: 1817461

URL: http://svn.apache.org/viewvc?rev=1817461&view=rev
Log:
OAK-6353 - Use Document order traversal for reindexing performed on 
DocumentNodeStore setups

Comparator to compare path elements

Added:
    
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparator.java
   (with props)
    
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparatorTest.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparator.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparator.java?rev=1817461&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparator.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparator.java
 Fri Dec  8 07:25:44 2017
@@ -0,0 +1,47 @@
+/*
+ * 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.index.indexer.document.flatfile;
+
+import java.util.Comparator;
+import java.util.Iterator;
+
+public class PathElementComparator implements Comparator<Iterable<String>> {
+    @Override
+    public int compare(Iterable<String> p1, Iterable<String> p2) {
+        Iterator<String> i1 = p1.iterator();
+        Iterator<String> i2 = p2.iterator();
+
+        //Shorter paths come first i.e. first parent then children
+        //TODO Rank jcr:content higher i.e. first child
+        while (i1.hasNext() || i2.hasNext()) {
+            if (!i1.hasNext()) {
+                return -1;
+            }
+            if (!i2.hasNext()) {
+                return 1;
+            }
+            int compare = i1.next().compareTo(i2.next());
+            if (compare != 0) {
+                return compare;
+            }
+        }
+        return 0;
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparatorTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparatorTest.java?rev=1817461&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparatorTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparatorTest.java
 Fri Dec  8 07:25:44 2017
@@ -0,0 +1,69 @@
+/*
+ * 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.index.indexer.document.flatfile;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.junit.Test;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+
+public class PathElementComparatorTest {
+
+    @Test
+    public void sortPathsParentChild() {
+        List<String> sorted = sortPaths(asList("/a", "/a b", "/a/bw"));
+        assertEquals(asList("/a", "/a/bw", "/a b"), sorted);
+    }
+
+    @Test
+    public void sort2() {
+        assertSorted(asList("/a", "/a/b", "/a/b/c", "/d", "/e/f", "/g"));
+        assertSorted(asList("/", "/a", "/a/b", "/a/b/c", "/d", "/e/f", "/g"));
+
+        //Duplicates
+        assertSorted(asList("/", "/a", "/a", "/a/b", "/a/b/c", "/d", "/e/f", 
"/g"));
+    }
+
+    private void assertSorted(List<String> sorted) {
+        List<String> copy = new ArrayList<>(sorted);
+        Collections.shuffle(copy);
+        List<String> sortedNew = sortPaths(copy);
+        assertEquals(sorted, sortedNew);
+    }
+
+    private List<String> sortPaths(List<String> paths){
+        return sortPaths(paths, new PathElementComparator());
+    }
+
+    private List<String> sortPaths(List<String> paths, 
Comparator<Iterable<String>> comparator) {
+        List<Iterable<String>> copy = 
paths.stream().map(PathUtils::elements).sorted(comparator).collect(Collectors.toList());
+        Joiner j = Joiner.on('/');
+        return copy.stream().map(e -> "/" + 
j.join(e)).collect(Collectors.toList());
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/PathElementComparatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to