fabriziofortino commented on a change in pull request #265: URL: https://github.com/apache/jackrabbit-oak/pull/265#discussion_r564318272
########## 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); + } + } + Set<String> indexKeys = indexeDefinitions.getChildren().keySet(); + try { + IndexDefMergerUtils.merge(newIndexes, indexeDefinitions); + Set<String> newIndexKeys = new HashSet<>(newIndexes.getChildren().keySet()); + newIndexKeys.removeAll(indexKeys); + if (newIndexKeys.isEmpty()) { + // No indexes to merge + } Review comment: this `if` is empty. Is this needed? ########## 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: the `containsKey` check could be removed ```suggestion indexMap.remove(superseded); ``` ########## 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/", ""); Review comment: `"/oak:index/"` is widely used. I would create a constant. ########## 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); + } + } + Set<String> indexKeys = indexeDefinitions.getChildren().keySet(); + try { + IndexDefMergerUtils.merge(newIndexes, indexeDefinitions); + Set<String> newIndexKeys = new HashSet<>(newIndexes.getChildren().keySet()); + newIndexKeys.removeAll(indexKeys); + if (newIndexKeys.isEmpty()) { + // No indexes to merge + } + for (String newIndexKey : newIndexKeys) { + JsonObject merged = newIndexes.getChildren().get(newIndexKey); + if (merged != null) { + targetFile.getChildren().put(newIndexKey, merged); + } + } + } catch (UnsupportedOperationException e) { + e.printStackTrace(); + targetFile.getProperties().put("failed", JsopBuilder.encode(e.toString())); + } + addIfNotEmpty(basePath, fileName, targetFile, target); + } + + private static void addIfNotEmpty(String basePath, String fileName, JsonObject targetFile, JsonObject target) { + if (!targetFile.getProperties().isEmpty() || !targetFile.getChildren().isEmpty()) { + String f = fileName; + if (f.startsWith(basePath)) { + f = f.substring(basePath.length()); + } + target.getChildren().put(f, targetFile); + } + } + + private static void showCustomIndexes(JsonObject indexDefinitions, String basePath, String fileName, JsonObject target) { + JsonObject targetFile = new JsonObject(true); + processAndRemoveIllegalIndexNames(indexDefinitions, targetFile); + removeUninterestingIndex(indexDefinitions); + removeUninterestingIndexProperties(indexDefinitions); + removeUnusedIndexes(indexDefinitions); + for(String k : indexDefinitions.getChildren().keySet()) { + if (!k.startsWith("/oak:index/")) { + targetFile.getProperties().put(k, JsopBuilder.encode("WARNING: Index not under /oak:index/")); + continue; + } + if (!k.contains("-custom-")) { + continue; + } + listNewAndCustomizedIndexes(indexDefinitions, k, targetFile); + } + addIfNotEmpty(basePath, fileName, targetFile, target); + } + + private static void compareIndexes(JsonObject indexDefinitions, String basePath, String fileName, String index1, String index2, JsonObject target) { + JsonObject targetFile = new JsonObject(true); + JsonObject i1 = indexDefinitions.getChildren().get(index1); + JsonObject i2 = indexDefinitions.getChildren().get(index2); + if (i1 != null && i2 != null) { + compareIndexes("", i1, i2, targetFile); + } + addIfNotEmpty(basePath, fileName, targetFile, target); + } + + private static void listNewAndCustomizedIndexes(JsonObject indexDefinitions, String indexNodeName, JsonObject target) { + JsonObject index = indexDefinitions.getChildren().get(indexNodeName); + String nodeName = indexNodeName.substring("/oak:index/".length()); + IndexName indexName = IndexName.parse(nodeName); + String ootb = indexName.getBaseName(); + if (indexName.getProductVersion() > 1) { + ootb += "-" + indexName.getProductVersion(); + } + simplifyForDisplay(indexDefinitions); + JsonObject ootbIndex = indexDefinitions.getChildren().get("/oak:index/" + ootb); + if (ootbIndex != null) { + JsonObject targetCustom = new JsonObject(true); + targetCustom.getProperties().put("customizes", JsopBuilder.encode("/oak:index/" + ootb)); + target.getChildren().put(indexNodeName, targetCustom); + compareIndexes("", ootbIndex, index, targetCustom); + } else { + target.getProperties().put(indexNodeName, JsopBuilder.encode("new")); + } + } + + private static void processAndRemoveIllegalIndexNames(JsonObject indexDefinitions, JsonObject target) { + Set<String> indexes = new HashSet<>(indexDefinitions.getChildren().keySet()); + for(String k : indexes) { + if (!k.startsWith("/oak:index/")) { + continue; + } + String nodeName = k.substring("/oak:index/".length()); + IndexName indexName = IndexName.parse(nodeName); + if (!indexName.isLegal()) { + target.getProperties().put(k, JsopBuilder.encode("WARNING: Invalid name")); + indexDefinitions.getChildren().remove(k); + } + } + } + + private static void removeUninterestingIndex(JsonObject indexDefinitions) { + } + + private static void compareIndexes(String path, JsonObject ootb, JsonObject custom, JsonObject target) { + LinkedHashMap<String, Boolean> properties = new LinkedHashMap<>(); + addAllProperties(ootb, properties); + addAllProperties(custom, properties); + for (String k : properties.keySet()) { + String op = ootb.getProperties().get(k); + String cp = custom.getProperties().get(k); + if (!Objects.equals(op, cp)) { + JsonObject change = new JsonObject(true); + if (op != null) { + change.getProperties().put("old", op); + } + if (cp != null) { + change.getProperties().put("new", cp); + } + target.getChildren().put(path + k, change); + } + } + LinkedHashMap<String, Boolean> children = new LinkedHashMap<>(); + addAllChildren(ootb, children); + addAllChildren(custom, children); + for (String k : children.keySet()) { + JsonObject oc = ootb.getChildren().get(k); + JsonObject cc = custom.getChildren().get(k); + if (!isSameJson(oc, cc)) { + if (oc == null) { + target.getProperties().put(path + k, JsopBuilder.encode("added")); + } else if (cc == null) { + target.getProperties().put(path + k, JsopBuilder.encode("removed")); + } else { + compareIndexes(path + k + "/", oc, cc, target); + } + } + } + compareOrder(path, ootb, custom, target); + } + + private static void addAllChildren(JsonObject source, LinkedHashMap<String, Boolean> target) { + for(String k : source.getChildren().keySet()) { + target.put(k, true); + } + } + + private static void compareOrder(String path, JsonObject ootb, JsonObject custom, JsonObject target) { + // list of entries, sorted by how they appear in the ootb case + ArrayList<String> bothSortedByOotb = new ArrayList<>(); + for(String k : ootb.getChildren().keySet()) { + if (custom.getChildren().containsKey(k)) { + bothSortedByOotb.add(k); + } + } + // list of entries, sorted by how they appear in the custom case + ArrayList<String> bothSortedByCustom = new ArrayList<>(); + for(String k : custom.getChildren().keySet()) { + if (ootb.getChildren().containsKey(k)) { + bothSortedByCustom.add(k); + } + } + if (!bothSortedByOotb.toString().equals(bothSortedByCustom.toString())) { + JsonObject change = new JsonObject(true); + change.getProperties().put("warning", JsopBuilder.encode("WARNING: order is different")); + change.getProperties().put("ootb", JsopBuilder.encode(bothSortedByOotb.toString())); + change.getProperties().put("custom", JsopBuilder.encode(bothSortedByCustom.toString())); + target.getChildren().put(path, change); + } + } + + private static boolean isSameJson(JsonObject a, JsonObject b) { + if (a == null || b == null) { + return a == null && b == null; + } + return a.toString().equals(b.toString()); + } + + private static void addAllProperties(JsonObject source, LinkedHashMap<String, Boolean> target) { + for(String k : source.getProperties().keySet()) { + target.put(k, true); + } + } + + private static void removeUnusedIndexes(JsonObject indexDefinitions) { + HashMap<String, IndexName> latest = new HashMap<>(); + Set<String> indexes = new HashSet<>(indexDefinitions.getChildren().keySet()); + for(String k : indexes) { + if (!k.startsWith("/oak:index/")) { + continue; + } + String nodeName = k.substring("/oak:index/".length()); + IndexName indexName = IndexName.parse(nodeName); + String baseName = indexName.getBaseName(); + IndexName old = latest.get(baseName); + if (old == null) { + latest.put(baseName, indexName); + } else { + if (old.compareTo(indexName) < 0) { + if (old.getCustomerVersion() > 0) { + indexDefinitions.getChildren().remove("/oak:index/" + old.getNodeName()); + } + latest.put(baseName, indexName); + } else { + indexDefinitions.getChildren().remove("/oak:index/" + nodeName); + } + } + } + } + + private static void removeUninterestingIndexProperties(JsonObject indexDefinitions) { + for(String k : indexDefinitions.getChildren().keySet()) { + JsonObject indexDef = indexDefinitions.getChildren().get(k); + indexDef.getProperties().remove("reindexCount"); + indexDef.getProperties().remove("reindex"); + indexDef.getProperties().remove("seed"); + indexDef.getProperties().remove(":version"); + } + } + + private static void simplifyForDisplay(JsonObject json) { + for(String k : json.getChildren().keySet()) { + JsonObject child = json.getChildren().get(k); + simplifyForDisplay(child); + child.getProperties().remove("jcr:primaryType"); + child.getProperties().remove("jcr:created"); + child.getProperties().remove("jcr:createdBy"); + child.getProperties().remove("jcr:lastModified"); + child.getProperties().remove("jcr:uuid"); + for(String p : child.getProperties().keySet()) { + String v = child.getProperties().get(p); + if (v.startsWith("\"str:") || v.startsWith("\"nam:")) { + v = "\"" + v.substring(5); + child.getProperties().put(p, v); + } else if (v.startsWith("[") && v.contains("\"nam:")) { + v = v.replaceAll("\\[\"nam:", "\\[\""); + v = v.replaceAll(", \"nam:", ", \""); + child.getProperties().put(p, v); + } else if (v.startsWith("\":blobId:")) { + String base64 = v.substring(9, v.length() - 1); + try { + String clear = new String(java.util.Base64.getDecoder().decode(base64), "UTF-8"); + v = JsopBuilder.encode(clear); + child.getProperties().put(p, v); + } catch (UnsupportedEncodingException e) { + throw new IllegalArgumentException(e); + } Review comment: `StandardCharsets.UTF_8` can be used. No need to try-catch `UnsupportedEncodingException ` ```suggestion String clear = new String(java.util.Base64.getDecoder().decode(base64), StandardCharsets.UTF_8); v = JsopBuilder.encode(clear); child.getProperties().put(p, v); ``` ---------------------------------------------------------------- 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: [email protected]
