tero 0.0.5 → 0.0.7
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 +199 -225
- package/dist/acid-engine.d.ts +214 -13
- package/dist/acid-engine.js +786 -282
- package/dist/backup.d.ts +31 -0
- package/dist/backup.js +137 -15
- package/dist/index.d.ts +268 -8
- package/dist/index.js +704 -71
- package/dist/recovery.d.ts +14 -0
- package/dist/recovery.js +100 -17
- package/package.json +5 -2
package/dist/acid-engine.d.ts
CHANGED
|
@@ -8,58 +8,259 @@ export interface LogEntry {
|
|
|
8
8
|
timestamp: number;
|
|
9
9
|
checksum: string;
|
|
10
10
|
}
|
|
11
|
+
export type SynchronousMode = 'full' | 'normal' | 'off';
|
|
11
12
|
export declare class WriteAheadLog {
|
|
12
13
|
private logPath;
|
|
13
14
|
private currentLSN;
|
|
14
|
-
private logBuffer;
|
|
15
|
-
private readonly BUFFER_SIZE;
|
|
16
15
|
private readonly LOG_FILE_SIZE_LIMIT;
|
|
17
|
-
|
|
16
|
+
private readonly ARCHIVE_KEEP_COUNT;
|
|
17
|
+
private synchronous;
|
|
18
|
+
private commitIntervalMs;
|
|
19
|
+
private dirty;
|
|
20
|
+
private groupCommitTimer?;
|
|
21
|
+
/**
|
|
22
|
+
* In-memory write buffer. WAL entries are stringified and pushed here instead of
|
|
23
|
+
* calling appendFileSync per entry. The buffer is flushed (one appendFileSync for
|
|
24
|
+
* all buffered entries) on durability barriers — commit in full mode, timer in
|
|
25
|
+
* normal mode, or explicit forceFlush. This eliminates N syscalls per transaction
|
|
26
|
+
* down to 1 syscall per flush.
|
|
27
|
+
*/
|
|
28
|
+
private writeBuffer;
|
|
29
|
+
private writeBufferSize;
|
|
30
|
+
private readonly FLUSH_THRESHOLD;
|
|
31
|
+
constructor(dbPath: string, synchronous?: SynchronousMode, commitIntervalMs?: number);
|
|
18
32
|
private initializeWAL;
|
|
33
|
+
/**
|
|
34
|
+
* fsync a file path by opening it read-only and synchronizing its (open) descriptor.
|
|
35
|
+
* This flushes the kernel page cache for the file to disk so commits are durable.
|
|
36
|
+
*/
|
|
37
|
+
private fsyncFile;
|
|
38
|
+
private fsyncDir;
|
|
39
|
+
/**
|
|
40
|
+
* Group-commit timer: in `normal` mode, fsyncs the WAL on a coalescing interval
|
|
41
|
+
* instead of per-commit. This amortizes the fsync cost across many commits,
|
|
42
|
+
* trading a small RPO window (commitIntervalMs, default 10ms) for 10–100x
|
|
43
|
+
* throughput. This is the same knob SQLite exposes as `PRAGMA synchronous=NORMAL`.
|
|
44
|
+
*/
|
|
45
|
+
private startGroupCommitTimer;
|
|
46
|
+
private stopGroupCommitTimer;
|
|
19
47
|
private recoverFromLog;
|
|
48
|
+
/**
|
|
49
|
+
* Stream WAL entries from disk line-by-line without loading the entire file
|
|
50
|
+
* into a single V8 string. Reads 64KB chunks, processes complete lines, and
|
|
51
|
+
* calls visitor(entry) for each valid (checksum-verified) entry.
|
|
52
|
+
*
|
|
53
|
+
* This prevents OOM on large WALs — a 200MB, 1M-entry log previously
|
|
54
|
+
* allocated ~400MB of V8 string objects via readFileSync + split(); now
|
|
55
|
+
* peak memory is bounded to ~64KB + any objects the visitor retains.
|
|
56
|
+
*/
|
|
57
|
+
streamLogEntries(visitor: (entry: LogEntry) => void): void;
|
|
20
58
|
private calculateChecksum;
|
|
21
59
|
private verifyChecksum;
|
|
60
|
+
/**
|
|
61
|
+
* Buffer a log entry in memory. The actual appendFileSync + fsync happens only
|
|
62
|
+
* when flushBuffer() is called — on COMMIT/ROLLBACK/CHECKPOINT barriers (full
|
|
63
|
+
* mode), on the group-commit timer (normal mode), or on explicit forceFlush.
|
|
64
|
+
*
|
|
65
|
+
* This eliminates per-entry syscalls: a transaction with N writes does 1
|
|
66
|
+
* appendFileSync (of all N+2 entries) instead of N+2 separate appends.
|
|
67
|
+
*/
|
|
22
68
|
writeLog(entry: Omit<LogEntry, 'lsn' | 'checksum' | 'timestamp'>): number;
|
|
23
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Flush the in-memory write buffer to disk in a single appendFileSync call.
|
|
71
|
+
* This is the ONLY place we call appendFileSync — all writeLog calls just
|
|
72
|
+
* buffer. After appending, optionally fsyncs (in full mode) and checks
|
|
73
|
+
* for rotation.
|
|
74
|
+
*/
|
|
75
|
+
flushBuffer(): void;
|
|
24
76
|
private checkLogRotation;
|
|
25
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Rotate the WAL: archive the current log to .wal.<timestamp>, start a fresh log,
|
|
79
|
+
* and write a CHECKPOINT entry at the head of the new log. Old archives beyond
|
|
80
|
+
* ARCHIVE_KEEP_COUNT are pruned locally (cloud upload in v2 retains durable copies).
|
|
81
|
+
*/
|
|
82
|
+
rotateLog(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Returns the paths of all locally retained WAL archives (newest first).
|
|
85
|
+
* Used by v2 backup to upload segments to the client's bucket.
|
|
86
|
+
*/
|
|
87
|
+
listArchives(): string[];
|
|
26
88
|
private cleanupOldArchives;
|
|
27
89
|
getLogEntries(fromLSN?: number): LogEntry[];
|
|
90
|
+
/**
|
|
91
|
+
* Explicit flush barrier. Always fsyncs regardless of synchronous mode, so
|
|
92
|
+
* callers can force durability on demand (e.g. before a bucket backup, on
|
|
93
|
+
* shutdown, or after a critical write). In `normal` mode this also clears
|
|
94
|
+
* the dirty flag so the next timer tick won't re-fsync.
|
|
95
|
+
*/
|
|
28
96
|
forceFlush(): void;
|
|
29
97
|
getCurrentLSN(): number;
|
|
30
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Truncate (clear) the WAL entirely. Only safe when there are no active transactions
|
|
100
|
+
* AND all data has been durably persisted to data files (which happens during commit).
|
|
101
|
+
* In v2 this is also called after a successful bucket snapshot upload to bound growth.
|
|
102
|
+
*/
|
|
31
103
|
truncateLog(): void;
|
|
104
|
+
destroy(): void;
|
|
32
105
|
}
|
|
33
106
|
export declare class LockManager {
|
|
34
107
|
private locks;
|
|
35
108
|
private readonly DEADLOCK_TIMEOUT;
|
|
36
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Acquire a lock. Returns `true` synchronously when the lock is granted
|
|
111
|
+
* immediately (uncontended fast path — zero Promise allocation). Returns a
|
|
112
|
+
* `Promise<void>` only when the lock is contended and the caller must wait.
|
|
113
|
+
* Callers should check: `if (result !== true) await result;`
|
|
114
|
+
*/
|
|
115
|
+
acquireLock(key: string, transactionId: string, lockType: 'shared' | 'exclusive'): true | Promise<void>;
|
|
37
116
|
private canGrantLock;
|
|
38
117
|
releaseLock(key: string, transactionId: string): void;
|
|
39
118
|
private processWaitQueue;
|
|
40
119
|
private removeLockRequest;
|
|
120
|
+
/**
|
|
121
|
+
* Release only the locks the given transaction actually holds. O(heldKeys) instead
|
|
122
|
+
* of O(allLocks) — the previous releaseAllLocks iterated every lock in the system
|
|
123
|
+
* on every commit, which dominated commit latency at scale.
|
|
124
|
+
*/
|
|
125
|
+
releaseLocksForTx(transactionId: string, heldKeys: Set<string>): void;
|
|
41
126
|
releaseAllLocks(transactionId: string): void;
|
|
42
127
|
detectDeadlock(): string[];
|
|
43
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Walk partitioned directories under dbPath and call `visitor(filePath)` for
|
|
131
|
+
* every *.json file found. Uses fs.opendir async iteration so each directory
|
|
132
|
+
* is streamed (no giant array allocation, no full readdir scan). This is
|
|
133
|
+
* what verifyDataIntegrity + backup enumeration use, replacing readdirSync.
|
|
134
|
+
*/
|
|
135
|
+
export declare function walkPartitions(dbPath: string, visitor: (filePath: string) => void | Promise<void>): Promise<void>;
|
|
44
136
|
export declare class ACIDStorageEngine {
|
|
45
137
|
private wal;
|
|
46
138
|
private lockManager;
|
|
47
139
|
private dbPath;
|
|
48
140
|
private activeTransactions;
|
|
49
|
-
|
|
141
|
+
/**
|
|
142
|
+
* In-memory pending-writes index per active transaction. This is the performant
|
|
143
|
+
* replacement for the previous per-op full-WAL re-read + filter. Each write/read
|
|
144
|
+
* within a transaction reads/writes this map directly in O(1) instead of replaying
|
|
145
|
+
* the entire WAL on every operation.
|
|
146
|
+
*
|
|
147
|
+
* Keyed by transactionId -> key -> { beforeImage, afterImage, op }
|
|
148
|
+
*/
|
|
149
|
+
private pendingWrites;
|
|
150
|
+
/**
|
|
151
|
+
* Compaction cadence: every COMMIT_INTERVAL commits, if there are no active
|
|
152
|
+
* transactions, we rotate the WAL into an archive and start a fresh log.
|
|
153
|
+
* This bounds WAL size without rewriting it on every commit.
|
|
154
|
+
*/
|
|
155
|
+
private readonly COMMIT_INTERVAL;
|
|
156
|
+
private commitCount;
|
|
157
|
+
/**
|
|
158
|
+
* Monotonic transaction counter + PID. Replaces randomUUID() in the hot path —
|
|
159
|
+
* a counter increment is ~10x faster than a crypto-random 128-bit UUID generation.
|
|
160
|
+
* Transaction IDs only need to be unique within a single process lifetime; on
|
|
161
|
+
* crash recovery the WAL is replayed and old txIds are resolved, so a fresh
|
|
162
|
+
* counter starting at 0 cannot collide with stale entries.
|
|
163
|
+
*/
|
|
164
|
+
private txCounter;
|
|
165
|
+
private readonly pid;
|
|
166
|
+
/**
|
|
167
|
+
* Deferred data-file write buffer. On commit, committed data moves here instead
|
|
168
|
+
* of being written to data files immediately. A background timer flushes this
|
|
169
|
+
* to disk every `dataFlushIntervalMs` (default 50ms). This is the SQLite WAL-mode
|
|
170
|
+
* architecture: the WAL is the durable copy; data files are a checkpointed cache
|
|
171
|
+
* rebuilt via redo on crash recovery.
|
|
172
|
+
*/
|
|
173
|
+
private committedBuffer;
|
|
174
|
+
private dataFlushTimer?;
|
|
175
|
+
private readonly DATA_FLUSH_INTERVAL_MS;
|
|
176
|
+
constructor(dbPath: string, synchronous?: SynchronousMode, commitIntervalMs?: number, dataFlushIntervalMs?: number);
|
|
50
177
|
private initializeStorage;
|
|
51
178
|
private performCrashRecovery;
|
|
52
179
|
private redoOperation;
|
|
53
180
|
private redoDelete;
|
|
54
181
|
private undoOperation;
|
|
182
|
+
/**
|
|
183
|
+
* Atomic data file write: write to a temp file then atomically rename over
|
|
184
|
+
* the target. The atomic rename guarantees readers never see a partial file
|
|
185
|
+
* (crash consistency). We intentionally do NOT fsync the temp file or parent
|
|
186
|
+
* directory here — the WAL is the durable ledger, and crash recovery's redo
|
|
187
|
+
* phase rebuilds data files from the WAL. This eliminates 2 of the 3 fsyncs
|
|
188
|
+
* per commit, leaving only the WAL fsync as the single durability barrier.
|
|
189
|
+
*/
|
|
190
|
+
private atomicWriteFile;
|
|
55
191
|
beginTransaction(): string;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
192
|
+
/**
|
|
193
|
+
* Write data for a transaction. Returns `void` synchronously when the lock is
|
|
194
|
+
* uncontended (the common case — fast path, zero Promise allocation). Returns
|
|
195
|
+
* `Promise<void>` only when the lock is contended and we must wait in queue.
|
|
196
|
+
* Callers should check: `if (result !== undefined) await result;`
|
|
197
|
+
*/
|
|
198
|
+
write(transactionId: string, key: string, data: any): void | Promise<void>;
|
|
199
|
+
/**
|
|
200
|
+
* Synchronous write work — called from write() after the lock is acquired.
|
|
201
|
+
* Pure in-memory + buffer operations: before-image lookup, deepMerge, WAL buffer,
|
|
202
|
+
* pendingWrites tracking. No syscalls, no Promises.
|
|
203
|
+
*/
|
|
204
|
+
private doWriteSync;
|
|
205
|
+
/**
|
|
206
|
+
* Read data for a transaction. Same pattern as write: returns the data
|
|
207
|
+
* synchronously when the lock is uncontended, returns a Promise only when
|
|
208
|
+
* contended.
|
|
209
|
+
*/
|
|
210
|
+
read(transactionId: string, key: string): any | Promise<any>;
|
|
211
|
+
private doReadSync;
|
|
212
|
+
/**
|
|
213
|
+
* Delete a key in a transaction. Same sync fast-path pattern as write().
|
|
214
|
+
*/
|
|
215
|
+
delete(transactionId: string, key: string): void | Promise<void>;
|
|
216
|
+
private doDeleteSync;
|
|
217
|
+
/**
|
|
218
|
+
* Commit a transaction. SYNCHRONOUS — the happy path has no awaits (WAL is
|
|
219
|
+
* buffered, data-file writes are deferred, locks are released sync). Making
|
|
220
|
+
* this sync eliminates 1-2 microtask ticks per commit (~5-10μs), which is
|
|
221
|
+
* significant when commit is the hot path of every create/update/delete.
|
|
222
|
+
*/
|
|
223
|
+
commitTransaction(transactionId: string): void;
|
|
224
|
+
/**
|
|
225
|
+
* Rollback a transaction. SYNCHRONOUS — matches commitTransaction. No async
|
|
226
|
+
* work on the happy path.
|
|
227
|
+
*/
|
|
228
|
+
rollbackTransaction(transactionId: string): void;
|
|
61
229
|
private deepMerge;
|
|
230
|
+
getTransactionStatus(transactionId: string): 'active' | 'committed' | 'aborted' | 'not_found';
|
|
231
|
+
/**
|
|
232
|
+
* O(1) check whether a transaction is active. Used by commit() in index.ts
|
|
233
|
+
* to avoid the O(N) getActiveTransactions().includes() allocation.
|
|
234
|
+
*/
|
|
235
|
+
isTransactionActive(transactionId: string): boolean;
|
|
62
236
|
getActiveTransactions(): string[];
|
|
63
237
|
forceCheckpoint(): void;
|
|
238
|
+
/**
|
|
239
|
+
* Returns a reference to the underlying WAL (used by v2 backup to upload
|
|
240
|
+
* archived segments to the client's bucket, and to drive snapshot+replay recovery).
|
|
241
|
+
*/
|
|
242
|
+
getWAL(): WriteAheadLog;
|
|
243
|
+
/**
|
|
244
|
+
* Check if a key has committed-but-unflushed data. Returns the data if present
|
|
245
|
+
* in committedBuffer, or `undefined` if not in the buffer (caller should check
|
|
246
|
+
* disk). Used by the get() fast path in index.ts.
|
|
247
|
+
*/
|
|
248
|
+
/**
|
|
249
|
+
* Return the pending afterImage for a key in an active transaction's pendingWrites.
|
|
250
|
+
* Used by index.ts write() to cache the MERGED result, not the raw user data.
|
|
251
|
+
*/
|
|
252
|
+
getPendingAfterImage(transactionId: string, key: string): any | undefined;
|
|
253
|
+
/**
|
|
254
|
+
* Check if a key has committed-but-unflushed data. Returns the data if present
|
|
255
|
+
* in committedBuffer, or `undefined` if not in the buffer (caller should check
|
|
256
|
+
* disk). Used by the get() fast path in index.ts.
|
|
257
|
+
*/
|
|
258
|
+
getCommittedData(key: string): any | undefined;
|
|
259
|
+
/**
|
|
260
|
+
* Force-flush the committedBuffer to data files. Called by the background timer
|
|
261
|
+
* and by forceCheckpoint(). Writes each buffered entry to its data file using
|
|
262
|
+
* the atomic temp→rename pattern, then clears the buffer.
|
|
263
|
+
*/
|
|
264
|
+
flushCommittedBuffer(): void;
|
|
64
265
|
destroy(): void;
|
|
65
266
|
}
|