Sfoglia il codice sorgente

calculate hashes of deltas more efficiently

Joeri Exelmans 1 anno fa
parent
commit
748046ccee
2 ha cambiato i file con 15 aggiunte e 63 eliminazioni
  1. 3 51
      lib/delta.ts
  2. 12 12
      lib/delta_registry.ts

+ 3 - 51
lib/delta.ts

@@ -269,53 +269,6 @@ export abstract class Edge extends PrimitiveDelta {
   }
 }
 
-// // An Edge that does not yet have a target
-// export class NewEdge extends Edge {
-//   readonly after: readonly ReadAllOutgoing[];
-
-//   constructor(source: NodeCreation, label: string, after: readonly ReadAllOutgoing[]) {
-//     super(source, label);
-//     this.after = after;
-//   }
-
-//   getDependencies(): readonly [Delta,string][] {
-//     return [
-//       [this.source, "SRC"],
-//       ...this.after.map(a => [a, "A"] as [Delta,string]),
-//     ];
-//   }
-
-//   serialize() {
-//     return {
-//       type: "NewEdge",
-//       source: this.source.hash.toString('hex'),
-//       label: this.label,
-//     };
-//   }
-// }
-
-// An Edge that has been assigned a target (by an EdgeUpdate) at least once.
-// export class ExistingEdge extends Edge {
-//   // the delta being overwritten
-//   readonly delta: EdgeUpdate;
-
-//   constructor(source: NodeCreation, label: string, delta: EdgeUpdate) {
-//     super(source, label);
-//     this.delta = delta;
-//   }
-
-//   getDependencies(): readonly [Delta,string][] {
-//     return [[this.delta, "U"]];
-//   }
-
-//   serialize() {
-//     return {
-//       type: "ExistingEdge",
-//       overwrites: this.delta.hash.toString('hex'),
-//     };
-//   }
-// }
-
 export class EdgeCreation extends Edge {
   // Dependencies:
   readonly after: readonly ReadAllOutgoing[];
@@ -346,6 +299,7 @@ export class EdgeCreation extends Edge {
     };
   }
 }
+
 export class EdgeUpdate extends Edge {
   // Dependencies
   readonly overwrites: Edge;
@@ -364,8 +318,6 @@ export class EdgeUpdate extends Edge {
     this.reads = reads;
     this.afterReads = afterReads;
 
-    // this.overwritable = new ExistingEdge(overwrites.source, overwrites.label, this);
-
     // Register our dependencies' inverse dependencies + detect conflicts:
     overwrites.registerWrite(this);
     target.registerDependency(this);
@@ -449,8 +401,8 @@ export class TargetValue implements Target {
 // One of the possibilities explored was to have a separate delta for Reading an Edge.
 // Problem is that deltas have content-based IDs, and all concurrent Reads (of the same Write) would be identical.
 // If two EdgeUpdates both read the same value, but one of them also overwrites this value, then there would not be a R/U conflict because they both perform the 'same' Read (same ID), but there should really be a conflict.
-// To solve this issue, every Read would need an additional unique ID attribute, but this is overkill: two concurrent identical EdgeUpdates that depend on an identical Read, would then become different (conflicting) operations.
-// So I conclude that it's better for a Reads to really just be dependencies of EdgeUpdates.
+// To solve this issue, every Read would need an additional unique ID attribute, but this is overkill: two concurrent identical EdgeUpdates that depend on an identical Read, would then become different (conflicting) operations, while they do exactly the same thing.
+// So I conclude that it's better for Reads to really just be dependencies of EdgeUpdates.
 
 // export class EdgeRead extends PrimitiveDelta {
 //   // Every read has a unique ID (otherwise, different concurrent reads would be represented by the same delta)

+ 12 - 12
lib/delta_registry.ts

@@ -47,8 +47,8 @@ export class DeltaRegistry {
   newReadAllOutgoing(node: NodeCreation, after: readonly EdgeCreation[]): ReadAllOutgoing {
     const hash = createHash('sha256')
       .update('node=')
-      .update(getHash(node))
-      .update(buffersXOR(...after.map(getHash)))
+      .update(node.hash)
+      .update(buffersXOR(...after.map(a => a.hash)))
       .digest();
     return this.createIdempotent(hash, () => new ReadAllOutgoing(hash, node, after));
   }
@@ -56,11 +56,11 @@ export class DeltaRegistry {
   newNodeDeletion(creation: NodeCreation, afterSrc: readonly EdgeUpdate[], afterTgt: readonly EdgeUpdate[]): NodeDeletion {
     const hash = createHash('sha256')
       .update('deletes=')
-      .update(getHash(creation))
+      .update(creation.hash)
       .update('afterSrc=')
-      .update(buffersXOR(...afterSrc.map(getHash)))
+      .update(buffersXOR(...afterSrc.map(a => a.hash)))
       .update('afterTgt=')
-      .update(buffersXOR(...afterTgt.map(getHash)))
+      .update(buffersXOR(...afterTgt.map(a => a.hash)))
       .digest();
     return this.createIdempotent(hash, () => new NodeDeletion(hash, creation, afterSrc, afterTgt));
   }
@@ -68,11 +68,11 @@ export class DeltaRegistry {
   newEdgeCreation(source: NodeCreation, label: string, after: readonly ReadAllOutgoing[] = []) {
     const hash = createHash('sha256')
       .update('source=')
-      .update(getHash(source))
+      .update(source.hash)
       .update('label=')
       .update(label)
       .update('after=')
-      .update(buffersXOR(...after.map(getHash)))
+      .update(buffersXOR(...after.map(a => a.hash)))
       .digest();
     return this.createIdempotent(hash, () => new EdgeCreation(hash, source, label, after));
   }
@@ -81,14 +81,14 @@ export class DeltaRegistry {
     const wrappedTarget = target instanceof NodeCreation ? new TargetNode(target) : new TargetValue(target);
     const hash = createHash('sha256')
       .update('overwrites=')
-      .update(getHash(overwrites))
+      .update(overwrites.hash)
       .update('target=')
       .update(getHash(wrappedTarget))
       .update('reads=')
       // XOR, because order in which 'reads' are specified shouldn't matter:
-      .update(buffersXOR(...reads.map(getHash)))
+      .update(buffersXOR(...reads.map(r => r.hash)))
       .update('afterReads=')
-      .update(buffersXOR(...afterReads.map(getHash)))
+      .update(buffersXOR(...afterReads.map(a => a.hash)))
       .digest();
     return this.createIdempotent(hash, () => new EdgeUpdate(hash, overwrites, wrappedTarget, reads, afterReads));
   }
@@ -97,9 +97,9 @@ export class DeltaRegistry {
     // XOR of hashes of deltas
     const hash = createHash('sha256')
       .update('deltas=')
-      .update(buffersXOR(...deltas.map(getHash)))
+      .update(buffersXOR(...deltas.map(d => d.hash)))
       .update('dependencies=')
-      .update(buffersXOR(...dependencies.map(([tx]) => getHash(tx))))
+      .update(buffersXOR(...dependencies.map(([tx]) => tx.hash)))
       .digest();
     return this.createIdempotent<Transaction>(hash, () => new Transaction(hash, deltas, description, dependencies));
   }