2024-12-23 17:29:38 -06:00
|
|
|
// Deltas target entities.
|
|
|
|
// We can maintain a record of all the targeted entities, and the deltas that targeted them
|
|
|
|
|
2024-12-26 15:59:03 -06:00
|
|
|
import Debug from 'debug';
|
2024-12-27 13:43:43 -06:00
|
|
|
import {Delta, DeltaFilter} from './delta';
|
|
|
|
import {DomainEntityID, PropertyID, PropertyTypes} from "./types";
|
2024-12-26 15:59:03 -06:00
|
|
|
const debug = Debug('lossless');
|
2024-12-23 17:29:38 -06:00
|
|
|
|
|
|
|
export type CollapsedPointer = {[key: string]: PropertyTypes};
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
export type CollapsedDelta = Omit<Delta, 'pointers'> & {
|
|
|
|
pointers: CollapsedPointer[];
|
|
|
|
};
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 23:30:21 -06:00
|
|
|
export type LosslessViewOne = {
|
|
|
|
referencedAs: string[];
|
|
|
|
properties: {
|
|
|
|
[key: PropertyID]: CollapsedDelta[]
|
|
|
|
}
|
|
|
|
};
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 23:30:21 -06:00
|
|
|
export type LosslessViewMany = {
|
|
|
|
[key: DomainEntityID]: LosslessViewOne;
|
|
|
|
};
|
2024-12-23 17:29:38 -06:00
|
|
|
|
|
|
|
class DomainEntityMap extends Map<DomainEntityID, DomainEntity> {};
|
|
|
|
|
|
|
|
class DomainEntity {
|
|
|
|
id: DomainEntityID;
|
2024-12-26 15:59:03 -06:00
|
|
|
properties = new Map<PropertyID, Set<Delta>>();
|
2024-12-23 17:29:38 -06:00
|
|
|
|
|
|
|
constructor(id: DomainEntityID) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
addDelta(delta: Delta) {
|
|
|
|
const targetContexts = delta.pointers
|
|
|
|
.filter(({target}) => target === this.id)
|
|
|
|
.map(({targetContext}) => targetContext)
|
|
|
|
.filter((targetContext) => typeof targetContext === 'string');
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
for (const targetContext of targetContexts) {
|
2024-12-26 15:59:03 -06:00
|
|
|
let propertyDeltas = this.properties.get(targetContext);
|
|
|
|
if (!propertyDeltas) {
|
|
|
|
propertyDeltas = new Set<Delta>();
|
|
|
|
this.properties.set(targetContext, propertyDeltas);
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
debug(`adding delta for entity ${this.id}`);
|
|
|
|
propertyDeltas.add(delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
const properties: {[key: PropertyID]: number} = {};
|
|
|
|
for (const [key, deltas] of this.properties.entries()) {
|
|
|
|
properties[key] = deltas.size;
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|
2024-12-26 15:59:03 -06:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
properties
|
|
|
|
};
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Lossless {
|
|
|
|
domainEntities = new DomainEntityMap();
|
|
|
|
|
2024-12-27 13:43:43 -06:00
|
|
|
ingestDelta(delta: Delta): LosslessViewMany {
|
2024-12-23 17:29:38 -06:00
|
|
|
const targets = delta.pointers
|
|
|
|
.filter(({targetContext}) => !!targetContext)
|
|
|
|
.map(({target}) => target)
|
|
|
|
.filter((target) => typeof target === 'string')
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
for (const target of targets) {
|
|
|
|
let ent = this.domainEntities.get(target);
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
if (!ent) {
|
|
|
|
ent = new DomainEntity(target);
|
|
|
|
this.domainEntities.set(target, ent);
|
|
|
|
}
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
debug('before add, domain entity:', JSON.stringify(ent));
|
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
ent.addDelta(delta);
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
debug('after add, domain entity:', JSON.stringify(ent));
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|
2024-12-27 13:43:43 -06:00
|
|
|
|
|
|
|
return this.view(targets);
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|
|
|
|
|
2024-12-23 20:44:54 -06:00
|
|
|
//TODO: json logic -- view(deltaFilter?: FilterExpr) {
|
2024-12-26 15:59:03 -06:00
|
|
|
view(entityIds?: DomainEntityID[], deltaFilter?: DeltaFilter): LosslessViewMany {
|
2024-12-23 23:30:21 -06:00
|
|
|
const view: LosslessViewMany = {};
|
2024-12-26 15:59:03 -06:00
|
|
|
entityIds = entityIds ?? Array.from(this.domainEntities.keys());
|
|
|
|
for (const id of entityIds) {
|
|
|
|
const ent = this.domainEntities.get(id);
|
|
|
|
if (!ent) continue;
|
|
|
|
|
|
|
|
debug(`domain entity ${id}`, JSON.stringify(ent));
|
|
|
|
|
2024-12-23 23:30:21 -06:00
|
|
|
const referencedAs = new Set<string>();
|
2024-12-26 15:59:03 -06:00
|
|
|
const properties: {
|
|
|
|
[key: PropertyID]: CollapsedDelta[]
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
for (const [key, deltas] of ent.properties.entries()) {
|
|
|
|
properties[key] = properties[key] || [];
|
|
|
|
|
|
|
|
for (const delta of deltas) {
|
|
|
|
|
2024-12-23 20:44:54 -06:00
|
|
|
if (deltaFilter) {
|
|
|
|
const include = deltaFilter(delta);
|
|
|
|
if (!include) continue;
|
|
|
|
}
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 23:30:21 -06:00
|
|
|
const pointers: CollapsedPointer[] = [];
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 23:30:21 -06:00
|
|
|
for (const {localContext, target} of delta.pointers) {
|
|
|
|
pointers.push({[localContext]: target});
|
|
|
|
if (target === ent.id) {
|
|
|
|
referencedAs.add(localContext);
|
|
|
|
}
|
|
|
|
}
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
const collapsedDelta: CollapsedDelta = {
|
|
|
|
...delta,
|
2024-12-23 23:30:21 -06:00
|
|
|
pointers
|
2024-12-23 17:29:38 -06:00
|
|
|
};
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
properties[key].push(collapsedDelta);
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|
|
|
|
}
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
view[ent.id] = {
|
|
|
|
referencedAs: Array.from(referencedAs.values()),
|
|
|
|
properties
|
|
|
|
};
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
2024-12-25 00:42:16 -06:00
|
|
|
|
|
|
|
// TODO: point-in-time queries
|
2024-12-23 17:29:38 -06:00
|
|
|
}
|