gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchvendor-day59b37cc1da|||PR Vendor Hybriel day59Atlasvendor-day59/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

  • b37cc1da|||PR Vendor Hybriel day59Atlas
  • 05665102Merge branch 'vendor-day56' into mainAtlas
  • 687296fb|||PR Vendor Hybriel day56Atlas
  • a6166d4eMerge branch 'hl-standard-3' into mainAtlas
  • 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas
  • add4b499|||PR Make calendar day actions keyboard accessibleAtlas
  • 79e49b4dMerge branch 'hl-standard' into mainAtlas
  • e9fec7bb|||PR HL-STANDARD clean-up: typed parameters, plain locals, by-reference cacheAtlas
  • eff166a4Re-vendor complete Hybriel plugins and runtime to day44mre
  • cef81394Wait for the DST fixture page instead of an unused socketmre
  • 42434f47Check lazy-session cookies after login with a stored anonymous fixturemre
  • 728e84e3Re-vendor complete Hybriel day41 plugin directoriesmre
  • cbd7ff43Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 59d1da0acalendar: plain declarations instead of needless let in 9 files (calendar#17): 49 declarations that are never reassigned; reassigned ones and the timer closure's p stay letmre
  • f5415cf9calendar: the Style blocks in the language's nested form (calendar#16): rules named after the View's own elements with their children nested, pseudo-classes and @media nested, merged duplicate select rule; same CSS, shots identical in Chrome at 4 sizesmre
  • edb7c947calendar: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 56+4 failing → 60 passedmre
  • bf8cc0fcStyles move into their components: the shell's styles into main.hl, the events and the editors into the month view's Style block; styles.hl keeps only the theme inherit, the accent and the message line; .gitignore ignores a bin symlink too; tests/styles-shots.mjs for the before/after pixel comparemre
  • 5d8579d7README: login.js stays by caramboleyo's explicit rulemre
  • cc759328Merge sb3-standby: calendar deploys through its standbymre
  • f2fcdc6dcalendar: a standby container (port 45015, profile standby, reminder clock off) and a /health page; deploy hands over through standby-swap.sh without a gapmre