Skip to content

Commit

Permalink
OAK-9723 Allow comparing index definitions against a file
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmueller committed Mar 11, 2022
1 parent 24c54e5 commit dece60c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,17 @@ private static JsonObject addParent(String key, JsonObject obj) {
static JsonObject compareIndexes(String directory, String index1, String index2) {
Path indexPath = Paths.get(directory);
JsonObject target = new JsonObject(true);
compareIndexesIndexesInDirectory(indexPath, index1, index2, target);
compareIndexesInDirectory(indexPath, index1, index2, target);
return target;
}

public static JsonObject compareIndexesAgainstBase(String directory, String indexBaseFile) {
Path indexPath = Paths.get(directory);
JsonObject baseIndexes = parseIndexDefinitions(indexBaseFile);
removeUninterestingIndexProperties(baseIndexes);
simplify(baseIndexes, true);
JsonObject target = new JsonObject(true);
compareIndexesInDirectory(indexPath, baseIndexes, target);
return target;
}

Expand Down Expand Up @@ -224,9 +234,9 @@ private static void sortPropertiesByName(JsonObject obj) {
}
}

private static void compareIndexesIndexesInDirectory(Path indexPath, String index1, String index2,
private static void compareIndexesInDirectory(Path indexPath, String index1, String index2,
JsonObject target) {
if (Files.isExecutable(indexPath)) {
if (Files.isDirectory(indexPath)) {
indexFiles(indexPath).forEach(path -> {
JsonObject indexDefinitions = IndexDiff.parseIndexDefinitions(path.toString());
compareIndexes(indexDefinitions, indexPath.toString(), path.toString(), index1, index2, target);
Expand All @@ -240,6 +250,19 @@ private static void compareIndexesIndexesInDirectory(Path indexPath, String inde
}
}

private static void compareIndexesInDirectory(Path indexPath, JsonObject baseIndexes, JsonObject target) {
if (Files.isDirectory(indexPath)) {
indexFiles(indexPath).forEach(path -> {
JsonObject indexDefinitions = IndexDiff.parseIndexDefinitions(path.toString());
removeUninterestingIndexProperties(indexDefinitions);
simplify(indexDefinitions, true);
compareIndexes(indexDefinitions, indexPath.toString(), path.toString(), baseIndexes, target);
});
} else {
throw new IllegalArgumentException("Not a directory: " + indexPath);
}
}

private static void collectCustomizationsInDirectory(Path indexPath, JsonObject target) {
indexFiles(indexPath).forEach(path -> {
JsonObject indexDefinitions = IndexDiff.parseIndexDefinitions(path.toString());
Expand Down Expand Up @@ -332,6 +355,23 @@ private static void compareIndexes(JsonObject indexDefinitions, String basePath,
addIfNotEmpty(basePath, fileName, targetFile, target);
}

private static void compareIndexes(JsonObject indexDefinitions, String basePath, String fileName,
JsonObject baseIndexes, JsonObject target) {
JsonObject targetFile = new JsonObject(true);
for (String indexName : baseIndexes.getChildren().keySet()) {
JsonObject baseIndex = baseIndexes.getChildren().get(indexName);
JsonObject compareIndex = indexDefinitions.getChildren().get(indexName);
if (compareIndex != null) {
JsonObject targetIndex = new JsonObject(true);
compareIndexes("", baseIndex, compareIndex, targetIndex);
if (!targetIndex.getChildren().isEmpty()) {
targetFile.getChildren().put(indexName, targetIndex);
}
}
}
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());
Expand Down Expand Up @@ -487,16 +527,22 @@ private static void removeUninterestingIndexProperties(JsonObject indexDefinitio
}

private static void simplify(JsonObject json) {
simplify(json, false);
}

private static void simplify(JsonObject json, boolean thoroughly) {
for(String k : json.getChildren().keySet()) {
JsonObject child = json.getChildren().get(k);
simplify(child);
simplify(child, thoroughly);

// the following properties are not strictly needed for display,
// but we keep them to avoid issues with validation
// child.getProperties().remove("jcr:created");
// child.getProperties().remove("jcr:createdBy");
// child.getProperties().remove("jcr:lastModified");
// child.getProperties().remove("jcr:lastModifiedBy");
// but we keep them by default, to avoid issues with validation
if (thoroughly) {
child.getProperties().remove("jcr:created");
child.getProperties().remove("jcr:createdBy");
child.getProperties().remove("jcr:lastModified");
child.getProperties().remove("jcr:lastModifiedBy");
}

// the UUID we remove, because duplicate UUIDs are not allowed
child.getProperties().remove("jcr:uuid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ public void execute(String... args) throws Exception {
.defaultsTo("");
OptionSpec<String> compareDirectoryOption = parser
.accepts("compare", "Path to index definition files (.json). " +
"Compare index1 and index2").withOptionalArg()
"Compare index1 and index2, or all indexes there to the indexBase file").withOptionalArg()
.defaultsTo("");
OptionSpec<String> index1Option = parser
.accepts("index1", "Index 1").withOptionalArg()
.defaultsTo("");
OptionSpec<String> index2Option = parser
.accepts("index2", "Index 2").withOptionalArg()
.defaultsTo("");
OptionSpec<String> indexBaseOption = parser
.accepts("indexBase", "Index base file").withOptionalArg()
.defaultsTo("");
OptionSpec<String> extractFileOption = parser
.accepts("extract", "File from where to extract (an) index definition(s) (.json). " +
"This will extract index1, or all indexes").withOptionalArg()
Expand Down Expand Up @@ -106,14 +109,22 @@ public void execute(String... args) throws Exception {
if (!compareDirectory.isEmpty()) {
String index1 = index1Option.value(options);
String index2 = index2Option.value(options);
if (index1.isEmpty() || index2.isEmpty()) {
parser.printHelpOn(System.out);
return;
String indexBaseFile = indexBaseOption.value(options);
if (!indexBaseFile.isEmpty()) {
System.out.println("Comparing indexes in " + indexBaseFile + " against all indexes in " +
" directory \"" +
compareDirectory + "\"");
System.out.println(IndexDiff.compareIndexesAgainstBase(compareDirectory, indexBaseFile));
} else {
if (index1.isEmpty() || index2.isEmpty()) {
parser.printHelpOn(System.out);
return;
}
System.out.println("Comparing indexes " + index1 + " and " + index2 +
" for directory \"" +
compareDirectory + "\"");
System.out.println(IndexDiff.compareIndexes(compareDirectory, index1, index2));
}
System.out.println("Comparing indexes " + index1 + " and " + index2 +
" for directory \"" +
compareDirectory + "\"");
System.out.println(IndexDiff.compareIndexes(compareDirectory, index1, index2));
}
if (!extractFile.isEmpty()) {
String index1 = index1Option.value(options);
Expand Down

0 comments on commit dece60c

Please sign in to comment.