╭ changed ────╮ │src/event.zig│ ╰─────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭───────────────────────────────────────────────────────────────────────────╮ │11 pub const Issue = @import("event/Issue.zig"); │ │12 │ │13 pub const event_id_size: usize = 32; │ │14 + │ │15 +// the branch haxy events are committed to before being consumed │ │16 +pub const events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/meta" };│ │17 │ │18 pub const EventKind = enum { │ │19 user, │ ╰───────────────────────────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │319 │ │320 if (last_object_id_maybe) |*last_object_id| { │ │321 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); │ │322 + } │ │323 +} │ │324 + │ │325 +pub fn currentMoment( │ │326 + comptime repo_opts: rp.RepoOpts(.xit), │ │327 + repo: *rp.Repo(.xit, repo_opts), │ │328 +) !rp.Repo(.xit, repo_opts).DB.HashMap(.read_only) { │ │329 + const DB = rp.Repo(.xit, repo_opts).DB; │ │330 + const history = try DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); │ │331 + const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; │ │332 + const moment = try DB.HashMap(.read_only).init(moment_cursor); │ │333 + const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound;│ │334 + var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; │ │335 + _ = try last_object_id_cursor.readBytes(&last_object_id); │ │336 + const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; │ │337 + const haxy = try DB.ArrayList(.read_only).init(haxy_cursor); │ │338 + const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; │ │339 + const haxy_moments = try DB.HashMap(.read_only).init(haxy_moments_cursor); │ │340 + const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound;│ │341 + return try DB.HashMap(.read_only).init(haxy_moment_cursor); │ │342 +} │ │343 + │ │344 +// commit each event as a JSON message on the events branch, then consume them │ │345 +// into the database │ │346 +pub fn commitAndConsume( │ │347 + comptime repo_opts: rp.RepoOpts(.xit), │ │348 + io: std.Io, │ │349 + allocator: std.mem.Allocator, │ │350 + repo: *rp.Repo(.xit, repo_opts), │ │351 + ref: rf.Ref, │ │352 + events: []const EventWithId, │ │353 +) !void { │ │354 + var json: std.Io.Writer.Allocating = .init(allocator); │ │355 + defer json.deinit(); │ │356 + │ │357 + for (events) |event| { │ │358 + json.clearRetainingCapacity(); │ │359 + try std.json.Stringify.value(event, .{}, &json.writer); │ │360 + _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, ref); │ │361 } │ │362 + │ │363 + try consume(repo_opts, io, allocator, repo, ref); │ │364 } │ │365 │ │366 // │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯