defuss-dbsonl 0.8.0 → 0.10.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 CHANGED
@@ -20,9 +20,11 @@ flowchart TD
20
20
 
21
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. Format 3, which new databases use, ends 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.
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: These choices spend extra write bandwidth on index paths and two small metadata writes during compaction. They remove full replay 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.
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
 
@@ -90,12 +92,16 @@ VERIFIED: Data written after the published end is private. A later writer trunca
90
92
 
91
93
  ## Transactions, memory and lifecycle
92
94
 
93
- VERIFIED: Batch values are encoded before admission returns, preventing caller mutation during queued work. Both per-transaction and aggregate pending-byte budgets are enforced. A transaction callback reads from one snapshot plus a bounded staged overlay. The final database revision must still equal its snapshot revision, including read-only transactions and range scans. This conservative validation provides serializability but can reject transactions touching disjoint keys. Automatic retries are omitted because callbacks may have external effects.
95
+ VERIFIED: Batch values are encoded, and caller frames copied, before admission returns, preventing caller mutation during queued work. Both per-transaction and aggregate pending-byte budgets are enforced. A transaction callback reads from one snapshot plus a bounded staged overlay. The final database revision must still equal its snapshot revision, including read-only transactions and range scans. This conservative validation provides serializability but can reject transactions touching disjoint keys. Automatic retries are omitted because callbacks may have external effects.
96
+
97
+ VERIFIED: `batchFrames` and `putFrame` commit a DSON frame the caller already encoded, so a caller that keeps an encoded snapshot of a value does not pay for a second encode. Each frame is copied and the copy decoded once at admission, under the limits every read applies, before it can stage. A public flag that marks frames as trusted was rejected: a damaged or malformed frame would commit and then fail at every later read, as `CORRUPT` or `LIMIT_EXCEEDED`, and compaction, which copies frames after checking only their checksums, would carry one whose checksums match into every new generation. Storing the frame as a byte-array value was rejected too: reads would return bytes instead of the value, and the engine would encode the bytes again. A transfer of the caller's buffer instead of a copy detaches the whole `ArrayBuffer`, and under Node 26 a `Buffer` of 300 bytes sat in a shared pool buffer of 65,600 bytes, so a transfer would take the caller's unrelated bytes with it.
98
+
99
+ VERIFIED: The check has a cost: a caller that encoded its values and committed the frames took 1.12 to 2.06 times as long as `batch` takes to encode and commit the same values ([PERFORMANCE.md](docs/PERFORMANCE.md#caller-encoded-frames-on-macos-arm64)). `db.frame(value)` removes it for a caller that only needs a snapshot to commit later: the engine encodes the value, keeps the bytes in a module-private `WeakMap` under a frozen handle it returns, and commits a handle it finds there without a copy or a decode; that took 0.99 to 1.11 of `batch`'s time. Only the engine can add a handle, so the check is skipped for bytes its own encoder produced and nothing else, which a public trusted flag could not ensure. The bytes are never exposed, so they cannot change after encoding. The cost: a handle does not cross a structured clone or a second copy of the package, such as its CommonJS build beside the ESM one; there it is refused with `TypeError`, and the caller passes a `Uint8Array` frame instead.
94
100
 
95
101
  VERIFIED: Closing a snapshot rejects new reads, drains admitted reads and releases its pin. Paused iterators do not prevent closure. Closing a database stops admission, drains operations, closes its snapshots and discards its cache. Cancellation is cooperative during waiting and copying. After publication starts, completion is success or an uncertain commit error rather than an ordinary cancellation.
96
102
 
97
103
  ## Operations, security and privacy
98
104
 
99
- VERIFIED: The trusted storage root is application-owned. Native byte and lock files reject symlink traversal where supported; database names are lowercase identifiers. Bounds and backward-only child references reject malformed addresses and disk cycles. CRC32C detects damage, not forgery. DSON values may be cyclic and contain binary data. Applications needing encryption or authentication must provide those policies separately.
105
+ VERIFIED: The trusted storage root is application-owned. Native byte and lock files reject symlink traversal where supported; database names are lowercase identifiers. Bounds and backward-only child references reject malformed addresses and disk cycles. CRC32C detects damage, not forgery; a caller's frame is therefore decoded at admission, so a frame with matching checksums but an invalid payload cannot commit. DSON values may be cyclic and contain binary data. Applications needing encryption or authentication must provide those policies separately.
100
106
 
101
107
  VERIFIED: There is no network service or background maintenance. Applications schedule `compact()` and `collect()` and close snapshots promptly. Maintenance needs enough disk space for the live copy. No telemetry or application-value logging occurs. Tests use temporary databases and real processes. Source, tests, examples and tooling each document their own responsibilities in ARCH.md.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.0
4
+
5
+ VERIFIED: `db.batchFrames(mutations, options?)` and `tx.putFrame(key, frame)` commit DSON frames the caller already encoded, with the revisions, conflicts, commit groups, budgets and durability of `batch` and `put`. A `Uint8Array` frame is copied at call time and decoded once to check it; one that is not exactly one valid frame throws `TypeError`, one over a budget `RangeError`. `db.frame(value)` encodes a value into an `OwnedFrame` whose bytes stay in the engine, which those calls commit without a copy or a check. In a benchmark of 10,000 records, a caller that encodes each value, decodes the copy and commits it with `batch` took 1.18 to 2.71 times as long as `batch` alone; with `Uint8Array` frames it took 1.12 to 2.06 times, with `frame()` 0.99 to 1.11 times ([PERFORMANCE.md](docs/PERFORMANCE.md#caller-encoded-frames-on-macos-arm64)). The file format is unchanged. `make bench-frames` reproduces the measurement.
6
+
7
+ ## 0.9.0
8
+
9
+ 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`.
10
+
11
+ ## 0.8.1 (not published; its changes ship in 0.9.0)
12
+
13
+ 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.
14
+
3
15
  ## 0.8.0
4
16
 
5
17
  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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # defuss-dbsonl 0.8.0
1
+ # defuss-dbsonl 0.10.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
 
@@ -50,6 +50,8 @@ try {
50
50
 
51
51
  The transaction callback runs once. A concurrent commit that changed a key the callback read makes it fail with `CONFLICT` instead of retrying, because a retry would repeat any side effects in your callback; after a scan, any concurrent commit does. Retry explicitly where that is safe. Concurrent batches and transactions on one handle publish together: one write and one pair of flushes for the queued group, while each keeps its own revision and receipt. `ifRevision` gives compare-and-swap for changes prepared outside a transaction.
52
52
 
53
+ A caller that already holds a value's DSON frame, for example because it keeps an encoded snapshot of the value, commits it with `batchFrames()` or `tx.putFrame()` instead of encoding the value again. DBSONL copies the frame and decodes it once to check it, so a damaged or partial frame is refused before it commits; reads return the value. A caller that encodes only to keep a snapshot until it commits can take it with `db.frame(value)` instead: the engine keeps the bytes, so committing the snapshot needs no copy and no check, and took 0.99 to 1.11 of the time of `batch` with the values, against 1.18 to 2.71 for encoding, decoding and committing the copy ([PERFORMANCE.md](docs/PERFORMANCE.md#caller-encoded-frames-on-macos-arm64)); [API](docs/API.md) lists the checks.
54
+
53
55
  For bulk reads, a snapshot's `scanBatches()` yields the same rows as `scan()` in arrays of up to 256. It tracks one pull per batch instead of one per row, so a full scan takes less time; [API](docs/API.md) has its byte budget and options.
54
56
 
55
57
  On native storage, `scanBatches()` uses worker threads by itself once a snapshot range holds at least 5000 rows: a pool of up to four workers decodes the rows and hands them back in key order. A full scan of 100,000 documents took 105 ms instead of 401 ms under Node. `createFileBackend(root, { scanWorkers: 0 })` turns the pool off; scans with a `sizeOf` callback, `scan()` row iterators and memory and browser backends stay in the calling thread.
@@ -139,6 +141,7 @@ make verify # lint, tests, coverage, packed-package e2e
139
141
  make bench # matched v0.2 versus current benchmarks
140
142
  make bench-scan # full scans, row by row and in batches, against the vendored 0.5.0
141
143
  make bench-redis # current build versus a throwaway loopback Redis, run as mise exec -- make bench-redis
144
+ make bench-frames # values through batch against caller-encoded frames and frame() handles through batchFrames
142
145
  make dev # rebuild the website demos and serve docs/ on 127.0.0.1:3000
143
146
  make vae # verification, review and documentation gate
144
147
  make release # tarball, source ZIP and checksums; never publishes or commits