Compare commits
2 Commits
8043b67258
...
5c1c8a23b8
Author | SHA1 | Date | |
---|---|---|---|
5c1c8a23b8 | |||
4542c4ce83 |
6
.windsurf/workflows/delta-builder.md
Normal file
6
.windsurf/workflows/delta-builder.md
Normal 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
|
@ -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
|
|
@ -158,7 +158,7 @@ describe('DeltaBuilder', () => {
|
|||||||
.buildV2();
|
.buildV2();
|
||||||
|
|
||||||
// Check for transaction ID in V2 pointers
|
// Check for transaction ID in V2 pointers
|
||||||
expect(delta.pointers['_transaction']).toBe(txId);
|
expect(delta.pointers['_transaction']).toEqual({ [txId]: 'deltas' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support negation', () => {
|
it('should support negation', () => {
|
||||||
|
@ -1,22 +1,19 @@
|
|||||||
import {Delta, DeltaFilter, DeltaV2} from '../src/core';
|
import {Delta, DeltaFilter, DeltaV2} from '../src/core';
|
||||||
import {Lossless} from '../src/views';
|
import {Lossless} from '../src/views';
|
||||||
import {RhizomeNode} from '../src/node';
|
import {RhizomeNode} from '../src/node';
|
||||||
|
import {createDelta} from '../src/core/delta-builder';
|
||||||
|
|
||||||
describe('Lossless', () => {
|
describe('Lossless', () => {
|
||||||
const node = new RhizomeNode();
|
const node = new RhizomeNode();
|
||||||
|
|
||||||
it('creates a lossless view of keanu as neo in the matrix', () => {
|
it('creates a lossless view of keanu as neo in the matrix', () => {
|
||||||
const delta = new DeltaV2({
|
const delta = createDelta('a', 'h')
|
||||||
creator: 'a',
|
.addPointer('actor', 'keanu', 'roles')
|
||||||
host: 'h',
|
.addPointer('role', 'neo', 'actor')
|
||||||
pointers: {
|
.addPointer('film', 'the_matrix', 'cast')
|
||||||
actor: {"keanu": "roles"},
|
.addPointer('base_salary', 1000000)
|
||||||
role: {"neo": "actor"},
|
.addPointer('salary_currency', 'usd')
|
||||||
film: {"the_matrix": "cast"},
|
.buildV1();
|
||||||
base_salary: 1000000,
|
|
||||||
salary_currency: "usd"
|
|
||||||
}
|
|
||||||
}).toV1();
|
|
||||||
|
|
||||||
expect(delta.pointers).toMatchObject([{
|
expect(delta.pointers).toMatchObject([{
|
||||||
localContext: "actor",
|
localContext: "actor",
|
||||||
@ -95,17 +92,13 @@ describe('Lossless', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('accepts DeltaV2 instances', () => {
|
it('accepts DeltaV2 instances', () => {
|
||||||
const delta = new DeltaV2({
|
const delta = createDelta('a', 'h')
|
||||||
creator: 'a',
|
.addPointer('actor', 'keanu', 'roles')
|
||||||
host: 'h',
|
.addPointer('role', 'neo', 'actor')
|
||||||
pointers: {
|
.addPointer('film', 'the_matrix', 'cast')
|
||||||
actor: {"keanu": "roles"},
|
.addPointer('base_salary', 1000000)
|
||||||
role: {"neo": "actor"},
|
.addPointer('salary_currency', 'usd')
|
||||||
film: {"the_matrix": "cast"},
|
.buildV2();
|
||||||
base_salary: 1000000,
|
|
||||||
salary_currency: "usd"
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const lossless = new Lossless(node);
|
const lossless = new Lossless(node);
|
||||||
|
|
||||||
@ -167,26 +160,20 @@ describe('Lossless', () => {
|
|||||||
const lossless = new Lossless(node);
|
const lossless = new Lossless(node);
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
lossless.ingestDelta(new Delta({
|
// First delta
|
||||||
creator: 'A',
|
lossless.ingestDelta(
|
||||||
host: 'H',
|
createDelta('A', 'H')
|
||||||
pointers: [{
|
.addPointer('1', 'ace', 'value')
|
||||||
localContext: "1",
|
.buildV1()
|
||||||
target: "ace",
|
);
|
||||||
targetContext: "value"
|
|
||||||
}]
|
|
||||||
}));
|
|
||||||
|
|
||||||
lossless.ingestDelta(new Delta({
|
// Second delta
|
||||||
creator: 'B',
|
lossless.ingestDelta(
|
||||||
host: 'H',
|
createDelta('B', 'H')
|
||||||
pointers: [{
|
|
||||||
// 10 11j 12q 13k 14a
|
// 10 11j 12q 13k 14a
|
||||||
localContext: "14",
|
.addPointer('14', 'ace', 'value')
|
||||||
target: "ace",
|
.buildV1()
|
||||||
targetContext: "value"
|
);
|
||||||
}]
|
|
||||||
}));
|
|
||||||
|
|
||||||
expect(lossless.view()).toMatchObject({
|
expect(lossless.view()).toMatchObject({
|
||||||
ace: {
|
ace: {
|
||||||
@ -251,51 +238,42 @@ describe('Lossless', () => {
|
|||||||
const transactionId = 'tx-filter-test';
|
const transactionId = 'tx-filter-test';
|
||||||
|
|
||||||
// Declare transaction with 3 deltas
|
// Declare transaction with 3 deltas
|
||||||
losslessT.ingestDelta(new Delta({
|
losslessT.ingestDelta(
|
||||||
creator: 'system',
|
createDelta('system', 'H')
|
||||||
host: 'H',
|
.declareTransaction(transactionId, 3)
|
||||||
pointers: [
|
.buildV1()
|
||||||
{ localContext: '_transaction', target: transactionId, targetContext: 'size' },
|
);
|
||||||
{ localContext: 'size', target: 3 }
|
|
||||||
]
|
|
||||||
}));
|
|
||||||
|
|
||||||
// A1: First delta from creator A
|
// A1: First delta from creator A
|
||||||
losslessT.ingestDelta(new Delta({
|
losslessT.ingestDelta(
|
||||||
creator: 'A',
|
createDelta('A', 'H')
|
||||||
host: 'H',
|
.inTransaction(transactionId)
|
||||||
pointers: [
|
.addPointer('step', 'process1', 'status')
|
||||||
{ localContext: '_transaction', target: transactionId, targetContext: 'deltas' },
|
.addPointer('value', 'started')
|
||||||
{ localContext: 'step', target: 'process1', targetContext: 'status' },
|
.buildV1()
|
||||||
{ localContext: 'value', target: 'started' }
|
);
|
||||||
]
|
|
||||||
}));
|
|
||||||
|
|
||||||
// B: Delta from creator B
|
// B: Delta from creator B
|
||||||
losslessT.ingestDelta(new Delta({
|
losslessT.ingestDelta(
|
||||||
creator: 'B',
|
createDelta('B', 'H')
|
||||||
host: 'H',
|
.inTransaction(transactionId)
|
||||||
pointers: [
|
.addPointer('step', 'process1', 'status')
|
||||||
{ localContext: '_transaction', target: transactionId, targetContext: 'deltas' },
|
.addPointer('value', 'processing')
|
||||||
{ localContext: 'step', target: 'process1', targetContext: 'status' },
|
.buildV1()
|
||||||
{ localContext: 'value', target: 'processing' }
|
);
|
||||||
]
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Transaction incomplete - nothing should show
|
// Transaction incomplete - nothing should show
|
||||||
const incompleteView = losslessT.view(['process1']);
|
const incompleteView = losslessT.view(['process1']);
|
||||||
expect(incompleteView.process1).toBeUndefined();
|
expect(incompleteView.process1).toBeUndefined();
|
||||||
|
|
||||||
// A2: Second delta from creator A completes transaction
|
// A2: Second delta from creator A completes transaction
|
||||||
losslessT.ingestDelta(new Delta({
|
losslessT.ingestDelta(
|
||||||
creator: 'A',
|
createDelta('A', 'H')
|
||||||
host: 'H',
|
.inTransaction(transactionId)
|
||||||
pointers: [
|
.addPointer('step', 'process1', 'status')
|
||||||
{ localContext: '_transaction', target: transactionId, targetContext: 'deltas' },
|
.addPointer('value', 'completed')
|
||||||
{ localContext: 'step', target: 'process1', targetContext: 'status' },
|
.buildV1()
|
||||||
{ localContext: 'value', target: 'completed' }
|
);
|
||||||
]
|
|
||||||
}));
|
|
||||||
|
|
||||||
// All deltas visible now
|
// All deltas visible now
|
||||||
const completeView = losslessT.view(['process1']);
|
const completeView = losslessT.view(['process1']);
|
||||||
|
@ -52,6 +52,18 @@ export class DeltaBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Declare a transaction with a size
|
||||||
|
* @param transactionId The ID of the transaction
|
||||||
|
* @param size The size of the transaction
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
declareTransaction(transactionId: string, size: number): this {
|
||||||
|
this.addPointer('_transaction', transactionId, 'size');
|
||||||
|
this.addPointer('size', size)
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark this delta as a negation of another delta
|
* Mark this delta as a negation of another delta
|
||||||
*/
|
*/
|
||||||
@ -99,7 +111,7 @@ export class DeltaBuilder {
|
|||||||
const pointers = { ...this.pointers };
|
const pointers = { ...this.pointers };
|
||||||
|
|
||||||
if (this.transactionId) {
|
if (this.transactionId) {
|
||||||
pointers['_transaction'] = this.transactionId;
|
pointers['_transaction'] = { [this.transactionId]: 'deltas' };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isNegation && this.negatedDeltaId) {
|
if (this.isNegation && this.negatedDeltaId) {
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user