gitoriaLog in with ident

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit14ba08c714ba08c7antcolony#40: mission references point to the moved missionsmre14ba08c7/plugins/mpackdb/mpackdb.zig

23.3 KB

  1. // hl:mpackdb plugin — native shared library (mission 066)
  2. // Zig port of mpackdb v1.0.7 (MessagePack DB with binary-search indexes).
  3. //
  4. // Layout:
  5. // msgpack.zig — MessagePack codec (msgpackr-compatible, see D12)
  6. // engine.zig — DB engine (MPackDB/IndexManager semantics)
  7. // mpackdb.zig — this file: plugin ABI surface
  8. //
  9. // Exports:
  10. // hl_mpackdb_open(path, primaryKey, indexes, compact) → handle
  11. // handle ops: insert(record) / find_all() / find_pk(v) /
  12. // find_index(field, v) / find_range(field, from, to) /
  13. // delete_pk(v) / update_pk(v, record) / count() /
  14. // compact() / persist() / last_error() / pk_name()
  15. //
  16. // The `hl:mpackdb` .hl surface (MPackDB.hl, cursor.hl, server.hl) is the public
  17. // API; these ops are its transport.
  18. //
  19. // Cursors materialize matches eagerly into a per-iterator arena; the runtime
  20. // deep-copies each record on next(), and the arena is freed at iterator deinit.
  21. const std = @import("std");
  22. const api = @import("plugin_api");
  23. pub const msgpack = @import("msgpack.zig");
  24. pub const engine = @import("engine.zig");
  25. const HlValue = api.HlValue;
  26. const HlObject = api.HlObject;
  27. const HlField = api.HlField;
  28. const HlIterator = api.HlIterator;
  29. const HlHandle = api.HlHandle;
  30. const HlString = api.HlString;
  31. var gpa_state = std.heap.DebugAllocator(.{}){};
  32. const gpa = gpa_state.allocator();
  33. fn hlStr(comptime s: []const u8) HlString {
  34. return .{ .ptr = s.ptr, .len = s.len };
  35. }
  36. // =========================================================================
  37. // Path resolution (mission 077, 074 GAP 9)
  38. //
  39. // CONTRACT — the same one hl:fs got in mission 068: a relative db path resolves
  40. // against the MAIN SCRIPT's directory, not the process CWD. `cd /tmp && hybriel
  41. // /srv/app/main.hl` must find /srv/app's database, not create a second one in
  42. // /tmp. Absolute paths are used as-is. The runtime injects the directory through
  43. // the optional plugin hook hl_mpackdb_set_script_dir right after dlopen.
  44. // =========================================================================
  45. var script_dir: ?[]u8 = null;
  46. export fn hl_mpackdb_set_script_dir(ptr: [*]const u8, len: usize) callconv(.c) void {
  47. if (script_dir) |old| gpa.free(old);
  48. script_dir = gpa.dupe(u8, ptr[0..len]) catch null;
  49. }
  50. /// Resolve a (possibly relative) db path against the script dir. The returned
  51. /// slice is gpa-allocated; caller frees.
  52. fn resolvePath(path: []const u8) ?[]u8 {
  53. if (path.len == 0 or path[0] == '/') return gpa.dupe(u8, path) catch null;
  54. if (script_dir) |sd| return std.fmt.allocPrint(gpa, "{s}/{s}", .{ sd, path }) catch null;
  55. return gpa.dupe(u8, path) catch null;
  56. }
  57. // =========================================================================
  58. // HlValue ⇄ msgpack.Value conversion
  59. // =========================================================================
  60. /// AN EMPTY LIST is marked by its `fields` address (ticket #58) — the runtime's
  61. /// contract, stated in native/src/plugin_loader.zig beside the same constant. Zero
  62. /// fields alone read back as an empty hybrid, and `tags = []` was stored as a map.
  63. const EMPTY_LIST_FIELDS: [*]HlField = @ptrFromInt(0x4C49_5354_0000_0008);
  64. fn isEmptyList(obj: *const HlObject) bool {
  65. return obj.field_count == 0 and obj.fields == EMPTY_LIST_FIELDS;
  66. }
  67. /// Hybriel value (as HlObject tree) → msgpack value, allocated in `arena`.
  68. /// An HlObject whose keys are exactly the sequence "0".."n-1" (in order)
  69. /// becomes a msgpack array; everything else becomes a map. Hybriel null and
  70. /// undefined both arrive as hl_null → msgpack nil.
  71. fn hlToMsgpack(arena: std.mem.Allocator, v: HlValue) msgpack.Value {
  72. switch (v.type) {
  73. .hl_null => return .nil,
  74. .hl_bool => return .{ .boolean = v.data.boolean },
  75. .hl_number => return .{ .number = v.data.number },
  76. .hl_string => {
  77. const s = v.data.string.ptr[0..v.data.string.len];
  78. const copy = arena.dupe(u8, s) catch return .nil;
  79. return .{ .str = copy };
  80. },
  81. .hl_object => {
  82. const obj = v.data.object;
  83. const fields = obj.fields[0..obj.field_count];
  84. // strict numeric sequence "0".."n-1" → array; an empty list carries its
  85. // mark (ticket #58), or `tags = []` was stored as an empty map
  86. var is_array = fields.len > 0 or isEmptyList(obj);
  87. for (fields, 0..) |f, i| {
  88. const k = f.key.ptr[0..f.key.len];
  89. var buf: [20]u8 = undefined;
  90. const expect = std.fmt.bufPrint(&buf, "{d}", .{i}) catch "";
  91. if (!std.mem.eql(u8, k, expect)) {
  92. is_array = false;
  93. break;
  94. }
  95. }
  96. if (is_array) {
  97. const items = arena.alloc(msgpack.Value, fields.len) catch return .nil;
  98. for (fields, 0..) |f, i| items[i] = hlToMsgpack(arena, f.value);
  99. return .{ .array = items };
  100. }
  101. const entries = arena.alloc(msgpack.Entry, fields.len) catch return .nil;
  102. for (fields, 0..) |f, i| {
  103. const k = arena.dupe(u8, f.key.ptr[0..f.key.len]) catch return .nil;
  104. entries[i] = .{ .key = .{ .str = k }, .value = hlToMsgpack(arena, f.value) };
  105. }
  106. return .{ .map = entries };
  107. },
  108. else => return .nil,
  109. }
  110. }
  111. /// msgpack value → HlValue tree allocated in `arena` (no deinit hooks — the
  112. /// runtime deep-copies immediately; the arena outlives the call).
  113. fn msgpackToHl(arena: std.mem.Allocator, v: msgpack.Value) HlValue {
  114. switch (v) {
  115. .nil, .undef => return api.makeNull(),
  116. .boolean => |b| return api.makeBool(b),
  117. .number => |n| return api.makeNumber(n),
  118. .str => |s| return api.makeString(s),
  119. .bin => |b| return api.makeString(b),
  120. .array => |items| {
  121. const fields = arena.alloc(HlField, items.len) catch return api.makeNull();
  122. for (items, 0..) |it, i| {
  123. var buf: [20]u8 = undefined;
  124. const k = std.fmt.bufPrint(&buf, "{d}", .{i}) catch "";
  125. const key = arena.dupe(u8, k) catch return api.makeNull();
  126. fields[i] = .{ .key = .{ .ptr = key.ptr, .len = key.len }, .value = msgpackToHl(arena, it) };
  127. }
  128. const obj = arena.create(HlObject) catch return api.makeNull();
  129. obj.* = .{ .fields = if (items.len == 0) EMPTY_LIST_FIELDS else fields.ptr, .field_count = fields.len, .deinit_fn = null };
  130. return api.makeObject(obj);
  131. },
  132. .map => |entries| {
  133. const fields = arena.alloc(HlField, entries.len) catch return api.makeNull();
  134. var n: usize = 0;
  135. for (entries) |e| {
  136. const key: []const u8 = switch (e.key) {
  137. .str => |s| s,
  138. .number => |num| blk: {
  139. var buf: [40]u8 = undefined;
  140. const k = engine.jsNumFmt(&buf, num);
  141. break :blk arena.dupe(u8, k) catch continue;
  142. },
  143. else => continue,
  144. };
  145. fields[n] = .{ .key = .{ .ptr = key.ptr, .len = key.len }, .value = msgpackToHl(arena, e.value) };
  146. n += 1;
  147. }
  148. const obj = arena.create(HlObject) catch return api.makeNull();
  149. obj.* = .{ .fields = fields.ptr, .field_count = n, .deinit_fn = null };
  150. return api.makeObject(obj);
  151. },
  152. .ext => {
  153. if (v.timestampMs()) |ms| return api.makeNumber(ms);
  154. return api.makeNull();
  155. },
  156. }
  157. }
  158. fn hlKey(v: HlValue) ?engine.Key {
  159. return switch (v.type) {
  160. .hl_number => .{ .num = v.data.number },
  161. .hl_string => .{ .str = v.data.string.ptr[0..v.data.string.len] },
  162. else => null,
  163. };
  164. }
  165. // =========================================================================
  166. // Record iterator (eagerly materialized)
  167. // =========================================================================
  168. const RecIterState = struct {
  169. arena_state: std.heap.ArenaAllocator,
  170. records: []msgpack.Value = &.{},
  171. idx: usize = 0,
  172. };
  173. fn recIterNext(ctx: ?*anyopaque) callconv(.c) HlValue {
  174. const state: *RecIterState = @ptrCast(@alignCast(ctx orelse return api.makeNull()));
  175. if (state.idx >= state.records.len) return api.makeNull();
  176. const v = state.records[state.idx];
  177. state.idx += 1;
  178. return msgpackToHl(state.arena_state.allocator(), v);
  179. }
  180. fn recIterDeinit(ctx: ?*anyopaque) callconv(.c) void {
  181. const state: *RecIterState = @ptrCast(@alignCast(ctx orelse return));
  182. state.arena_state.deinit();
  183. gpa.destroy(state);
  184. }
  185. fn makeRecordIterator(records: []msgpack.Value, arena_state: std.heap.ArenaAllocator) HlValue {
  186. const state = gpa.create(RecIterState) catch return api.makeNull();
  187. state.* = .{ .arena_state = arena_state, .records = records };
  188. const iter = gpa.create(HlIterator) catch {
  189. gpa.destroy(state);
  190. return api.makeNull();
  191. };
  192. iter.* = .{
  193. .context = @ptrCast(state),
  194. .next_fn = &recIterNext,
  195. .deinit_fn = &recIterDeinitOuter,
  196. };
  197. return api.makeIterator(iter);
  198. }
  199. // The runtime's IterWrapper deinit calls iter.deinit_fn then frees its own
  200. // wrapper — but the HlIterator struct itself is ours to free.
  201. fn recIterDeinitOuter(ctx: ?*anyopaque) callconv(.c) void {
  202. recIterDeinit(ctx);
  203. }
  204. // =========================================================================
  205. // DB handle
  206. // =========================================================================
  207. //
  208. // Process-wide table registry (ticket #110). Within one process, a table
  209. // path has ONE open engine.Db, shared by every hl_mpackdb_open() on it —
  210. // whatever module instance or realm calls it. `hl:mpackdb`'s .so is loaded
  211. // once and mapped once by the dynamic linker no matter how many realms
  212. // dlopen it (same path, same mapping), so these are already process-wide
  213. // statics; the registry just gives multiple opens of the same table a
  214. // single Db to share instead of a fresh one each time. A single realm's
  215. // event loop drives every call here — mpackdb has never been thread-safe
  216. // (it uses the non-thread-safe DebugAllocator already) — so no mutex.
  217. //
  218. // The file is never compacted or rewritten while a handle on it is live:
  219. // only the FIRST open of a path in this process actually runs
  220. // engine.Db.open (and so is the only one that can compact-on-open, per
  221. // ticket #21's existing rule); every later open of the same path reuses
  222. // that live Db untouched, and its own primaryKey/indexes/compact args are
  223. // ignored — the table's schema was fixed by the first opener. Compaction
  224. // happens only when asked for explicitly (compactNow()) or when the LAST
  225. // handle on a path closes.
  226. const SharedDb = struct {
  227. db: *engine.Db,
  228. key: []const u8, // owned; also the registry's stored key for this entry
  229. refcount: usize,
  230. };
  231. var registry: std.StringHashMapUnmanaged(*SharedDb) = .empty;
  232. fn acquireShared(path: []const u8, opts: engine.OpenOptions) ?*SharedDb {
  233. const key = engine.tableKey(gpa, path) catch return null;
  234. if (registry.get(key)) |existing| {
  235. gpa.free(key);
  236. existing.refcount += 1;
  237. return existing;
  238. }
  239. const db = engine.Db.open(gpa, path, opts) catch |e| {
  240. std.debug.print("mpackdb plugin: open failed: {s}\n", .{@errorName(e)});
  241. gpa.free(key);
  242. return null;
  243. };
  244. const shared = gpa.create(SharedDb) catch {
  245. db.close();
  246. gpa.free(key);
  247. return null;
  248. };
  249. shared.* = .{ .db = db, .key = key, .refcount = 1 };
  250. registry.put(gpa, key, shared) catch {
  251. db.close();
  252. gpa.destroy(shared);
  253. gpa.free(key);
  254. return null;
  255. };
  256. return shared;
  257. }
  258. /// Drop this handle's reference. On the LAST reference: compact away any
  259. /// tombstones (ticket #110 — that's the one moment compaction may run
  260. /// implicitly) and close for real.
  261. fn releaseShared(shared: *SharedDb) void {
  262. shared.refcount -= 1;
  263. if (shared.refcount > 0) return;
  264. _ = registry.remove(shared.key);
  265. if (shared.db.deleted.items.len > 0) {
  266. shared.db.compact() catch |e| {
  267. std.debug.print("mpackdb plugin: close-compact failed: {s}\n", .{@errorName(e)});
  268. };
  269. }
  270. shared.db.close();
  271. gpa.free(shared.key);
  272. gpa.destroy(shared);
  273. }
  274. const DbState = struct {
  275. shared: *SharedDb,
  276. };
  277. export fn hl_mpackdb_open(argc: u32, argv: [*]const HlValue) callconv(.c) HlValue {
  278. if (argc < 1 or argv[0].type != .hl_string) return api.makeNull();
  279. const raw_path = argv[0].data.string.ptr[0..argv[0].data.string.len];
  280. // mission 077: script-relative, like every other path in Hybriel.
  281. const path = resolvePath(raw_path) orelse return api.makeNull();
  282. defer gpa.free(path);
  283. var opts = engine.OpenOptions{};
  284. if (argc > 1 and argv[1].type == .hl_string and argv[1].data.string.len > 0) {
  285. opts.primary_key = argv[1].data.string.ptr[0..argv[1].data.string.len];
  286. }
  287. var index_names: [64][]const u8 = undefined;
  288. var index_count: usize = 0;
  289. if (argc > 2 and argv[2].type == .hl_object) {
  290. const obj = argv[2].data.object;
  291. for (obj.fields[0..obj.field_count]) |f| {
  292. if (f.value.type != .hl_string) continue;
  293. if (index_count >= index_names.len) break;
  294. index_names[index_count] = f.value.data.string.ptr[0..f.value.data.string.len];
  295. index_count += 1;
  296. }
  297. }
  298. opts.indexes = index_names[0..index_count];
  299. if (argc > 3 and argv[3].type == .hl_bool) {
  300. opts.compact = argv[3].data.boolean;
  301. }
  302. const shared = acquireShared(path, opts) orelse return api.makeNull();
  303. const state = gpa.create(DbState) catch {
  304. releaseShared(shared);
  305. return api.makeNull();
  306. };
  307. state.* = .{ .shared = shared };
  308. const handle = gpa.create(HlHandle) catch {
  309. releaseShared(shared);
  310. gpa.destroy(state);
  311. return api.makeNull();
  312. };
  313. handle.* = .{
  314. .context = @ptrCast(state),
  315. .call_fn = &handleCall,
  316. .close_fn = &handleClose,
  317. .type_name = hlStr("MPackDB"),
  318. };
  319. return api.makeHandle(handle);
  320. }
  321. fn errDeinit(val: *HlValue) callconv(.c) void {
  322. if (val.type == .hl_error) {
  323. const s = val.data.string;
  324. gpa.free(@constCast(s.ptr[0..s.len]));
  325. }
  326. }
  327. /// mission 077 (074 GAP 10): querying a field that carries no index is a
  328. /// MISTAKE, not an empty result — mpackdb only answers through binary-searched
  329. /// indexes, so `[]` was indistinguishable from "no such record" and hid the
  330. /// missing declaration. The message names the field, the op and every index the
  331. /// db was actually opened with (insertion order: primary key first).
  332. fn makeNoIndexError(db: *engine.Db, op: []const u8, field: []const u8) HlValue {
  333. var list: std.ArrayList(u8) = .empty;
  334. defer list.deinit(gpa);
  335. for (db.idx.keys()) |k| {
  336. if (list.items.len > 0) list.appendSlice(gpa, ", ") catch {};
  337. list.append(gpa, '\'') catch {};
  338. list.appendSlice(gpa, k) catch {};
  339. list.append(gpa, '\'') catch {};
  340. }
  341. const declared = if (list.items.len == 0) "none" else list.items;
  342. const msg = std.fmt.allocPrint(
  343. gpa,
  344. "mpackdb.{s}: no index on field '{s}' — this db is indexed on [{s}]; every query goes through an index (declare it in `indexes` when opening, or query the primary key)",
  345. .{ op, field, declared },
  346. ) catch return api.makeError("mpackdb: no index on that field");
  347. var v = api.makeError(msg);
  348. v.deinit_fn = &errDeinit;
  349. return v;
  350. }
  351. fn handleClose(ctx: ?*anyopaque) callconv(.c) void {
  352. const state: *DbState = @ptrCast(@alignCast(ctx orelse return));
  353. releaseShared(state.shared);
  354. gpa.destroy(state);
  355. }
  356. fn pkStringDeinit(val: *HlValue) callconv(.c) void {
  357. if (val.type == .hl_string) {
  358. const s = val.data.string;
  359. gpa.free(@constCast(s.ptr[0..s.len]));
  360. }
  361. }
  362. fn handleCall(ctx: ?*anyopaque, op: HlString, argc: u32, argv: [*]const HlValue) callconv(.c) HlValue {
  363. const state: *DbState = @ptrCast(@alignCast(ctx orelse return api.makeNull()));
  364. const db = state.shared.db;
  365. const method = op.ptr[0..op.len];
  366. if (std.mem.eql(u8, method, "insert")) {
  367. if (argc < 1 or argv[0].type != .hl_object) return api.makeNull();
  368. var arena_state = std.heap.ArenaAllocator.init(gpa);
  369. defer arena_state.deinit();
  370. const arena = arena_state.allocator();
  371. const rec = hlToMsgpack(arena, argv[0]);
  372. if (rec != .map) return api.makeNull();
  373. const res = db.insert(arena, rec, false) catch |e| {
  374. if (e != error.DuplicateKey) {
  375. std.debug.print("mpackdb plugin: insert failed: {s}\n", .{@errorName(e)});
  376. db.last_error = std.fmt.bufPrint(&db.last_error_buf, "insert failed: {s}", .{@errorName(e)}) catch "insert failed";
  377. }
  378. return api.makeNull();
  379. };
  380. switch (res) {
  381. .pk_num => |n| return api.makeNumber(n),
  382. .pk_str => |s| {
  383. const copy = gpa.dupe(u8, s) catch return api.makeNull();
  384. var out = api.makeString(copy);
  385. out.deinit_fn = &pkStringDeinit;
  386. return out;
  387. },
  388. .record => return api.makeBool(true),
  389. }
  390. }
  391. if (std.mem.eql(u8, method, "find_all")) {
  392. var arena_state = std.heap.ArenaAllocator.init(gpa);
  393. const arena = arena_state.allocator();
  394. var records: std.ArrayList(msgpack.Value) = .empty;
  395. db.refresh();
  396. var scanner = engine.Db.Scanner.init(db, arena, true);
  397. while (true) {
  398. const maybe = scanner.next(arena) catch break;
  399. const rec = maybe orelse break;
  400. records.append(arena, rec.value) catch break;
  401. }
  402. scanner.deinit();
  403. const slice = records.toOwnedSlice(arena) catch &.{};
  404. return makeRecordIterator(@constCast(slice), arena_state);
  405. }
  406. if (std.mem.eql(u8, method, "find_pk")) {
  407. if (argc < 1) return api.makeNull();
  408. const key = hlKey(argv[0]) orelse return api.makeNull();
  409. var arena_state = std.heap.ArenaAllocator.init(gpa);
  410. const arena = arena_state.allocator();
  411. var locs: std.ArrayList(engine.Loc) = .empty;
  412. db.findPkLocs(arena, key, &locs) catch {
  413. arena_state.deinit();
  414. return api.makeNull();
  415. };
  416. return locsToIterator(db, arena_state, locs.items);
  417. }
  418. if (std.mem.eql(u8, method, "find_index")) {
  419. if (argc < 2 or argv[0].type != .hl_string) return api.makeNull();
  420. const field = argv[0].data.string.ptr[0..argv[0].data.string.len];
  421. const key = hlKey(argv[1]) orelse return api.makeNull();
  422. var arena_state = std.heap.ArenaAllocator.init(gpa);
  423. const arena = arena_state.allocator();
  424. var locs: std.ArrayList(engine.Loc) = .empty;
  425. db.findIndexLocs(arena, field, key, &locs) catch |e| {
  426. arena_state.deinit();
  427. if (e == error.NoSuchIndex) return makeNoIndexError(db, "find", field);
  428. return api.makeNull();
  429. };
  430. return locsToIterator(db, arena_state, locs.items);
  431. }
  432. if (std.mem.eql(u8, method, "find_range")) {
  433. if (argc < 1 or argv[0].type != .hl_string) return api.makeNull();
  434. const field = argv[0].data.string.ptr[0..argv[0].data.string.len];
  435. const from: ?engine.Key = if (argc > 1) hlKey(argv[1]) else null;
  436. const to: ?engine.Key = if (argc > 2) hlKey(argv[2]) else null;
  437. var arena_state = std.heap.ArenaAllocator.init(gpa);
  438. const arena = arena_state.allocator();
  439. var locs: std.ArrayList(engine.Loc) = .empty;
  440. db.findRangeLocs(arena, field, from, to, &locs) catch |e| {
  441. arena_state.deinit();
  442. if (e == error.NoSuchIndex) return makeNoIndexError(db, "range", field);
  443. return api.makeNull();
  444. };
  445. return locsToIterator(db, arena_state, locs.items);
  446. }
  447. if (std.mem.eql(u8, method, "delete_pk")) {
  448. if (argc < 1) return api.makeNull();
  449. const key = hlKey(argv[0]) orelse return api.makeNull();
  450. var arena_state = std.heap.ArenaAllocator.init(gpa);
  451. defer arena_state.deinit();
  452. const arena = arena_state.allocator();
  453. var deleted: std.ArrayList(msgpack.Value) = .empty;
  454. const n = db.deletePk(arena, key, &deleted) catch |e| {
  455. std.debug.print("mpackdb plugin: delete failed: {s}\n", .{@errorName(e)});
  456. return api.makeNull();
  457. };
  458. return api.makeNumber(@floatFromInt(n));
  459. }
  460. if (std.mem.eql(u8, method, "update_pk")) {
  461. if (argc < 2 or argv[1].type != .hl_object) return api.makeNull();
  462. const key = hlKey(argv[0]) orelse return api.makeNull();
  463. var arena_state = std.heap.ArenaAllocator.init(gpa);
  464. defer arena_state.deinit();
  465. const arena = arena_state.allocator();
  466. const rec = hlToMsgpack(arena, argv[1]);
  467. if (rec != .map) return api.makeNull();
  468. const n = db.updatePk(arena, key, rec) catch |e| {
  469. if (e == error.RecordLacksPrimaryKey) {
  470. db.last_error = std.fmt.bufPrint(&db.last_error_buf, "update refused: the record carries no '{s}' (update's record must carry the primary key)", .{db.pk orelse ""}) catch "update refused: the record carries no primary key";
  471. return api.makeNull();
  472. }
  473. if (e != error.DuplicateKey) {
  474. std.debug.print("mpackdb plugin: update failed: {s}\n", .{@errorName(e)});
  475. }
  476. return api.makeNull();
  477. };
  478. return api.makeNumber(@floatFromInt(n));
  479. }
  480. if (std.mem.eql(u8, method, "count")) {
  481. var arena_state = std.heap.ArenaAllocator.init(gpa);
  482. defer arena_state.deinit();
  483. const arena = arena_state.allocator();
  484. db.refresh();
  485. var scanner = engine.Db.Scanner.init(db, arena, true);
  486. defer scanner.deinit();
  487. var n: usize = 0;
  488. while (true) {
  489. const maybe = scanner.next(arena) catch break;
  490. if (maybe == null) break;
  491. n += 1;
  492. }
  493. return api.makeNumber(@floatFromInt(n));
  494. }
  495. if (std.mem.eql(u8, method, "compact")) {
  496. db.compact() catch |e| {
  497. std.debug.print("mpackdb plugin: compact failed: {s}\n", .{@errorName(e)});
  498. };
  499. return api.makeNull();
  500. }
  501. if (std.mem.eql(u8, method, "persist")) {
  502. db.persistNow() catch |e| {
  503. std.debug.print("mpackdb plugin: persist failed: {s}\n", .{@errorName(e)});
  504. };
  505. return api.makeNull();
  506. }
  507. if (std.mem.eql(u8, method, "last_error")) {
  508. return api.makeString(db.last_error);
  509. }
  510. // The primary key field name with its */@/! prefixes stripped — the .hl
  511. // wrapper needs the bare name to route find(field, …) to find_pk.
  512. if (std.mem.eql(u8, method, "pk_name")) {
  513. const p = db.pk orelse return api.makeNull();
  514. return api.makeString(p);
  515. }
  516. return api.makeNull();
  517. }
  518. /// Decode records at `locs` into the iterator arena and wrap them.
  519. fn locsToIterator(db: *engine.Db, arena_state: std.heap.ArenaAllocator, locs: []const engine.Loc) HlValue {
  520. var st = arena_state;
  521. const arena = st.allocator();
  522. var records: std.ArrayList(msgpack.Value) = .empty;
  523. for (locs) |loc| {
  524. const rec = db.readAt(arena, loc) catch continue;
  525. records.append(arena, rec) catch break;
  526. }
  527. const slice = records.toOwnedSlice(arena) catch &.{};
  528. return makeRecordIterator(@constCast(slice), st);
  529. }
  530. test {
  531. _ = msgpack;
  532. _ = engine;
  533. }

Branches

Latest commits

  • 14ba08c7antcolony#40: mission references point to the moved missionsmre
  • 99c73346antcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 76edaa62calendar: Hybriel master ff51cf46 (re-vendor round, static workaround removed)mre
  • 90a3fc2cdeploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • 6722b72ddeploy.sh: never send .git or .gitignore to Byrodinmre
  • be099807State of 2026-09-27, before the move to gitoriamre