fabriziofortino commented on code in PR #1086:
URL: https://github.com/apache/jackrabbit-oak/pull/1086#discussion_r1379040534


##########
oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/tools/IndexComparator.java:
##########
@@ -0,0 +1,247 @@
+/* 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.index.lucene.tools;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Function;
+
+import org.apache.jackrabbit.oak.commons.json.JsonObject;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.plugins.index.lucene.directory.LocalIndexDir;
+import 
org.apache.jackrabbit.oak.plugins.index.lucene.reader.DefaultIndexReader;
+import org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader;
+import org.apache.lucene.index.Fields;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.store.SimpleFSDirectory;
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * Tool to compare Lucene indexes.
+ * Indexes are considered equal if everything except the following is equal:
+ * - checksums of .si, .cfe, .cfs files
+ * - size of .si, .cfe, .cfs files (up to 1000 bytes or 0.1% difference is 
acceptable)
+ */
+public class IndexComparator {
+
+    public static void main(String... args) throws Exception {
+        if (args.length < 1) {
+            System.out.println("Usage: java " + 
IndexComparator.class.getName() +
+                    " <lucene index directory> [<second lucene index 
directory>]");
+            System.out.println(
+                    "If only one directory is specified, then the output is 
json " +
+                    "that contains the statistics of the index.");
+            System.out.println(
+                    "Instead of a directory, each argument can be a json file 
" +
+                    "that contains the statistics of a previous run.");
+            return;
+        }
+        String stats1 = getStats(new File(args[0]));
+        if (args.length > 1) {
+            String stats2 = getStats(new File(args[1]));
+            System.out.println(diff(stats1, stats2));
+        } else {
+            System.out.println(stats1);
+        }
+    }
+
+    private static String diff(String stats1, String stats2) {
+        JsonObject o1 = JsonObject.fromJson(stats1, true);
+        JsonObject o2 = JsonObject.fromJson(stats2, true);
+        o1.getProperties().remove("directory");
+        o2.getProperties().remove("directory");
+        JsopBuilder result = new JsopBuilder();
+        result.object();
+        diff(o1, o2, "", result);
+        result.endObject();
+        if (result.toString().equals("{}")) {
+            return "";
+        }
+        return JsopBuilder.prettyPrint(result.toString());
+    }
+
+    private static void diff(JsonObject o1, JsonObject o2, String path, 
JsopBuilder result) {
+        boolean ignoreChecksums = false;
+        if (path.startsWith("/files/")) {
+            if (path.endsWith("index-details.txt")) {
+                // ignore index details
+                ignoreChecksums = true;
+            } else if (path.endsWith(".si")) {
+                // ".si" files contain a timestamp
+                ignoreChecksums = true;
+            } else if (path.endsWith(".cfs")
+                    || path.endsWith(".cfe")) {
+                // ".cfs" and ".cfe files depends on
+                // the aggregation order
+                ignoreChecksums = true;
+            }
+        }
+        Set<String> both = new HashSet<>(o1.getProperties().keySet());
+        both.addAll(o2.getProperties().keySet());
+        for (String k : both) {
+            String v1 = o1.getProperties().get(k);
+            String v2 = o2.getProperties().get(k);
+            if (v1 == null || v2 == null || !v1.equals(v2)) {
+                if (ignoreChecksums) {
+                    if ("sha256".equals(k) || "md5".equals(k)) {
+                        continue;
+                    }
+                    if ("bytes".equals(k) && v1 != null && v2 != null) {
+                        long x1 = Long.parseLong(v1);
+                        long x2 = Long.parseLong(v2);
+                        if (Math.abs(x1 - x2) < 1000) {
+                            // less than 1000 bytes difference
+                            // if aggregation order is different
+                            continue;
+                        } else if (Math.abs((double) x1 / x2 - 1.0) < 0.001) {
+                            // less than 0.1% difference
+                            // if aggregation order is different
+                            continue;
+                        }
+                    }
+                }
+                result.key(path + "/" + k).object().
+                    key("v1").encodedValue(v1).
+                    key("v2").encodedValue(v2).
+                    endObject();
+            }
+        }
+        both = new HashSet<>(o1.getChildren().keySet());
+        both.addAll(o2.getChildren().keySet());
+        for (String k : both) {
+            JsonObject v1 = o1.getChildren().get(k);
+            JsonObject v2 = o2.getChildren().get(k);
+            if (v1 == null || v2 == null) {
+                result.key(path + "/" + k).object().
+                    key("v1").encodedValue("" + v1).
+                    key("v2").encodedValue("" + v2).
+                    endObject();
+            } else {
+                diff(v1, v2, path + "/" + k, result);
+            }
+        }
+    }
+
+    /**
+     * Create or load a string that contains the JSON representation of the
+     * statistics of an index.
+     *
+     * @param file the Lucene directory, or JSON file
+     * @return the statistics in JSON form
+     */
+    private static String getStats(File file) throws Exception {
+        if (file.isDirectory()) {
+            // comparing the directory
+            return getStatsFromLucene(file);
+        } else {
+            // reading a pre-existing (json) file that contains
+            // the statistics
+            return new String(Files.readAllBytes(file.toPath()), 
StandardCharsets.UTF_8);

Review Comment:
   this can be replaced with 
   
   ```suggestion
               return Files.readString(file.toPath());
   ```



##########
oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/tools/IndexComparator.java:
##########
@@ -0,0 +1,247 @@
+/* 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.index.lucene.tools;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Function;
+
+import org.apache.jackrabbit.oak.commons.json.JsonObject;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.plugins.index.lucene.directory.LocalIndexDir;
+import 
org.apache.jackrabbit.oak.plugins.index.lucene.reader.DefaultIndexReader;
+import org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader;
+import org.apache.lucene.index.Fields;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.store.SimpleFSDirectory;
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * Tool to compare Lucene indexes.
+ * Indexes are considered equal if everything except the following is equal:
+ * - checksums of .si, .cfe, .cfs files
+ * - size of .si, .cfe, .cfs files (up to 1000 bytes or 0.1% difference is 
acceptable)
+ */
+public class IndexComparator {
+
+    public static void main(String... args) throws Exception {
+        if (args.length < 1) {
+            System.out.println("Usage: java " + 
IndexComparator.class.getName() +
+                    " <lucene index directory> [<second lucene index 
directory>]");
+            System.out.println(
+                    "If only one directory is specified, then the output is 
json " +
+                    "that contains the statistics of the index.");
+            System.out.println(
+                    "Instead of a directory, each argument can be a json file 
" +
+                    "that contains the statistics of a previous run.");
+            return;
+        }
+        String stats1 = getStats(new File(args[0]));
+        if (args.length > 1) {
+            String stats2 = getStats(new File(args[1]));
+            System.out.println(diff(stats1, stats2));
+        } else {
+            System.out.println(stats1);
+        }
+    }
+
+    private static String diff(String stats1, String stats2) {
+        JsonObject o1 = JsonObject.fromJson(stats1, true);
+        JsonObject o2 = JsonObject.fromJson(stats2, true);
+        o1.getProperties().remove("directory");
+        o2.getProperties().remove("directory");
+        JsopBuilder result = new JsopBuilder();
+        result.object();
+        diff(o1, o2, "", result);
+        result.endObject();
+        if (result.toString().equals("{}")) {
+            return "";
+        }
+        return JsopBuilder.prettyPrint(result.toString());
+    }
+
+    private static void diff(JsonObject o1, JsonObject o2, String path, 
JsopBuilder result) {
+        boolean ignoreChecksums = false;
+        if (path.startsWith("/files/")) {
+            if (path.endsWith("index-details.txt")) {
+                // ignore index details
+                ignoreChecksums = true;
+            } else if (path.endsWith(".si")) {
+                // ".si" files contain a timestamp
+                ignoreChecksums = true;
+            } else if (path.endsWith(".cfs")
+                    || path.endsWith(".cfe")) {
+                // ".cfs" and ".cfe files depends on
+                // the aggregation order
+                ignoreChecksums = true;
+            }
+        }
+        Set<String> both = new HashSet<>(o1.getProperties().keySet());
+        both.addAll(o2.getProperties().keySet());
+        for (String k : both) {
+            String v1 = o1.getProperties().get(k);
+            String v2 = o2.getProperties().get(k);
+            if (v1 == null || v2 == null || !v1.equals(v2)) {
+                if (ignoreChecksums) {
+                    if ("sha256".equals(k) || "md5".equals(k)) {
+                        continue;
+                    }
+                    if ("bytes".equals(k) && v1 != null && v2 != null) {
+                        long x1 = Long.parseLong(v1);
+                        long x2 = Long.parseLong(v2);
+                        if (Math.abs(x1 - x2) < 1000) {
+                            // less than 1000 bytes difference
+                            // if aggregation order is different
+                            continue;
+                        } else if (Math.abs((double) x1 / x2 - 1.0) < 0.001) {
+                            // less than 0.1% difference
+                            // if aggregation order is different
+                            continue;
+                        }
+                    }
+                }
+                result.key(path + "/" + k).object().
+                    key("v1").encodedValue(v1).
+                    key("v2").encodedValue(v2).
+                    endObject();
+            }
+        }
+        both = new HashSet<>(o1.getChildren().keySet());
+        both.addAll(o2.getChildren().keySet());
+        for (String k : both) {
+            JsonObject v1 = o1.getChildren().get(k);
+            JsonObject v2 = o2.getChildren().get(k);
+            if (v1 == null || v2 == null) {
+                result.key(path + "/" + k).object().
+                    key("v1").encodedValue("" + v1).
+                    key("v2").encodedValue("" + v2).
+                    endObject();
+            } else {
+                diff(v1, v2, path + "/" + k, result);
+            }
+        }
+    }
+
+    /**
+     * Create or load a string that contains the JSON representation of the
+     * statistics of an index.
+     *
+     * @param file the Lucene directory, or JSON file
+     * @return the statistics in JSON form
+     */
+    private static String getStats(File file) throws Exception {
+        if (file.isDirectory()) {
+            // comparing the directory
+            return getStatsFromLucene(file);
+        } else {
+            // reading a pre-existing (json) file that contains
+            // the statistics
+            return new String(Files.readAllBytes(file.toPath()), 
StandardCharsets.UTF_8);
+        }
+    }
+
+    private static String getStatsFromLucene(File directory) throws Exception {
+        LocalIndexDir dir = new LocalIndexDir(directory);
+        JsopBuilder builder = new JsopBuilder();
+        builder.object();
+        builder.key("directory").value(directory.getAbsolutePath());
+        builder.key("files").object();
+        fileStats(directory, builder);
+        builder.endObject();
+        builder.key("luceneIndex").object();
+        builder.key("jcrPath").value(dir.getJcrPath());
+        try (SimpleFSDirectory luceneDir = new SimpleFSDirectory(new 
File(directory, "data"))) {
+            try (LuceneIndexReader luceneReader = new 
DefaultIndexReader(luceneDir, null, null)) {
+                IndexReader reader = luceneReader.getReader();
+                builder.key("numDocs").value(reader.numDocs());
+                builder.key("maxDoc").value(reader.maxDoc());
+                builder.key("numDeletedDocs").value(reader.numDeletedDocs());
+                Fields fields = MultiFields.getFields(reader);
+                if (fields != null) {
+                    builder.key("fields").object();
+                    for (String f : fields) {
+                        builder.key(f).object();
+                        builder.key("docCount").value(reader.getDocCount(f));
+                        Terms terms = MultiFields.getTerms(reader, f);
+                        TermsEnum iterator = terms.iterator(null);
+                        BytesRef byteRef = null;

Review Comment:
   unnecessary
   
   ```suggestion
                           BytesRef byteRef;
   ```



##########
oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/tools/IndexComparator.java:
##########
@@ -0,0 +1,247 @@
+/* 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.index.lucene.tools;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Function;
+
+import org.apache.jackrabbit.oak.commons.json.JsonObject;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.plugins.index.lucene.directory.LocalIndexDir;
+import 
org.apache.jackrabbit.oak.plugins.index.lucene.reader.DefaultIndexReader;
+import org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader;
+import org.apache.lucene.index.Fields;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.store.SimpleFSDirectory;
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * Tool to compare Lucene indexes.
+ * Indexes are considered equal if everything except the following is equal:
+ * - checksums of .si, .cfe, .cfs files
+ * - size of .si, .cfe, .cfs files (up to 1000 bytes or 0.1% difference is 
acceptable)
+ */
+public class IndexComparator {
+
+    public static void main(String... args) throws Exception {
+        if (args.length < 1) {
+            System.out.println("Usage: java " + 
IndexComparator.class.getName() +
+                    " <lucene index directory> [<second lucene index 
directory>]");
+            System.out.println(
+                    "If only one directory is specified, then the output is 
json " +
+                    "that contains the statistics of the index.");
+            System.out.println(
+                    "Instead of a directory, each argument can be a json file 
" +
+                    "that contains the statistics of a previous run.");
+            return;
+        }
+        String stats1 = getStats(new File(args[0]));
+        if (args.length > 1) {
+            String stats2 = getStats(new File(args[1]));
+            System.out.println(diff(stats1, stats2));
+        } else {
+            System.out.println(stats1);
+        }
+    }
+
+    private static String diff(String stats1, String stats2) {
+        JsonObject o1 = JsonObject.fromJson(stats1, true);
+        JsonObject o2 = JsonObject.fromJson(stats2, true);
+        o1.getProperties().remove("directory");
+        o2.getProperties().remove("directory");
+        JsopBuilder result = new JsopBuilder();
+        result.object();
+        diff(o1, o2, "", result);
+        result.endObject();
+        if (result.toString().equals("{}")) {
+            return "";
+        }
+        return JsopBuilder.prettyPrint(result.toString());
+    }
+
+    private static void diff(JsonObject o1, JsonObject o2, String path, 
JsopBuilder result) {
+        boolean ignoreChecksums = false;
+        if (path.startsWith("/files/")) {
+            if (path.endsWith("index-details.txt")) {
+                // ignore index details
+                ignoreChecksums = true;
+            } else if (path.endsWith(".si")) {
+                // ".si" files contain a timestamp
+                ignoreChecksums = true;
+            } else if (path.endsWith(".cfs")
+                    || path.endsWith(".cfe")) {
+                // ".cfs" and ".cfe files depends on
+                // the aggregation order
+                ignoreChecksums = true;
+            }
+        }
+        Set<String> both = new HashSet<>(o1.getProperties().keySet());
+        both.addAll(o2.getProperties().keySet());
+        for (String k : both) {
+            String v1 = o1.getProperties().get(k);
+            String v2 = o2.getProperties().get(k);
+            if (v1 == null || v2 == null || !v1.equals(v2)) {

Review Comment:
   this can be simplified
   
   ```suggestion
               if (v1 == null || !v1.equals(v2)) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to