import { AuthorizationKey, DocumentBaseline, ObjectIdentifier, Operation, applyPatch, areOidsRelated, assert, assignOid, cloneDeep, compareTimestampSchemaVersions, getWallClockTime, } from '@verdant-web/common'; import { Context } from '../context/context.js'; import { EntityChange } from './types.js'; export type EntityMetadataView = { view: any; fromOlderVersion: boolean; deleted: boolean; empty: boolean; updatedAt: number; latestTimestamp: string | null; authz?: AuthorizationKey; }; export class EntityMetadata { private ctx; private baseline: DocumentBaseline | null = null; // these must be kept in timestamp order. private confirmedOperations: Operation[] = []; // operations applied locally, but not sent to persistence // until 'committed' by pending operations. this powers the // self-healing pruning system, which injects these ephemeral // operations to materialize pruned 'fixed' data in place of // 'real' invalid data so the user can keep using the app. as // soon as the user makes modifications to the entity, this // ephemeral pruned data is applied underneath it. private ephemeralOperations?: Operation[] = []; // pending operations are 'in flight' into persistence, but // if you killed the power now they'd be lost. they don't have an // officially applied timestamp yet either, so they are applied // last, after all confirmed/ephemerals. private pendingOperations: Operation[] = []; readonly oid; constructor({ oid, ctx, confirmedOperations, pendingOperations, baseline, }: { oid: ObjectIdentifier; ctx: Context; confirmedOperations?: Operation[]; pendingOperations?: Operation[]; baseline?: DocumentBaseline; }) { assert(oid, 'oid is required'); this.ctx = ctx; this.oid = oid; if (confirmedOperations) { this.confirmedOperations = confirmedOperations; } if (pendingOperations) { this.pendingOperations = pendingOperations; } if (baseline) { this.baseline = baseline; } } /** * Compute the current view of the entity. */ computeView = (omitPending = false): EntityMetadataView => { const base = cloneDeep(this.baseline?.snapshot ?? undefined); const baselineTimestamp = this.baseline?.timestamp ?? null; // start with the baseline authz, if any. further init ops // may overwrite this. let authz = this.baseline?.authz; const confirmedResult = this.applyOperations( // apply ops to baseline base, // deleted if there's no baseline !base, // we're applying confirmed ops first this.confirmedOperations, // latest timestamp is the baseline timestamp, if any baselineTimestamp, // only apply ops after the baseline timestamp baselineTimestamp, ); // now's the time to declare we saw the future if we did. if (confirmedResult.futureSeen) { this.ctx.globalEvents.emit('futureSeen', confirmedResult.futureSeen); } if (confirmedResult.authz) { authz = confirmedResult.authz; } const ephemeralResult = !this.ephemeralOperations || omitPending ? confirmedResult : this.applyOperations( confirmedResult.view, confirmedResult.deleted, this.ephemeralOperations, confirmedResult.latestTimestamp, null, ); const pendingResult = omitPending ? confirmedResult : this.applyOperations( ephemeralResult.view, ephemeralResult.deleted, // now we're applying pending operations this.pendingOperations, // keep our latest timestamp up to date ephemeralResult.latestTimestamp, // we don't use after for pending ops, they're all // logically in the future null, ); if (pendingResult.authz) { authz = pendingResult.authz; } // before letting this data out into the wild, we need // to associate its oid if (pendingResult.view) { assignOid(pendingResult.view, this.oid); } // note whether confirmed data has an operation/baseline from the current // schema or not. const fromOlderVersion = !!confirmedResult.latestTimestamp && compareTimestampSchemaVersions( confirmedResult.latestTimestamp, this.ctx.time.now, ) < 0; const empty = !this.baseline && !this.pendingOperations.length && !this.confirmedOperations.length; if (empty) { this.ctx.log('warn', `Tried to load Entity ${this.oid} with no data`); } const updatedAtTimestamp = pendingResult.latestTimestamp ?? confirmedResult.latestTimestamp ?? baselineTimestamp; const updatedAt = updatedAtTimestamp ? getWallClockTime(updatedAtTimestamp) : 0; if (!pendingResult.view && !pendingResult.deleted && !empty) { this.ctx.log( 'warn', `Entity ${this.oid} has no view, no deleted flag, and not empty`, ); } return { view: pendingResult.view ?? undefined, deleted: pendingResult.deleted, empty, fromOlderVersion, updatedAt, latestTimestamp: updatedAtTimestamp, authz, }; }; addBaseline = (baseline: DocumentBaseline): void => { // opt out if our baseline is newer if (this.baseline && this.baseline.timestamp >= baseline.timestamp) { return; } this.baseline = baseline; // we can now drop any confirmed ops older than the baseline while (this.confirmedOperations[0]?.timestamp < baseline.timestamp) { this.confirmedOperations.shift(); } }; /** * @returns total number of new operations added */ addConfirmedOperations = (operations: Operation[]): number => { let totalAdded = 0; // the operations must be inserted in timestamp order for (const op of operations) { const index = this.confirmedOperations.findIndex( (o) => o.timestamp >= op.timestamp, ); if (index !== -1) { // ensure we don't have a duplicate if (this.confirmedOperations[index].timestamp !== op.timestamp) { // otherwise, insert at the right place this.confirmedOperations.splice(index, 0, op); totalAdded++; } } else { // otherwise, append this.confirmedOperations.push(op); totalAdded++; } // remove this incoming op from pending if it's in there const pendingPrior = this.pendingOperations.length; this.discardPendingOperation(op); totalAdded -= pendingPrior - this.pendingOperations.length; } return totalAdded; }; addPendingOperation = (operation: Operation) => { this.pendingOperations.push(operation); }; addEphemeralOperation = (operation: Operation) => { if (!this.ephemeralOperations) { this.ephemeralOperations = []; } this.ephemeralOperations.push(operation); }; clearEphemeralOperations = () => { const old = this.ephemeralOperations; this.ephemeralOperations = []; return old; }; // while this may not be the most efficient way to remove an operation, // in practice the ops we discard are generally some of the first in // the list, so the find is quick. discardPendingOperation = (operation: Operation) => { // only one op can match, so - avoiding filter() here, // we want short circuiting. const indexOfOperation = this.pendingOperations.findIndex( (op) => op.timestamp === operation.timestamp, ); if (indexOfOperation === -1) { return; } this.pendingOperations.splice(indexOfOperation, 1); }; private applyOperations = ( base: any, deleted: boolean, operations: Operation[], latestTimestamp: string | null, after: string | null, ): { view: any; latestTimestamp: string | null; deleted: boolean; futureSeen: string | undefined; authz?: AuthorizationKey; } => { let futureSeen: string | undefined = undefined; let authz: AuthorizationKey | undefined = undefined; const now = this.ctx.time.now; for (const op of operations) { // ignore ops before our after cutoff if (after && op.timestamp <= after) { continue; } // don't apply future ops if (compareTimestampSchemaVersions(op.timestamp, now) > 0) { futureSeen = op.timestamp; continue; } // we don't actually delete the view when a delete op // comes in. the view remains useful for calculating // undo operations. if (op.data.op === 'delete') { deleted = true; } else { try { base = applyPatch(base, op.data); if (op.data.op === 'initialize') { deleted = false; if (op.authz) { authz = op.authz; } } } catch (err) { this.ctx.log( 'critical', `Failed to apply operation to entity ${this.oid}: ${JSON.stringify( op, )}`, err, ); throw err; } } // track the latest timestamp if (!latestTimestamp || op.timestamp > latestTimestamp) { latestTimestamp = op.timestamp; } } return { view: base, latestTimestamp: latestTimestamp ?? null, deleted, futureSeen, authz, }; }; } /** * Represents the metadata for a group of entities underneath a Document. * Metadata is separated out this way so that these classes can be * garbage collected when the root Document goes out of scope. */ export class EntityFamilyMetadata { private ctx; private entities: Map = new Map(); private onPendingOperations; private rootOid: ObjectIdentifier; constructor({ ctx, onPendingOperations, rootOid, }: { ctx: Context; onPendingOperations: (ops: Operation[]) => void; rootOid: ObjectIdentifier; }) { this.ctx = ctx; this.rootOid = rootOid; this.onPendingOperations = onPendingOperations; } get = (oid: ObjectIdentifier) => { assert(oid, 'oid is required'); if (!this.entities.has(oid)) { this.entities.set(oid, new EntityMetadata({ oid, ctx: this.ctx })); } return this.entities.get(oid)!; }; getAllOids = () => { return Array.from(this.entities.keys()); }; addConfirmedData = ({ baselines = [], operations = {}, isLocal = false, }: { baselines?: DocumentBaseline[]; operations?: Record; isLocal?: boolean; }) => { const changes: Record = {}; for (const baseline of baselines) { if (!areOidsRelated(this.rootOid, baseline.oid)) { throw new Error( `Invalid baseline for entity ${this.rootOid}: ` + JSON.stringify(baseline), ); } this.get(baseline.oid).addBaseline(baseline); changes[baseline.oid] ??= { oid: baseline.oid, isLocal }; } for (const [oid, ops] of Object.entries(operations)) { if (!areOidsRelated(this.rootOid, oid)) { throw new Error( `Invalid operations for entity ${this.rootOid}: ` + JSON.stringify(ops), ); } const added = this.get(oid).addConfirmedOperations(ops); if (added !== 0) { changes[oid] ??= { oid, isLocal }; } } return Object.values(changes); }; /** * Adds local, unconfirmed operations to the system. * The API is different here to streamline for the way * local changes are usually handled, as a list. */ addPendingData = (operations: Operation[]) => { // when pending data is applied, we go ahead and // write all ephemeral changes first. this.flushAllEphemeral(); const changes: Record = {}; for (const op of operations) { this.get(op.oid).addPendingOperation(op); changes[op.oid] ??= { oid: op.oid, isLocal: true }; } this.onPendingOperations(operations); return Object.values(changes); }; private ephemeralMemo = new Array(); private flushAllEphemeral = () => { for (const ent of this.entities.values()) { const ops = ent.clearEphemeralOperations(); if (ops) { this.ephemeralMemo.push(...ops); } } if (this.ephemeralMemo.length) { const ephemeralCopy = this.ephemeralMemo.slice(); // must clear this first to avoid infinite recursion this.ephemeralMemo.length = 0; this.addPendingData(ephemeralCopy); } }; addEphemeralData = (operations: Operation[]) => { const changes: Record = {}; for (const op of operations) { this.get(op.oid).addEphemeralOperation(op); changes[op.oid] ??= { oid: op.oid, isLocal: true }; } return Object.values(changes); }; replaceAllData = ({ operations = {}, baselines = [], }: { operations?: Record; baselines?: DocumentBaseline[]; }) => { const oids = Array.from(this.entities.keys()); this.entities.clear(); const changes: Record = {}; // changes apply to all the entities we removed things from, too for (const oid of oids) { changes[oid] = { oid, isLocal: false }; } for (const baseline of baselines) { if (!areOidsRelated(this.rootOid, baseline.oid)) { throw new Error( `Invalid baseline for entity ${this.rootOid}: ` + JSON.stringify(baseline), ); } this.get(baseline.oid).addBaseline(baseline); changes[baseline.oid] ??= { oid: baseline.oid, isLocal: false }; } for (const [oid, ops] of Object.entries(operations)) { if (!areOidsRelated(this.rootOid, oid)) { throw new Error( `Invalid operations for entity ${this.rootOid}: ` + JSON.stringify(ops), ); } this.get(oid).addConfirmedOperations(ops); changes[oid] ??= { oid, isLocal: false }; } return Object.values(changes); }; discardPendingOperation = (operation: Operation) => { const ent = this.entities.get(operation.oid); ent?.discardPendingOperation(operation); }; }