import type { Kysely, Transaction } from "kysely"; import type { CollectionMembershipCache } from "../cache/collection-membership-cache.js"; import type { DocumentMetaCache } from "../cache/document-meta-cache.js"; import type { KyselyOperationIndex } from "../cache/kysely-operation-index.js"; import type { KyselyWriteCache } from "../cache/kysely-write-cache.js"; import type { IOperationIndex } from "../cache/operation-index-types.js"; import type { IWriteCache } from "../cache/write/interfaces.js"; import type { IDocumentMetaCache } from "../cache/document-meta-cache-types.js"; import type { ICollectionMembershipCache } from "../cache/collection-membership-cache.js"; import type { IOperationStore, IWriteIdentityStore, } from "../storage/interfaces.js"; import type { KyselyOperationStore } from "../storage/kysely/store.js"; import type { KyselyKeyframeStore } from "../storage/kysely/keyframe-store.js"; import { acquirePurgeLocks, findPurged, } from "../storage/kysely/document-purges.js"; import { KyselyDocumentPurger } from "../storage/kysely/document-purger.js"; import { KyselyWriteIdentityStore } from "../storage/kysely/write-identity-store.js"; import type { Database } from "../storage/kysely/types.js"; /** Per-document purge locks, held until the job's transaction ends. */ export interface DocumentLocks { shared(ids: Iterable): Promise; exclusive(id: string): Promise; /** The tombstoned ids among `ids`, read in the job's transaction. */ purged(ids: Iterable): Promise>; } /** For scopes without a transaction to hold a lock in. */ export const NOOP_DOCUMENT_LOCKS: DocumentLocks = { shared: () => Promise.resolve(), exclusive: () => Promise.resolve(), purged: () => Promise.resolve(new Set()), }; /** For scopes without a store to record identity in; resolves nothing. */ export const NOOP_WRITE_IDENTITY_STORE: IWriteIdentityStore = { upsert: () => Promise.resolve(), resolve: () => Promise.resolve(undefined), describe: () => Promise.resolve(undefined), delete: () => Promise.resolve(), }; export interface ExecutionStores { operationStore: IOperationStore; operationIndex: IOperationIndex; writeCache: IWriteCache; documentMetaCache: IDocumentMetaCache; collectionMembershipCache: ICollectionMembershipCache; documentLocks: DocumentLocks; /** The write-side identity table, bound to the job's transaction. */ writeIdentity: IWriteIdentityStore; /** Absent without a transaction, which a purge cannot run outside of. */ purger?: KyselyDocumentPurger; } export interface IExecutionScope { run( fn: (stores: ExecutionStores) => Promise, signal?: AbortSignal, ): Promise; } export class DefaultExecutionScope implements IExecutionScope { constructor( private operationStore: IOperationStore, private operationIndex: IOperationIndex, private writeCache: IWriteCache, private documentMetaCache: IDocumentMetaCache, private collectionMembershipCache: ICollectionMembershipCache, private writeIdentity: IWriteIdentityStore = NOOP_WRITE_IDENTITY_STORE, ) {} async run( fn: (stores: ExecutionStores) => Promise, signal?: AbortSignal, ): Promise { signal?.throwIfAborted(); return fn({ operationStore: this.operationStore, operationIndex: this.operationIndex, writeCache: this.writeCache, documentMetaCache: this.documentMetaCache, collectionMembershipCache: this.collectionMembershipCache, documentLocks: NOOP_DOCUMENT_LOCKS, writeIdentity: this.writeIdentity, }); } } export class KyselyExecutionScope implements IExecutionScope { constructor( private db: Kysely, private operationStore: KyselyOperationStore, private operationIndex: KyselyOperationIndex, private keyframeStore: KyselyKeyframeStore, private writeCache: KyselyWriteCache, private documentMetaCache: DocumentMetaCache, private collectionMembershipCache: CollectionMembershipCache, private writeIdentityStore: KyselyWriteIdentityStore = new KyselyWriteIdentityStore( db, ), ) {} async run( fn: (stores: ExecutionStores) => Promise, signal?: AbortSignal, ): Promise { signal?.throwIfAborted(); return this.db.transaction().execute(async (trx: Transaction) => { // A shared lock keeps a live id live until commit; its re-checks are moot. const sharedLocked = new Set(); const liveIds = new Set(); const scopedOperationStore = this.operationStore.withTransaction( trx, liveIds, ); const scopedOperationIndex = this.operationIndex.withTransaction( trx, liveIds, ); const scopedKeyframeStore = this.keyframeStore.withTransaction(trx); return fn({ operationStore: scopedOperationStore, operationIndex: scopedOperationIndex, writeCache: this.writeCache.withScopedStores( scopedOperationStore, scopedKeyframeStore, ), documentMetaCache: this.documentMetaCache.withScopedStore(scopedOperationStore), collectionMembershipCache: this.collectionMembershipCache.withScopedIndex(scopedOperationIndex), documentLocks: { shared: async (ids) => { const list = [...ids]; await acquirePurgeLocks(trx, list, "shared"); for (const id of list) sharedLocked.add(id); }, exclusive: (id) => acquirePurgeLocks(trx, [id], "exclusive"), purged: async (ids) => { const list = [...ids]; const found = await findPurged(trx, list); for (const id of list) { if (sharedLocked.has(id) && !found.has(id)) liveIds.add(id); } return found; }, }, writeIdentity: this.writeIdentityStore.withTransaction(trx), purger: new KyselyDocumentPurger(trx), }); }); } }