|
@@ -0,0 +1,79 @@
|
|
|
+import {Delta} from "./delta";
|
|
|
+
|
|
|
+import {
|
|
|
+ NodeCreation,
|
|
|
+ NodeDeletion,
|
|
|
+ EdgeCreation,
|
|
|
+ EdgeUpdate,
|
|
|
+ EdgeTargetType,
|
|
|
+ PrimitiveRegistry,
|
|
|
+ PrimitiveDelta,
|
|
|
+} from "./primitive_delta";
|
|
|
+
|
|
|
+import {
|
|
|
+ CompositeLevel,
|
|
|
+} from "./composite_delta";
|
|
|
+
|
|
|
+import {UUID} from "./types";
|
|
|
+
|
|
|
+export class DeltaParser {
|
|
|
+ primitiveRegistry: PrimitiveRegistry;
|
|
|
+ compositeLevel: CompositeLevel;
|
|
|
+
|
|
|
+ constructor(primitiveRegistry, compositeLevel) {
|
|
|
+ this.primitiveRegistry = primitiveRegistry;
|
|
|
+ this.compositeLevel = compositeLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ private getDependency<T>(hash): T {
|
|
|
+ const result = this.primitiveRegistry.deltas.get(hash);
|
|
|
+ if (result === undefined) throw new Error("Could not dependency: " + hash);
|
|
|
+ return result as T;
|
|
|
+ }
|
|
|
+
|
|
|
+ loadEdgeTarget({type, ...rest}): EdgeTargetType {
|
|
|
+ if (type === "value") {
|
|
|
+ const {value} = rest;
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ if (type === "node") {
|
|
|
+ const {creation} = rest;
|
|
|
+ return this.getDependency<NodeCreation>(creation);
|
|
|
+ }
|
|
|
+ throw new Error("Unknown edge target type: " + type);
|
|
|
+ }
|
|
|
+
|
|
|
+ loadDelta({type, ...rest}): Delta {
|
|
|
+ if (type === "NodeCreation") {
|
|
|
+ const {id} = rest;
|
|
|
+ return this.primitiveRegistry.newNodeCreation(new UUID(id));
|
|
|
+ }
|
|
|
+ if (type === "EdgeCreation") {
|
|
|
+ const {source, label, target} = rest;
|
|
|
+ return this.primitiveRegistry.newEdgeCreation(
|
|
|
+ this.getDependency<NodeCreation>(source),
|
|
|
+ label,
|
|
|
+ this.loadEdgeTarget(target));
|
|
|
+ }
|
|
|
+ if (type === "EdgeUpdate") {
|
|
|
+ const {overwrites, target} = rest;
|
|
|
+ return this.primitiveRegistry.newEdgeUpdate(
|
|
|
+ this.getDependency<EdgeCreation|EdgeUpdate>(overwrites),
|
|
|
+ this.loadEdgeTarget(target));
|
|
|
+ }
|
|
|
+ if (type === "NodeDeletion") {
|
|
|
+ const {creation, deletedOutgoingEdges, afterIncomingEdges} = rest;
|
|
|
+ return this.primitiveRegistry.newNodeDeletion(
|
|
|
+ this.getDependency<NodeCreation>(creation),
|
|
|
+ deletedOutgoingEdges.map(d => this.getDependency<EdgeCreation|EdgeUpdate>(d)),
|
|
|
+ afterIncomingEdges.map(d => this.getDependency<EdgeUpdate|NodeDeletion>(d)));
|
|
|
+ }
|
|
|
+ if (type === "CompositeDelta") {
|
|
|
+ const {deltas, description} = rest;
|
|
|
+ return this.compositeLevel.createComposite(
|
|
|
+ deltas.map(d => this.loadDelta(d)),
|
|
|
+ description);
|
|
|
+ }
|
|
|
+ throw new Error("Unknown delta type: " + type);
|
|
|
+ }
|
|
|
+}
|