@tearleads/client-sdk 0.1.211 → 0.1.214
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/containerContents/documentDiscovery.js +2 -2
- package/client/containerContents/documentDiscovery.js.map +1 -1
- package/client/organizations/organizationReadModels.js +3 -3
- package/client/organizations/organizationReadModels.js.map +1 -1
- package/data/persistence/container-contents/containerContentsPersistence.js +2 -2
- package/data/persistence/container-contents/containerContentsPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js +2 -2
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js +2 -2
- package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerPendingUpdatePersistence.js +6 -6
- package/data/persistence/container-contents/containerPendingUpdatePersistence.js.map +1 -1
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.d.ts +11 -2
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.js +89 -77
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.js.map +1 -1
- package/data/persistence/documents/internal/discardDocument.js +4 -4
- package/data/persistence/documents/internal/discardDocument.js.map +1 -1
- package/data/persistence/documents/internal/discoveredDocumentUpsert.js +3 -3
- package/data/persistence/documents/internal/discoveredDocumentUpsert.js.map +1 -1
- package/data/persistence/documents/internal/documentDeletionPersistence.js +8 -8
- package/data/persistence/documents/internal/documentDeletionPersistence.js.map +1 -1
- package/data/persistence/documents/internal/documentMutationPersistence.js +4 -3
- package/data/persistence/documents/internal/documentMutationPersistence.js.map +1 -1
- package/data/persistence/documents/internal/documentPullContinuationPersistence.js +2 -2
- package/data/persistence/documents/internal/documentPullContinuationPersistence.js.map +1 -1
- package/data/persistence/documents/internal/rowQueryPersistence.js +3 -3
- package/data/persistence/documents/internal/rowQueryPersistence.js.map +1 -1
- package/data/persistence/documents/internal/syncQueuePersistence.js +12 -13
- package/data/persistence/documents/internal/syncQueuePersistence.js.map +1 -1
- package/data/persistence/documents/records/documentHistoryPersistence.d.ts +24 -20
- package/data/persistence/documents/records/documentHistoryPersistence.js +59 -90
- package/data/persistence/documents/records/documentHistoryPersistence.js.map +1 -1
- package/data/persistence/documents/records/documentPendingUpdatePersistence.d.ts +10 -25
- package/data/persistence/documents/records/documentPendingUpdatePersistence.js +19 -79
- package/data/persistence/documents/records/documentPendingUpdatePersistence.js.map +1 -1
- package/data/persistence/documents/records/documentQueuedUpdateReplacement.d.ts +4 -14
- package/data/persistence/documents/records/documentQueuedUpdateReplacement.js +27 -42
- package/data/persistence/documents/records/documentQueuedUpdateReplacement.js.map +1 -1
- package/data/persistence/documents/records/documentRecordPersistence.d.ts +5 -8
- package/data/persistence/documents/records/documentRecordPersistence.js +2 -55
- package/data/persistence/documents/records/documentRecordPersistence.js.map +1 -1
- package/data/persistence/documents/records/documentRecordRepository.d.ts +133 -0
- package/data/persistence/documents/records/documentRecordRepository.js +286 -0
- package/data/persistence/documents/records/documentRecordRepository.js.map +1 -0
- package/data/persistence/documents/records/documentSyncFailurePersistence.d.ts +9 -12
- package/data/persistence/documents/records/documentSyncFailurePersistence.js +19 -34
- package/data/persistence/documents/records/documentSyncFailurePersistence.js.map +1 -1
- package/data/persistence/documents/records/index.d.ts +4 -19
- package/data/persistence/documents/records/index.js +5 -29
- package/data/persistence/documents/records/index.js.map +1 -1
- package/data/persistence/organizations/organizationPresentationDenialPersistence.js +0 -43
- package/data/persistence/organizations/organizationPresentationDenialPersistence.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelMemberLoad.d.ts +7 -2
- package/data/persistence/organizations/organizationReadModelMemberLoad.js +22 -27
- package/data/persistence/organizations/organizationReadModelMemberLoad.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelPersistence.js +38 -81
- package/data/persistence/organizations/organizationReadModelPersistence.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelPurge.js +0 -38
- package/data/persistence/organizations/organizationReadModelPurge.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelRepository.js +264 -0
- package/data/persistence/organizations/organizationReadModelRepository.js.map +1 -0
- package/package.json +1 -1
- package/stores/documents/documentStore/sync/attachmentUploadFailure.js +2 -2
- package/stores/documents/documentStore/sync/attachmentUploadFailure.js.map +1 -1
- package/stores/documents/documentStore/sync/syncAttachments.js +2 -2
- package/stores/documents/documentStore/sync/syncAttachments.js.map +1 -1
- package/stores/documents/documentStore/sync/syncShared.js +2 -2
- package/stores/documents/documentStore/sync/syncShared.js.map +1 -1
- package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js.map +1 -1
- package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js +2 -2
- package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js.map +1 -1
- package/workflows/container-contents/pendingWrites/index.js +4 -3
- package/workflows/container-contents/pendingWrites/index.js.map +1 -1
- package/workflows/container-contents/recovery/accessRestoration.js +2 -2
- package/workflows/container-contents/recovery/accessRestoration.js.map +1 -1
- package/workflows/documents/index.d.ts +1 -1
- package/workflows/documents/index.js +1 -1
- package/workflows/documents/index.js.map +1 -1
- package/workflows/documents/syncRekeys/syncRecoveryRekey.js +3 -2
- package/workflows/documents/syncRekeys/syncRecoveryRekey.js.map +1 -1
- package/workflows/organizations/groupNames/currentOrganizationGroupNames.js +6 -2
- package/workflows/organizations/groupNames/currentOrganizationGroupNames.js.map +1 -1
- package/workflows/organizations/index.d.ts +0 -1
- package/workflows/organizations/index.js +0 -1
- package/workflows/organizations/index.js.map +1 -1
- package/workflows/organizations/policyHistory/policyHistoryAccessDenial.js +7 -4
- package/workflows/organizations/policyHistory/policyHistoryAccessDenial.js.map +1 -1
- package/workflows/organizations/readModel/dataUsageReadModel.js +2 -2
- package/workflows/organizations/readModel/dataUsageReadModel.js.map +1 -1
- package/workflows/organizations/readModel/listLocalOrganizations.js +6 -4
- package/workflows/organizations/readModel/listLocalOrganizations.js.map +1 -1
- package/workflows/organizations/readModel/localReadModelDetails.js +8 -9
- package/workflows/organizations/readModel/localReadModelDetails.js.map +1 -1
- package/workflows/organizations/readModel/organizationPresentationAccessState.js +2 -2
- package/workflows/organizations/readModel/organizationPresentationAccessState.js.map +1 -1
- package/workflows/organizations/readModel/readModelProjection.d.ts +1 -1
- package/workflows/organizations/readModel/readModelProjection.js +12 -13
- package/workflows/organizations/readModel/readModelProjection.js.map +1 -1
- package/data/persistence/organizations/organizationAccessRevocationPersistence.js +0 -49
- package/data/persistence/organizations/organizationAccessRevocationPersistence.js.map +0 -1
- package/data/persistence/organizations/organizationGroupNamePersistence.js +0 -33
- package/data/persistence/organizations/organizationGroupNamePersistence.js.map +0 -1
- package/data/persistence/organizations/organizationProfileDocumentPointers.js +0 -32
- package/data/persistence/organizations/organizationProfileDocumentPointers.js.map +0 -1
- package/data/persistence/organizations/organizationReadModelCursorPersistence.d.ts +0 -15
- package/data/persistence/organizations/organizationReadModelCursorPersistence.js +0 -25
- package/data/persistence/organizations/organizationReadModelCursorPersistence.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentPendingUpdatePersistence.js","sourceRoot":"","sources":["documentPendingUpdatePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,GAAG,EACH,EAAE,EACF,OAAO,EACP,SAAS,EACT,EAAE,EACF,EAAE,EAEF,GAAG,GACJ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,sBAAsB,EACtB,SAAS,GACV,MAAM,6CAA6C,CAAC;AAErD,OAAO,EAAE,iCAAiC,EAAE,MAAM,0CAA0C,CAAC;AAU7F;;;;;GAKG;AACH,MAAM,aAAa,GAAG,QAAQ,CAAC;AAK/B,MAAM,UAAU,aAAa,CAAC,KAAoB;IAChD,OAAO,GAAG,CACR,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,C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BAAsB,CAAC;SAC9B,GAAG,CAAC;QACH,QAAQ,EAAE,IAAI;QACd,yBAAyB,EAAE,IAAI;QAC/B,uBAAuB,EAAE,IAAI;QAC7B,mBAAmB,EAAE,IAAI;QACzB,UAAU,EAAE,CAAC;KACd,CAAC;SACD,KAAK,CAAC,QAAQ,CAAC;SACf,GAAG,EAAE,CAAC;AACX,CAAC;AAED,0DAA0D;AAC1D,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,OAAgB,EAChB,EAAU;IAEV,MAAM,iCAAiC,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QACxE,MAAM,sBAAsB,CAAC,EAAE,EAAE,EAAE,CAAC,sBAAsB,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,kCAAkC,CACtD,EAAkB,EAClB,MAAgC;IAEhC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IAChC,MAAM,sBAAsB,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;AACrE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,OAAgB,EAChB,KAAoB;IAEpB,MAAM,iCAAiC,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QACxE,MAAM,kCAAkC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n and,\n asc,\n eq,\n inArray,\n isNotNull,\n lt,\n or,\n type SQL,\n sql,\n} from \"drizzle-orm\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport { getClientSQLitePersistenceRuntime } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport type {\n DocumentScope,\n PendingUpdateFields,\n PendingUpdateOrigin,\n QueuedPendingUpdateRecord,\n SelectedPendingUpdateRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * Queued updates are the rows of the local update log that carry an\n * `updateId`. A tail row (origin \"local\") stays in the log after settlement as\n * history until compaction; an \"outbox\" row has no tail role and settlement\n * deletes it.\n */\nconst OUTBOX_ORIGIN = \"outbox\";\n\ntype QueueStatement = Pick<ClientSQLiteTransactionScope, \"delete\" | \"update\">;\ntype QueueRead = Pick<ClientSQLiteTransactionScope, \"selectDistinct\">;\n\nexport function queuedInScope(scope: DocumentScope) {\n return and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n isNotNull(documentHistoryUpdates.updateId),\n );\n}\n\nfunction mapSelectedPendingUpdate(\n row: SelectedPendingUpdateRow,\n): QueuedPendingUpdateRecord {\n // A queued row always carries its submission vectors; a row without them\n // cannot be submitted and must fail closed rather than send empty ranges.\n if (\n row.partialStartVersionVector === null ||\n row.partialEndVersionVector === null\n ) {\n throw new Error(\"Queued document update is missing its version vectors\");\n }\n if (row.origin !== \"local\" && row.origin !== OUTBOX_ORIGIN) {\n throw new Error(\"Queued document update has an unqueueable origin\");\n }\n return {\n id: String(row.id ?? \"\"),\n origin: row.origin,\n updateData: row.updateData,\n partialStartVersionVector: row.partialStartVersionVector,\n partialEndVersionVector: row.partialEndVersionVector,\n sourceVersionVector: row.sourceVersionVector,\n rekeyCount: row.rekeyCount,\n };\n}\n\n/** The ids among `localIds` (one app kind) with at least one queued update. */\nexport async function listLocalIdsWithQueuedUpdates(\n db: QueueRead,\n appKind: string,\n localIds: readonly string[],\n): Promise<string[]> {\n const uniqueIds = [...new Set(localIds)];\n if (uniqueIds.length === 0) return [];\n const rows = await db\n .selectDistinct({ localId: documentHistoryUpdates.localId })\n .from(documentHistoryUpdates)\n .where(\n and(\n eq(documentHistoryUpdates.appKind, appKind),\n inArray(documentHistoryUpdates.localId, uniqueIds),\n isNotNull(documentHistoryUpdates.updateId),\n ),\n )\n .orderBy(asc(documentHistoryUpdates.localId));\n return rows.map((row) => row.localId);\n}\n\nexport async function hasQueuedDocumentUpdates(\n db: QueueRead,\n scope: DocumentScope,\n): Promise<boolean> {\n const ids = await listLocalIdsWithQueuedUpdates(db, scope.appKind, [\n scope.localId,\n ]);\n return ids.length > 0;\n}\n\nexport async function listDocumentPendingUpdates(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<QueuedPendingUpdateRecord[]> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select({\n id: documentHistoryUpdates.updateId,\n origin: documentHistoryUpdates.origin,\n updateData: documentHistoryUpdates.updateData,\n partialStartVersionVector:\n documentHistoryUpdates.partialStartVersionVector,\n partialEndVersionVector: documentHistoryUpdates.partialEndVersionVector,\n sourceVersionVector: documentHistoryUpdates.sourceVersionVector,\n rekeyCount: documentHistoryUpdates.rekeyCount,\n })\n .from(documentHistoryUpdates)\n .where(queuedInScope(scope))\n .orderBy(...queueOrder());\n\n return rows.map(mapSelectedPendingUpdate);\n}\n\n/** Queue order: insertion time, then insertion order within one instant. */\nexport function queueOrder() {\n return [asc(documentHistoryUpdates.createdAt), asc(sql`rowid`)] as const;\n}\n\n/**\n * Write fence inside an open transaction: the scope's document still has the\n * captured server document id and recovery generation.\n */\nexport async function documentWriteFenceMatchesInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"select\">,\n scope: DocumentScope,\n fence: {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n },\n): Promise<boolean> {\n const matchingDocuments = await tx\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n sql`${documents.documentId} IS ${fence.expectedDocumentId}`,\n eq(documents.recoveryGeneration, fence.expectedRecoveryGeneration),\n ),\n )\n .limit(1);\n return matchingDocuments.length > 0;\n}\n\n/** One log row for a queued update; `origin` decides its tail role. */\nexport function queuedUpdateRow(input: {\n createdAt: string;\n origin: PendingUpdateOrigin;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n updateId: string;\n}) {\n return {\n id: crypto.randomUUID(),\n appKind: input.scope.appKind,\n localId: input.scope.localId,\n updateData: input.pendingUpdate.updateData,\n origin: input.origin,\n createdAt: input.createdAt,\n updateId: input.updateId,\n partialStartVersionVector: input.pendingUpdate.partialStartVersionVector,\n partialEndVersionVector: input.pendingUpdate.partialEndVersionVector,\n sourceVersionVector: input.pendingUpdate.sourceVersionVector ?? null,\n };\n}\n\n/**\n * Queue an update that has no history-tail role: container metadata (its\n * content is its checkpoint) and full-state re-uploads after a remote reset.\n */\nexport async function enqueueDocumentPendingUpdate(\n execSql: ExecSql,\n scope: DocumentScope,\n pendingUpdate: PendingUpdateFields,\n): Promise<string> {\n const updateId = crypto.randomUUID();\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await db\n .insert(documentHistoryUpdates)\n .values(\n queuedUpdateRow({\n createdAt: new Date().toISOString(),\n origin: OUTBOX_ORIGIN,\n pendingUpdate,\n scope,\n updateId,\n }),\n )\n .run();\n });\n return updateId;\n}\n\n/**\n * Append a locally authored update to the history tail AND queue it, as one\n * row in one transaction. A crash can therefore never leave the op restorable\n * from history while missing from the outgoing queue, where a null-marker\n * restore would classify it as covered and never send it.\n */\nexport async function enqueueDocumentPendingUpdateWithHistory(\n execSql: ExecSql,\n scope: DocumentScope,\n pendingUpdate: PendingUpdateFields,\n options?: {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n },\n): Promise<string | null> {\n const createdAt = new Date().toISOString();\n return getClientSQLitePersistenceRuntime(execSql).transaction(\n async (tx) => {\n if (\n options &&\n !(await documentWriteFenceMatchesInTransaction(tx, scope, options))\n ) {\n return null;\n }\n\n return insertDocumentPendingUpdateWithHistoryInTransaction({\n createdAt,\n pendingUpdate,\n scope,\n tx,\n });\n },\n { behavior: \"immediate\" },\n );\n}\n\nexport async function insertDocumentPendingUpdateWithHistoryInTransaction(input: {\n createdAt: string;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n tx: ClientSQLiteTransactionScope;\n}): Promise<string> {\n const updateId = crypto.randomUUID();\n await input.tx\n .insert(documentHistoryUpdates)\n .values(\n queuedUpdateRow({\n createdAt: input.createdAt,\n origin: \"local\",\n pendingUpdate: input.pendingUpdate,\n scope: input.scope,\n updateId,\n }),\n )\n .run();\n return updateId;\n}\n\n/**\n * Assign a fresh id to a queued update, keeping every other column. A lost\n * sync ack leaves the server holding the update under the original id with\n * ciphertext a rebuilt submission can never reproduce (each rebuild encrypts\n * with a fresh IV), so the id is permanently poisoned: every resubmission\n * 409s and the whole outgoing batch rolls back. Re-keying lets the next sync\n * pass submit the same ops under a conflict-free id; the server-side copy is\n * harmless because CRDT update import is idempotent.\n *\n * Returns null when no row carries the id, so callers never report progress\n * for a row that lives in some other queue (or nowhere). The UPDATE's own\n * RETURNING row is the progress signal — a pre-flight SELECT would race a\n * second tab sharing the SQLite file, which can settle the row between the\n * read and the write and turn \"rekeyed\" into a spurious re-arm.\n *\n * The persisted per-row bound is enforced here as well: a row already at\n * MAX_PENDING_UPDATE_REKEYS is left untouched and reported as no progress.\n * The in-memory lane counter bounds a busy-loop within one session; this\n * bound survives restarts, so a poisoned row cannot re-key forever at three\n * passes per launch.\n */\nexport const MAX_PENDING_UPDATE_REKEYS = 5;\n\n/**\n * Reset the durable re-key budget for every queued update in a scope. The\n * cap exists to stop a server that keeps conflicting fresh ids from driving\n * a network-speed loop — but a budget burned during an unrelated outage\n * (e.g. yesterday's stale-bundle deadlock 409ing every submit) should not be\n * terminal forever. An explicit manual retry is the deliberate, rate-limited\n * signal that conditions changed, so it gets a fresh budget.\n */\nexport async function resetDocumentPendingUpdateRekeyBudget(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await db\n .update(documentHistoryUpdates)\n .set({ rekeyCount: 0 })\n .where(queuedInScope(scope));\n });\n}\n\nexport async function rekeyDocumentPendingUpdate(\n execSql: ExecSql,\n id: string,\n): Promise<string | null> {\n const nextId = crypto.randomUUID();\n let rekeyed = false;\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n const updated = await db\n .update(documentHistoryUpdates)\n .set({\n updateId: nextId,\n rekeyCount: sql`${documentHistoryUpdates.rekeyCount} + 1`,\n })\n .where(\n and(\n eq(documentHistoryUpdates.updateId, id),\n lt(documentHistoryUpdates.rekeyCount, MAX_PENDING_UPDATE_REKEYS),\n ),\n )\n .returning({ id: documentHistoryUpdates.updateId });\n rekeyed = updated.length > 0;\n });\n return rekeyed ? nextId : null;\n}\n\n/**\n * Settle queued updates: an outbox row is deleted, and a tail row stops being\n * queued but stays as history until compaction folds it into a checkpoint.\n */\nexport async function settleQueuedDocumentUpdatesInTransaction(\n tx: QueueStatement,\n scope: DocumentScope,\n updateIds: readonly string[],\n): Promise<void> {\n const uniqueIds = [...new Set(updateIds)];\n if (uniqueIds.length === 0) return;\n const selected = and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n inArray(documentHistoryUpdates.updateId, uniqueIds),\n );\n await unqueueSelectedUpdates(tx, selected);\n}\n\nasync function unqueueSelectedUpdates(\n tx: QueueStatement,\n selected: SQL | undefined,\n): Promise<void> {\n await tx\n .delete(documentHistoryUpdates)\n .where(and(selected, eq(documentHistoryUpdates.origin, OUTBOX_ORIGIN)))\n .run();\n await tx\n .update(documentHistoryUpdates)\n .set({\n updateId: null,\n partialStartVersionVector: null,\n partialEndVersionVector: null,\n sourceVersionVector: null,\n rekeyCount: 0,\n })\n .where(selected)\n .run();\n}\n\n/** Settle one queued update by id, whatever its scope. */\nexport async function settleDocumentPendingUpdate(\n execSql: ExecSql,\n id: string,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await unqueueSelectedUpdates(db, eq(documentHistoryUpdates.updateId, id));\n });\n}\n\n/**\n * Stop queueing every update in the given scopes: outbox rows are dropped,\n * and local tail rows keep their content as history (it is already in the\n * document).\n */\nexport async function unqueueDocumentScopesInTransaction(\n tx: QueueStatement,\n scopes: readonly DocumentScope[],\n): Promise<void> {\n if (scopes.length === 0) return;\n await unqueueSelectedUpdates(tx, or(...scopes.map(queuedInScope)));\n}\n\nexport async function unqueueDocumentPendingUpdates(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await unqueueDocumentScopesInTransaction(db, [scope]);\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"documentPendingUpdatePersistence.js","sourceRoot":"","sources":["documentPendingUpdatePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,GAAG,EACH,EAAE,EACF,OAAO,EACP,SAAS,EACT,EAAE,EACF,EAAE,EAEF,GAAG,GACJ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,sBAAsB,EACtB,SAAS,GACV,MAAM,6CAA6C,CAAC;AAUrD;;;;;GAKG;AACH,MAAM,aAAa,GAAG,QAAQ,CAAC;AAK/B,MAAM,UAAU,aAAa,CAAC,KAAoB;IAChD,OAAO,GAAG,CACR,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,SAAS,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAC3C,CAAC;AACJ,CAAC;AAED,SAAS,wBAAwB,CAC/B,GAA6B;IAE7B,yEAAyE;IACzE,0EAA0E;IAC1E,IACE,GAAG,CAAC,yBAAyB,KAAK,IAAI;QACt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,CACtD,EAAkB,EAClB,MAAgC;IAEhC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IAChC,MAAM,sBAAsB,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;AACrE,CAAC","sourcesContent":["import {\n and,\n asc,\n eq,\n inArray,\n isNotNull,\n lt,\n or,\n type SQL,\n sql,\n} from \"drizzle-orm\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type {\n DocumentScope,\n PendingUpdateFields,\n PendingUpdateOrigin,\n QueuedPendingUpdateRecord,\n SelectedPendingUpdateRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * Queued updates are the rows of the local update log that carry an\n * `updateId`. A tail row (origin \"local\") stays in the log after settlement as\n * history until compaction; an \"outbox\" row has no tail role and settlement\n * deletes it.\n */\nconst OUTBOX_ORIGIN = \"outbox\";\n\ntype QueueStatement = Pick<ClientSQLiteTransactionScope, \"delete\" | \"update\">;\ntype QueueRead = Pick<ClientSQLiteTransactionScope, \"selectDistinct\">;\n\nexport function queuedInScope(scope: DocumentScope) {\n return and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n isNotNull(documentHistoryUpdates.updateId),\n );\n}\n\nfunction mapSelectedPendingUpdate(\n row: SelectedPendingUpdateRow,\n): QueuedPendingUpdateRecord {\n // A queued row always carries its submission vectors; a row without them\n // cannot be submitted and must fail closed rather than send empty ranges.\n if (\n row.partialStartVersionVector === null ||\n row.partialEndVersionVector === null\n ) {\n throw new Error(\"Queued document update is missing its version vectors\");\n }\n if (row.origin !== \"local\" && row.origin !== OUTBOX_ORIGIN) {\n throw new Error(\"Queued document update has an unqueueable origin\");\n }\n return {\n id: String(row.id ?? \"\"),\n origin: row.origin,\n updateData: row.updateData,\n partialStartVersionVector: row.partialStartVersionVector,\n partialEndVersionVector: row.partialEndVersionVector,\n sourceVersionVector: row.sourceVersionVector,\n rekeyCount: row.rekeyCount,\n };\n}\n\n/** The ids among `localIds` (one app kind) with at least one queued update. */\nexport async function listLocalIdsWithQueuedUpdates(\n db: QueueRead,\n appKind: string,\n localIds: readonly string[],\n): Promise<string[]> {\n const uniqueIds = [...new Set(localIds)];\n if (uniqueIds.length === 0) return [];\n const rows = await db\n .selectDistinct({ localId: documentHistoryUpdates.localId })\n .from(documentHistoryUpdates)\n .where(\n and(\n eq(documentHistoryUpdates.appKind, appKind),\n inArray(documentHistoryUpdates.localId, uniqueIds),\n isNotNull(documentHistoryUpdates.updateId),\n ),\n )\n .orderBy(asc(documentHistoryUpdates.localId));\n return rows.map((row) => row.localId);\n}\n\nexport async function hasQueuedDocumentUpdates(\n db: QueueRead,\n scope: DocumentScope,\n): Promise<boolean> {\n const ids = await listLocalIdsWithQueuedUpdates(db, scope.appKind, [\n scope.localId,\n ]);\n return ids.length > 0;\n}\n\nexport async function selectDocumentPendingUpdates(\n db: Pick<ClientSQLiteTransactionScope, \"select\">,\n scope: DocumentScope,\n): Promise<QueuedPendingUpdateRecord[]> {\n const rows = await db\n .select({\n id: documentHistoryUpdates.updateId,\n origin: documentHistoryUpdates.origin,\n updateData: documentHistoryUpdates.updateData,\n partialStartVersionVector:\n documentHistoryUpdates.partialStartVersionVector,\n partialEndVersionVector: documentHistoryUpdates.partialEndVersionVector,\n sourceVersionVector: documentHistoryUpdates.sourceVersionVector,\n rekeyCount: documentHistoryUpdates.rekeyCount,\n })\n .from(documentHistoryUpdates)\n .where(queuedInScope(scope))\n .orderBy(...queueOrder());\n\n return rows.map(mapSelectedPendingUpdate);\n}\n\n/** Queue order: insertion time, then insertion order within one instant. */\nexport function queueOrder() {\n return [asc(documentHistoryUpdates.createdAt), asc(sql`rowid`)] as const;\n}\n\n/**\n * Write fence inside an open transaction: the scope's document still has the\n * captured server document id and recovery generation.\n */\nexport async function documentWriteFenceMatchesInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"select\">,\n scope: DocumentScope,\n fence: {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n },\n): Promise<boolean> {\n const matchingDocuments = await tx\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n sql`${documents.documentId} IS ${fence.expectedDocumentId}`,\n eq(documents.recoveryGeneration, fence.expectedRecoveryGeneration),\n ),\n )\n .limit(1);\n return matchingDocuments.length > 0;\n}\n\n/** One log row for a queued update; `origin` decides its tail role. */\nexport function queuedUpdateRow(input: {\n createdAt: string;\n origin: PendingUpdateOrigin;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n updateId: string;\n}) {\n return {\n id: crypto.randomUUID(),\n appKind: input.scope.appKind,\n localId: input.scope.localId,\n updateData: input.pendingUpdate.updateData,\n origin: input.origin,\n createdAt: input.createdAt,\n updateId: input.updateId,\n partialStartVersionVector: input.pendingUpdate.partialStartVersionVector,\n partialEndVersionVector: input.pendingUpdate.partialEndVersionVector,\n sourceVersionVector: input.pendingUpdate.sourceVersionVector ?? null,\n };\n}\n\n/**\n * Queue an update that has no history-tail role: container metadata (its\n * content is its checkpoint) and full-state re-uploads after a remote reset.\n */\nexport async function insertDocumentPendingUpdateInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"insert\">,\n input: {\n createdAt: string;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n updateId: string;\n },\n): Promise<void> {\n await tx\n .insert(documentHistoryUpdates)\n .values(queuedUpdateRow({ ...input, origin: OUTBOX_ORIGIN }))\n .run();\n}\n\nexport async function insertDocumentPendingUpdateWithHistoryInTransaction(input: {\n createdAt: string;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n tx: ClientSQLiteTransactionScope;\n}): Promise<string> {\n const updateId = crypto.randomUUID();\n await input.tx\n .insert(documentHistoryUpdates)\n .values(\n queuedUpdateRow({\n createdAt: input.createdAt,\n origin: \"local\",\n pendingUpdate: input.pendingUpdate,\n scope: input.scope,\n updateId,\n }),\n )\n .run();\n return updateId;\n}\n\n/**\n * Assign a fresh id to a queued update, keeping every other column. A lost\n * sync ack leaves the server holding the update under the original id with\n * ciphertext a rebuilt submission can never reproduce (each rebuild encrypts\n * with a fresh IV), so the id is permanently poisoned: every resubmission\n * 409s and the whole outgoing batch rolls back. Re-keying lets the next sync\n * pass submit the same ops under a conflict-free id; the server-side copy is\n * harmless because CRDT update import is idempotent.\n *\n * Returns null when no row carries the id, so callers never report progress\n * for a row that lives in some other queue (or nowhere). The UPDATE's own\n * RETURNING row is the progress signal — a pre-flight SELECT would race a\n * second tab sharing the SQLite file, which can settle the row between the\n * read and the write and turn \"rekeyed\" into a spurious re-arm.\n *\n * The persisted per-row bound is enforced here as well: a row already at\n * MAX_PENDING_UPDATE_REKEYS is left untouched and reported as no progress.\n * The in-memory lane counter bounds a busy-loop within one session; this\n * bound survives restarts, so a poisoned row cannot re-key forever at three\n * passes per launch.\n */\nexport const MAX_PENDING_UPDATE_REKEYS = 5;\n\n/** Re-key one queued row below the bound; true when the UPDATE matched it. */\nexport async function rekeyDocumentPendingUpdateInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"update\">,\n id: string,\n nextId: string,\n): Promise<boolean> {\n const updated = await tx\n .update(documentHistoryUpdates)\n .set({\n updateId: nextId,\n rekeyCount: sql`${documentHistoryUpdates.rekeyCount} + 1`,\n })\n .where(\n and(\n eq(documentHistoryUpdates.updateId, id),\n lt(documentHistoryUpdates.rekeyCount, MAX_PENDING_UPDATE_REKEYS),\n ),\n )\n .returning({ id: documentHistoryUpdates.updateId });\n return updated.length > 0;\n}\n\n/**\n * Settle queued updates: an outbox row is deleted, and a tail row stops being\n * queued but stays as history until compaction folds it into a checkpoint.\n */\nexport async function settleQueuedDocumentUpdatesInTransaction(\n tx: QueueStatement,\n scope: DocumentScope,\n updateIds: readonly string[],\n): Promise<void> {\n const uniqueIds = [...new Set(updateIds)];\n if (uniqueIds.length === 0) return;\n const selected = and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n inArray(documentHistoryUpdates.updateId, uniqueIds),\n );\n await unqueueSelectedUpdates(tx, selected);\n}\n\nasync function unqueueSelectedUpdates(\n tx: QueueStatement,\n selected: SQL | undefined,\n): Promise<void> {\n await tx\n .delete(documentHistoryUpdates)\n .where(and(selected, eq(documentHistoryUpdates.origin, OUTBOX_ORIGIN)))\n .run();\n await tx\n .update(documentHistoryUpdates)\n .set({\n updateId: null,\n partialStartVersionVector: null,\n partialEndVersionVector: null,\n sourceVersionVector: null,\n rekeyCount: 0,\n })\n .where(selected)\n .run();\n}\n\n/** Settle one queued update by id, whatever its scope. */\nexport async function settleDocumentPendingUpdateInTransaction(\n tx: QueueStatement,\n id: string,\n): Promise<void> {\n await unqueueSelectedUpdates(tx, eq(documentHistoryUpdates.updateId, id));\n}\n\n/**\n * Stop queueing every update in the given scopes: outbox rows are dropped,\n * and local tail rows keep their content as history (it is already in the\n * document).\n */\nexport async function unqueueDocumentScopesInTransaction(\n tx: QueueStatement,\n scopes: readonly DocumentScope[],\n): Promise<void> {\n if (scopes.length === 0) return;\n await unqueueSelectedUpdates(tx, or(...scopes.map(queuedInScope)));\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ClientSQLiteTransactionScope } from "../../../sqlite/sqlitePersistenceRuntime.js";
|
|
2
2
|
import type { DocumentScope, PendingUpdateFields } from "./documentPersistenceTypes.js";
|
|
3
3
|
/** Replace a run of queued local updates with one merged update. */
|
|
4
4
|
export interface QueuedUpdateReplacement {
|
|
@@ -11,17 +11,7 @@ export interface QueuedUpdateReplacement {
|
|
|
11
11
|
rekeyCount: number;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* the result is null: the document still has the captured document id and
|
|
17
|
-
* recovery generation, and the members are still queued under their ids as
|
|
18
|
-
* ordinary local rows (no outbox row, no rotation checkpoint) forming one
|
|
19
|
-
* contiguous run in queue order that starts with the first listed member.
|
|
20
|
-
*
|
|
21
|
-
* The first member's row is updated in place, so it keeps its row id,
|
|
22
|
-
* `created_at` and rowid (its queue and history-tail position), and receives
|
|
23
|
-
* a fresh update id (an id is never reused for different content), the
|
|
24
|
-
* merged data and vectors, and `rekeyCount`. The other members are deleted.
|
|
25
|
-
* Resolves to the merged row's new update id.
|
|
14
|
+
* The checks and writes of `replaceDocumentQueuedUpdates`, inside the
|
|
15
|
+
* caller's transaction: null unless every check holds.
|
|
26
16
|
*/
|
|
27
|
-
export declare function
|
|
17
|
+
export declare function replaceDocumentQueuedUpdatesInTransaction(tx: ClientSQLiteTransactionScope, scope: DocumentScope, replacement: QueuedUpdateReplacement): Promise<string | null>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { eq, inArray } from "drizzle-orm";
|
|
2
2
|
import { documentHistoryUpdates } from "../../../sqlite/schema/documentRecordSchema.js";
|
|
3
|
-
import { getClientSQLitePersistenceRuntime } from "../../../sqlite/sqlitePersistenceRuntime.js";
|
|
4
3
|
import { documentWriteFenceMatchesInTransaction, queuedInScope, queueOrder, } from "./documentPendingUpdatePersistence.js";
|
|
5
4
|
/**
|
|
6
5
|
* The row ids of the members when they are still exactly a contiguous run of
|
|
@@ -35,48 +34,34 @@ async function contiguousMemberRowIds(tx, scope, memberUpdateIds) {
|
|
|
35
34
|
return rowIds.length === memberUpdateIds.length ? rowIds : null;
|
|
36
35
|
}
|
|
37
36
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* the result is null: the document still has the captured document id and
|
|
41
|
-
* recovery generation, and the members are still queued under their ids as
|
|
42
|
-
* ordinary local rows (no outbox row, no rotation checkpoint) forming one
|
|
43
|
-
* contiguous run in queue order that starts with the first listed member.
|
|
44
|
-
*
|
|
45
|
-
* The first member's row is updated in place, so it keeps its row id,
|
|
46
|
-
* `created_at` and rowid (its queue and history-tail position), and receives
|
|
47
|
-
* a fresh update id (an id is never reused for different content), the
|
|
48
|
-
* merged data and vectors, and `rekeyCount`. The other members are deleted.
|
|
49
|
-
* Resolves to the merged row's new update id.
|
|
37
|
+
* The checks and writes of `replaceDocumentQueuedUpdates`, inside the
|
|
38
|
+
* caller's transaction: null unless every check holds.
|
|
50
39
|
*/
|
|
51
|
-
export async function
|
|
52
|
-
if (
|
|
40
|
+
export async function replaceDocumentQueuedUpdatesInTransaction(tx, scope, replacement) {
|
|
41
|
+
if (!(await documentWriteFenceMatchesInTransaction(tx, scope, replacement))) {
|
|
53
42
|
return null;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
.where(inArray(documentHistoryUpdates.id, mergedAwayRowIds))
|
|
78
|
-
.run();
|
|
79
|
-
return updateId;
|
|
80
|
-
}, { behavior: "immediate" });
|
|
43
|
+
}
|
|
44
|
+
const rowIds = await contiguousMemberRowIds(tx, scope, replacement.memberUpdateIds);
|
|
45
|
+
const [firstRowId, ...mergedAwayRowIds] = rowIds ?? [];
|
|
46
|
+
if (firstRowId === undefined)
|
|
47
|
+
return null;
|
|
48
|
+
const updateId = crypto.randomUUID();
|
|
49
|
+
await tx
|
|
50
|
+
.update(documentHistoryUpdates)
|
|
51
|
+
.set({
|
|
52
|
+
updateId,
|
|
53
|
+
updateData: replacement.merged.updateData,
|
|
54
|
+
partialStartVersionVector: replacement.merged.partialStartVersionVector,
|
|
55
|
+
partialEndVersionVector: replacement.merged.partialEndVersionVector,
|
|
56
|
+
sourceVersionVector: null,
|
|
57
|
+
rekeyCount: replacement.rekeyCount,
|
|
58
|
+
})
|
|
59
|
+
.where(eq(documentHistoryUpdates.id, firstRowId))
|
|
60
|
+
.run();
|
|
61
|
+
await tx
|
|
62
|
+
.delete(documentHistoryUpdates)
|
|
63
|
+
.where(inArray(documentHistoryUpdates.id, mergedAwayRowIds))
|
|
64
|
+
.run();
|
|
65
|
+
return updateId;
|
|
81
66
|
}
|
|
82
67
|
//# sourceMappingURL=documentQueuedUpdateReplacement.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentQueuedUpdateReplacement.js","sourceRoot":"","sources":["documentQueuedUpdateReplacement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6CAA6C,CAAC;AAErF,OAAO,
|
|
1
|
+
{"version":3,"file":"documentQueuedUpdateReplacement.js","sourceRoot":"","sources":["documentQueuedUpdateReplacement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6CAA6C,CAAC;AAErF,OAAO,EACL,sCAAsC,EACtC,aAAa,EACb,UAAU,GACX,MAAM,oCAAoC,CAAC;AAiB5C;;;;GAIG;AACH,KAAK,UAAU,sBAAsB,CACnC,EAAgC,EAChC,KAAoB,EACpB,eAAkC;IAElC,MAAM,MAAM,GAAG,MAAM,EAAE;SACpB,MAAM,CAAC;QACN,EAAE,EAAE,sBAAsB,CAAC,EAAE;QAC7B,MAAM,EAAE,sBAAsB,CAAC,MAAM;QACrC,mBAAmB,EAAE,sBAAsB,CAAC,mBAAmB;QAC/D,QAAQ,EAAE,sBAAsB,CAAC,QAAQ;KAC1C,CAAC;SACD,IAAI,CAAC,sBAAsB,CAAC;SAC5B,KAAK,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5B,CAAC;IACF,MAAM,CAAC,UAAU,EAAE,GAAG,gBAAgB,CAAC,GAAG,MAAM,IAAI,EAAE,CAAC;IACvD,IAAI,UAAU,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IAC1C,MAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;IACrC,MAAM,EAAE;SACL,MAAM,CAAC,sBAAsB,CAAC;SAC9B,GAAG,CAAC;QACH,QAAQ;QACR,UAAU,EAAE,WAAW,CAAC,MAAM,CAAC,UAAU;QACzC,yBAAyB,EAAE,WAAW,CAAC,MAAM,CAAC,yBAAyB;QACvE,uBAAuB,EAAE,WAAW,CAAC,MAAM,CAAC,uBAAuB;QACnE,mBAAmB,EAAE,IAAI;QACzB,UAAU,EAAE,WAAW,CAAC,UAAU;KACnC,CAAC;SACD,KAAK,CAAC,EAAE,CAAC,sBAAsB,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;SAChD,GAAG,EAAE,CAAC;IACT,MAAM,EAAE;SACL,MAAM,CAAC,sBAAsB,CAAC;SAC9B,KAAK,CAAC,OAAO,CAAC,sBAAsB,CAAC,EAAE,EAAE,gBAAgB,CAAC,CAAC;SAC3D,GAAG,EAAE,CAAC;IACT,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import { eq, inArray } from \"drizzle-orm\";\nimport { documentHistoryUpdates } from \"../../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport {\n documentWriteFenceMatchesInTransaction,\n queuedInScope,\n queueOrder,\n} from \"./documentPendingUpdatePersistence\";\nimport type {\n DocumentScope,\n PendingUpdateFields,\n} from \"./documentPersistenceTypes\";\n\n/** Replace a run of queued local updates with one merged update. */\nexport interface QueuedUpdateReplacement {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n /** Members' current update ids, in queue order; at least two. */\n memberUpdateIds: readonly string[];\n /** The merged update. It is never a rotation checkpoint. */\n merged: Omit<PendingUpdateFields, \"sourceVersionVector\">;\n rekeyCount: number;\n}\n\n/**\n * The row ids of the members when they are still exactly a contiguous run of\n * ordinary local queued rows, starting with the first listed member;\n * otherwise null.\n */\nasync function contiguousMemberRowIds(\n tx: ClientSQLiteTransactionScope,\n scope: DocumentScope,\n memberUpdateIds: readonly string[],\n): Promise<string[] | null> {\n const queued = await tx\n .select({\n id: documentHistoryUpdates.id,\n origin: documentHistoryUpdates.origin,\n sourceVersionVector: documentHistoryUpdates.sourceVersionVector,\n updateId: documentHistoryUpdates.updateId,\n })\n .from(documentHistoryUpdates)\n .where(queuedInScope(scope))\n .orderBy(...queueOrder());\n const first = queued.findIndex((row) => row.updateId === memberUpdateIds[0]);\n if (first < 0) return null;\n const run = queued.slice(first, first + memberUpdateIds.length);\n const rowIds: string[] = [];\n for (const [index, row] of run.entries()) {\n if (\n row.id === null ||\n row.updateId !== memberUpdateIds[index] ||\n row.origin !== \"local\" ||\n row.sourceVersionVector !== null\n ) {\n return null;\n }\n rowIds.push(row.id);\n }\n return rowIds.length === memberUpdateIds.length ? rowIds : null;\n}\n\n/**\n * The checks and writes of `replaceDocumentQueuedUpdates`, inside the\n * caller's transaction: null unless every check holds.\n */\nexport async function replaceDocumentQueuedUpdatesInTransaction(\n tx: ClientSQLiteTransactionScope,\n scope: DocumentScope,\n replacement: QueuedUpdateReplacement,\n): Promise<string | null> {\n if (!(await documentWriteFenceMatchesInTransaction(tx, scope, replacement))) {\n return null;\n }\n const rowIds = await contiguousMemberRowIds(\n tx,\n scope,\n replacement.memberUpdateIds,\n );\n const [firstRowId, ...mergedAwayRowIds] = rowIds ?? [];\n if (firstRowId === undefined) return null;\n const updateId = crypto.randomUUID();\n await tx\n .update(documentHistoryUpdates)\n .set({\n updateId,\n updateData: replacement.merged.updateData,\n partialStartVersionVector: replacement.merged.partialStartVersionVector,\n partialEndVersionVector: replacement.merged.partialEndVersionVector,\n sourceVersionVector: null,\n rekeyCount: replacement.rekeyCount,\n })\n .where(eq(documentHistoryUpdates.id, firstRowId))\n .run();\n await tx\n .delete(documentHistoryUpdates)\n .where(inArray(documentHistoryUpdates.id, mergedAwayRowIds))\n .run();\n return updateId;\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { DocumentRecord, DocumentScope, SelectedDocumentRecordRow } from "./documentPersistenceTypes.js";
|
|
1
|
+
import type { DocumentRecord, SelectedDocumentRecordRow } from "./documentPersistenceTypes.js";
|
|
3
2
|
/**
|
|
4
3
|
* The ONE select shape for a full documents-row record: every reader that
|
|
5
4
|
* feeds mapSelectedDocumentRecord spreads this, so a new record column cannot
|
|
@@ -270,11 +269,9 @@ export declare const documentRecordSelection: {
|
|
|
270
269
|
}>;
|
|
271
270
|
};
|
|
272
271
|
export declare function mapSelectedDocumentRecord(row: SelectedDocumentRecordRow): DocumentRecord;
|
|
273
|
-
export declare function loadDocumentRecord(execSql: ExecSql, scope: DocumentScope): Promise<DocumentRecord | null>;
|
|
274
|
-
export declare function findLocalIdByDocumentId(execSql: ExecSql, appKind: string, documentId: string): Promise<string | null>;
|
|
275
272
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
273
|
+
* Duplicate remote identities can survive an interrupted local adoption, so
|
|
274
|
+
* the row to use for a document id is chosen by this order: queued or
|
|
275
|
+
* deferred edits first, then a stable fallback.
|
|
278
276
|
*/
|
|
279
|
-
export declare function
|
|
280
|
-
export declare function deleteDocumentRecord(execSql: ExecSql, scope: DocumentScope): Promise<void>;
|
|
277
|
+
export declare function localIdPreferenceOrder(): import("drizzle-orm").SQL<unknown>[];
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { desc, sql } from "drizzle-orm";
|
|
2
2
|
import { deserializeDocumentSyncPullContinuation } from "../../../documents/shared/sync/pullContinuation.js";
|
|
3
3
|
import { normalizeEffectiveAccessLevel } from "../../../keying/accessLevel.js";
|
|
4
4
|
import { documentHistoryUpdates, documents, } from "../../../sqlite/schema/documentRecordSchema.js";
|
|
5
|
-
import { sqliteRowsPerStatement } from "../../../sqlite/sqliteBindBudget.js";
|
|
6
|
-
import { getClientSQLitePersistenceRuntime } from "../../../sqlite/sqlitePersistenceRuntime.js";
|
|
7
5
|
/**
|
|
8
6
|
* The ONE select shape for a full documents-row record: every reader that
|
|
9
7
|
* feeds mapSelectedDocumentRecord spreads this, so a new record column cannot
|
|
@@ -58,21 +56,12 @@ export function mapSelectedDocumentRecord(row) {
|
|
|
58
56
|
}
|
|
59
57
|
return record;
|
|
60
58
|
}
|
|
61
|
-
export async function loadDocumentRecord(execSql, scope) {
|
|
62
|
-
const { db } = getClientSQLitePersistenceRuntime(execSql);
|
|
63
|
-
const rows = await db
|
|
64
|
-
.select(documentRecordSelection)
|
|
65
|
-
.from(documents)
|
|
66
|
-
.where(and(eq(documents.appKind, scope.appKind), eq(documents.localId, scope.localId)))
|
|
67
|
-
.limit(1);
|
|
68
|
-
return rows[0] ? mapSelectedDocumentRecord(rows[0]) : null;
|
|
69
|
-
}
|
|
70
59
|
/**
|
|
71
60
|
* Duplicate remote identities can survive an interrupted local adoption, so
|
|
72
61
|
* the row to use for a document id is chosen by this order: queued or
|
|
73
62
|
* deferred edits first, then a stable fallback.
|
|
74
63
|
*/
|
|
75
|
-
function localIdPreferenceOrder() {
|
|
64
|
+
export function localIdPreferenceOrder() {
|
|
76
65
|
const hasPendingUpdates = sql `EXISTS (
|
|
77
66
|
SELECT 1
|
|
78
67
|
FROM ${documentHistoryUpdates}
|
|
@@ -93,46 +82,4 @@ function localIdPreferenceOrder() {
|
|
|
93
82
|
desc(documents.localId),
|
|
94
83
|
];
|
|
95
84
|
}
|
|
96
|
-
export async function findLocalIdByDocumentId(execSql, appKind, documentId) {
|
|
97
|
-
const { db } = getClientSQLitePersistenceRuntime(execSql);
|
|
98
|
-
const rows = await db
|
|
99
|
-
.select({ localId: documents.localId })
|
|
100
|
-
.from(documents)
|
|
101
|
-
.where(and(eq(documents.appKind, appKind), eq(documents.documentId, documentId)))
|
|
102
|
-
.orderBy(...localIdPreferenceOrder())
|
|
103
|
-
.limit(1);
|
|
104
|
-
return rows[0]?.localId ?? null;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* `findLocalIdByDocumentId` for many document ids: one read per chunk, in the
|
|
108
|
-
* same preference order, keeping the first row per document id.
|
|
109
|
-
*/
|
|
110
|
-
export async function findLocalIdsByDocumentIds(execSql, appKind, documentIds) {
|
|
111
|
-
const { db } = getClientSQLitePersistenceRuntime(execSql);
|
|
112
|
-
const localIds = new Map();
|
|
113
|
-
const unique = Array.from(new Set(documentIds));
|
|
114
|
-
// One bind for the app kind; the rest carry document ids.
|
|
115
|
-
const chunkSize = sqliteRowsPerStatement(1, 1);
|
|
116
|
-
for (let offset = 0; offset < unique.length; offset += chunkSize) {
|
|
117
|
-
const rows = await db
|
|
118
|
-
.select({ documentId: documents.documentId, localId: documents.localId })
|
|
119
|
-
.from(documents)
|
|
120
|
-
.where(and(eq(documents.appKind, appKind), inArray(documents.documentId, unique.slice(offset, offset + chunkSize))))
|
|
121
|
-
.orderBy(...localIdPreferenceOrder());
|
|
122
|
-
for (const row of rows) {
|
|
123
|
-
if (row.documentId !== null && !localIds.has(row.documentId)) {
|
|
124
|
-
localIds.set(row.documentId, row.localId);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return localIds;
|
|
129
|
-
}
|
|
130
|
-
export async function deleteDocumentRecord(execSql, scope) {
|
|
131
|
-
await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
|
|
132
|
-
await db
|
|
133
|
-
.delete(documents)
|
|
134
|
-
.where(and(eq(documents.appKind, scope.appKind), eq(documents.localId, scope.localId)))
|
|
135
|
-
.run();
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
85
|
//# sourceMappingURL=documentRecordPersistence.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentRecordPersistence.js","sour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,UAAU,KAAK,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC7D,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;YAC5C,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,OAAgB,EAChB,KAAoB;IAEpB,MAAM,iCAAiC,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QACxE,MAAM,EAAE;aACL,MAAM,CAAC,SAAS,CAAC;aACjB,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACpC,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CACrC,CACF;aACA,GAAG,EAAE,CAAC;IACX,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { and, desc, eq, inArray, sql } from \"drizzle-orm\";\nimport { deserializeDocumentSyncPullContinuation } from \"../../../documents/shared/sync/pullContinuation\";\nimport { normalizeEffectiveAccessLevel } from \"../../../keying/accessLevel\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport { sqliteRowsPerStatement } from \"../../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport type {\n DocumentRecord,\n DocumentScope,\n SelectedDocumentRecordRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * The ONE select shape for a full documents-row record: every reader that\n * feeds mapSelectedDocumentRecord spreads this, so a new record column cannot\n * silently miss one of them.\n */\nexport const documentRecordSelection = {\n id: documents.localId,\n documentId: documents.documentId,\n recoveryGeneration: documents.recoveryGeneration,\n recoveryDocumentId: documents.recoveryDocumentId,\n snapshotEndVersion: documents.snapshotEndVersion,\n accessEpoch: documents.accessEpoch,\n accessStateHash: documents.accessStateHash,\n effectiveAccessLevel: documents.effectiveAccessLevel,\n lastCommitLsn: documents.lastCommitLsn,\n documentManifestBundle: documents.documentManifestBundle,\n contentKeyBundle: documents.contentKeyBundle,\n documentKekTargets: documents.documentKekTargets,\n pendingBaseVersion: documents.pendingBaseVersion,\n pullContinuation: documents.pullContinuation,\n};\n\nexport function mapSelectedDocumentRecord(\n row: SelectedDocumentRecordRow,\n): DocumentRecord {\n const record: DocumentRecord = {\n id: row.id,\n documentId: row.documentId,\n recoveryGeneration: row.recoveryGeneration,\n snapshotEndVersion: row.snapshotEndVersion,\n accessEpoch: row.accessEpoch,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n row.effectiveAccessLevel,\n ),\n lastCommitLsn: row.lastCommitLsn,\n contentKeyBundle: row.contentKeyBundle,\n documentKekTargets: row.documentKekTargets,\n documentManifestBundle: row.documentManifestBundle,\n };\n\n if (row.accessStateHash !== null) {\n record.accessStateHash = row.accessStateHash;\n }\n\n if (row.recoveryDocumentId !== null) {\n record.recoveryDocumentId = row.recoveryDocumentId;\n }\n\n // Only surface the marker when one was persisted, so a row that never stored\n // one keeps its prior shape (and init falls through to seeding it).\n if (row.pendingBaseVersion !== null) {\n record.pendingBaseVersion = row.pendingBaseVersion;\n }\n\n const pullContinuation = deserializeDocumentSyncPullContinuation(\n row.pullContinuation,\n );\n if (pullContinuation !== null) {\n record.pullContinuation = pullContinuation;\n } else if (row.pullContinuation !== null) {\n record.pullContinuationRecoveryRequired = true;\n }\n\n return record;\n}\n\nexport async function loadDocumentRecord(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<DocumentRecord | null> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select(documentRecordSelection)\n .from(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n ),\n )\n .limit(1);\n\n return rows[0] ? mapSelectedDocumentRecord(rows[0]) : null;\n}\n\n/**\n * Duplicate remote identities can survive an interrupted local adoption, so\n * the row to use for a document id is chosen by this order: queued or\n * deferred edits first, then a stable fallback.\n */\nfunction localIdPreferenceOrder() {\n const hasPendingUpdates = sql<number>`EXISTS (\n SELECT 1\n FROM ${documentHistoryUpdates}\n WHERE ${documentHistoryUpdates.appKind} = ${documents.appKind}\n AND ${documentHistoryUpdates.localId} = ${documents.localId}\n AND ${documentHistoryUpdates.updateId} IS NOT NULL\n )`;\n const hasPendingLocalWork = sql<number>`(\n ${hasPendingUpdates}\n OR (\n ${documents.pendingBaseVersion} IS NOT NULL\n AND ${documents.pendingBaseVersion} <> ${documents.snapshotEndVersion}\n )\n )`;\n return [\n desc(hasPendingLocalWork),\n desc(documents.updatedAt),\n desc(documents.localId),\n ];\n}\n\nexport async function findLocalIdByDocumentId(\n execSql: ExecSql,\n appKind: string,\n documentId: string,\n): Promise<string | null> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(eq(documents.appKind, appKind), eq(documents.documentId, documentId)),\n )\n .orderBy(...localIdPreferenceOrder())\n .limit(1);\n\n return rows[0]?.localId ?? null;\n}\n\n/**\n * `findLocalIdByDocumentId` for many document ids: one read per chunk, in the\n * same preference order, keeping the first row per document id.\n */\nexport async function findLocalIdsByDocumentIds(\n execSql: ExecSql,\n appKind: string,\n documentIds: ReadonlyArray<string>,\n): Promise<Map<string, string>> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const localIds = new Map<string, string>();\n const unique = Array.from(new Set(documentIds));\n // One bind for the app kind; the rest carry document ids.\n const chunkSize = sqliteRowsPerStatement(1, 1);\n for (let offset = 0; offset < unique.length; offset += chunkSize) {\n const rows = await db\n .select({ documentId: documents.documentId, localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, appKind),\n inArray(\n documents.documentId,\n unique.slice(offset, offset + chunkSize),\n ),\n ),\n )\n .orderBy(...localIdPreferenceOrder());\n for (const row of rows) {\n if (row.documentId !== null && !localIds.has(row.documentId)) {\n localIds.set(row.documentId, row.localId);\n }\n }\n }\n return localIds;\n}\n\nexport async function deleteDocumentRecord(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await db\n .delete(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n ),\n )\n .run();\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"documentRecordPersistence.js","sourceRoot":"","sources":["documentRecordPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,uCAAuC,EAAE,MAAM,iDAAiD,CAAC;AAC1G,OAAO,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EACL,sBAAsB,EACtB,SAAS,GACV,MAAM,6CAA6C,CAAC;AAMrD;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,EAAE,EAAE,SAAS,CAAC,OAAO;IACrB,UAAU,EAAE,SAAS,CAAC,UAAU;IAChC,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,WAAW,EAAE,SAAS,CAAC,WAAW;IAClC,eAAe,EAAE,SAAS,CAAC,eAAe;IAC1C,oBAAoB,EAAE,SAAS,CAAC,oBAAoB;IACpD,aAAa,EAAE,SAAS,CAAC,aAAa;IACtC,sBAAsB,EAAE,SAAS,CAAC,sBAAsB;IACxD,gBAAgB,EAAE,SAAS,CAAC,gBAAgB;IAC5C,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,kBAAk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,gCAAgC,GAAG,IAAI,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,sBAAsB;IACpC,MAAM,iBAAiB,GAAG,GAAG,CAAQ;;WAE5B,sBAAsB;YACrB,sBAAsB,CAAC,OAAO,MAAM,SAAS,CAAC,OAAO;YACrD,sBAAsB,CAAC,OAAO,MAAM,SAAS,CAAC,OAAO;YACrD,sBAAsB,CAAC,QAAQ;IACvC,CAAC;IACH,MAAM,mBAAmB,GAAG,GAAG,CAAQ;MACnC,iBAAiB;;QAEf,SAAS,CAAC,kBAAkB;YACxB,SAAS,CAAC,kBAAkB,OAAO,SAAS,CAAC,kBAAkB;;IAEvE,CAAC;IACH,OAAO;QACL,IAAI,CAAC,mBAAmB,CAAC;QACzB,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC;QACzB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;KACxB,CAAC;AACJ,CAAC","sourcesContent":["import { desc, sql } from \"drizzle-orm\";\nimport { deserializeDocumentSyncPullContinuation } from \"../../../documents/shared/sync/pullContinuation\";\nimport { normalizeEffectiveAccessLevel } from \"../../../keying/accessLevel\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport type {\n DocumentRecord,\n SelectedDocumentRecordRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * The ONE select shape for a full documents-row record: every reader that\n * feeds mapSelectedDocumentRecord spreads this, so a new record column cannot\n * silently miss one of them.\n */\nexport const documentRecordSelection = {\n id: documents.localId,\n documentId: documents.documentId,\n recoveryGeneration: documents.recoveryGeneration,\n recoveryDocumentId: documents.recoveryDocumentId,\n snapshotEndVersion: documents.snapshotEndVersion,\n accessEpoch: documents.accessEpoch,\n accessStateHash: documents.accessStateHash,\n effectiveAccessLevel: documents.effectiveAccessLevel,\n lastCommitLsn: documents.lastCommitLsn,\n documentManifestBundle: documents.documentManifestBundle,\n contentKeyBundle: documents.contentKeyBundle,\n documentKekTargets: documents.documentKekTargets,\n pendingBaseVersion: documents.pendingBaseVersion,\n pullContinuation: documents.pullContinuation,\n};\n\nexport function mapSelectedDocumentRecord(\n row: SelectedDocumentRecordRow,\n): DocumentRecord {\n const record: DocumentRecord = {\n id: row.id,\n documentId: row.documentId,\n recoveryGeneration: row.recoveryGeneration,\n snapshotEndVersion: row.snapshotEndVersion,\n accessEpoch: row.accessEpoch,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n row.effectiveAccessLevel,\n ),\n lastCommitLsn: row.lastCommitLsn,\n contentKeyBundle: row.contentKeyBundle,\n documentKekTargets: row.documentKekTargets,\n documentManifestBundle: row.documentManifestBundle,\n };\n\n if (row.accessStateHash !== null) {\n record.accessStateHash = row.accessStateHash;\n }\n\n if (row.recoveryDocumentId !== null) {\n record.recoveryDocumentId = row.recoveryDocumentId;\n }\n\n // Only surface the marker when one was persisted, so a row that never stored\n // one keeps its prior shape (and init falls through to seeding it).\n if (row.pendingBaseVersion !== null) {\n record.pendingBaseVersion = row.pendingBaseVersion;\n }\n\n const pullContinuation = deserializeDocumentSyncPullContinuation(\n row.pullContinuation,\n );\n if (pullContinuation !== null) {\n record.pullContinuation = pullContinuation;\n } else if (row.pullContinuation !== null) {\n record.pullContinuationRecoveryRequired = true;\n }\n\n return record;\n}\n\n/**\n * Duplicate remote identities can survive an interrupted local adoption, so\n * the row to use for a document id is chosen by this order: queued or\n * deferred edits first, then a stable fallback.\n */\nexport function localIdPreferenceOrder() {\n const hasPendingUpdates = sql<number>`EXISTS (\n SELECT 1\n FROM ${documentHistoryUpdates}\n WHERE ${documentHistoryUpdates.appKind} = ${documents.appKind}\n AND ${documentHistoryUpdates.localId} = ${documents.localId}\n AND ${documentHistoryUpdates.updateId} IS NOT NULL\n )`;\n const hasPendingLocalWork = sql<number>`(\n ${hasPendingUpdates}\n OR (\n ${documents.pendingBaseVersion} IS NOT NULL\n AND ${documents.pendingBaseVersion} <> ${documents.snapshotEndVersion}\n )\n )`;\n return [\n desc(hasPendingLocalWork),\n desc(documents.updatedAt),\n desc(documents.localId),\n ];\n}\n"]}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { type DocumentSyncPullContinuation } from "../../../documents/shared/sync/pullContinuation.js";
|
|
2
|
+
import { type ExecSql } from "../../../sqlite/sqlSchema.js";
|
|
3
|
+
import { type DocumentHistoryCheckpointReplacement, type DocumentHistoryRestoreState, type DocumentHistoryTailEntry, type DocumentHistoryTailSize, type DocumentHistoryUpdateOrigin } from "./documentHistoryPersistence.js";
|
|
4
|
+
import type { DocumentRecord, DocumentScope, PendingUpdateFields, QueuedPendingUpdateRecord } from "./documentPersistenceTypes.js";
|
|
5
|
+
import { type QueuedUpdateReplacement } from "./documentQueuedUpdateReplacement.js";
|
|
6
|
+
import { type DocumentSyncFailureFields } from "./documentSyncFailurePersistence.js";
|
|
7
|
+
/** The exact continuation `invalidateDocumentSyncPullContinuation` replaces. */
|
|
8
|
+
export interface DocumentSyncPullContinuationInvalidation {
|
|
9
|
+
accessEpoch: number;
|
|
10
|
+
accessStateHash: string | null;
|
|
11
|
+
appKind: string;
|
|
12
|
+
continuation: DocumentSyncPullContinuation;
|
|
13
|
+
contentKeyBundle: string | null;
|
|
14
|
+
documentId: string;
|
|
15
|
+
documentKekTargets: string | null;
|
|
16
|
+
documentManifestBundle: string | null;
|
|
17
|
+
lastCommitLsn: string | null;
|
|
18
|
+
localId: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A document scope's record row, its history checkpoint and tail, its queue
|
|
22
|
+
* of pending updates and its recorded sync failure, bound to one executor
|
|
23
|
+
* and clock. Each method keeps the transaction shape it always had: reads
|
|
24
|
+
* run on the bound handle, writers run their own serialized mutation (or an
|
|
25
|
+
* immediate transaction where noted). Build it from the executor you hold;
|
|
26
|
+
* inside a locked section that is the locked handle. Work inside a caller's
|
|
27
|
+
* transaction takes the handle per call through the `*InTransaction`
|
|
28
|
+
* functions instead.
|
|
29
|
+
*/
|
|
30
|
+
export declare class DocumentRecordRepository {
|
|
31
|
+
private readonly execSql;
|
|
32
|
+
private readonly now;
|
|
33
|
+
constructor(execSql: ExecSql, now?: () => Date);
|
|
34
|
+
/** Replace only the exact rejected continuation with a durable recovery marker. */
|
|
35
|
+
invalidateDocumentSyncPullContinuation(input: DocumentSyncPullContinuationInvalidation): Promise<DocumentRecord | null>;
|
|
36
|
+
loadDocumentRecord(scope: DocumentScope): Promise<DocumentRecord | null>;
|
|
37
|
+
findLocalIdByDocumentId(appKind: string, documentId: string): Promise<string | null>;
|
|
38
|
+
/**
|
|
39
|
+
* `findLocalIdByDocumentId` for many document ids: one read per chunk, in
|
|
40
|
+
* the same preference order, keeping the first row per document id.
|
|
41
|
+
*/
|
|
42
|
+
findLocalIdsByDocumentIds(appKind: string, documentIds: ReadonlyArray<string>): Promise<Map<string, string>>;
|
|
43
|
+
deleteDocumentRecord(scope: DocumentScope): Promise<void>;
|
|
44
|
+
listDocumentPendingUpdates(scope: DocumentScope): Promise<QueuedPendingUpdateRecord[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Queue an update that has no history-tail role: container metadata (its
|
|
47
|
+
* content is its checkpoint) and full-state re-uploads after a remote reset.
|
|
48
|
+
*/
|
|
49
|
+
enqueueDocumentPendingUpdate(scope: DocumentScope, pendingUpdate: PendingUpdateFields): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* Append a locally authored update to the history tail AND queue it, as one
|
|
52
|
+
* row in one immediate transaction. A crash can therefore never leave the
|
|
53
|
+
* op restorable from history while missing from the outgoing queue, where a
|
|
54
|
+
* null-marker restore would classify it as covered and never send it.
|
|
55
|
+
*/
|
|
56
|
+
enqueueDocumentPendingUpdateWithHistory(scope: DocumentScope, pendingUpdate: PendingUpdateFields, options?: {
|
|
57
|
+
expectedDocumentId: string | null;
|
|
58
|
+
expectedRecoveryGeneration: number;
|
|
59
|
+
}): Promise<string | null>;
|
|
60
|
+
/**
|
|
61
|
+
* Reset the durable re-key budget for every queued update in a scope. The
|
|
62
|
+
* cap exists to stop a server that keeps conflicting fresh ids from driving
|
|
63
|
+
* a network-speed loop — but a budget burned during an unrelated outage
|
|
64
|
+
* (e.g. yesterday's stale-bundle deadlock 409ing every submit) should not
|
|
65
|
+
* be terminal forever. An explicit manual retry is the deliberate,
|
|
66
|
+
* rate-limited signal that conditions changed, so it gets a fresh budget.
|
|
67
|
+
*/
|
|
68
|
+
resetDocumentPendingUpdateRekeyBudget(scope: DocumentScope): Promise<void>;
|
|
69
|
+
/** See `MAX_PENDING_UPDATE_REKEYS`: the new id, or null with no progress. */
|
|
70
|
+
rekeyDocumentPendingUpdate(id: string): Promise<string | null>;
|
|
71
|
+
/** Settle one queued update by id, whatever its scope. */
|
|
72
|
+
settleDocumentPendingUpdate(id: string): Promise<void>;
|
|
73
|
+
unqueueDocumentPendingUpdates(scope: DocumentScope): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Replace queued local updates with one merged update, all or nothing, in
|
|
76
|
+
* one immediate transaction. Every check must hold or nothing changes and
|
|
77
|
+
* the result is null: the document still has the captured document id and
|
|
78
|
+
* recovery generation, and the members are still queued under their ids as
|
|
79
|
+
* ordinary local rows (no outbox row, no rotation checkpoint) forming one
|
|
80
|
+
* contiguous run in queue order that starts with the first listed member.
|
|
81
|
+
*
|
|
82
|
+
* The first member's row is updated in place, so it keeps its row id,
|
|
83
|
+
* `created_at` and rowid (its queue and history-tail position), and
|
|
84
|
+
* receives a fresh update id (an id is never reused for different
|
|
85
|
+
* content), the merged data and vectors, and `rekeyCount`. The other
|
|
86
|
+
* members are deleted. Resolves to the merged row's new update id.
|
|
87
|
+
*/
|
|
88
|
+
replaceDocumentQueuedUpdates(scope: DocumentScope, replacement: QueuedUpdateReplacement): Promise<string | null>;
|
|
89
|
+
appendDocumentHistoryUpdates(scope: DocumentScope, updates: readonly string[], origin: DocumentHistoryUpdateOrigin): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* The checkpoint + ordered tail for a scope, or null when NEITHER exists (a
|
|
92
|
+
* discovered shell that has not hydrated content yet; the caller starts the
|
|
93
|
+
* document empty and the remote pull supplies the content). A tail WITHOUT
|
|
94
|
+
* a checkpoint — a crash between the first enqueued edit's tail append and
|
|
95
|
+
* the birth-checkpoint seed — restores as tail-only (`snapshot: ""`): those
|
|
96
|
+
* tail rows can be the only durable copy of a local edit, so they must
|
|
97
|
+
* never be silently ignored.
|
|
98
|
+
*/
|
|
99
|
+
loadDocumentHistoryRestoreState(scope: DocumentScope): Promise<DocumentHistoryRestoreState | null>;
|
|
100
|
+
/**
|
|
101
|
+
* Size of the tail compaction can shrink. Queued rows are excluded: they
|
|
102
|
+
* stay until settlement, so counting them would re-export full history on
|
|
103
|
+
* every persist while offline edits wait to sync.
|
|
104
|
+
*/
|
|
105
|
+
readDocumentHistoryTailSize(scope: DocumentScope): Promise<DocumentHistoryTailSize>;
|
|
106
|
+
/**
|
|
107
|
+
* The current compactable tail rows WITH their payloads, so a compactor can
|
|
108
|
+
* prove each row's ops are covered by the snapshot it is about to install
|
|
109
|
+
* (another pane's append may carry ops this pane's document has not merged
|
|
110
|
+
* yet). Queued rows are never candidates.
|
|
111
|
+
*/
|
|
112
|
+
listDocumentHistoryTailEntries(scope: DocumentScope): Promise<DocumentHistoryTailEntry[]>;
|
|
113
|
+
/**
|
|
114
|
+
* Replace the scope's checkpoint with a fresh full-history snapshot and
|
|
115
|
+
* delete ONLY the tail rows the caller captured before exporting it. Every
|
|
116
|
+
* append path writes its tail row after the ops are already in the live
|
|
117
|
+
* document, so any row captured before the export is provably covered by
|
|
118
|
+
* the snapshot — while a row appended concurrently (between export and
|
|
119
|
+
* this delete) survives for the next compaction instead of being lost.
|
|
120
|
+
*/
|
|
121
|
+
replaceDocumentHistoryCheckpoint(scope: DocumentScope, input: DocumentHistoryCheckpointReplacement): Promise<boolean>;
|
|
122
|
+
deleteDocumentHistory(scope: DocumentScope): Promise<void>;
|
|
123
|
+
/**
|
|
124
|
+
* Record the last terminal failure or incoming-update quarantine on the
|
|
125
|
+
* scope's row; the caller supplies `attemptedAt`. A scope with no
|
|
126
|
+
* `documents` row has nothing left to explain, so it is a no-op there.
|
|
127
|
+
*/
|
|
128
|
+
recordDocumentSyncFailure(scope: DocumentScope, failure: DocumentSyncFailureFields): Promise<void>;
|
|
129
|
+
/** Whether any queued local write has recorded a terminal failure. */
|
|
130
|
+
hasRecordedTerminalSyncFailures(): Promise<boolean>;
|
|
131
|
+
/** Drop the recorded failure for a scope. */
|
|
132
|
+
clearDocumentSyncFailure(scope: DocumentScope): Promise<void>;
|
|
133
|
+
}
|