╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
90 } 91 92 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 93 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 94 95 if (existing_record_maybe) |existing_record| { 96 // updates preserve the original creation metadata 97 record_to_write.created_order = existing_record.created_order; 98 const order_key = evt.orderKeyDesc(existing_record.created_order, event_id); 99 100 // drop the old active indexes; active values are re-added below 101 if (!existing_record.removed) { 102 const existing_path = try existing_record.indexKey(arena.allocator()); 103 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 104 105 const old_user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, existing_record.event.user_id)); 106 const old_user_repos = try DB.SortedSet(.read_write).init(old_user_repos_cursor); 107 _ = try old_user_repos.remove(&order_key); 108 } 109 } 110 111 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 112 const repo = try DB.HashMap(.read_write).init(repo_cursor); 113 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 114 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, existing_record_maybe, record_to_write); 115 116 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 117 118 // the id set retains removed records so merges can carry removals 119 if (existing_cursor_maybe == null) { 120 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 121 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 122 try repo_id_set.put(&order_key); 123 } 124 125 const active = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, active_id_set_key))); 126 if (record_to_write.removed) { 127 _ = try active.remove(&order_key); 128 } else { 129 try active.put(&order_key); 130 const repo_path = try record_to_write.indexKey(arena.allocator()); 131 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 132 133 // each user's repos are ordered by creation time, newest first 134 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, record_to_write.event.user_id)); 135 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 136 try user_repos.put(&order_key); 137 } 138 } 139 140 // a repo plus its event id (the id is the on-disk repo directory name, so the 141 // caller can locate the working repo under <server>/repos/<hex event id>). 142 pub const RepoWithId = struct { 143 repo: Record, 144 event_id: [evt.event_id_size]u8, 145 }; 146 147 // read a repo by its owner's name and repo name. the owner name is resolved to 148 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 149 // matching the /repo/username/reponame url. 150 pub fn readByOwnerAndName( 151 comptime DB: type, 152 comptime hash_kind: hash.HashKind, 153 haxy_moment: DB.HashMap(.read_only), 154 arena: *std.heap.ArenaAllocator, 155 owner_name: []const u8, 156 repo_name: []const u8, 157 ) !?RepoWithId { 158 // owner name -> user id 159 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 160 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 161 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 162 var user_id: [evt.event_id_size]u8 = undefined; 163 _ = try user_id_cursor.readBytes(&user_id); 164 165 // "user-id/repo-name" -> repo event id 166 const repo_path = try (Record{ .event = .{ .user_id = user_id[0..], .name = repo_name, .description = "" } }).indexKey(arena.allocator()); 167 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 168 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 169 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 170 var repo_id: [evt.event_id_size]u8 = undefined; 171 _ = try repo_id_cursor.readBytes(&repo_id); 172 173 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 174 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 175 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 176 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 177 return .{ .repo = try evt.read(Record, DB, hash_kind, arena, repo_map), .event_id = repo_id }; 178 } 179 180 pub fn readById( 181 comptime DB: type, 182 comptime hash_kind: hash.HashKind, 183 haxy_moment: DB.HashMap(.read_only), 184 arena: *std.heap.ArenaAllocator, 185 id: []const u8, 186 ) !?Record { 187 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 188 const records = try DB.HashMap(.read_only).init(records_cursor); 189 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 190 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 191 } 192