From 4542c4ce836774860d231c8a7069ad970c940304 Mon Sep 17 00:00:00 2001 From: Lentil Hoffman Date: Fri, 20 Jun 2025 10:46:34 -0500 Subject: [PATCH] Use deltaBuilder in lossless.decompose --- .windsurf/workflows/delta-builder.md | 6 ++++ .windsurf/workflows/delta-format.md | 7 ---- src/views/lossless.ts | 53 +++++++++++++++------------- 3 files changed, 35 insertions(+), 31 deletions(-) create mode 100644 .windsurf/workflows/delta-builder.md delete mode 100644 .windsurf/workflows/delta-format.md diff --git a/.windsurf/workflows/delta-builder.md b/.windsurf/workflows/delta-builder.md new file mode 100644 index 0000000..64cc47c --- /dev/null +++ b/.windsurf/workflows/delta-builder.md @@ -0,0 +1,6 @@ +--- +description: Update the current file to use delta builder +--- + +Replace each deltav2 instantiation with a fluent call to createDelta from delta builder, using the following process: + - pass creator and host as arguments to createDelta \ No newline at end of file diff --git a/.windsurf/workflows/delta-format.md b/.windsurf/workflows/delta-format.md deleted file mode 100644 index 5f0bb63..0000000 --- a/.windsurf/workflows/delta-format.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -description: Update deltas to use the object style for pointers ---- - -- in the current file, for each v1 delta, rewrite it as a v2 delta - - make sure the new delta is isomorphic to the original - - do not include a timestamp \ No newline at end of file diff --git a/src/views/lossless.ts b/src/views/lossless.ts index a36fee0..a1ed380 100644 --- a/src/views/lossless.ts +++ b/src/views/lossless.ts @@ -9,6 +9,7 @@ import {Transactions} from '../features/transactions'; import {DomainEntityID, PropertyID, PropertyTypes, TransactionID, ViewMany} from "../core/types"; import {Negation} from '../features/negation'; import {NegationHelper} from '../features/negation'; +import { createDelta } from '../core/delta-builder'; const debug = Debug('rz:lossless'); export type CollapsedPointer = {[key: PropertyID]: PropertyTypes}; @@ -199,31 +200,35 @@ export class Lossless { for (const delta of deltas) { if (!seenDeltaIds.has(delta.id)) { seenDeltaIds.add(delta.id); - // Convert CollapsedDelta back to Delta - const fullDelta = new Delta({ - id: delta.id, - creator: delta.creator, - host: delta.host, - timeCreated: delta.timeCreated, - pointers: delta.pointers.map(pointer => { - // Convert back to V1 pointer format for Delta constructor - const pointerEntries = Object.entries(pointer); - if (pointerEntries.length === 1) { - const [localContext, target] = pointerEntries[0]; - if (typeof target === 'string' && this.domainEntities.has(target)) { - // This is a reference pointer to an entity - // The targetContext is the property ID this delta appears under - return { localContext, target, targetContext: propertyId }; - } else { - // Scalar pointer - return { localContext, target: target as PropertyTypes }; - } + + // Create a new delta using DeltaBuilder + const builder = createDelta(delta.creator, delta.host) + .withId(delta.id) + .withTimestamp(delta.timeCreated); + + // Add all pointers from the collapsed delta + for (const pointer of delta.pointers) { + const pointerEntries = Object.entries(pointer); + if (pointerEntries.length === 1) { + const [localContext, target] = pointerEntries[0]; + if (target === null || target === undefined) { + continue; // Skip null/undefined targets } - // Fallback for unexpected pointer structure - return { localContext: 'unknown', target: 'unknown' }; - }) - }); - allDeltas.push(fullDelta); + if (typeof target === 'string' && this.domainEntities.has(target)) { + // This is a reference pointer to an entity + builder.addPointer(localContext, target, propertyId); + } else if (typeof target === 'string' || typeof target === 'number' || typeof target === 'boolean') { + // Scalar pointer with valid type + builder.addPointer(localContext, target); + } else { + // For other types (objects, arrays), convert to string + builder.addPointer(localContext, JSON.stringify(target)); + } + } + } + + // Build the delta and add to results + allDeltas.push(builder.buildV1()); } } }