tero 0.1.0 → 0.3.0
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 +64 -1
- package/dist/acid-engine.d.ts +224 -9
- package/dist/acid-engine.js +775 -158
- package/dist/backup.d.ts +38 -3
- package/dist/backup.js +269 -68
- package/dist/index.d.ts +51 -4
- package/dist/index.js +267 -135
- package/dist/recovery.js +2 -13
- package/dist/s3-config.d.ts +3 -0
- package/dist/s3-config.js +21 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,10 +46,16 @@ const db = new Tero({
|
|
|
46
46
|
|
|
47
47
|
await db.create('user1', { name: 'Alice', email: 'alice@example.com' });
|
|
48
48
|
const user = await db.get('user1');
|
|
49
|
-
await db.update('user1', { age: 30 });
|
|
49
|
+
await db.update('user1', { age: 30 }); // deep-merges into the document
|
|
50
|
+
await db.set('user1', { name: 'Alice' }); // replaces it: fields not given are removed
|
|
50
51
|
await db.remove('user1');
|
|
51
52
|
```
|
|
52
53
|
|
|
54
|
+
`update()` merges, so it can add or change fields but never remove one. When you
|
|
55
|
+
hold the whole document (read → modify → write), use `set()` — it is an upsert
|
|
56
|
+
that replaces the document exactly. Transactions have `tx.set()` and
|
|
57
|
+
`batchWrite()` accepts `{ key, data, replace: true }`.
|
|
58
|
+
|
|
53
59
|
## ACID transactions
|
|
54
60
|
|
|
55
61
|
All mutation methods (`create`, `update`, `remove`) are auto-wrapped in atomic ACID transactions with two-phase locking. `get()` provides a zero-latency point-in-time snapshot read (LRU cache → committed buffer → partitioned disk). For multi-step atomic workflows, use explicit transactions:
|
|
@@ -228,12 +234,69 @@ const db = new Tero({
|
|
|
228
234
|
directory: './data', // default: 'TeroDB'
|
|
229
235
|
cacheSize: 1000, // default: 100, configurable LRU capacity
|
|
230
236
|
synchronous: 'full', // 'full' (default, fsync per commit), 'normal' (coalesced timer), or 'off'
|
|
237
|
+
checkpointBatchSize: 5000,// default: 1000, committedBuffer entries flushed per tick
|
|
238
|
+
archiveKeepCount: 50, // default: 20, rotated 1MB WAL segments kept locally
|
|
231
239
|
fileLock: true, // default: true, cross-process .lock directory guard
|
|
240
|
+
durableCheckpointIntervalMs: 30000, // default: 30000; fsync data files + log a durable
|
|
241
|
+
// checkpoint so a restart replays only WAL written since
|
|
232
242
|
backup: { ... }, // optional: install a BackupConfig at construction
|
|
233
243
|
hydrateOnStartup: { ... },// optional: v2 hydration before engine init
|
|
234
244
|
});
|
|
235
245
|
```
|
|
236
246
|
|
|
247
|
+
### Crash recovery
|
|
248
|
+
|
|
249
|
+
Recovery is redo-only and starts at the last durable checkpoint (taken every
|
|
250
|
+
`durableCheckpointIntervalMs` and on close), so boot time and memory are bounded
|
|
251
|
+
by one checkpoint interval of writes, not by how long the process ran.
|
|
252
|
+
Uncommitted transactions never reach data files, so nothing is undone.
|
|
253
|
+
|
|
254
|
+
### Cloud recovery modes
|
|
255
|
+
|
|
256
|
+
`configureDataRecovery({ ..., mode })`:
|
|
257
|
+
|
|
258
|
+
- `'lazy'` (default): a missing key is fetched from the bucket on read, and
|
|
259
|
+
deletes/re-creates are mirrored there with tombstones.
|
|
260
|
+
- `'off'`: the bucket is never touched by `get/create/update/delete` — use it
|
|
261
|
+
when the local disk is authoritative and restores happen explicitly
|
|
262
|
+
(`Tero.restoreFromLiveBackup`, `recoverAllFromCloud`) before the database opens.
|
|
263
|
+
|
|
264
|
+
S3 clients use a 30s request timeout and 5s connect timeout by default
|
|
265
|
+
(`cloudStorage.requestTimeoutMs`, `cloudStorage.connectionTimeoutMs`).
|
|
266
|
+
|
|
267
|
+
### Live backup cadence and checkpoints
|
|
268
|
+
|
|
269
|
+
```javascript
|
|
270
|
+
db.enableLiveBackup({
|
|
271
|
+
consistency: 'interval', // or 'per-second' (intervalMs ≤ 1000)
|
|
272
|
+
intervalMs: 5000, // RPO; one PUT per ship — slower = cheaper
|
|
273
|
+
checkpointIntervalMs: 900000, // default 15 min, 0 disables
|
|
274
|
+
initialCheckpointDelayMs: 15000, // default 0: let the app boot before the full upload
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
The first checkpoint after every start is a FULL one (it re-bases whatever is on
|
|
279
|
+
disk — a restore, history whose WAL was pruned), so a restart never leaves
|
|
280
|
+
documents that exist only in old WAL. Periodic durable checkpoints fsync on the
|
|
281
|
+
libuv thread pool, so they don't block the event loop.
|
|
282
|
+
|
|
283
|
+
Each checkpoint uploads the documents changed since the last one, records the
|
|
284
|
+
replay floor in `latest.json`, and deletes bucket WAL objects that end before it.
|
|
285
|
+
A point-in-time restore can only go back to the latest checkpoint; asking for an
|
|
286
|
+
earlier moment throws instead of returning newer data.
|
|
287
|
+
|
|
288
|
+
### WAL retention and live backup
|
|
289
|
+
|
|
290
|
+
Every commit is appended to a WAL that rotates into immutable 1MB segments
|
|
291
|
+
(`.wal.seg-<start>-<end>`); those segments are what a point-in-time restore
|
|
292
|
+
replays, so `archiveKeepCount` is a recovery window, not a cache.
|
|
293
|
+
|
|
294
|
+
With `liveBackup` enabled the keep count is only a floor: a segment whose
|
|
295
|
+
entries have not been durably uploaded yet is **never** pruned, so a bucket
|
|
296
|
+
outage cannot turn a rotation into data loss. That deferral is bounded by a
|
|
297
|
+
per-WAL byte budget (64MB by default) — past it, retention falls back to
|
|
298
|
+
prune-oldest and logs a warning, because filling the disk would be worse.
|
|
299
|
+
|
|
237
300
|
## Testing
|
|
238
301
|
|
|
239
302
|
```bash
|
package/dist/acid-engine.d.ts
CHANGED
|
@@ -33,20 +33,84 @@ export interface LogEntry {
|
|
|
33
33
|
key?: string;
|
|
34
34
|
beforeImage?: JsonValue;
|
|
35
35
|
afterImage?: JsonValue;
|
|
36
|
+
/** CHECKPOINT only: every commit with lsn <= this record is durably on the data files. */
|
|
37
|
+
durable?: boolean;
|
|
38
|
+
/** CHECKPOINT only: last commit LSN captured BEFORE the data files were synced. Commits above it are redone on recovery even when they sit below this record's own LSN (they landed while the checkpoint was in flight). */
|
|
39
|
+
durableLsn?: number;
|
|
40
|
+
/** CHECKPOINT only: oldest LSN a transaction committing after this checkpoint may have written at. */
|
|
41
|
+
floorLsn?: number;
|
|
36
42
|
timestamp: number;
|
|
37
43
|
checksum: string;
|
|
38
44
|
}
|
|
39
45
|
export type SynchronousMode = 'full' | 'normal' | 'off';
|
|
46
|
+
/**
|
|
47
|
+
* Start a fresh WAL whose numbering continues at `lsn` (used after a restore).
|
|
48
|
+
* The single record is a durable CHECKPOINT: the restored data files are the
|
|
49
|
+
* complete state, so recovery replays nothing, and every LSN the engine issues
|
|
50
|
+
* from here on is above anything already in the bucket — a new session can never
|
|
51
|
+
* ship WAL objects that collide with history.
|
|
52
|
+
*/
|
|
53
|
+
export declare function writeWalSeed(dbPath: string, lsn: number): void;
|
|
54
|
+
/**
|
|
55
|
+
* Raw WAL lines with LSN > afterLsn, without parsing records. A record's own LSN is
|
|
56
|
+
* the last `"lsn":` field in its line (it is serialized after the document), so
|
|
57
|
+
* the document's content can never be mistaken for it. A trailing line without a
|
|
58
|
+
* newline (a write in progress) is left for the next read. Checksums are not
|
|
59
|
+
* verified here; whoever replays the lines (restore) verifies every record.
|
|
60
|
+
*/
|
|
61
|
+
export declare function readRawWalLines(path: string, afterLsn: number): {
|
|
62
|
+
lines: string[];
|
|
63
|
+
firstLsn: number | null;
|
|
64
|
+
lastLsn: number | null;
|
|
65
|
+
};
|
|
40
66
|
export declare function streamLogFile(filePath: string, visitor: (entry: LogEntry) => void, allowTornTail?: boolean): {
|
|
41
67
|
firstLsn: number | null;
|
|
42
68
|
lastLsn: number | null;
|
|
43
69
|
count: number;
|
|
44
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Rotated WAL segments a database keeps locally by default.
|
|
73
|
+
*
|
|
74
|
+
* The old value (3) was a 3MB cache, not a retention window: a 1MB WAL can
|
|
75
|
+
* rotate several times per second under load, so three segments could be
|
|
76
|
+
* unlinked inside the live shipper's 1s tick — the exact window in which their
|
|
77
|
+
* entries would have been uploaded. 20 segments (= 20MB at the 1MB segment
|
|
78
|
+
* size) gives the shipper ~20 rotations of slack. `setArchiveProtector` is what
|
|
79
|
+
* actually guarantees an unshipped segment survives, this is just the floor for
|
|
80
|
+
* databases without live backup.
|
|
81
|
+
*/
|
|
82
|
+
export declare const DEFAULT_ARCHIVE_KEEP_COUNT = 20;
|
|
83
|
+
/**
|
|
84
|
+
* Hard ceiling on the archives retained *only* because they have not been
|
|
85
|
+
* shipped yet (on top of the keep count). This is the disk-safety valve: a
|
|
86
|
+
* bucket that is unreachable for hours must not let the local WAL grow without
|
|
87
|
+
* bound. Segments past this budget are pruned oldest-first and a warning is
|
|
88
|
+
* emitted, because that is the one path where unshipped WAL is discarded.
|
|
89
|
+
*/
|
|
90
|
+
export declare const DEFAULT_ARCHIVE_PROTECT_MAX_BYTES: number;
|
|
91
|
+
export interface WriteAheadLogOptions {
|
|
92
|
+
/** Rotated segments retained locally. Default: DEFAULT_ARCHIVE_KEEP_COUNT. */
|
|
93
|
+
archiveKeepCount?: number;
|
|
94
|
+
/** Byte budget for not-yet-shipped segments. Default: DEFAULT_ARCHIVE_PROTECT_MAX_BYTES. */
|
|
95
|
+
archiveProtectMaxBytes?: number;
|
|
96
|
+
}
|
|
45
97
|
export declare class WriteAheadLog {
|
|
46
98
|
private logPath;
|
|
47
99
|
private currentLSN;
|
|
48
100
|
private readonly LOG_FILE_SIZE_LIMIT;
|
|
49
|
-
|
|
101
|
+
/** Retained rotated segments — see DEFAULT_ARCHIVE_KEEP_COUNT. */
|
|
102
|
+
private archiveKeepCount;
|
|
103
|
+
/** Disk-safety valve — see DEFAULT_ARCHIVE_PROTECT_MAX_BYTES. */
|
|
104
|
+
private archiveProtectMaxBytes;
|
|
105
|
+
/**
|
|
106
|
+
* Owner-supplied predicate answering "has this segment's last LSN NOT been
|
|
107
|
+
* durably shipped yet?". Live backup installs one so rotation can never
|
|
108
|
+
* unlink a segment before its entries reached the bucket. Absent for
|
|
109
|
+
* local-only databases, where pruning is unconditional.
|
|
110
|
+
*/
|
|
111
|
+
private archiveProtector?;
|
|
112
|
+
/** Rate limit for the disk-safety-valve warning (see the constant). */
|
|
113
|
+
private lastProtectOverflowWarnAt;
|
|
50
114
|
private synchronous;
|
|
51
115
|
private commitIntervalMs;
|
|
52
116
|
private dirty;
|
|
@@ -61,7 +125,7 @@ export declare class WriteAheadLog {
|
|
|
61
125
|
private writeBuffer;
|
|
62
126
|
private writeBufferSize;
|
|
63
127
|
private readonly FLUSH_THRESHOLD;
|
|
64
|
-
constructor(dbPath: string, synchronous?: SynchronousMode, commitIntervalMs?: number);
|
|
128
|
+
constructor(dbPath: string, synchronous?: SynchronousMode, commitIntervalMs?: number, options?: WriteAheadLogOptions);
|
|
65
129
|
private initializeWAL;
|
|
66
130
|
/**
|
|
67
131
|
* fsync a file path by opening it read-only and synchronizing its (open) descriptor.
|
|
@@ -75,9 +139,16 @@ export declare class WriteAheadLog {
|
|
|
75
139
|
* trading a small RPO window (commitIntervalMs, default 10ms) for 10–100x
|
|
76
140
|
* throughput. This is the same knob SQLite exposes as `PRAGMA synchronous=NORMAL`.
|
|
77
141
|
*/
|
|
142
|
+
private groupFsyncInFlight;
|
|
78
143
|
private startGroupCommitTimer;
|
|
79
144
|
private stopGroupCommitTimer;
|
|
80
145
|
private recoverFromLog;
|
|
146
|
+
/**
|
|
147
|
+
* LSN of the first record in the active log (null while it is empty). Tracked
|
|
148
|
+
* in memory so rotation doesn't have to re-read and re-checksum the whole
|
|
149
|
+
* file — at high write rates that re-read cost as much as the writes.
|
|
150
|
+
*/
|
|
151
|
+
private activeFirstLsn;
|
|
81
152
|
/**
|
|
82
153
|
* Stream WAL entries from disk line-by-line without loading the entire file
|
|
83
154
|
* into a single V8 string. Reads 64KB chunks, processes complete lines, and
|
|
@@ -91,6 +162,16 @@ export declare class WriteAheadLog {
|
|
|
91
162
|
* - Monotonic LSN strictly required within the log stream
|
|
92
163
|
*/
|
|
93
164
|
streamLogEntries(visitor: (entry: LogEntry) => void): void;
|
|
165
|
+
/**
|
|
166
|
+
* The newest durable CHECKPOINT, searching newest file first and stopping at the
|
|
167
|
+
* first file that has one — older segments are never read.
|
|
168
|
+
*/
|
|
169
|
+
findLastDurableCheckpoint(): {
|
|
170
|
+
lsn: number;
|
|
171
|
+
floorLsn: number;
|
|
172
|
+
} | null;
|
|
173
|
+
/** Stream entries from the segments that can contain LSN >= minLsn, then the active log. */
|
|
174
|
+
streamSince(minLsn: number, visitor: (entry: LogEntry) => void): void;
|
|
94
175
|
streamAllLogEntries(visitor: (entry: LogEntry) => void): void;
|
|
95
176
|
private calculateChecksum;
|
|
96
177
|
private verifyChecksum;
|
|
@@ -102,7 +183,13 @@ export declare class WriteAheadLog {
|
|
|
102
183
|
* This eliminates per-entry syscalls: a transaction with N writes does 1
|
|
103
184
|
* appendFileSync (of all N+2 entries) instead of N+2 separate appends.
|
|
104
185
|
*/
|
|
105
|
-
|
|
186
|
+
/**
|
|
187
|
+
* @param afterImageJson JSON.stringify(entry.afterImage), when the caller already
|
|
188
|
+
* has it: the record is then assembled around it instead of serializing the
|
|
189
|
+
* document again. The text is byte-identical to JSON.stringify(record), so
|
|
190
|
+
* checksums verify exactly as before.
|
|
191
|
+
*/
|
|
192
|
+
writeLog(entry: Omit<LogEntry, 'lsn' | 'checksum' | 'timestamp'>, afterImageJson?: string): number;
|
|
106
193
|
/**
|
|
107
194
|
* Flush the in-memory write buffer to disk in a single appendFileSync call.
|
|
108
195
|
* This is the ONLY place we call appendFileSync — all writeLog calls just
|
|
@@ -118,16 +205,54 @@ export declare class WriteAheadLog {
|
|
|
118
205
|
* 3. Fsync archive and parent directory.
|
|
119
206
|
* 4. Create fresh empty WAL and fsync it and parent directory.
|
|
120
207
|
* 5. Emit a CHECKPOINT entry at the head of the new log.
|
|
121
|
-
* 6. Clean up old archives beyond
|
|
208
|
+
* 6. Clean up old archives beyond the keep count, sparing any segment the
|
|
209
|
+
* archive protector reports as not-yet-shipped (see cleanupOldArchives).
|
|
122
210
|
*/
|
|
123
211
|
rotateLog(): string | null;
|
|
212
|
+
/**
|
|
213
|
+
* Every structurally valid rotated segment on disk, oldest first.
|
|
214
|
+
*
|
|
215
|
+
* Strictly matches deterministic segments `.wal.seg-<start>-<end>`; a partial
|
|
216
|
+
* or foreign file in the directory is ignored rather than guessed at. The end
|
|
217
|
+
* LSN is what retention decisions are made on (see cleanupOldArchives).
|
|
218
|
+
*/
|
|
219
|
+
private listSegments;
|
|
124
220
|
/**
|
|
125
221
|
* Returns the paths of all locally retained WAL archives (newest first).
|
|
126
222
|
* Used by v2 backup to upload segments to the client's bucket.
|
|
127
223
|
* Strictly matches deterministic structured segments .wal.seg-<start>-<end>.
|
|
128
224
|
*/
|
|
129
225
|
listArchives(): string[];
|
|
226
|
+
/**
|
|
227
|
+
* Declare a segment as "must not be pruned" — used by live backup so a
|
|
228
|
+
* rotation can never unlink WAL that has not reached the bucket yet.
|
|
229
|
+
*
|
|
230
|
+
* The predicate receives a segment's last LSN and returns true while that
|
|
231
|
+
* LSN is still unshipped. Protection is best-effort by design: it is capped
|
|
232
|
+
* by the protect byte budget so an unreachable bucket degrades into the old
|
|
233
|
+
* prune-oldest behaviour instead of filling the disk. Pass null to detach
|
|
234
|
+
* (e.g. when live backup is disabled and pruning should be unconditional).
|
|
235
|
+
*/
|
|
236
|
+
setArchiveProtector(protector: ((segmentEndLsn: number) => boolean) | null): void;
|
|
237
|
+
/**
|
|
238
|
+
* Oldest LSN crash recovery still needs (the last durable checkpoint's replay
|
|
239
|
+
* floor). Segments ending at or after it are never pruned by the keep count:
|
|
240
|
+
* dropping one would lose commits whose data files are not fsynced yet.
|
|
241
|
+
*/
|
|
242
|
+
private recoveryFloor;
|
|
243
|
+
setRecoveryFloor(lsn: number | null): void;
|
|
244
|
+
/**
|
|
245
|
+
* Retain the newest `keepCount` segments and delete the rest, oldest first —
|
|
246
|
+
* except any segment the archive protector still considers unshipped, which
|
|
247
|
+
* is deferred as long as the protect byte budget allows.
|
|
248
|
+
*
|
|
249
|
+
* `force` skips both the keep count and the protector entirely: it is the
|
|
250
|
+
* "wipe the WAL" path (truncateLog), where the caller has explicitly
|
|
251
|
+
* declared the archived entries worthless.
|
|
252
|
+
*/
|
|
130
253
|
private cleanupOldArchives;
|
|
254
|
+
/** Size of a segment in bytes; 0 when it is already gone or unreadable. */
|
|
255
|
+
private segmentSize;
|
|
131
256
|
getLogEntries(fromLSN?: number): LogEntry[];
|
|
132
257
|
/**
|
|
133
258
|
* Explicit flush barrier. Always fsyncs regardless of synchronous mode, so
|
|
@@ -136,6 +261,8 @@ export declare class WriteAheadLog {
|
|
|
136
261
|
* the dirty flag so the next timer tick won't re-fsync.
|
|
137
262
|
*/
|
|
138
263
|
forceFlush(): void;
|
|
264
|
+
/** Path of the active log file (for raw readers such as the live shipper). */
|
|
265
|
+
getLogPath(): string;
|
|
139
266
|
getCurrentLSN(): number;
|
|
140
267
|
/**
|
|
141
268
|
* Truncate (clear) the WAL entirely.
|
|
@@ -186,6 +313,34 @@ export declare class LockManager {
|
|
|
186
313
|
* partition the engine reads from (deterministic from the key).
|
|
187
314
|
*/
|
|
188
315
|
export declare function partitionedPath(dbPath: string, key: string): string;
|
|
316
|
+
/**
|
|
317
|
+
* Remove temp files left behind by a process that died between writing its temp
|
|
318
|
+
* file and renaming it into place.
|
|
319
|
+
*
|
|
320
|
+
* Safety rests on three properties, in order of importance:
|
|
321
|
+
*
|
|
322
|
+
* 1. It runs ONLY from the constructor, after the cross-process `.lock` is
|
|
323
|
+
* held and after crash recovery has run. No writer exists yet, so a temp
|
|
324
|
+
* file observed here can only predate this process. Writes this process
|
|
325
|
+
* makes later are renamed away synchronously and are never observable.
|
|
326
|
+
* 2. The pid embedded in the name must differ from ours (see
|
|
327
|
+
* isOrphanedTempFile), so an in-flight write is never unlinked out from
|
|
328
|
+
* under its own `renameSync`.
|
|
329
|
+
* 3. A name that does not parse as a temp file is left untouched rather than
|
|
330
|
+
* guessed at.
|
|
331
|
+
*
|
|
332
|
+
* The embedded timestamp must also be older than `minAgeMs`, which keeps the
|
|
333
|
+
* sweep inert even if a second process does hold the directory open and writes
|
|
334
|
+
* a temp file while we are walking it.
|
|
335
|
+
*
|
|
336
|
+
* Only temp files are unlinked, and the parent directory is fsynced so the
|
|
337
|
+
* unlink survives power loss. Failing to remove a temp file never fails
|
|
338
|
+
* startup -- it is a space leak, not a correctness problem.
|
|
339
|
+
*/
|
|
340
|
+
export declare function sweepStaleTempFiles(dbPath: string, minAgeMs?: number): {
|
|
341
|
+
scanned: number;
|
|
342
|
+
removed: number;
|
|
343
|
+
};
|
|
189
344
|
/**
|
|
190
345
|
* Walk partitioned directories under dbPath and call `visitor(filePath)` for
|
|
191
346
|
* every *.json file found. Uses fs.opendir async iteration so each directory
|
|
@@ -251,6 +406,22 @@ export declare class ACIDStorageEngine {
|
|
|
251
406
|
* Every committed write/delete stores its immutable DirtyEntry here with commit LSN.
|
|
252
407
|
*/
|
|
253
408
|
private dirtyKeys;
|
|
409
|
+
/**
|
|
410
|
+
* Dirty-key tracking is only needed while a consumer (live backup) acknowledges
|
|
411
|
+
* entries. Without one, every committed document would be cloned into this map
|
|
412
|
+
* forever — an unbounded copy of the whole working set. The engine tracks by
|
|
413
|
+
* default (standalone snapshots rely on it); Tero turns it off unless live
|
|
414
|
+
* backup is enabled.
|
|
415
|
+
*/
|
|
416
|
+
private trackDirty;
|
|
417
|
+
/** Data files written without fsync (normal mode) since the last durable checkpoint. */
|
|
418
|
+
private unsyncedFiles;
|
|
419
|
+
private unsyncedDirs;
|
|
420
|
+
/** Last durable checkpoint: commits <= lsn are on disk; replay starts at floorLsn. */
|
|
421
|
+
private durableCheckpoint;
|
|
422
|
+
private commitsSinceDurableCheckpoint;
|
|
423
|
+
private recoveredCommits;
|
|
424
|
+
private durableCheckpointTimer?;
|
|
254
425
|
private activeSnapshotLsn;
|
|
255
426
|
private snapshotBeforeImages;
|
|
256
427
|
private lastCommittedLSN;
|
|
@@ -259,17 +430,31 @@ export declare class ACIDStorageEngine {
|
|
|
259
430
|
isDegraded(): boolean;
|
|
260
431
|
getLastMaintenanceError(): Error | null;
|
|
261
432
|
getLastCommittedLSN(): number;
|
|
262
|
-
peekDirtyKeys(upToLsn?: number): Map<string, DirtyEntry>;
|
|
433
|
+
peekDirtyKeys(upToLsn?: number, clone?: boolean): Map<string, DirtyEntry>;
|
|
263
434
|
acknowledgeDirtyKeys(acked: Iterable<{
|
|
264
435
|
key: string;
|
|
265
436
|
lsn: number;
|
|
266
437
|
}>): void;
|
|
267
438
|
getDirtyKeyCount(): number;
|
|
439
|
+
/** Enable/disable dirty-key tracking. Disabling drops everything tracked so far. */
|
|
440
|
+
setDirtyTracking(enabled: boolean): void;
|
|
441
|
+
isDirtyTrackingEnabled(): boolean;
|
|
442
|
+
/**
|
|
443
|
+
* Oldest LSN any transaction that has not committed yet may have written at.
|
|
444
|
+
* A checkpoint (local or cloud) taken now must keep WAL from this LSN onward:
|
|
445
|
+
* a transaction that BEGAN before the checkpoint and COMMITS after it has its
|
|
446
|
+
* WRITE records below the checkpoint LSN.
|
|
447
|
+
*/
|
|
448
|
+
getReplayFloorLsn(): number;
|
|
449
|
+
getDurableCheckpoint(): {
|
|
450
|
+
lsn: number;
|
|
451
|
+
floorLsn: number;
|
|
452
|
+
} | null;
|
|
268
453
|
/**
|
|
269
454
|
* Creates an isolated logical snapshot handle as of upToLsn.
|
|
270
455
|
* Enforces single-active-snapshot invariant and provides explicit close().
|
|
271
456
|
*/
|
|
272
|
-
beginSnapshot(upToLsn: number): SnapshotHandle;
|
|
457
|
+
beginSnapshot(upToLsn: number, preCapturedDirty?: Map<string, DirtyEntry>): SnapshotHandle;
|
|
273
458
|
/**
|
|
274
459
|
* Streams a version-consistent logical snapshot of the database as of upToLsn.
|
|
275
460
|
* Fully protected against concurrent writes via Copy-On-Write snapshotBeforeImages.
|
|
@@ -283,14 +468,27 @@ export declare class ACIDStorageEngine {
|
|
|
283
468
|
private _streamSnapshotFiles;
|
|
284
469
|
private synchronous;
|
|
285
470
|
private checkpointBatchSize;
|
|
286
|
-
constructor(dbPath: string, synchronous?: SynchronousMode, commitIntervalMs?: number, dataFlushIntervalMs?: number, checkpointBatchSize?: number);
|
|
471
|
+
constructor(dbPath: string, synchronous?: SynchronousMode, commitIntervalMs?: number, dataFlushIntervalMs?: number, checkpointBatchSize?: number, archiveKeepCount?: number, durableCheckpointIntervalMs?: number);
|
|
287
472
|
private fsyncFile;
|
|
288
473
|
private fsyncDir;
|
|
289
474
|
private initializeStorage;
|
|
475
|
+
/**
|
|
476
|
+
* Redo-only crash recovery, bounded by the last durable checkpoint.
|
|
477
|
+
*
|
|
478
|
+
* Data files only ever receive COMMITTED after-images (writes are deferred to
|
|
479
|
+
* committedBuffer until commit), so there is nothing to undo: an uncommitted
|
|
480
|
+
* transaction never touched a data file. (Earlier versions "undid" such
|
|
481
|
+
* transactions by writing their before-image — on every boot, after redo —
|
|
482
|
+
* which rolled newer commits back to stale data.)
|
|
483
|
+
*
|
|
484
|
+
* The last durable CHECKPOINT is found newest-file-first. Only segments that
|
|
485
|
+
* reach its replay floor are read: one pass collects transactions committed
|
|
486
|
+
* after the checkpoint, a second re-applies just their records in WAL order,
|
|
487
|
+
* streaming — nothing but transaction ids is held in memory.
|
|
488
|
+
*/
|
|
290
489
|
private performCrashRecovery;
|
|
291
490
|
private redoOperation;
|
|
292
491
|
private redoDelete;
|
|
293
|
-
private undoOperation;
|
|
294
492
|
/**
|
|
295
493
|
* Atomic data file write: write to a temp file then atomically rename over
|
|
296
494
|
* the target. The atomic rename guarantees readers never see a partial file
|
|
@@ -308,7 +506,7 @@ export declare class ACIDStorageEngine {
|
|
|
308
506
|
* `Promise<void>` only when the lock is contended and we must wait in queue.
|
|
309
507
|
* Callers should check: `if (result !== undefined) await result;`
|
|
310
508
|
*/
|
|
311
|
-
write(transactionId: string, key: string, data: any, cachedBeforeImage?: any): void | Promise<void>;
|
|
509
|
+
write(transactionId: string, key: string, data: any, cachedBeforeImage?: any, replace?: boolean, dataJson?: string): void | Promise<void>;
|
|
312
510
|
/**
|
|
313
511
|
* Synchronous write work — called from write() after the lock is acquired.
|
|
314
512
|
* Pure in-memory + buffer operations: before-image lookup, deepMerge, WAL buffer,
|
|
@@ -350,6 +548,8 @@ export declare class ACIDStorageEngine {
|
|
|
350
548
|
* work on the happy path.
|
|
351
549
|
*/
|
|
352
550
|
rollbackTransaction(transactionId: string): void;
|
|
551
|
+
/** Last committed image of a key: committedBuffer first, then the data file. null when absent. */
|
|
552
|
+
private readCommittedState;
|
|
353
553
|
private deepMerge;
|
|
354
554
|
getTransactionStatus(transactionId: string): 'active' | 'committed' | 'aborted' | 'not_found';
|
|
355
555
|
/**
|
|
@@ -358,6 +558,21 @@ export declare class ACIDStorageEngine {
|
|
|
358
558
|
*/
|
|
359
559
|
isTransactionActive(transactionId: string): boolean;
|
|
360
560
|
getActiveTransactions(): string[];
|
|
561
|
+
private checkpointInFlight;
|
|
562
|
+
/**
|
|
563
|
+
* forceCheckpoint() without blocking the event loop on fsync: data files are
|
|
564
|
+
* written now, fsynced on the libuv pool, and only then is the durable
|
|
565
|
+
* CHECKPOINT logged. Used by the periodic timer; close() uses the sync form.
|
|
566
|
+
*/
|
|
567
|
+
forceCheckpointAsync(): Promise<void>;
|
|
568
|
+
private inFlightFsync;
|
|
569
|
+
/**
|
|
570
|
+
* Durable checkpoint. Afterwards every commit up to now is fsynced into the data
|
|
571
|
+
* files, so crash recovery only replays WAL written after this point.
|
|
572
|
+
*
|
|
573
|
+
* Order matters: capture the LSN and replay floor first (synchronously, so no
|
|
574
|
+
* commit can slip between), then make data durable, then log the CHECKPOINT.
|
|
575
|
+
*/
|
|
361
576
|
forceCheckpoint(): void;
|
|
362
577
|
/**
|
|
363
578
|
* Returns a reference to the underlying WAL (used by v2 backup to upload
|