Use deltaBuilder in lossless.decompose

This commit is contained in:
Lentil Hoffman 2025-06-20 10:46:34 -05:00
parent 8043b67258
commit 4542c4ce83
Signed by: lentil
GPG Key ID: 0F5B99F3F4D0C087
3 changed files with 35 additions and 31 deletions

View File

@ -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

View File

@ -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

View File

@ -9,6 +9,7 @@ import {Transactions} from '../features/transactions';
import {DomainEntityID, PropertyID, PropertyTypes, TransactionID, ViewMany} from "../core/types"; import {DomainEntityID, PropertyID, PropertyTypes, TransactionID, ViewMany} from "../core/types";
import {Negation} from '../features/negation'; import {Negation} from '../features/negation';
import {NegationHelper} from '../features/negation'; import {NegationHelper} from '../features/negation';
import { createDelta } from '../core/delta-builder';
const debug = Debug('rz:lossless'); const debug = Debug('rz:lossless');
export type CollapsedPointer = {[key: PropertyID]: PropertyTypes}; export type CollapsedPointer = {[key: PropertyID]: PropertyTypes};
@ -199,31 +200,35 @@ export class Lossless {
for (const delta of deltas) { for (const delta of deltas) {
if (!seenDeltaIds.has(delta.id)) { if (!seenDeltaIds.has(delta.id)) {
seenDeltaIds.add(delta.id); seenDeltaIds.add(delta.id);
// Convert CollapsedDelta back to Delta
const fullDelta = new Delta({ // Create a new delta using DeltaBuilder
id: delta.id, const builder = createDelta(delta.creator, delta.host)
creator: delta.creator, .withId(delta.id)
host: delta.host, .withTimestamp(delta.timeCreated);
timeCreated: delta.timeCreated,
pointers: delta.pointers.map(pointer => { // Add all pointers from the collapsed delta
// Convert back to V1 pointer format for Delta constructor for (const pointer of delta.pointers) {
const pointerEntries = Object.entries(pointer); const pointerEntries = Object.entries(pointer);
if (pointerEntries.length === 1) { if (pointerEntries.length === 1) {
const [localContext, target] = pointerEntries[0]; const [localContext, target] = pointerEntries[0];
if (typeof target === 'string' && this.domainEntities.has(target)) { if (target === null || target === undefined) {
// This is a reference pointer to an entity continue; // Skip null/undefined targets
// The targetContext is the property ID this delta appears under
return { localContext, target, targetContext: propertyId };
} else {
// Scalar pointer
return { localContext, target: target as PropertyTypes };
}
} }
// Fallback for unexpected pointer structure if (typeof target === 'string' && this.domainEntities.has(target)) {
return { localContext: 'unknown', target: 'unknown' }; // This is a reference pointer to an entity
}) builder.addPointer(localContext, target, propertyId);
}); } else if (typeof target === 'string' || typeof target === 'number' || typeof target === 'boolean') {
allDeltas.push(fullDelta); // 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());
} }
} }
} }