╭ Fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const hash = xit.hash; 5 6 user_id: []const u8, 7 repo_id: []const u8, 8 9 // what the db stores: the event's data plus the commit-derived fields 10 pub const Record = struct { 11 event: Self, 12 removed: bool = false, 13 created_order: u64 = 0, 14 }; 15 16 const Self = @This(); 17 18 pub const merge_policy: evt.MergePolicy = .target_wins; 19 pub const record_map_key = "event-id->fork"; 20 pub const id_set_key = "fork-id-set"; 21 pub const user_id_to_fork_id_set_key = "user-id->fork-id-set"; 22 pub const repo_id_to_fork_id_set_key = "repo-id->fork-id-set"; 23 24 fn validate(record: Record) !void { 25 if (record.event.user_id.len != evt.event_id_size) return error.InvalidUserId; 26 if (record.event.repo_id.len != evt.event_id_size) return error.InvalidRepoId; 27 } 28 29 pub fn consume( 30 comptime DB: type, 31 comptime hash_kind: hash.HashKind, 32 haxy_moment: DB.HashMap(.read_write), 33 event_id: *const [evt.event_id_size]u8, 34 record_maybe: ?Record, 35 arena: *std.heap.ArenaAllocator, 36 _: ?[]const u8, 37 ) !void { 38 const fork_key = hash.hashInt(hash_kind, event_id); 39 const records = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key))); 40 const user_forks = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, user_id_to_fork_id_set_key))); 41 const repo_forks = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, repo_id_to_fork_id_set_key))); 42 43 var existing_maybe: ?Record = null; 44 const existing_cursor_maybe = try records.getCursor(fork_key); 45 if (existing_cursor_maybe) |cursor| { 46 existing_maybe = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)); 47 } 48 49 var record = if (record_maybe) |value| 50 value 51 else blk: { 52 var existing = existing_maybe orelse return error.EventNotFound; 53 existing.removed = true; 54 break :blk existing; 55 }; 56 57 if (!record.removed) try validate(record); 58 59 if (existing_maybe) |existing| { 60 if (!std.mem.eql(u8, existing.event.user_id, record.event.user_id) or 61 !std.mem.eql(u8, existing.event.repo_id, record.event.repo_id)) return error.ForkChanged; 62 record.created_order = existing.created_order; 63 if (!existing.removed) { 64 const order_key = evt.orderKeyDesc(existing.created_order, event_id); 65 try removeFromParentSet(DB, hash_kind, user_forks, existing.event.user_id, &order_key); 66 try removeFromParentSet(DB, hash_kind, repo_forks, existing.event.repo_id, &order_key); 67 } 68 } 69 70 const fork_cursor = try records.putCursor(fork_key); 71 try evt.upsert(Record, DB, hash_kind, try DB.HashMap(.read_write).init(fork_cursor), record); 72 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .fork, record.created_order, record.removed); 73 74 const order_key = evt.orderKeyDesc(record.created_order, event_id); 75 if (existing_cursor_maybe == null) { 76 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key))); 77 try ids.put(&order_key); 78 } 79 80 if (!record.removed) { 81 const by_user = try DB.SortedSet(.read_write).init(try user_forks.putCursor(hash.hashInt(hash_kind, record.event.user_id))); 82 try by_user.put(&order_key); 83 const by_repo = try DB.SortedSet(.read_write).init(try repo_forks.putCursor(hash.hashInt(hash_kind, record.event.repo_id))); 84 try by_repo.put(&order_key); 85 } 86 } 87 88 fn removeFromParentSet( 89 comptime DB: type, 90 comptime hash_kind: hash.HashKind, 91 index: DB.HashMap(.read_write), 92 parent_id: []const u8, 93 order_key: *const [@sizeOf(u64) + evt.event_id_size]u8, 94 ) !void { 95 const cursor = try index.putCursor(hash.hashInt(hash_kind, parent_id)); 96 const ids = try DB.SortedSet(.read_write).init(cursor); 97 _ = try ids.remove(order_key); 98 } 99 100 pub fn readById( 101 comptime DB: type, 102 comptime hash_kind: hash.HashKind, 103 haxy_moment: DB.HashMap(.read_only), 104 arena: *std.heap.ArenaAllocator, 105 fork_id: []const u8, 106 ) !?Record { 107 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 108 const records = try DB.HashMap(.read_only).init(records_cursor); 109 const fork_cursor = try records.getCursor(hash.hashInt(hash_kind, fork_id)) orelse return null; 110 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(fork_cursor)); 111 } 112