╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
122 // view paginates through. the key embeds the event id, so the set needs 123 // no value. 124 if (existing_cursor_maybe == null) { 125 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "repo-id-set")); 126 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 127 const order_key = evt.orderKeyDesc(event_to_write.created_ts, event_id); 128 try repo_id_set.put(&order_key); 129 } 130 131 // each user's repos are a set ordered by creation time (newest first), so 132 // the user page paginates them the same way the home repos list does. 133 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, event.user_id)); 134 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 135 const user_repo_order_key = evt.orderKeyDesc(event_to_write.created_ts, event_id); 136 try user_repos.put(&user_repo_order_key); 137 } else { 138 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 139 const existing_repo = try DB.HashMap(.read_only).init(existing_repo_cursor); 140 const existing_repo_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 141 142 const existing_path = try existing_repo_event.indexKey(arena.allocator()); 143 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 144 145 // drop it from the ordered set using its recorded creation timestamp 146 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "repo-id-set")); 147 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 148 const order_key = evt.orderKeyDesc(existing_repo_event.created_ts, event_id); 149 _ = try repo_id_set.remove(&order_key); 150 151 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 152 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 153 154 const user_key = hash.hashInt(hash_kind, existing_repo_event.user_id); 155 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(user_key); 156 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 157 _ = try user_repos.remove(&order_key); 158 159 _ = try conflicts.remove(&order_key); 160 _ = try repo_id_to_field_to_oid.remove(repo_key); 161 } 162 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 163 } 164 } 165 166 // a repo plus its event id (the id is the on-disk repo directory name, so the 167 // caller can locate the working repo under <server>/repos/<hex event id>). 168 pub const RepoWithId = struct { 169 repo: Self, 170 event_id: [evt.event_id_size]u8, 171 }; 172 173 // read a repo by its owner's name and repo name. the owner name is resolved to 174 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 175 // matching the /repo/username/reponame url. 176 pub fn readByOwnerAndName( 177 comptime DB: type, 178 comptime hash_kind: hash.HashKind, 179 haxy_moment: DB.HashMap(.read_only), 180 arena: *std.heap.ArenaAllocator, 181 owner_name: []const u8, 182 repo_name: []const u8, 183 ) !?RepoWithId { 184 // owner name -> user id 185 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 186 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 187 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 188 var user_id: [evt.event_id_size]u8 = undefined; 189 _ = try user_id_cursor.readBytes(&user_id); 190 191 // "user-id/repo-name" -> repo event id 192 const repo_path = try (Self{ .user_id = user_id[0..], .name = repo_name, .description = "" }).indexKey(arena.allocator()); 193 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 194 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 195 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 196 var repo_id: [evt.event_id_size]u8 = undefined; 197 _ = try repo_id_cursor.readBytes(&repo_id); 198 199 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 200 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 201 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 202 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 203 return .{ .repo = try evt.read(@This(), DB, hash_kind, arena, repo_map), .event_id = repo_id }; 204 } 205