static void appendCommonContent(Entity.Blueprint b, IntermediateHashContext ctx) { appendContent(b.getName(), ctx); appendContent(b.getProperties(), ctx); }
public static Hashes of(InventoryStructure<?> root, CanonicalPath rootPath) { return ComputeHash.of(root, rootPath, true, true, true); }
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 Tree treeOf(InventoryStructure<?> inventory) { @SuppressWarnings("unchecked") Tree.AbstractBuilder<?>[] tbld = new Tree.AbstractBuilder[1]; Consumer<ComputeHash.IntermediateHashContext> startChild = context -> { if (tbld[0] == null) { tbld[0] = Tree.builder(); } else { tbld[0] = tbld[0].startChild(); } }; BiConsumer<ComputeHash.IntermediateHashContext, ComputeHash.IntermediateHashResult> endChild = (ctx, result) -> { if (tbld[0] instanceof Tree.ChildBuilder) { tbld[0].withHash(result.identityHash).withPath(result.path); @SuppressWarnings("unchecked") Tree.AbstractBuilder<?> parent = ((Tree.ChildBuilder<?>) tbld[0]).getParent(); parent.addChild(((Tree.ChildBuilder<?>) tbld[0]).build()); tbld[0] = parent; } }; //identity hash is not computed using any relative paths, so we can pass null to the root path of the //computation. ComputeHash.IntermediateHashResult res = ComputeHash.treeOf(inventory, null, true, false, false, startChild, endChild, p -> null); tbld[0].withPath(res.path).withHash(res.identityHash); return ((Tree.Builder)tbld[0]).build(); }
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(); }
.treeOf(structure, rootPath, true, true, true, startChild, endChild, hashLoader);
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 ); }
public static String of(InventoryStructure<?> inventory) { //identity hash is not dependent on the root path - I.e. no relative paths in the inventory structure are used //to compute the identity hash... return ComputeHash.of(inventory, null, true, false, false).getIdentityHash(); }
public static Tree treeOf(InventoryStructure<?> root, CanonicalPath rootPath, Function<RelativePath, Hashes> hashLoader) { Tree.AbstractBuilder<?>[] tbld = new Tree.AbstractBuilder[1]; Consumer<IntermediateHashContext> startChild = context -> { if (tbld[0] == null) { tbld[0] = Tree.builder(); } else { tbld[0] = tbld[0].startChild(); } }; BiConsumer<IntermediateHashContext, IntermediateHashResult> endChild = (ctx, result) -> { if (tbld[0] instanceof Tree.ChildBuilder) { tbld[0].withHash(new Hashes(result)).withPath(result.path); @SuppressWarnings("unchecked") Tree.AbstractBuilder<?> parent = ((Tree.ChildBuilder<?>) tbld[0]).getParent(); parent.addChild(((Tree.ChildBuilder<?>) tbld[0]).build()); tbld[0] = parent; } }; IntermediateHashResult res = ComputeHash.treeOf(root, rootPath, true, true, true, startChild, endChild, hashLoader); tbld[0].withPath(res.path).withHash(new Hashes(res)); return ((Tree.Builder)tbld[0]).build(); }
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(); }
/** * The root path is required so that paths in the blueprints can be correctly relativized. * * @param structure the structure to compute the root hash of. * @param rootPath the canonical path of the root of the inventory structure * @return the sync hash of the root of the structure */ public static String of(InventoryStructure<?> structure, CanonicalPath rootPath) { return ComputeHash.of(structure, rootPath, true, true, true).getSyncHash(); }
/** * The entity path is currently only required for resources, which base their content hash on the resource type * path which can be specified using a relative or canonical path in the blueprint. For the content hash to be * consistent we need to convert to just a single representation of the path. * * @param entity the entity to produce the hash of * @param entityPath the canonical path of the entity (can be null for all but resources) * @return the content hash of the entity */ public static String of(Entity.Blueprint entity, CanonicalPath entityPath) { return ComputeHash.of(InventoryStructure.of(entity).build(), entityPath, false, true, false).getContentHash(); } }