@tanstack/db-sqlite-persistence-core 0.4.7 → 0.4.9
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/README.md +32 -3
- package/dist/cjs/broadcast-coordinator.cjs +282 -43
- package/dist/cjs/broadcast-coordinator.cjs.map +1 -1
- package/dist/cjs/broadcast-coordinator.d.cts +25 -8
- package/dist/cjs/committed-tx-anchor.cjs +9 -0
- package/dist/cjs/committed-tx-anchor.cjs.map +1 -0
- package/dist/cjs/committed-tx-anchor.d.cts +1 -0
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/persisted.cjs +1599 -313
- package/dist/cjs/persisted.cjs.map +1 -1
- package/dist/cjs/persisted.d.cts +106 -13
- package/dist/cjs/remote-subset-wire.cjs +11 -0
- package/dist/cjs/remote-subset-wire.cjs.map +1 -1
- package/dist/cjs/remote-subset-wire.d.cts +1 -0
- package/dist/cjs/sqlite-core-adapter.cjs +705 -64
- package/dist/cjs/sqlite-core-adapter.cjs.map +1 -1
- package/dist/cjs/sqlite-core-adapter.d.cts +53 -10
- package/dist/esm/broadcast-coordinator.d.ts +25 -8
- package/dist/esm/broadcast-coordinator.js +283 -44
- package/dist/esm/broadcast-coordinator.js.map +1 -1
- package/dist/esm/committed-tx-anchor.d.ts +1 -0
- package/dist/esm/committed-tx-anchor.js +9 -0
- package/dist/esm/committed-tx-anchor.js.map +1 -0
- package/dist/esm/index.js +6 -2
- package/dist/esm/persisted.d.ts +106 -13
- package/dist/esm/persisted.js +1601 -315
- package/dist/esm/persisted.js.map +1 -1
- package/dist/esm/remote-subset-wire.d.ts +1 -0
- package/dist/esm/remote-subset-wire.js +11 -0
- package/dist/esm/remote-subset-wire.js.map +1 -1
- package/dist/esm/sqlite-core-adapter.d.ts +53 -10
- package/dist/esm/sqlite-core-adapter.js +708 -67
- package/dist/esm/sqlite-core-adapter.js.map +1 -1
- package/package.json +2 -2
- package/src/broadcast-coordinator.ts +415 -62
- package/src/committed-tx-anchor.ts +13 -0
- package/src/persisted.ts +2283 -370
- package/src/remote-subset-wire.ts +12 -0
- package/src/sqlite-core-adapter.ts +891 -68
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LoadSubsetOptions } from '@tanstack/db';
|
|
2
|
-
import { HydrationPersistenceAdapter, PersistedIndexSpec, PersistedKeySetEvidence, PersistedRowScanOptions, PersistedScannedRow, PersistedTx, PersistenceAdapter, ReplayableTxDelta, SQLiteDriver } from './persisted.cjs';
|
|
2
|
+
import { CommittedTxAnchor, HydrationPersistenceAdapter, PersistedCacheGenerationClaim, PersistedIndexSpec, PersistedKeySetEvidence, PersistedRowScanOptions, PersistedScannedRow, PersistedStorageTarget, PersistedTx, PersistenceAdapter, ReconciledCommittedTx, ReplayableTxDelta, SQLiteDriver } from './persisted.cjs';
|
|
3
3
|
type SQLiteCoreAdapterSchemaMismatchPolicy = `sync-present-reset` | `sync-absent-error` | `reset`;
|
|
4
4
|
export type SQLiteCoreAdapterOptions = {
|
|
5
5
|
driver: SQLiteDriver;
|
|
@@ -8,6 +8,10 @@ export type SQLiteCoreAdapterOptions = {
|
|
|
8
8
|
appliedTxPruneMaxRows?: number;
|
|
9
9
|
appliedTxPruneMaxAgeSeconds?: number;
|
|
10
10
|
pullSinceReloadThreshold?: number;
|
|
11
|
+
/** Milliseconds without renewal before a sync run loses cache access. */
|
|
12
|
+
cacheGenerationClaimTtlMs?: number;
|
|
13
|
+
/** Host clock, injectable for expiry histories. */
|
|
14
|
+
now?: () => number;
|
|
11
15
|
};
|
|
12
16
|
export type SQLitePullSinceResult<TKey extends string | number> = {
|
|
13
17
|
latestRowVersion: number;
|
|
@@ -19,6 +23,7 @@ export type SQLitePullSinceResult<TKey extends string | number> = {
|
|
|
19
23
|
deletedKeys: Array<TKey>;
|
|
20
24
|
deltas: Array<ReplayableTxDelta<Record<string, unknown>, TKey>>;
|
|
21
25
|
};
|
|
26
|
+
export declare const DEFAULT_CACHE_GENERATION_CLAIM_TTL_MS: number;
|
|
22
27
|
/**
|
|
23
28
|
* Default cap on retained `applied_tx` rows per collection. The log is a
|
|
24
29
|
* replayable cache, so a bounded row count keeps SQLite files from growing
|
|
@@ -43,9 +48,21 @@ export declare class SQLiteCorePersistenceAdapter implements PersistenceAdapter
|
|
|
43
48
|
private readonly appliedTxPruneMaxAgeSeconds;
|
|
44
49
|
private readonly pullSinceReloadThreshold;
|
|
45
50
|
private readonly replacementBatchSize;
|
|
51
|
+
private readonly cacheGenerationClaimTtlMs;
|
|
52
|
+
private readonly now;
|
|
46
53
|
private initialized;
|
|
47
54
|
private readonly collectionTableCache;
|
|
48
55
|
private readonly collectionTableLoads;
|
|
56
|
+
claimCacheGeneration(collectionId: string): Promise<PersistedCacheGenerationClaim>;
|
|
57
|
+
rotateCacheGeneration(collectionId: string, claimId: string, resetMetadata?: {
|
|
58
|
+
key: string;
|
|
59
|
+
value: unknown;
|
|
60
|
+
}, expectedStorageCollectionId?: string): Promise<PersistedCacheGenerationClaim>;
|
|
61
|
+
releaseCacheGenerationClaim(claimId: string): Promise<void>;
|
|
62
|
+
renewCacheGenerationClaim(storageCollectionId: string, claimId: string): Promise<number | undefined>;
|
|
63
|
+
private renewCacheGenerationClaimUnscheduled;
|
|
64
|
+
private collectRetiredCacheGenerations;
|
|
65
|
+
getCacheGenerationNow(): number;
|
|
49
66
|
constructor(options: SQLiteCoreAdapterOptions);
|
|
50
67
|
runInHydrationScope<T>(task: (adapter: HydrationPersistenceAdapter) => Promise<T>): Promise<T>;
|
|
51
68
|
runInRegularScope<T>(task: (adapter: HydrationPersistenceAdapter) => Promise<T>): Promise<T>;
|
|
@@ -54,17 +71,20 @@ export declare class SQLiteCorePersistenceAdapter implements PersistenceAdapter
|
|
|
54
71
|
private resolveScheduler;
|
|
55
72
|
private runInTransaction;
|
|
56
73
|
private assertCurrentSchemaVersion;
|
|
57
|
-
|
|
74
|
+
private assertCacheGenerationReadClaim;
|
|
75
|
+
loadSubset(target: PersistedStorageTarget, options: LoadSubsetOptions, ctx?: {
|
|
58
76
|
requiredIndexSignatures?: ReadonlyArray<string>;
|
|
77
|
+
cacheGenerationClaimId?: string;
|
|
59
78
|
}): Promise<Array<{
|
|
60
79
|
key: string | number;
|
|
61
80
|
value: Record<string, unknown>;
|
|
62
81
|
metadata?: unknown;
|
|
63
82
|
}>>;
|
|
64
83
|
private loadSubsetUnscheduled;
|
|
65
|
-
loadResumeSnapshot(
|
|
84
|
+
loadResumeSnapshot(target: PersistedStorageTarget, ctx?: {
|
|
66
85
|
requiredIndexSignatures?: ReadonlyArray<string>;
|
|
67
86
|
includeRows?: boolean;
|
|
87
|
+
cacheGenerationClaimId?: string;
|
|
68
88
|
}): Promise<{
|
|
69
89
|
rows: Array<{
|
|
70
90
|
key: string | number;
|
|
@@ -82,20 +102,41 @@ export declare class SQLiteCorePersistenceAdapter implements PersistenceAdapter
|
|
|
82
102
|
resetEpoch: number;
|
|
83
103
|
}>;
|
|
84
104
|
private loadResumeSnapshotUnscheduled;
|
|
85
|
-
applyCommittedTx(
|
|
105
|
+
applyCommittedTx(target: PersistedStorageTarget, tx: PersistedTx): Promise<void>;
|
|
106
|
+
reconcileCommittedTx(target: PersistedStorageTarget, tx: PersistedTx, anchor: CommittedTxAnchor): Promise<ReconciledCommittedTx>;
|
|
86
107
|
private applyCommittedTxUnscheduled;
|
|
87
|
-
|
|
108
|
+
private reconcileCommittedTxUnscheduled;
|
|
109
|
+
private applyCommittedTxInternal;
|
|
110
|
+
loadCollectionMetadata(target: PersistedStorageTarget, ctx?: {
|
|
111
|
+
cacheGenerationClaimId?: string;
|
|
112
|
+
}): Promise<Array<{
|
|
88
113
|
key: string;
|
|
89
114
|
value: unknown;
|
|
90
115
|
}>>;
|
|
91
116
|
private loadCollectionMetadataUnscheduled;
|
|
92
|
-
scanRows(
|
|
117
|
+
scanRows(target: PersistedStorageTarget, options?: PersistedRowScanOptions, ctx?: {
|
|
118
|
+
cacheGenerationClaimId?: string;
|
|
119
|
+
}): Promise<Array<PersistedScannedRow>>;
|
|
93
120
|
private scanRowsUnscheduled;
|
|
94
|
-
ensureIndex(
|
|
121
|
+
ensureIndex(target: PersistedStorageTarget, signature: string, spec: PersistedIndexSpec, ctx?: {
|
|
122
|
+
cacheGenerationClaimId?: string;
|
|
123
|
+
}): Promise<void>;
|
|
95
124
|
private ensureIndexUnscheduled;
|
|
96
|
-
markIndexRemoved(
|
|
125
|
+
markIndexRemoved(target: PersistedStorageTarget, signature: string, ctx?: {
|
|
126
|
+
cacheGenerationClaimId?: string;
|
|
127
|
+
}): Promise<void>;
|
|
97
128
|
private markIndexRemovedUnscheduled;
|
|
98
|
-
|
|
129
|
+
reserveLeadershipTerm(target: PersistedStorageTarget, observedTerm: number, ctx?: {
|
|
130
|
+
cacheGenerationClaimId?: string;
|
|
131
|
+
}): Promise<{
|
|
132
|
+
latestTerm: number;
|
|
133
|
+
latestSeq: number;
|
|
134
|
+
latestRowVersion: number;
|
|
135
|
+
}>;
|
|
136
|
+
private reserveLeadershipTermUnscheduled;
|
|
137
|
+
getStreamPosition(target: PersistedStorageTarget, ctx?: {
|
|
138
|
+
cacheGenerationClaimId?: string;
|
|
139
|
+
}): Promise<{
|
|
99
140
|
latestTerm: number;
|
|
100
141
|
latestSeq: number;
|
|
101
142
|
latestRowVersion: number;
|
|
@@ -104,7 +145,9 @@ export declare class SQLiteCorePersistenceAdapter implements PersistenceAdapter
|
|
|
104
145
|
private readLatestRowVersion;
|
|
105
146
|
private readStreamPosition;
|
|
106
147
|
private readKeySetEvidence;
|
|
107
|
-
pullSince(
|
|
148
|
+
pullSince(target: PersistedStorageTarget, fromRowVersion: number, ctx?: {
|
|
149
|
+
cacheGenerationClaimId?: string;
|
|
150
|
+
}): Promise<SQLitePullSinceResult<string | number>>;
|
|
108
151
|
private pullSinceUnscheduled;
|
|
109
152
|
private loadSubsetInternal;
|
|
110
153
|
private applyInMemoryWhere;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { LoadSubsetOptions } from '@tanstack/db';
|
|
2
2
|
import { RemoteSubsetOwner } from './remote-subset-owner.js';
|
|
3
|
-
import { ApplyCommittedTxResponse, ApplyLocalMutationsResponse, HydrationPersistenceAdapter, PersistedCollectionCoordinator, PersistedIndexSpec, PersistedMutationEnvelope, PersistedTx, PersistenceAdapter, ProtocolEnvelope, PullSinceResponse } from './persisted.js';
|
|
4
|
-
type CoordinatorAdapter = Pick<PersistenceAdapter, `loadSubset` | `applyCommittedTx` | `ensureIndex` | `runInRegularScope`> & {
|
|
5
|
-
pullSince?: (
|
|
3
|
+
import { ApplyCommittedTxResponse, ApplyLocalMutationsResponse, CommittedTxAnchor, HydrationPersistenceAdapter, PersistedCollectionCoordinator, PersistedIndexSpec, PersistedMutationEnvelope, PersistedStorageTarget, PersistedTx, PersistenceAdapter, ProtocolEnvelope, PullSinceResponse, ReconcileCommittedTxResponse } from './persisted.js';
|
|
4
|
+
type CoordinatorAdapter = Pick<PersistenceAdapter, `loadSubset` | `applyCommittedTx` | `reconcileCommittedTx` | `ensureIndex` | `runInRegularScope`> & {
|
|
5
|
+
pullSince?: (target: PersistedStorageTarget, fromRowVersion: number, ctx?: {
|
|
6
|
+
cacheGenerationClaimId?: string;
|
|
7
|
+
}) => Promise<{
|
|
6
8
|
latestRowVersion: number;
|
|
7
9
|
requiresFullReload: true;
|
|
8
10
|
} | {
|
|
@@ -11,7 +13,16 @@ type CoordinatorAdapter = Pick<PersistenceAdapter, `loadSubset` | `applyCommitte
|
|
|
11
13
|
changedKeys: Array<string | number>;
|
|
12
14
|
deletedKeys: Array<string | number>;
|
|
13
15
|
}>;
|
|
14
|
-
getStreamPosition?: (
|
|
16
|
+
getStreamPosition?: (target: PersistedStorageTarget, ctx?: {
|
|
17
|
+
cacheGenerationClaimId?: string;
|
|
18
|
+
}) => Promise<{
|
|
19
|
+
latestTerm: number;
|
|
20
|
+
latestSeq: number;
|
|
21
|
+
latestRowVersion: number;
|
|
22
|
+
}>;
|
|
23
|
+
reserveLeadershipTerm?: (target: PersistedStorageTarget, observedTerm: number, ctx?: {
|
|
24
|
+
cacheGenerationClaimId?: string;
|
|
25
|
+
}) => Promise<{
|
|
15
26
|
latestTerm: number;
|
|
16
27
|
latestSeq: number;
|
|
17
28
|
latestRowVersion: number;
|
|
@@ -30,6 +41,7 @@ export declare class BroadcastCollectionCoordinator implements PersistedCollecti
|
|
|
30
41
|
private readonly coordinatorName;
|
|
31
42
|
private defaultAdapter;
|
|
32
43
|
private readonly collectionAdapters;
|
|
44
|
+
private readonly collectionClaimIds;
|
|
33
45
|
private readonly remoteSubsetOwners;
|
|
34
46
|
private readonly remoteSubsetIds;
|
|
35
47
|
private readonly outboundRemoteSubsetAcquisitions;
|
|
@@ -56,7 +68,7 @@ export declare class BroadcastCollectionCoordinator implements PersistedCollecti
|
|
|
56
68
|
*/
|
|
57
69
|
setAdapter(adapter: CoordinatorAdapter): void;
|
|
58
70
|
/** Register the persistence adapter that owns one collection. */
|
|
59
|
-
setAdapterForCollection(collectionId: string, adapter: CoordinatorAdapter): void;
|
|
71
|
+
setAdapterForCollection(collectionId: string, adapter: CoordinatorAdapter, cacheGenerationClaimId?: string): void;
|
|
60
72
|
registerRemoteSubsetOwner(collectionId: string, owner: RemoteSubsetOwner): () => void;
|
|
61
73
|
getNodeId(): string;
|
|
62
74
|
subscribe(collectionId: string, onMessage: (message: ProtocolEnvelope<unknown>) => void): () => void;
|
|
@@ -65,11 +77,13 @@ export declare class BroadcastCollectionCoordinator implements PersistedCollecti
|
|
|
65
77
|
ensureLeadership(collectionId: string): Promise<void>;
|
|
66
78
|
requestEnsureRemoteSubset(collectionId: string, options: LoadSubsetOptions): Promise<void>;
|
|
67
79
|
requestReleaseRemoteSubset(collectionId: string, options: LoadSubsetOptions): Promise<void>;
|
|
80
|
+
private scheduleRemoteSubsetReleaseRetry;
|
|
68
81
|
private acquireRemoteSubset;
|
|
69
|
-
requestEnsurePersistedIndex(collectionId: string, signature: string, spec: PersistedIndexSpec, scopedAdapter?: HydrationPersistenceAdapter, localEnsureCompleted?: boolean): Promise<void>;
|
|
70
|
-
requestApplyLocalMutations(collectionId: string, mutations: Array<PersistedMutationEnvelope
|
|
82
|
+
requestEnsurePersistedIndex(collectionId: string, signature: string, spec: PersistedIndexSpec, scopedAdapter?: HydrationPersistenceAdapter, localEnsureCompleted?: boolean, cacheGenerationClaimId?: string): Promise<void>;
|
|
83
|
+
requestApplyLocalMutations(collectionId: string, mutations: Array<PersistedMutationEnvelope>, cacheGenerationClaimId?: string): Promise<ApplyLocalMutationsResponse>;
|
|
71
84
|
requestApplyCommittedTx(collectionId: string, tx: PersistedTx, scopedAdapter?: HydrationPersistenceAdapter): Promise<ApplyCommittedTxResponse>;
|
|
72
|
-
|
|
85
|
+
reconcileCommittedTx(collectionId: string, tx: PersistedTx, anchor: CommittedTxAnchor, scopedAdapter?: HydrationPersistenceAdapter): Promise<ReconcileCommittedTxResponse>;
|
|
86
|
+
pullSince(collectionId: string, fromRowVersion: number, scopedAdapter?: HydrationPersistenceAdapter, cacheGenerationClaimId?: string): Promise<PullSinceResponse>;
|
|
73
87
|
dispose(): void;
|
|
74
88
|
private getOrCreateCollectionState;
|
|
75
89
|
private ensureCollectionState;
|
|
@@ -105,6 +119,9 @@ export declare class BroadcastCollectionCoordinator implements PersistedCollecti
|
|
|
105
119
|
private handleApplyCommittedTx;
|
|
106
120
|
private runDeduplicatedEnvelope;
|
|
107
121
|
private applyCommittedTxOnce;
|
|
122
|
+
private publishCommittedTx;
|
|
123
|
+
private handleReconcileCommittedTx;
|
|
124
|
+
private nextPersistedTxIfLeader;
|
|
108
125
|
private applyDurablyAtNextStreamPosition;
|
|
109
126
|
private handlePullSince;
|
|
110
127
|
private withScheduledWriterLock;
|