╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
87 pub fn consume( 88 comptime DB: type, 89 comptime hash_kind: hash.HashKind, 90 haxy_moment: DB.HashMap(.read_write), 91 event_id: *const [evt.event_id_size]u8, 92 record_maybe: ?Record, 93 arena: *std.heap.ArenaAllocator, 94 _: ?[]const u8, 95 ) !void { 96 const repo_key = hash.hashInt(hash_kind, event_id); 97 98 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 99 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 100 101 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 102 const name_to_repo_id = try DB.SortedMap(.read_write).init(name_to_repo_id_cursor); 103 104 var existing_record_maybe: ?Record = null; 105 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 106 if (existing_cursor_maybe) |existing_cursor| { 107 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 108 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 109 } 110 111 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 112 113 if (!record_to_write.removed) try validateName(record_to_write.event.name); 114 115 if (existing_record_maybe) |existing_record| { 116 // updates preserve the original creation metadata 117 record_to_write.created_order = existing_record.created_order; 118 119 // drop the old active index; active values are re-added below 120 if (!existing_record.removed) { 121 _ = try name_to_repo_id.remove(existing_record.event.name); 122 } 123 } 124 125 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 126 const repo = try DB.HashMap(.read_write).init(repo_cursor); 127 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 128 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, existing_record_maybe, record_to_write); 129 130 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 131 132 // the id set retains removed records so merges can carry removals 133 if (existing_cursor_maybe == null) { 134 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 135 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 136 try repo_id_set.put(&order_key); 137 } 138 139 if (!record_to_write.removed) { 140 try name_to_repo_id.put(record_to_write.event.name, .{ .bytes = event_id }); 141 } 142 } 143 144 // a repo plus its event id (the id is the on-disk repo directory name, so the 145 // caller can locate the working repo under <server>/repos/<hex event id>). 146 pub const RepoWithId = struct { 147 repo: Record, 148 event_id: [evt.event_id_size]u8, 149 }; 150 151 // a repo as one viewer sees it 152 pub const RepoWithRole = struct { 153 repo: Record, 154 event_id: [evt.event_id_size]u8, 155 role: Role, 156 }; 157 158 // read a repo by its name from its owner's user repo 159 pub fn readByName( 160 comptime DB: type, 161 comptime hash_kind: hash.HashKind, 162 user_moment: DB.HashMap(.read_only), 163 arena: *std.heap.ArenaAllocator, 164 name: []const u8, 165 ) !?RepoWithId { 166 const name_to_repo_id_cursor = try user_moment.getCursor(hash.hashInt(hash_kind, name_index_key)) orelse return null; 167 const name_to_repo_id = try DB.SortedMap(.read_only).init(name_to_repo_id_cursor); 168 const repo_id_cursor = try name_to_repo_id.getCursor(name) orelse return null; 169 var repo_id: [evt.event_id_size]u8 = undefined; 170 _ = try repo_id_cursor.readBytes(&repo_id); 171 const repo = (try readById(DB, hash_kind, user_moment, arena, &repo_id)) orelse return null; 172 return .{ .repo = repo, .event_id = repo_id }; 173 } 174 175 pub fn readById( 176 comptime DB: type, 177 comptime hash_kind: hash.HashKind, 178 haxy_moment: DB.HashMap(.read_only), 179 arena: *std.heap.ArenaAllocator, 180 id: []const u8, 181 ) !?Record { 182 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 183 const records = try DB.HashMap(.read_only).init(records_cursor); 184 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 185 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 186 } 187