You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by GitBox <gi...@apache.org> on 2021/01/28 16:28:19 UTC

[GitHub] [jackrabbit-oak] thomasmueller commented on a change in pull request #265: OAK-9325 Tool to compare index definitions

thomasmueller commented on a change in pull request #265:
URL: https://github.com/apache/jackrabbit-oak/pull/265#discussion_r566231204



##########
File path: oak-run/src/main/java/org/apache/jackrabbit/oak/index/merge/IndexDiff.java
##########
@@ -0,0 +1,434 @@
+/*
+ * 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.merge;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import org.apache.jackrabbit.oak.commons.json.JsonObject;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.plugins.index.search.spi.query.IndexName;
+
+/**
+ * The index diff tools allows to compare and merge indexes
+ */
+public class IndexDiff {
+
+    static JsonObject extract(String extractFile, String indexName) {
+        JsonObject indexDefs = parseIndexDefinitions(extractFile);
+        JsonObject index = indexDefs.getChildren().get(indexName);
+        removeUninterestingIndexProperties(indexDefs);
+        simplifyForDisplay(index);
+        return index;
+    }
+
+    public static void extractAll(String extractFile, String extractTargetDirectory) {
+        new File(extractTargetDirectory).mkdirs();
+        JsonObject indexDefs = parseIndexDefinitions(extractFile);
+        removeUninterestingIndexProperties(indexDefs);
+        sortPropertiesByName(indexDefs);
+        for (String child : indexDefs.getChildren().keySet()) {
+            JsonObject index = indexDefs.getChildren().get(child);
+            simplifyForDisplay(index);
+            String fileName = child.replaceAll("/oak:index/", "");
+            fileName = fileName.replace(':', '-');
+            Path p = Paths.get(extractTargetDirectory, fileName + ".json");
+            try {
+                Files.write(p, index.toString().getBytes());
+            } catch (IOException e) {
+                throw new IllegalStateException("Error writing file: " + p, e);
+            }
+        }
+    }
+
+    static JsonObject collectCustomizations(String directory) {
+        Path indexPath = Paths.get(directory);
+        JsonObject target = new JsonObject(true);
+        collectCustomizationsInDirectory(indexPath, target);
+        return target;
+    }
+
+    static JsonObject mergeIndexes(String directory, String newIndexFile) {
+        JsonObject newIndex = null;
+        if (newIndexFile != null && !newIndexFile.isEmpty()) {
+            newIndex = parseIndexDefinitions(newIndexFile);
+        }
+        Path indexPath = Paths.get(directory);
+        JsonObject target = new JsonObject(true);
+        mergeIndexesInDirectory(indexPath, newIndex, target);
+        return target;
+    }
+
+    static JsonObject compareIndexes(String directory, String index1, String index2) {
+        Path indexPath = Paths.get(directory);
+        JsonObject target = new JsonObject(true);
+        compareIndexesIndexesInDirectory(indexPath, index1, index2, target);
+        return target;
+    }
+
+    private static Stream<Path> indexFiles(Path indexPath) {
+        try {
+            return Files.walk(indexPath).
+            filter(path -> Files.isRegularFile(path)).
+            filter(path -> path.toString().endsWith(".json")).
+            filter(path -> !path.toString().endsWith("allnamespaces.json")).
+            filter(path -> !path.toString().endsWith("-info.json")).
+            filter(path -> !path.toString().endsWith("-stats.json"));
+        } catch (IOException e) {
+            throw new IllegalArgumentException("Error reading from " + indexPath, e);
+        }
+    }
+
+    private static void sortPropertiesByName(JsonObject obj) {
+        ArrayList<String> props = new ArrayList<>(obj.getProperties().keySet());
+        if (!props.isEmpty()) {
+            props.sort(null);
+            for(String key : props) {
+                String value = obj.getProperties().remove(key);
+                obj.getProperties().put(key, value);
+            }
+        }
+        for(String child : obj.getChildren().keySet()) {
+            JsonObject c = obj.getChildren().get(child);
+            sortPropertiesByName(c);
+        }
+    }
+
+    private static void compareIndexesIndexesInDirectory(Path indexPath, String index1, String index2,
+            JsonObject target) {
+        if (Files.isExecutable(indexPath)) {
+            indexFiles(indexPath).forEach(path -> {
+                JsonObject indexDefinitions = IndexDiff.parseIndexDefinitions(path.toString());
+                compareIndexes(indexDefinitions, indexPath.toString(), path.toString(), index1, index2, target);
+            });
+        } else {
+            JsonObject allIndexDefinitions = IndexDiff.parseIndexDefinitions(indexPath.toString());
+            for(String key : allIndexDefinitions.getChildren().keySet()) {
+                JsonObject indexDefinitions = allIndexDefinitions.getChildren().get(key);
+                compareIndexes(indexDefinitions, "", key, index1, index2, target);
+            }
+        }
+    }
+
+    private static void collectCustomizationsInDirectory(Path indexPath, JsonObject target) {
+        indexFiles(indexPath).forEach(path -> {
+            JsonObject indexDefinitions = IndexDiff.parseIndexDefinitions(path.toString());
+            showCustomIndexes(indexDefinitions, indexPath.toString(), path.toString(), target);
+        });
+    }
+
+    private static void mergeIndexesInDirectory(Path indexPath, JsonObject newIndex, JsonObject target) {
+        indexFiles(indexPath).forEach(path -> {
+            JsonObject indexDefinitions = IndexDiff.parseIndexDefinitions(path.toString());
+            mergeIndexes(indexDefinitions, indexPath.toString(), path.toString(), newIndex, target);
+        });
+    }
+
+    private static void mergeIndexes(JsonObject indexeDefinitions, String basePath, String fileName, JsonObject newIndexes, JsonObject target) {
+        JsonObject targetFile = new JsonObject(true);
+        if (newIndexes != null) {
+            for (String newIndexKey : newIndexes.getChildren().keySet()) {
+                if (indexeDefinitions.getChildren().containsKey(newIndexKey)) {
+                    targetFile.getProperties().put(newIndexKey, JsopBuilder.encode("WARNING: already exists"));
+                }
+            }
+        } else {
+            newIndexes = new JsonObject(true);
+        }
+        // the superseded indexes of the old repository
+        List<String> supersededKeys = new ArrayList<>(IndexMerge.getSupersededIndexDefs(indexeDefinitions));
+        Collections.sort(supersededKeys);
+
+        // keep only new indexes that are not superseded
+        Map<String, JsonObject> indexMap = indexeDefinitions.getChildren();
+        for (String superseded : supersededKeys) {
+            if (indexMap.containsKey(superseded)) {
+                indexMap.remove(superseded);
+            }

Review comment:
       +1




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

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