public static String of(MetadataPack.Members metadata) { ComputeHash.HashConstructor ctor = new ComputeHash.HashConstructor(new ComputeHash.DigestComputingWriter( ComputeHash.newDigest())); ComputeHash.HashableView metadataView = ComputeHash.HashableView.of(metadata); SortedSet<Entity.Blueprint> all = new TreeSet<>(ComputeHash.BLUEPRINT_COMPARATOR); all.addAll(metadata.getMetricTypes()); all.addAll(metadata.getResourceTypes()); StringBuilder result = new StringBuilder(); for (Entity.Blueprint bl : all) { //identity hash not dependent on relative paths (if any) in the data, so null for entityPath is ok. ComputeHash.IntermediateHashResult res = ComputeHash.computeHash(null, bl, metadataView, ctor, true, false, false, rp -> null, rp -> null); result.append(res.identityHash); } ComputeHash.DigestComputingWriter digestor = ctor.getDigestor(); digestor.reset(); digestor.append(result); digestor.close(); return digestor.digest(); }
static Hashes of(InventoryStructure<?> inventory, CanonicalPath rootPath, boolean computeIdentity, boolean computeContent, boolean computeSync) { ComputeHash.HashConstructor ctor = new ComputeHash.HashConstructor(new ComputeHash.DigestComputingWriter( ComputeHash.newDigest())); IntermediateHashResult res = ComputeHash.computeHash(rootPath, inventory.getRoot(), ComputeHash.HashableView.of(inventory), ctor, computeIdentity, computeContent, computeSync, rp -> null, rp -> null); return new Hashes(res.identityHash, res.contentHash, res.syncHash); }
public static String of(Iterator<? extends Entity<? extends Entity.Blueprint, ?>> entities, Inventory inventory) { SortedSet<Entity<? extends Entity.Blueprint, ?>> sortedEntities = new TreeSet<>(ComputeHash.ENTITY_COMPARATOR); entities.forEachRemaining(sortedEntities::add); ComputeHash.HashConstructor ctor = new ComputeHash.HashConstructor(new ComputeHash.DigestComputingWriter( ComputeHash.newDigest())); StringBuilder resultHash = new StringBuilder(); sortedEntities.forEach((e) -> { InventoryStructure<?> structure = InventoryStructure.of(e, inventory); ComputeHash.HashableView v = ComputeHash.HashableView.of(structure); ComputeHash.IntermediateHashResult res = ComputeHash .computeHash(e.getPath(), structure.getRoot(), v, ctor, true, false, false, rp -> null, rp -> null); resultHash.append(res.identityHash); }); ctor.getDigestor().reset(); ctor.getDigestor().append(resultHash); ctor.getDigestor().close(); return ctor.getDigestor().digest(); }
static IntermediateHashResult treeOf(InventoryStructure<?> inventory, CanonicalPath rootPath, boolean computeIdentity, boolean computeContent, boolean computeSync, Consumer<IntermediateHashContext> onStartChild, BiConsumer<IntermediateHashContext, IntermediateHashResult> onEndChild, Function<RelativePath, Hashes> hashLoader) { ComputeHash.DigestComputingWriter wrt = new ComputeHash.DigestComputingWriter(ComputeHash.newDigest()); ComputeHash.HashConstructor ctor = new ComputeHash.HashConstructor(wrt) { @Override public void startChild(ComputeHash.IntermediateHashContext context) { super.startChild(context); onStartChild.accept(context); } @Override public void endChild(ComputeHash.IntermediateHashContext ctx, ComputeHash.IntermediateHashResult result) { super.endChild(ctx, result); onEndChild.accept(ctx, result); } }; return computeHash(rootPath, inventory.getRoot(), ComputeHash.HashableView.of(inventory), ctor, computeIdentity, computeContent, computeSync, //we don't want the root element in the relative paths of the children so that they are easily //appendable to the root. (rp) -> rp.slide(1, 0), hashLoader ); }