Skip to content

Commit

Permalink
chore: add code to compare metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Mandeep Singh committed Dec 12, 2024
1 parent 6016e97 commit d7a9b37
Showing 1 changed file with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.sort.ExternalSort;
import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
import org.apache.jackrabbit.oak.spi.state.NodeState;
Expand All @@ -17,13 +18,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

public class UUIDConflictDetector implements AutoCloseable {
Expand Down Expand Up @@ -200,6 +195,7 @@ private void compareUUIDs(File sourceFile, File targetFile) throws IOException {
String uuidWithPaths = sourceUUID + ": " + sourcePath + " " + targetPath;
conflictWriter.write(uuidWithPaths);
conflictWriter.newLine();
resolveConflict(sourcePath, targetPath);
}
sourceLine = sourceReader.readLine();
targetLine = targetReader.readLine();
Expand All @@ -209,6 +205,36 @@ private void compareUUIDs(File sourceFile, File targetFile) throws IOException {
log.info("uuid conflict comparison in {}, {} files completed in: {} ms", sourceFile.getName(), targetFile.getName(), System.currentTimeMillis() - startTime);
}

private void resolveConflict(String sourcePath, String targetPath) {
boolean isMetadataMatch = compareMetadata(sourcePath, targetPath);
if (isMetadataMatch) {
// proceed with Binary Comparison
} else {
log.info("metadata mismatch for source path: {}, target path: {}", sourcePath, targetPath);
}
}

private boolean compareMetadata(String sourcePath, String targetPath) {
Map<String, String> sourceMetadata = fetchMetadata(sourcePath, sourceStore);
Map<String, String> targetMetadata = fetchMetadata(targetPath, targetStore);
List<String> properties = Arrays.asList("jcr:primaryType", "jcr:mixinTypes", "dam:size", "dam:MIMEType", "dam:FileFormat");
return properties.stream().allMatch(property -> {
String sourceValue = sourceMetadata.get(property);
String targetValue = targetMetadata.get(property);
return StringUtils.equals(sourceValue, targetValue);
});
}

private Map<String, String> fetchMetadata(String path, NodeStore nodeStore) {
Map<String, String> metadata = new HashMap<>();
NodeState root = nodeStore.getRoot();
NodeState node = getNodeAtPath(root, path);
node.getProperties().forEach(property -> {
metadata.put(property.getName(), property.getValue(Type.NAME));
});
return metadata;
}

public long getTimeStamp() {
return timeStamp == 0L ? Instant.now().toEpochMilli() : timeStamp;
}
Expand All @@ -226,4 +252,4 @@ public void close() {
}
}
}
}
}

0 comments on commit d7a9b37

Please sign in to comment.