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 {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
// 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
}
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 };
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 {
// Scalar pointer
return { localContext, target: target as PropertyTypes };
// For other types (objects, arrays), convert to string
builder.addPointer(localContext, JSON.stringify(target));
}
}
// Fallback for unexpected pointer structure
return { localContext: 'unknown', target: 'unknown' };
})
});
allDeltas.push(fullDelta);
}
// Build the delta and add to results
allDeltas.push(builder.buildV1());
}
}
}