import { createCompoundIndexValue, createLowerBoundIndexValue, createUpperBoundIndexValue, DocumentBaseline, getOidRoot, getOidSubIdRange, ObjectIdentifier, } from '@verdant-web/common'; import { ContextWithoutPersistence } from '../../../context/context.js'; import { AbstractTransaction, AckInfo, ClientOperation, CommonQueryOptions, Iterator, LocalReplicaInfo, PersistenceMetadataDb, } from '../../interfaces.js'; import { IdbService } from '../IdbService.js'; import { OPERATION_INDEX_NAMES } from '../indexNames.js'; import { closeDatabase, getSizeOfObjectStore } from '../util.js'; export type StoredClientOperation = ClientOperation & { /** This acts as the primary key */ [OPERATION_INDEX_NAMES.OID__TIMESTAMP]: string; [OPERATION_INDEX_NAMES.IS_LOCAL__TIMESTAMP]: string; [OPERATION_INDEX_NAMES.ROOT_OID__TIMESTAMP]: string; }; export type StoredSchema = { type: 'schema'; schema: string; }; export class IdbMetadataDb extends IdbService implements PersistenceMetadataDb { constructor(db: IDBDatabase, ctx: ContextWithoutPersistence) { super(db, ctx); this.addDispose(() => { this.ctx.log('info', `Closing metadata DB for`, ctx.namespace); return closeDatabase(db); }); } transaction = async ( opts: { mode?: 'readwrite' | 'readonly'; storeNames: string[]; abort?: AbortSignal; }, procedure: (tx: IDBTransaction) => Promise, ) => { const tx = this.createTransaction(opts.storeNames, { mode: opts.mode, abort: opts.abort, }); const result = await procedure(tx); return result; }; getAckInfo = async (): Promise => { const result = await this.run('info', (store) => store.get('ack')); if (result) { return result; } else { return { globalAckTimestamp: null, }; } }; setGlobalAck = async (ack: string): Promise => { await this.run( 'info', (store) => store.put({ type: 'ack', globalAckTimestamp: ack }), { mode: 'readwrite' }, ); }; getLocalReplica = async ( opts?: CommonQueryOptions, ): Promise => { return this.run( 'info', (store) => store.get('localReplicaInfo'), opts, ); }; updateLocalReplica = async ( data: LocalReplicaInfo, opts?: CommonQueryOptions, ): Promise => { try { await this.run( 'info', (store) => store.put({ ...data, type: 'localReplicaInfo', }), { mode: 'readwrite', transaction: opts?.transaction, }, ); } catch (e) { this.ctx.log('critical', 'Error updating local replica', data, e); throw e; } }; iterateDocumentBaselines = async ( rootOid: string, iterator: (baseline: DocumentBaseline) => void, opts?: CommonQueryOptions, ): Promise => { await this.iterate( 'baselines', (store) => { const root = getOidRoot(rootOid); const [start, end] = getOidSubIdRange(rootOid); return [ store.openCursor(IDBKeyRange.only(root)), store.openCursor(IDBKeyRange.bound(start, end, false, false)), ]; }, iterator, opts, ); }; iterateCollectionBaselines = async ( collection: string, iterator: (baseline: DocumentBaseline) => void, opts?: CommonQueryOptions, ): Promise => { await this.iterate( 'baselines', (store) => { return [ store.openCursor( IDBKeyRange.bound(collection, collection + '\uffff', false, false), ), ]; }, iterator, opts, ); }; iterateAllBaselines = async ( iterator: Iterator, opts: CommonQueryOptions, ): Promise => { await this.iterate( 'baselines', (store) => store.index('timestamp').openCursor(), iterator, opts, ); }; getBaseline = ( oid: string, opts?: CommonQueryOptions, ): Promise => { return this.run( 'baselines', (store) => store.get(oid), opts, ); }; setBaselines = async ( baselines: DocumentBaseline[], opts: CommonQueryOptions = writeOpts, ): Promise => { await this.runAll( 'baselines', (store) => baselines.map((b) => store.put(b)), opts, ); }; deleteBaseline = async ( oid: string, opts: CommonQueryOptions = writeOpts, ): Promise => { await this.run('baselines', (store) => store.delete(oid), opts); }; iterateDocumentOperations = ( rootOid: string, iterator: (op: StoredClientOperation) => void, opts?: CommonQueryOptions & { to?: string | null }, ): Promise => { return this.iterate( 'operations', (store) => { const index = store.index(OPERATION_INDEX_NAMES.ROOT_OID__TIMESTAMP); const start = createLowerBoundIndexValue(rootOid); const end = opts?.to ? createCompoundIndexValue(rootOid, opts.to) : createUpperBoundIndexValue(rootOid); const range = IDBKeyRange.bound(start, end, false, false); return index.openCursor(range); }, iterator, opts, ); }; iterateEntityOperations = ( oid: string, iterator: (op: StoredClientOperation) => void, opts?: CommonQueryOptions & { to?: string | null; reverse?: boolean }, ): Promise => { // NOTE: this is simplified from original impl. // perhaps I missed some nuance as to why it was // so complex before? return this.iterate( 'operations', (store) => { const start = createLowerBoundIndexValue(oid); const end = opts?.to ? createCompoundIndexValue(oid, opts.to) : createUpperBoundIndexValue(oid); const range = IDBKeyRange.bound(start, end, false, false); return store.openCursor(range, opts?.reverse ? 'prev' : 'next'); }, iterator, opts, ); }; deleteEntityOperations = ( oid: string, opts: CommonQueryOptions & { to: string | null }, ): Promise => { return this.iterate( 'operations', (store) => { const start = createLowerBoundIndexValue(oid); const end = opts?.to ? createCompoundIndexValue(oid, opts.to) : createUpperBoundIndexValue(oid); const range = IDBKeyRange.bound(start, end, false, false); return store.openCursor(range); }, (op, store) => { store.delete(op.oid_timestamp); }, opts, ); }; iterateCollectionOperations = ( collection: string, iterator: (op: StoredClientOperation) => void, opts?: CommonQueryOptions, ): Promise => { return this.iterate( 'operations', (store) => { return store.openCursor( IDBKeyRange.bound(collection, collection + '\uffff', false, false), 'next', ); }, iterator, opts, ); }; iterateLocalOperations = ( iterator: (op: StoredClientOperation) => void, opts?: CommonQueryOptions & { after?: string | null; }, ): Promise => { return this.iterate( 'operations', (store) => { const start = opts?.after ? createCompoundIndexValue(true, opts.after) : createLowerBoundIndexValue(true); const end = createUpperBoundIndexValue(true); const index = store.index(OPERATION_INDEX_NAMES.IS_LOCAL__TIMESTAMP); return index.openCursor( // NOTE: differs from original impl -- last arg is 'false' instead of 'true' IDBKeyRange.bound(start, end, !!opts?.after, false), 'next', ); }, iterator, ); }; iterateAllOperations = ( iterator: (op: StoredClientOperation) => void, opts?: CommonQueryOptions & { before?: string | null; from?: string | null; }, ): Promise => { return this.iterate( 'operations', (store) => { const start = opts?.from ? createLowerBoundIndexValue(opts.from) : undefined; const end = opts?.before ? createUpperBoundIndexValue(opts.before) : createLowerBoundIndexValue(true); const range = start && end ? IDBKeyRange.bound(start, end, false, true) : start ? IDBKeyRange.lowerBound(start, false) : end ? IDBKeyRange.upperBound(end, true) : undefined; return store.index('timestamp').openCursor(range, 'next'); }, iterator, opts, ); }; addOperations = async ( ops: StoredClientOperation[], opts: CommonQueryOptions = writeOpts, ): Promise => { let affected = new Set(); await this.runAll( 'operations', (store) => ops.map((op) => { affected.add(getOidRoot(op.oid)); return store.put(this.addOperationIndexes(op)); }), opts, ); return Array.from(affected); }; reset = async ({ clearReplica, transaction, }: { clearReplica?: boolean; transaction?: AbstractTransaction; } = {}): Promise => { const tx = (transaction as IDBTransaction) || this.createTransaction(['info', 'operations', 'baselines'], { mode: 'readwrite', }); await Promise.all([ this.resetLocalReplica(tx, clearReplica), this.resetBaselines(tx), this.resetOperations(tx), ]); }; stats = async (): Promise<{ operationsSize: { count: number; size: number }; baselinesSize: { count: number; size: number }; }> => { const ops = await getSizeOfObjectStore(this.db, 'operations'); const baselines = await getSizeOfObjectStore(this.db, 'baselines'); return { operationsSize: ops, baselinesSize: baselines }; }; private resetLocalReplica = async (tx: IDBTransaction, clear = false) => { if (clear) { return this.run('info', (store) => store.delete('localReplicaInfo'), { mode: 'readwrite', transaction: tx, }); } else { const localInfo = await this.getLocalReplica({ transaction: tx, }); if (localInfo) { localInfo.ackedLogicalTime = null; localInfo.lastSyncedLogicalTime = null; await this.run( 'info', (store) => store.put({ ...localInfo, type: 'localReplicaInfo', }), { mode: 'readwrite', transaction: tx, }, ); } } }; private resetBaselines = async (tx: IDBTransaction) => { return this.clear('baselines', tx); }; private resetOperations = async (tx: IDBTransaction) => { return this.clear('operations', tx); }; private addOperationIndexes = ( op: ClientOperation, ): StoredClientOperation => { return { ...op, [OPERATION_INDEX_NAMES.OID__TIMESTAMP]: createCompoundIndexValue( op.oid, op.timestamp, ) as string, [OPERATION_INDEX_NAMES.IS_LOCAL__TIMESTAMP]: createCompoundIndexValue( op.isLocal, op.timestamp, ) as string, [OPERATION_INDEX_NAMES.ROOT_OID__TIMESTAMP]: createCompoundIndexValue( getOidRoot(op.oid), op.timestamp, ) as string, }; }; purgeDocumentData( oid: ObjectIdentifier, opts?: CommonQueryOptions | undefined, ): Promise { const rootOid = getOidRoot(oid); const tx = opts?.transaction as IDBTransaction | undefined; return this.transaction( { mode: 'readwrite', storeNames: ['baselines', 'operations'], }, async (ownedTx) => { const transaction = tx ?? ownedTx; await this.iterate( 'baselines', (store) => { const [start, end] = getOidSubIdRange(rootOid); return [ store.openCursor(IDBKeyRange.only(rootOid)), store.openCursor(IDBKeyRange.bound(start, end, false, false)), ]; }, (baseline, store) => { store.delete(baseline.oid); }, { mode: 'readwrite', transaction }, ); await this.iterate( 'operations', (store) => { const index = store.index( OPERATION_INDEX_NAMES.ROOT_OID__TIMESTAMP, ); const start = createLowerBoundIndexValue(rootOid); const end = createUpperBoundIndexValue(rootOid); return index.openCursor( IDBKeyRange.bound(start, end, false, false), ); }, (op, store) => { store.delete(op[OPERATION_INDEX_NAMES.OID__TIMESTAMP]); }, { mode: 'readwrite', transaction }, ); }, ); } } const writeOpts = { mode: 'readwrite' } as const;