╭ 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 stage: Stage = .draft, 9 10 // what the db stores: the event's data plus the commit-derived fields 11 pub const Record = struct { 12 event: Self, 13 removed: bool = false, 14 created_order: u64 = 0, 15 updated_order: u64 = 0, 16 }; 17 18 const Self = @This(); 19 20 pub const Stage = enum { 21 draft, 22 publish, 23 }; 24 25 pub const merge_policy: evt.MergePolicy = .target_wins; 26 pub const record_map_key = "event-id->fork"; 27 pub const all_id_set_key = "fork-id-set"; 28 pub const user_id_to_fork_id_set_key = "user-id->fork-id-set"; 29 pub const repo_user_to_draft_id_set_key = "repo+user->draft-id-set"; 30 pub const DraftKey = [evt.event_id_size * 2]u8; 31 32 pub fn draftKey(repo_id: []const u8, user_id: []const u8) DraftKey { 33 var key: DraftKey = undefined; 34 @memcpy(key[0..evt.event_id_size], repo_id); 35 @memcpy(key[evt.event_id_size..], user_id); 36 return key; 37 } 38 39 fn validate(record: Record) !void { 40 if (record.event.user_id.len != evt.event_id_size) return error.InvalidUserId; 41 if (record.event.repo_id.len != evt.event_id_size) return error.InvalidRepoId; 42 } 43 44 pub fn consume( 45 comptime DB: type, 46 comptime hash_kind: hash.HashKind, 47 haxy_moment: DB.HashMap(.read_write), 48 event_id: *const [evt.event_id_size]u8, 49 record_maybe: ?Record, 50 arena: *std.heap.ArenaAllocator, 51 _: ?[]const u8, 52 ) !void { 53 const fork_key = hash.hashInt(hash_kind, event_id); 54 const records = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key))); 55 const user_forks = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, user_id_to_fork_id_set_key))); 56 const drafts = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, repo_user_to_draft_id_set_key))); 57 58 var existing_maybe: ?Record = null; 59 const existing_cursor_maybe = try records.getCursor(fork_key); 60 if (existing_cursor_maybe) |cursor| { 61 existing_maybe = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)); 62 } 63 64 var record = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_maybe); 65 66 if (!record.removed) try validate(record); 67 68 if (existing_maybe) |existing| { 69 if (!std.mem.eql(u8, existing.event.user_id, record.event.user_id) or 70 !std.mem.eql(u8, existing.event.repo_id, record.event.repo_id)) return error.ForkChanged; 71 if (existing.event.stage == .publish and record.event.stage != .publish) return error.ForkAlreadyPublished; 72 record.created_order = existing.created_order; 73 if (!existing.removed) { 74 const order_key = evt.orderKeyDesc(existing.created_order, event_id); 75 try removeFromParentSet(DB, hash_kind, user_forks, existing.event.user_id, &order_key); 76 if (existing.event.stage == .draft) { 77 const key = draftKey(existing.event.repo_id, existing.event.user_id); 78 try removeFromParentSet(DB, hash_kind, drafts, &key, &order_key); 79 } 80 } 81 } 82 83 const fork_cursor = try records.putCursor(fork_key); 84 try evt.upsert(Record, DB, hash_kind, try DB.HashMap(.read_write).init(fork_cursor), record); 85 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .fork, existing_maybe, record); 86 87 const order_key = evt.orderKeyDesc(record.created_order, event_id); 88 if (existing_cursor_maybe == null) { 89 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key))); 90 try ids.put(&order_key); 91 } 92 93 if (!record.removed) { 94 const by_user = try DB.SortedSet(.read_write).init(try user_forks.putCursor(hash.hashInt(hash_kind, record.event.user_id))); 95 try by_user.put(&order_key); 96 if (record.event.stage == .draft) { 97 const key = draftKey(record.event.repo_id, record.event.user_id); 98 const by_repo_user = try DB.SortedSet(.read_write).init(try drafts.putCursor(hash.hashInt(hash_kind, &key))); 99 try by_repo_user.put(&order_key); 100 } 101 } 102 } 103 104 fn removeFromParentSet( 105 comptime DB: type, 106 comptime hash_kind: hash.HashKind, 107 index: DB.HashMap(.read_write), 108 parent_id: []const u8, 109 order_key: *const [@sizeOf(u64) + evt.event_id_size]u8, 110 ) !void { 111 const cursor = try index.putCursor(hash.hashInt(hash_kind, parent_id)); 112 const ids = try DB.SortedSet(.read_write).init(cursor); 113 _ = try ids.remove(order_key); 114 } 115 116 pub fn readById( 117 comptime DB: type, 118 comptime hash_kind: hash.HashKind, 119 haxy_moment: DB.HashMap(.read_only), 120 arena: *std.heap.ArenaAllocator, 121 fork_id: []const u8, 122 ) !?Record { 123 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 124 const records = try DB.HashMap(.read_only).init(records_cursor); 125 const fork_cursor = try records.getCursor(hash.hashInt(hash_kind, fork_id)) orelse return null; 126 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(fork_cursor)); 127 } 128