gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfa00cd35fa00cd35log: Kunibert pings provenmrefa00cd35/plugins/mpackdb/MPackDB.hl

5.6 KB

  1. \* hl:mpackdb — the MessagePack file database. Construct it and it opens
  2. (the file root IS the constructor):
  3. db = new MPackDB(file = '/tmp/users.db', primaryKey = '*id',
  4. indexes = ['!email', 'age'])
  5. Key prefixes follow the JS API: `*name` auto-increment number pk (a fresh
  6. table counts from 1; an existing one keeps its stored counter), `@name`
  7. uuid pk, plain `name` string pk; a LEADING `!` on an index makes it
  8. unique, `*`/`@` on an index select numeric/uuid key parsing. On-disk
  9. format is compatible with the JS mpackdb 1.0.7 — a db written by either
  10. side is readable and mutable by the other. *\
  11. /* Wraps the Zig engine (plugins/mpackdb/{msgpack,engine}.zig, mission 066).
  12. `>` / `<>` hydration does NOT run the run body — construct with args. */
  13. \* The database is open (fired from the constructor's run body). *\
  14. emit opened(
  15. MPackDB db \\ this database
  16. )
  17. \* Opening failed. *\
  18. emit error(
  19. Hybrid e \\ the failure
  20. )
  21. \* close() finished. *\
  22. emit closed(
  23. MPackDB db \\ this database
  24. )
  25. String file = "" \\ the .db path, script-relative
  26. String primaryKey = "" \\ pk declaration: *name | @name | name | '' for none
  27. Hybrid indexes = [] \\ index declarations, '!' prefix = unique
  28. Boolean compact = true \\ compact the file on open
  29. Hybrid #handle
  30. Boolean #warned = false \\ find(null, null) over a big table was reported on stderr already
  31. String #pk \\ the primary key FIELD NAME, prefixes stripped (null when none)
  32. open() {
  33. #handle = __native("mpackdb.open", file, primaryKey, indexes, compact)
  34. if (#handle != null) {
  35. #pk = #handle.pk_name()
  36. emit opened(this)
  37. } else {
  38. emit error({ message = "mpackdb: cannot open " + file + ": " + lastError() })
  39. }
  40. }
  41. open()
  42. \* The primary key field name, prefixes stripped (null when none). *\
  43. pkField() {
  44. return #pk
  45. }
  46. \* Insert one record. Answers the primary key it was stored under (a
  47. generated one for `*`/`@` pks), true for a pk-less db, or null on failure
  48. — lastError() has the reason (e.g. a unique-index violation). *\
  49. put(
  50. Hybrid record \\ the record to store
  51. ) {
  52. return #handle.insert(record)
  53. }
  54. \* Read only the named top-level fields when fields is a list; null reads all.
  55. First record with this primary key, or null. (Named fetch(), not get():
  56. `get` is reserved for getter syntax.) *\
  57. fetch(key, Hybrid fields = null) {
  58. it = #handle.find_pk(key, fields)
  59. if (it == null) {
  60. return null
  61. }
  62. first = it.next()
  63. // run it to its end: the plugin frees what the lookup read there (hybriel#126)
  64. while (it.next() != null) {}
  65. return first
  66. }
  67. \* Raw iterator over matches. field null → every live record; the pk and any
  68. declared index are binary-searched — other fields need an index declared. *\
  69. iterate(field, value, Hybrid fields = null) {
  70. if (field == null) {
  71. return #handle.find_all(fields)
  72. }
  73. if (field == #pk) {
  74. return #handle.find_pk(value, fields)
  75. }
  76. return #handle.find_index(field, value, fields)
  77. }
  78. \* All matching records as a Hybrid; find(null) answers everything. find(null, null) over more than
  79. 100 000 records prints ONE line per table on stderr (the array holds the whole table decoded:
  80. use iterate() or cursor()); the result is the same. *\
  81. find(field, value, Hybrid fields = null) {
  82. out = drain(iterate(field, value, fields))
  83. if (field == null && value == null && out.length > 100000 && !#warned) {
  84. // one line per table: a whole big table decoded into one array holds ~27x its file in RAM
  85. #warned = true
  86. console.error("mpackdb: " + file + ": find(null, null) built an array of " + out.length + " records; use iterate()")
  87. }
  88. return out
  89. }
  90. \* Ascending index range scan, both bounds inclusive; null bound = open end. *\
  91. range(field, low, high, Hybrid fields = null) {
  92. return drain(#handle.find_range(field, low, high, fields))
  93. }
  94. \* Lazy Cursor over the same matches — records decode one at a time. *\
  95. cursor(field, value, Hybrid fields = null) {
  96. return {
  97. #iter = iterate(field, value, fields)
  98. } > new Cursor()
  99. }
  100. \* Delete every record with this primary key; answers how many went. *\
  101. delete(key) {
  102. return #handle.delete_pk(key)
  103. }
  104. \* Replace the record(s) at this pk; answers how many were replaced. `record`
  105. need not carry the pk: it is taken from `key` (the JS update() contract). *\
  106. update(key, record) {
  107. return #handle.update_pk(key, record)
  108. }
  109. \* Conditional replacement under the store lock. Every named expectation field
  110. uses == against the current record. True means replaced, false means absent
  111. or mismatched, null means failure (lastError()). Like update, key supplies
  112. a missing primary key. *\
  113. compareAndSet(key, Hybrid replacement, Hybrid expect) {
  114. return #handle.compare_and_set(key, replacement, expect)
  115. }
  116. \* Live record count. *\
  117. count() {
  118. return #handle.count()
  119. }
  120. \* Rewrite the data file without tombstones and rebuild the indexes. *\
  121. compactNow() {
  122. #handle.compact()
  123. }
  124. \* Cursors made by any db of this process that are neither exhausted nor closed
  125. (a `for` loop left by break/return closes its cursor at once; mission 404). *\
  126. openCursors() {
  127. return #handle.open_cursors()
  128. }
  129. \* How many cursors a `for` loop closed by leaving early (break/return). *\
  130. earlyCloses() {
  131. return #handle.early_closes()
  132. }
  133. \* Flush meta/index state to disk now. *\
  134. persist() {
  135. #handle.persist()
  136. }
  137. \* Reason for the last failed open/put/update ('' when none). *\
  138. lastError() {
  139. if (#handle == null) { return __native("mpackdb.last_error") }
  140. return #handle.last_error()
  141. }
  142. \* Close the database; the closed event fires. *\
  143. close() {
  144. #handle.close()
  145. emit closed(this)
  146. }
  147. // Internal: collect an iterator into an array.
  148. drain(it) {
  149. let out = []
  150. if (it == null) {
  151. return out
  152. }
  153. for (rec of it) {
  154. out.push(rec)
  155. }
  156. return out
  157. }

Branches

Latest commits

  • fa00cd35log: Kunibert pings provenmre
  • 3a68cfaclog: components decisionmre
  • 87de8e31log: Kunibert connected to 9 app reposmre
  • c22aff69log: PR-only rollout startedmre
  • 25309981log: STOP and paused workers 2026-11-08mre
  • 5286803alog: worker briefs and reports of 2026-11-08mre
  • 85c86605log: 2026-11-08 Arachne on list shapemre
  • cb634512log: 2026-11-08 keyed lists livemre
  • 07c98d4aLists: keyed rows with @id, remove(id) and one whole-list assignment instead of hand-rebuilt loopsmre
  • 0db0d81elog: 2026-11-08 day52 live, W30 startedmre
  • b05f6d92tests/status: the gate's 3 s app token expired during the restart; it lives 60 s like status-keyedmre
  • 1e8f4f0avendor: Hybriel set-day52 (fault-path leak fix #335, keyed rows #331)mre
  • f5f0fb7blog: 2026-11-08 day50/#29 live, H25 correction, V52 startedmre
  • dc77fb95main: offline note from __hlOnline, tick removed (#29)mre
  • 37a86d93tests/reviews: seeded fake hook's DNS failures are not server errors (flaky gate)mre
  • 07ab845bvendor: Hybriel set-day50 (run() leak fix #335)mre
  • 71b2fa4blog: hybriel#335mre
  • bd94cc7elog: #25 cause found (run() leak)mre
  • 565cb508log: H25 round 1mre
  • 90a0af84log: H25 startedmre