gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitac6268e3ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mreac6268e3/plugins/mpackdb/README.md

6.3 KB

  1. # `hl:mpackdb` — the MessagePack file database
  2. A document store in plain files. Server realm. The class surface and its
  3. generated API docs live in `MPackDB.hl` and `cursor.hl`
  4. (`projects/homepage/generated/pages/mpackdb.md`).
  5. ```hybriel
  6. import { MPackDB } from 'hl:mpackdb'
  7. db = new MPackDB(file = 'users.db', primaryKey = '*id', indexes = ['!email', 'age'])
  8. id = db.put({ name = "Alice"; email = "[email protected]"; age = 30; }) \\ 1
  9. ```
  10. ## Primary keys
  11. | declaration | key | generated by `put()` |
  12. |---|---|---|
  13. | `*name` | a number | the table's counter: **1, 2, 3, …** |
  14. | `@name` | a string | a random 12-character id |
  15. | `name` | a string | nothing — the record must carry it |
  16. **A fresh `*id` table starts at 1** (ticket #4, the creator's ruling "start at
  17. 1"). The counter is stored as `nextId` in `<base>.meta.json`, and an existing
  18. table keeps counting from its stored value. A table written before this rule
  19. therefore goes on from wherever it was, and its record 0 remains record 0.
  20. A record put with an explicit key takes that key if it is free, 0 included.
  21. ## One open table per process (ticket #110)
  22. Within one process, a table path has **one** open table, shared by every
  23. `open()` — another module instance, another realm, it doesn't matter, they
  24. all get the same handle. The file is never compacted or rewritten while a
  25. handle on it is live; compaction happens only when asked for explicitly
  26. (`compactNow()` / `#handle.compact()`) or when the **last** handle on that
  27. path closes. A second `open()` of an already-open table ignores its own
  28. `primaryKey`/`indexes`/`compact` arguments — the schema was fixed by whoever
  29. opened it first.
  30. This matters because compaction rewrites `<base>.mpack` and shifts every
  31. record after the first tombstone to a new offset. Before this rule, a second
  32. `open()` from anywhere in the process ran its own compact-on-open (ticket
  33. #21) against the same file, and the first handle's already-looked-up offsets
  34. went stale under it — reads through the first handle then missed or
  35. misread records until something compacted again. Related: ticket #21
  36. ("opening a table rewrote its files").
  37. ## Compaction never loses data (ticket #176)
  38. - **Across processes**, too: every open handle holds a shared `flock` on
  39. `<base>.flock`. A compaction (on open, `compactNow()`, or the last close)
  40. needs it exclusive and is **skipped** — not an error; `lastError()` says
  41. so — while another process has the store open.
  42. - A handle notices when its data file was rewritten anyway (another
  43. process's compaction, the JS mpackdb): the inode, a shrink, or a newer
  44. compaction generation in the meta. It re-reads the meta and re-indexes the
  45. new file before it answers a read. Before, it read its old offsets in the
  46. new file — the middle of other records.
  47. - Compaction checks every frame before it renames anything. A frame of size
  48. <= 4, a size past the end of the file that is not a torn tail, a live
  49. frame that does not decode to exactly one map, or a deleted offset that is
  50. not a frame start makes it **refuse**: the error names the offset, the data
  51. file and meta stay as they were. A refused compaction on open does not fail
  52. the open. The one thing dropped is a torn tail (an append cut short by a
  53. crash), and it is reported.
  54. - The new data file is fsynced, renamed, its directory fsynced, and only
  55. then the meta written. The meta's `dataFile` names the data file it was
  56. written for (inode, size, compaction generation, a checksum of its deleted
  57. list); a meta of a replaced file has its tombstones dropped, not applied
  58. to the new file. `idxstate.json`'s `dataIno` does the same for the index
  59. files.
  60. ## Cursors stream, and they are snapshots (mission 393)
  61. `iterate(null, null)` (the full scan), `iterate(field, value)` and the index
  62. range behind `range()` answer an iterator that decodes **one record per
  63. `next()`** into a per-record arena that is reset each time. `iterate(null,
  64. null)` holds no record at all (memory is O(1) in the table size: the tracker's
  65. 1.4 M-row episodes walk in the memory of one row); an index query keeps only
  66. the matching locations (16 bytes each) and reads each record when asked.
  67. `find()` / `range()` still drain the cursor into one array, with the same
  68. results as before.
  69. **The rule when the store changes while a cursor is open: the cursor is a
  70. snapshot of the moment the query ran.**
  71. - It yields the records that existed and were live then: the file size and the
  72. set of tombstones are frozen at the start (a record deleted before is never
  73. seen).
  74. - A record inserted, or the new version an `update` appends, after the start is
  75. not seen; the old version, if it was live at the start, still is.
  76. - A record deleted after the start is still yielded (the cursor reads the
  77. bytes it saw).
  78. - A compaction, by this process or another, or a foreign rewrite of the file
  79. does **not** invalidate it and raises no error: the cursor holds its own
  80. `dup()` of the data file it started on and reads that (by then unlinked) file
  81. to the end. The disk space of that file lives until the cursor is exhausted
  82. or collected.
  83. - Another process's appends and deletes are not seen (the cursor is a view of
  84. the state this handle had when it started).
  85. - A read error mid-stream ends the cursor early.
  86. The tombstone set is built once per handle and kept in step with every delete;
  87. it is rebuilt after a compaction or when a meta another writer changed is
  88. adopted. Lookups, ranges and scans no longer rebuild it on every call, so the
  89. cost of a slice no longer grows with the number of tombstones.
  90. `count()` counts frame headers without decoding a record.
  91. ## Compatibility with the JS mpackdb
  92. The on-disk format is the JS mpackdb 1.0.7 format: `<base>.mpack`,
  93. `<base>.meta.json`, one `<base>.<field>.txt` per index, `<base>.idxstate.json`
  94. and the `<base>.lock` protocol (`<base>.flock` and the #176 fields above are
  95. this engine's additions; the JS mpackdb ignores them). Either side reads and changes the other's
  96. files. The first id is not part of the format; `nextId` is. Therefore:
  97. - a table the JS mpackdb created carries `nextId` and keeps its sequence here,
  98. even when that sequence started at 0;
  99. - a table Hybriel created carries `nextId` too, and the JS mpackdb continues it;
  100. - the one divergence is a table with **no** `.meta.json` yet: Hybriel's first
  101. id is 1, the JS mpackdb's is 0.
  102. The other documented divergences are listed at the top of `engine.zig`.

Branches

Latest commits

  • ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mre
  • 322b3293mission 046: photoQueue guard for non-row fetch results (damaged persons index)mre
  • e5b99aecmission 046: photoQueue in slices (people.hl photoQueueStart/Step, jobs.hl photoTick), tests/photoqueue.hlmre
  • 31dcf2admission 045: report, LOGmre
  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre
  • 3db7dc31merge w44-jobs-slices into w43-trkmemmre
  • 593dab6fmission 043: report, LOGmre
  • 1a5a4818boot scans back to iterate (slices measured slower: titles 114 s, episodes 141 s, persons failed); lambda per rowmre
  • 03d29893boot scans read primary-key slices (lib/slices.hl from mission 044): iterate is eager too; people view log line back; allShows/allEpisodes stay for toolsmre
  • 23375c58mission 044: report, LOG, STATUSmre
  • c6fd928emission 044: 4096 slices for tables over 200 K rows, 15 ms budget, TRACKER_SLICE_DEPTH for testsmre
  • fb057680mission 044: jobs.hl imports mergeLeft (gate found it), mission file, slices benchmre
  • 0d6293ebmission 043: mission filemre
  • d25bd14amission 044: sliced merge prepare + sliced collections prepare; jobs-dump old/new check + bulk fixturemre
  • 633ee210mission 044: sliced table scans (slices.hl, pk-range slices) for the adult/kind/status/art/credits/details-repair queuesmre
  • 55bbd619boot: one streamed pass per table (lib/bootscan.hl) instead of find(null,null) per module; follows/watches/franchises scans streammre
  • 1d465d3ere-vendor plugins to Hybriel master f9614007 (mpackdb #202 copy fix, libmpackdb sha 6fb3b9d6)mre
  • 6648ba44mission 042: real-browser E2E on ce6f2e0a re-vendor 69/0 at 390 + 1280mre