╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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