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


##########
oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/tools/IndexComparator.java:
##########
@@ -0,0 +1,230 @@
+/* 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>]");
+            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")) {
+                // ".cfs" files depends on the aggregation order
+                ignoreChecksums = true;
+            } else if (path.endsWith(".cfe")) {
+                // ".cfs" files depends on the aggregation order
+                ignoreChecksums = true;
+            }

Review Comment:
   The comments are different, and I would like to preserve this.



-- 
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