gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2047955b2047955bMerge branch 'u12-gateports' into mainTracker2047955b/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

  • 2047955bMerge branch 'u12-gateports' into mainTracker
  • 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
  • 0b86324aMerge branch 'records-dp73' into mainTracker
  • 9ce5f59b|||PR records: DP73 live 613ae0e; deploy.sh keeps 2 backupsTracker
  • 613ae0ecMerge branch 'bs3' into mainTracker
  • 1383264b|||PR BS3: keep shared synchronous sorting fastTracker
  • 63e5ba1a|||PR BS3: guard boot imports and share resumable sort buffersTracker
  • 310613f1Merge branch 'records-dp72' into mainTracker
  • 6f7b61ba|||PR records: deploy-swap fix live, Kunibert findingsTracker
  • c41587d2Merge branch 'bs2' into mainTracker
  • 0dd538a6|||PR BS2: finish the boot index in slices so a booting instance keeps answeringTracker
  • 019d5cedMerge branch 'records-1110' into mainTracker
  • f88ae8b9|||PR records: day56 toggle live, reviewTracker
  • b6b3bb4dMerge branch 'ap56e' into mainTracker
  • 29a26da9|||PR AP56e: guard hidden show helpers during session syncTracker
  • 7c9ec236|||PR AP56e: re-vendor Hybriel set day56 (source 36ae83a8, bin c1f595a2)Tracker
  • 4b54f6f9Merge branch 'records-audit' into mainTracker
  • cab4a3bc|||PR records: branch audit, PR-onlyTracker
  • 641e1c4cMerge branch 'records-dp71' into mainTracker
  • 85a6e9f5|||PR records: DP71 checked in ChromeTracker