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

View File

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