gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc98e2043c98e2043log: T36A rollbackmrec98e2043/plugins/mpackdb/MPackDB.hl

5.3 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. \* First record with this primary key, or null. (Named fetch(), not get():
  55. `get` is reserved for getter syntax.) *\
  56. fetch(key) {
  57. let it = #handle.find_pk(key)
  58. if (it == null) {
  59. return null
  60. }
  61. let first = it.next()
  62. // run it to its end: the plugin frees what the lookup read there (hybriel#126)
  63. while (it.next() != null) {}
  64. return first
  65. }
  66. \* Raw iterator over matches. field null → every live record; the pk and any
  67. declared index are binary-searched — other fields need an index declared. *\
  68. iterate(field, value) {
  69. if (field == null) {
  70. return #handle.find_all()
  71. }
  72. if (field == #pk) {
  73. return #handle.find_pk(value)
  74. }
  75. return #handle.find_index(field, value)
  76. }
  77. \* All matching records as a Hybrid; find(null) answers everything. find(null, null) over more than
  78. 100 000 records prints ONE line per table on stderr (the array holds the whole table decoded:
  79. use iterate() or cursor()); the result is the same. *\
  80. find(field, value) {
  81. let out = drain(iterate(field, value))
  82. if (field == null && value == null && out.length > 100000 && !#warned) {
  83. // one line per table: a whole big table decoded into one array holds ~27x its file in RAM
  84. #warned = true
  85. console.error("mpackdb: " + file + ": find(null, null) built an array of " + out.length + " records; use iterate()")
  86. }
  87. return out
  88. }
  89. \* Ascending index range scan, both bounds inclusive; null bound = open end. *\
  90. range(field, low, high) {
  91. return drain(#handle.find_range(field, low, high))
  92. }
  93. \* Lazy Cursor over the same matches — records decode one at a time. *\
  94. cursor(field, value) {
  95. return {
  96. #iter = iterate(field, value);
  97. } > new Cursor()
  98. }
  99. \* Delete every record with this primary key; answers how many went. *\
  100. delete(key) {
  101. return #handle.delete_pk(key)
  102. }
  103. \* Replace the record(s) at this pk; answers how many were replaced. `record`
  104. need not carry the pk: it is taken from `key` (the JS update() contract). *\
  105. update(key, record) {
  106. return #handle.update_pk(key, record)
  107. }
  108. \* Conditional replacement under the store lock. Every named expectation field
  109. uses == against the current record. True means replaced, false means absent
  110. or mismatched, null means failure (lastError()). Like update, key supplies
  111. a missing primary key. *\
  112. compareAndSet(key, Hybrid replacement, Hybrid expect) {
  113. return #handle.compare_and_set(key, replacement, expect)
  114. }
  115. \* Live record count. *\
  116. count() {
  117. return #handle.count()
  118. }
  119. \* Rewrite the data file without tombstones and rebuild the indexes. *\
  120. compactNow() {
  121. #handle.compact()
  122. }
  123. \* Cursors made by any db of this process that are neither exhausted nor closed
  124. (a `for` loop left by break/return closes its cursor at once; mission 404). *\
  125. openCursors() {
  126. return #handle.open_cursors()
  127. }
  128. \* How many cursors a `for` loop closed by leaving early (break/return). *\
  129. earlyCloses() {
  130. return #handle.early_closes()
  131. }
  132. \* Flush meta/index state to disk now. *\
  133. persist() {
  134. #handle.persist()
  135. }
  136. \* Reason for the last failed open/put/update ('' when none). *\
  137. lastError() {
  138. if (#handle == null) { return __native("mpackdb.last_error") }
  139. return #handle.last_error()
  140. }
  141. \* Close the database; the closed event fires. *\
  142. close() {
  143. #handle.close()
  144. emit closed(this)
  145. }
  146. // Internal: collect an iterator into an array.
  147. drain(it) {
  148. let out = []
  149. if (it == null) {
  150. return out
  151. }
  152. for (rec of it) {
  153. out.push(rec)
  154. }
  155. return out
  156. }

Branches

Latest commits

  • c98e2043log: T36A rollbackmre
  • ae905f15Merge t36a: tracker#36 apply rechecks, reportmre
  • 796bfaceT36A: report validated fixes, failed live SSH check and verified rollbackmre
  • 55257852log: #53 live, #56 mergedmre
  • 78b39f1bMerge hm56-rows: tracker#56 list builders insert oncemre
  • 7aea52batmdb fix: protect owners without slugs and record passing gatesmre
  • 9b3c2559tmdb fix: revalidate planned identities and reserve target idsmre
  • 6da612fclog: T36A tool defectmre
  • d56ee0daT36A: report unsafe TMDB apply revalidation and stop before livemre
  • 08e55255log: HM56 done, T36A startedmre
  • 91bcdf0eAvoid per-row list copies and reproduce keyed-row blockersmre
  • 5fd2ac75status: #36 GOmre
  • 0d253b45mission: T36Amre
  • 2aa22d23log: #36 GOmre
  • 833aee2flog: #50 closed, HM56 startedmre
  • ac58c1e5mission: HM56mre
  • 58979c0clog: HM54 held for decision, DP65 startedmre
  • bb2ebc67log: #64 live, #53 mergedmre
  • 27cde42fMerge hm53-dom: tracker#53 inputs bound to membersmre
  • 2156cc7flog: HM54 reviewmre