defuss-dbsonl 0.7.0 → 0.9.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/ARCH.md +6 -4
- package/CHANGELOG.md +14 -0
- package/README.md +1 -1
- package/dist/index.cjs +394 -80
- package/dist/index.d.cts +37 -5
- package/dist/index.d.mts +37 -5
- package/dist/index.mjs +1 -1
- package/dist/scan-worker.mjs +2 -2
- package/dist/{server-BXybcXSY.mjs → server-Y88y4TNX.mjs} +1 -1
- package/dist/server.mjs +2 -2
- package/dist/{store-Bd1KZ-N7.mjs → store-DsWjd2ZI.mjs} +393 -79
- package/dist/{types-CSHnjgvj.d.cts → types-CsFsnMNZ.d.cts} +15 -5
- package/dist/{types-CSHnjgvj.d.mts → types-CsFsnMNZ.d.mts} +15 -5
- package/docs/API.md +4 -4
- package/docs/FORMAT.md +44 -11
- package/package.json +1 -1
package/ARCH.md
CHANGED
|
@@ -18,11 +18,13 @@ flowchart TD
|
|
|
18
18
|
|
|
19
19
|
## Why this design
|
|
20
20
|
|
|
21
|
-
VERIFIED: A persistent B+tree avoids a database-sized RAM map without requiring a second database dependency. Fanout 32 trades modest page sizes for logarithmic tree navigation. Values are separate DSON frames; pages contain only keys, references and revisions. Rewriting a path rather than mutating it gives readers stable roots with no undo log. A batch coalesces edits in private dirty nodes and persists each final dirty page once, avoiding unobservable intermediate roots. An edit whose path is already dirty or cached runs synchronously; only a page read awaits. A bounded cache stores decoded pages, never application values. It evicts by second chance: a hit only marks its page, and eviction moves a marked page to the back once. Exact LRU reordered the Map on every hit, about 5% of a memory commit.
|
|
21
|
+
VERIFIED: A persistent B+tree avoids a database-sized RAM map without requiring a second database dependency. Fanout 32 trades modest page sizes for logarithmic tree navigation. Values are separate DSON frames; pages contain only keys, references and revisions. Rewriting a path rather than mutating it gives readers stable roots with no undo log. A batch coalesces edits in private dirty nodes and persists each final dirty page once, avoiding unobservable intermediate roots. An edit whose path is already dirty or cached runs synchronously; only a page read awaits. A bounded cache stores decoded pages, never application values. It evicts by second chance: a hit only marks its page, and eviction moves a marked page to the back once. Exact LRU reordered the Map on every hit, about 5% of a memory commit. Formats 2 and 3 write index pages with `encodePage`, which produces the same frames as `encodeFrame` in trusted mode about 30% faster because it knows the few property names pages use. Format 4 writes compact pages instead: a DSON frame of one byte array with keys sharing the previous key's prefix and varint numbers ([FORMAT.md](docs/FORMAT.md#compact-pages-format-4)). Copy-on-write rewrites every page a commit touches, so page bytes are written once per commit and copied again by every compaction; DSON object pages repeated five property names per entry and stored keys in UTF-16 and numbers as float64. In defuss-db's benchmark store, a 64-document commit rewrote 148 pages, 823 KiB for 13 KiB of documents. Compact pages are a tenth of the size, and a 100,000-document load into that store took 8.7 to 9.5 s instead of 13.0 to 15.7 s under Node 26.13.1, with 5 background compactions instead of 11 and a 33.1 MiB file afterwards instead of 102.2 MiB. A commit edits cached copies of the pages on its paths instead of rereading them, and adds the pages it wrote once their root is published; a page address within a generation never changes meaning, because only an unpublished tail is ever rewritten. Open reads constant-size metadata and generation length, not every value.
|
|
22
22
|
|
|
23
|
-
VERIFIED: A two-slot manifest avoids relying on atomic rename, which Storage and OPFS do not expose uniformly. Each slot is a checksummed DSON frame in a 4096-byte region. The newest valid publication wins. Writers append after the published end, flush, then replace the older slot. A partial slot is ignored.
|
|
23
|
+
VERIFIED: A two-slot manifest avoids relying on atomic rename, which Storage and OPFS do not expose uniformly. Each slot is a checksummed DSON frame in a 4096-byte region. The newest valid publication wins. Writers append after the published end, flush, then replace the older slot. A partial slot is ignored. Formats 3 to 5 end every commit with a commit record in the generation; on native files, whose writes every process sees before a flush, the generation flush alone makes a commit durable and the slot is written without one. A lone durable transaction then took 4.6 ms instead of 8.1 ms under Node, where every flush is a full `F_FULLFSYNC` of about 3.9 ms; the alternative, flushing the head too, doubles that. Recovery after a power loss walks the frames after the head's end and adopts each record that repeats the generation's random seal and chains to the commit before it, as long as every frame up to it passes its checksum ([FORMAT.md](docs/FORMAT.md#commit-records-format-3)). The head is flushed every 16 MiB of generation bytes, which bounds that walk. OPFS and directory handles flush the slot on every commit, because their writes are not known to reach other workers before a flush. An empty head whose generations are all empty is an interrupted create: only `create: true` initializes it, because no slot was published; invalid fields in a checksummed slot, a missing referenced generation or committed truncation fail closed. A failed publication reports `COMMIT_UNCERTAIN`; reread the revision before retrying. Successful publication increments the logical revision once per nonempty batch.
|
|
24
24
|
|
|
25
|
-
VERIFIED:
|
|
25
|
+
VERIFIED: Format 5, which new databases use, writes index pages only at checkpoints. Between them each commit appends a compact list of its key changes before its record, and the writing handle keeps the changed pages in memory; every other handle replays the records since the last checkpoint onto the written root and keeps the result, so it replays each commit once. A checkpoint comes when the log reaches 16 MiB or the pages held in memory would exceed `maxCachePages`, which bounds both replay and memory. On a 100,000-document defuss-db load with one ordered index this took 2.50 s instead of 3.35 to 3.92 s in commits of 256 and wrote 29 MiB instead of 107 MiB, because a copy-on-write tree otherwise rewrites every page a commit touches. The log stays in the generation: a separate write-ahead file, the plausible alternative, would add a second file to flush, coordinate and truncate, while format 3's records already gave recovery and a durable commit with one flush.
|
|
26
|
+
|
|
27
|
+
VERIFIED: These choices spend extra write bandwidth on index paths at checkpoints and two small metadata writes during compaction. They remove full replay, since format 5 replays at most the log since its last checkpoint, and read-held writer exclusion. Benchmark evidence separates batch and per-record flush workloads rather than treating them as equivalent. A universal Pareto-optimality claim would require a defined workload distribution and objective function.
|
|
26
28
|
|
|
27
29
|
## Ownership and lock order
|
|
28
30
|
|
|
@@ -82,7 +84,7 @@ VERIFIED: Mutations keep their Storage handles open between commits. `src/kept-f
|
|
|
82
84
|
|
|
83
85
|
## Reclamation and crashes
|
|
84
86
|
|
|
85
|
-
VERIFIED: Compaction copies live values with a streaming bulk builder that retains at most 32 references per level. It writes both head slots to the new generation before returning. Collection refreshes the alternate slot to the selected head, then tries exclusive pins on obsolete generations. Busy generations remain. Candidate pins protect a concurrent compactor from collection. Abandoned candidates from killed processes become collectible.
|
|
87
|
+
VERIFIED: Compaction copies live values with a streaming bulk builder that retains at most 32 references per level. It copies each value's frame bytes after checking its header and payload checksums, in batches of about 4 MiB read in file order, instead of decoding and re-encoding values in key order: a 100,000-document defuss-db store with one ordered index compacted in 0.25 to 0.34 s instead of 2.9 to 3.6 s, because in key order nearly every value missed the 64 KiB read window. Decoding each value, the plausible alternative, would also catch a frame whose checksum matches but whose value does not decode; such a frame is copied unchanged and still fails where it is read and in `verify()`. Concurrent reads were measured and dropped: 1, 8 and 32 reads in flight took the same time on a warm file cache. It writes both head slots to the new generation before returning. Collection refreshes the alternate slot to the selected head, then tries exclusive pins on obsolete generations. Busy generations remain. Candidate pins protect a concurrent compactor from collection. Abandoned candidates from killed processes become collectible.
|
|
86
88
|
|
|
87
89
|
VERIFIED: Native lock files for reclaimed generations are removed while the commit lock excludes new pin creation. The current generation and fixed database locks remain. This prevents an inode replacement race between old and new lock owners. Cleanup requires exclusive ownership; there are no age-based stale-lock guesses.
|
|
88
90
|
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.9.0
|
|
4
|
+
|
|
5
|
+
VERIFIED: New databases use format 5, which writes index pages only at checkpoints. Between them, a commit appends its values, a compact list of its key changes and its record, and flushes once; the handle that wrote it keeps the changed pages in memory, and every other handle, process or worker replays the records since the last checkpoint. A commit is a checkpoint when the log reaches 16 MiB or the pages held in memory would exceed `maxCachePages`. On a 100,000-document defuss-db load with one ordered index, commits of 256 documents took 2.50 s in all instead of 3.35 to 3.92 s, a 64-document commit 4.39 ms instead of 5.89 to 6.08 ms, and the load wrote 29 to 31 MiB instead of 107 to 166 MiB. Format 2 to 4 databases keep their format; 0.8 refuses format 5 with `UNSUPPORTED`.
|
|
6
|
+
|
|
7
|
+
## 0.8.1 (not published; its changes ship in 0.9.0)
|
|
8
|
+
|
|
9
|
+
VERIFIED: `compact()` copies value frames instead of decoding and re-encoding each value, and reads them in file order in batches of about 4 MiB. A 100,000-document defuss-db store with one ordered index compacted in 0.25 to 0.34 s instead of 2.9 to 3.6 s on macOS under Node.js. The file format is unchanged.
|
|
10
|
+
|
|
11
|
+
## 0.8.0
|
|
12
|
+
|
|
13
|
+
VERIFIED: A full page cache evicts down to 90% of its limits in one pass over its Map, instead of asking the Map for a new iterator for every evicted page. Each new iterator walked the slots that earlier deletes had left at the front of the Map: 4.3 µs per eviction with 10,000 pages cached, against 0.2 µs with one iterator per pass. In defuss-db's benchmark store of 100,000 documents with one index, committing 64 documents took 7.0 and 7.8 ms instead of 9.3 and 9.4 ms (medians of 41 commits, two alternating runs each), and the 100,000-document bulk load 12.8 and 13.6 s instead of 13.8 and 18.4 s. Second-chance eviction is unchanged, and a pass never evicts the page it has just loaded; `tests/mvcc.test.ts` checks both limits at one page and at 40.
|
|
14
|
+
|
|
15
|
+
VERIFIED: New databases use format 4: format 3's commit records with compact index pages. A page is a DSON frame of one byte array in which each key stores only what differs from the previous key, as UTF-16 code units in one byte each where they fit, and every number as a varint ([FORMAT.md](docs/FORMAT.md#compact-pages-format-4)). On the 8,336 leaves of a 100,000-document defuss-db store with one index, a page took 552 bytes instead of 5,528; under Node 24.22.1 encoding took 4.8 to 5.0 µs instead of 22.4 to 24.6 µs, and decoding with validation 3.6 to 3.7 µs instead of 17.3 to 18.9 µs. In defuss-db's benchmark (Node 26.13.1, two alternating runs each, both with the eviction change above), loading 100,000 documents in commits of 256 took 8.7 and 9.5 s instead of 13.0 and 15.7 s, with 5 background compactions instead of 11 and a 33.1 MiB file instead of 102.2 MiB; committing 64 documents took 6.0 and 6.2 ms instead of 7.9 and 9.2 ms. Under Bun 1.4.4 the load took 7.1 s instead of 11.7 s and the 64-document commit 5.9 ms instead of 7.3 ms. Warm reads did not change: a read by ID took 10.8 to 10.9 µs against 11.7 to 12.0 µs under Node, and Bun's 20.6 to 26.3 µs varied as much within each format as between them. Format 2 and 3 databases keep their format and their pages, so 0.7 can still share a format 3 database; 0.7 refuses a format 4 database with `UNSUPPORTED`. `tests/format4.test.ts` covers keys that a shared prefix or a byte per unit could break, deletes, compaction, reopening and malformed pages; `tests/format3.test.ts` covers a format 3 database that stays format 3.
|
|
16
|
+
|
|
3
17
|
## 0.7.0
|
|
4
18
|
|
|
5
19
|
VERIFIED: Durable commits and point reads on native storage took less time in `make bench-redis` than three runs earlier the same day. Point reads took 19 instead of 38 µs under Node and 12 instead of 47 µs under Bun, lone transactions 3.86 instead of 4.35 ms and 3.97 instead of 4.26 ms, and four contending writers 0.98 instead of 1.23 ms and 1.02 instead of 1.17 ms per commit. Three changes did this. Reads of up to 16 KiB are blocking calls under asynchronous I/O; the new `syncReadBytes` option sets the bound, and 0 restores thread-pool reads. The head and generation files stay open between commits as Storage shared writers, shared by every handle in the realm and closed with the last one. Holders of a shared lock on one connection share one flock, which keeps four writers on one handle in commit groups of four. Each batch's encoded bytes are counted once.
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# defuss-dbsonl 0.
|
|
1
|
+
# defuss-dbsonl 0.9.0
|
|
2
2
|
|
|
3
3
|
Transactional key-value storage for Node.js, Bun and browser workers. Values are DSON, so dates, binary views, bigint, `undefined` and cyclic references survive storage. It is built on [defuss-dson](https://dson.defuss.org) 1.5.1 and [defuss-storage](https://storage.defuss.org) 0.4.1 and ships TypeScript declarations with ESM and CommonJS builds. It is designed as the backing store and API of [defuss-dataview](https://dataview.defuss.org), which adds filters, sorting and paging for table and tree grids. Project website: [dbsonl.defuss.org](https://dbsonl.defuss.org).
|
|
4
4
|
|