added lossy view

This commit is contained in:
Ladd Hoffman 2024-12-23 23:30:21 -06:00
parent 68e611a050
commit 42df273052
2 changed files with 96 additions and 67 deletions

View File

@ -33,6 +33,8 @@ describe('Lossless', () => {
expect(lossless.view()).toEqual({
keanu: {
referencedAs: ["actor"],
properties: {
roles: [{
creator: "a",
host: "h",
@ -45,7 +47,10 @@ describe('Lossless', () => {
],
}],
},
},
neo: {
referencedAs: ["role"],
properties: {
actor: [{
creator: "a",
host: "h",
@ -58,7 +63,10 @@ describe('Lossless', () => {
],
}],
},
},
the_matrix: {
referencedAs: ["film"],
properties: {
cast: [{
creator: "a",
host: "h",
@ -70,10 +78,9 @@ describe('Lossless', () => {
{salary_currency: "usd"},
],
}],
}
},
},
});
});
describe('can filter deltas', () => {
@ -103,6 +110,8 @@ describe('Lossless', () => {
expect(lossless.view()).toEqual({
ace: {
referencedAs: ["1", "14"],
properties: {
value: [{
creator: 'A',
host: 'H',
@ -117,6 +126,7 @@ describe('Lossless', () => {
]
}],
}
}
});
});
@ -127,6 +137,8 @@ describe('Lossless', () => {
expect(lossless.view(filter)).toEqual({
ace: {
referencedAs: ["1"],
properties: {
value: [{
creator: 'A',
host: 'H',
@ -135,6 +147,7 @@ describe('Lossless', () => {
]
}]
}
}
});
});
});

View File

@ -6,11 +6,19 @@ import {Delta, DeltaFilter, PropertyTypes} from "./types";
type DomainEntityID = string;
type PropertyID = string;
export type LosslessView = {[key: string]: {[key: string]: Delta[]}};
export type CollapsedPointer = {[key: string]: PropertyTypes};
export type CollapsedDelta = Omit<Delta, 'pointers'> & {
pointers: CollapsedPointer[];
};
export type LosslessViewOne = {
referencedAs: string[];
properties: {
[key: PropertyID]: CollapsedDelta[]
}
};
export type LosslessViewMany = {
[key: DomainEntityID]: LosslessViewOne;
};
class DomainEntityMap extends Map<DomainEntityID, DomainEntity> {};
@ -66,26 +74,34 @@ export class Lossless {
}
//TODO: json logic -- view(deltaFilter?: FilterExpr) {
view(deltaFilter?: DeltaFilter) {
const view: {[key: DomainEntityID]: {[key: PropertyID]: CollapsedDelta[]}} = {};
view(deltaFilter?: DeltaFilter): LosslessViewMany {
const view: LosslessViewMany = {};
for (const ent of this.domainEntities.values()) {
// const obj: {[key: PropertyID]: CollapsedDelta[]} = {};
view[ent.id] = {};
const referencedAs = new Set<string>();
view[ent.id] = {
referencedAs: [],
properties: {}
};
for (const prop of ent.properties.values()) {
view[ent.id][prop.id] = view[ent.id][prop.id] || [];
view[ent.id].properties[prop.id] = view[ent.id].properties[prop.id] || [];
for (const delta of prop.deltas) {
if (deltaFilter) {
const include = deltaFilter(delta);
if (!include) continue;
}
const pointers: CollapsedPointer[] = [];
for (const {localContext, target} of delta.pointers) {
pointers.push({[localContext]: target});
if (target === ent.id) {
referencedAs.add(localContext);
}
}
const collapsedDelta: CollapsedDelta = {
...delta,
pointers: delta.pointers
.map(({localContext, target}) => ({
[localContext]: target
}))
pointers
};
view[ent.id][prop.id].push(collapsedDelta);
view[ent.id].referencedAs = Array.from(referencedAs.values());
view[ent.id].properties[prop.id].push(collapsedDelta);
}
}
}