jukebox
All repositories: gitoria
6.6 KB
\* hl:mpackdb — the MessagePack file database. Construct it and it opens(the file root IS the constructor):db = new MPackDB(file = '/tmp/users.db', primaryKey = '*id',indexes = ['!email', 'age'])Key prefixes follow the JS API: `*name` auto-increment number pk (a freshtable counts from 1; an existing one keeps its stored counter), `@name`uuid pk, plain `name` string pk; a LEADING `!` on an index makes itunique, `*`/`@` on an index select numeric/uuid key parsing. On-diskformat is compatible with the JS mpackdb 1.0.7 — a db written by eitherside is readable and mutable by the other. *\/* Wraps the Zig engine (plugins/mpackdb/{msgpack,engine}.zig, mission 066).`>` / `<>` hydration does NOT run the run body — construct with args. */\* The database is open (fired from the constructor's run body). *\emit opened(MPackDB db \\ this database)\* Opening failed. *\emit error(Hybrid e \\ the failure)\* close() finished. *\emit closed(MPackDB db \\ this database)\* One successful record mutation; delivered synchronously after the store unlocks. *\emit change(String kind, String op, Number | String key, Hybrid doc, Hybrid prev)String file = "" \\ the .db path, script-relativeString primaryKey = "" \\ pk declaration: *name | @name | name | '' for noneHybrid indexes = [] \\ index declarations, '!' prefix = uniqueBoolean compact = true \\ compact the file on openHybrid #handleBoolean #warned = false \\ find(null, null) over a big table was reported on stderr alreadyString #pk \\ the primary key FIELD NAME, prefixes stripped (null when none)open() {#handle = __native("mpackdb.open", file, primaryKey, indexes, compact)if (#handle != null) {#pk = #handle.pk_name()emit opened(this)} else {emit error({ message = "mpackdb: cannot open " + file + ": " + lastError() })}}open()\* The primary key field name, prefixes stripped (null when none). *\pkField() {return #pk}\* Insert one record. Answers the primary key it was stored under (agenerated one for `*`/`@` pks), true for a pk-less db, or null on failure— lastError() has the reason (e.g. a unique-index violation). *\put(Hybrid record \\ the record to store) {return mutate('insert', null, record, null)}\* Read only the named top-level fields when fields is a list; null reads all.First record with this primary key, or null. (Named fetch(), not get():`get` is reserved for getter syntax.) *\fetch(key, Hybrid fields = null) {it = #handle.find_pk(key, fields)if (it == null) {return null}first = it.next()// run it to its end: the plugin frees what the lookup read there (hybriel#126)while (it.next() != null) {}return first}\* Raw iterator over matches. field null → every live record; the pk and anydeclared index are binary-searched — other fields need an index declared. *\iterate(field, value, Hybrid fields = null) {if (field == null) {return #handle.find_all(fields)}if (field == #pk) {return #handle.find_pk(value, fields)}return #handle.find_index(field, value, fields)}\* All matching records as a Hybrid; find(null) answers everything. find(null, null) over more than100 000 records prints ONE line per table on stderr (the array holds the whole table decoded:use iterate() or cursor()); the result is the same. *\find(field, value, Hybrid fields = null) {out = drain(iterate(field, value, fields))if (field == null && value == null && out.length > 100000 && !#warned) {// one line per table: a whole big table decoded into one array holds ~27x its file in RAM#warned = trueconsole.error("mpackdb: " + file + ": find(null, null) built an array of " + out.length + " records; use iterate()")}return out}\* Ascending index range scan, both bounds inclusive; null bound = open end. *\range(field, low, high, Hybrid fields = null) {return drain(#handle.find_range(field, low, high, fields))}\* Lazy Cursor over the same matches — records decode one at a time. *\cursor(field, value, Hybrid fields = null) {return {#iter = iterate(field, value, fields)} > new Cursor()}\* Delete every record with this primary key; answers how many went. *\delete(key) {return mutate('delete', key, null, null)}\* Replace the record(s) at this pk; answers how many were replaced. `record`need not carry the pk: it is taken from `key` (the JS update() contract). *\update(key, record) {return mutate('update', key, record, null)}\* Conditional replacement under the store lock. Every named expectation fielduses == against the current record. True means replaced, false means absentor mismatched, null means failure (lastError()). Like update, key suppliesa missing primary key. *\compareAndSet(key, Hybrid replacement, Hybrid expect) {return mutate('compareAndSet', key, replacement, expect)}\* Live record count. *\count() {return #handle.count()}\* Rewrite the data file without tombstones and rebuild the indexes. *\compactNow() {#handle.compact()}\* Cursors made by any db of this process that are neither exhausted nor closed(a `for` loop left by break/return closes its cursor at once; mission 404). *\openCursors() {return #handle.open_cursors()}\* How many cursors a `for` loop closed by leaving early (break/return). *\earlyCloses() {return #handle.early_closes()}\* Flush meta/index state to disk now. *\persist() {#handle.persist()}\* Reason for the last failed open/put/update ('' when none). *\lastError() {if (#handle == null) { return __native("mpackdb.last_error") }return #handle.last_error()}\* Close the database; the closed event fires. *\close() {#handle.close()emit closed(this)}// Internal: collect an iterator into an array.drain(it) {let out = []if (it == null) {return out}for (rec of it) {out.push(rec)}return out}\* Insert a missing primary key or replace its record, under one store lock.Answers the primary key (true without a pk), or null; lastError() gives the reason. *\upsert(Hybrid record) {return mutate('upsert', null, record, null)}// Consume the owned native result BEFORE emitting: handlers may reenter this db.mutate(String op, Number | String key, Hybrid record, Hybrid expect) {it = #handle.mutate(op, key, record, expect)if (it == null) { return null }result = it.next()while (it.next() != null) {}if (result.kind == 'insert') {storedKey = #pk == null ? null : result.doc[#pk]emit change('insert', op, storedKey, result.doc, null)} else {for (previous of result.prev) {storedKey = #pk == null ? null : previous[#pk]emit change(result.kind, op, storedKey, result.doc, previous)}}return result.answer}
Branches
Latest commits
- 10f8a8c8Merge branch 'code-order' into mainJukebox
- 204fc828Merge branch 'day72' into mainJukebox
- 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
- 33072bb5|||PR Record day72 gates and Chrome proofmre
- 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
- 6422a020Sync media-player at 51c3818mre
- b582ba1cVendor complete Hybriel day72 plugin setmre
- 02dccbc9Merge branch 'records-1119' into mainJukebox
- a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
- fc7db075Merge branch 'reconnect-retry' into mainJukebox
- ac6f4c6fL2: plain declaration for a in pickedNextmre
- 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
- 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
- 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
- 1f0cd77bMerge branch 'records-day41' into mainJukebox
- 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
- 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
- 04f5d9d5Merge branch 'day41' into mainJukebox
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre