╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
69 } 70 71 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 72 const repo = try DB.HashMap(.read_write).init(repo_cursor); 73 try evt.upsert(@This(), DB, hash_kind, repo, event_to_write); 74 75 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 76 77 // first time we've seen this repo: add it to the ordered set the repos 78 // view paginates through. the key (orderKey) embeds the event id, so the 79 // set needs no value. 80 if (existing_cursor_maybe == null) { 81 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "repo-id-set")); 82 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 83 const order_key = evt.orderKey(event_to_write.created_ts, event_id); 84 try repo_id_set.put(&order_key); 85 } 86 87 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 88 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 89 90 // each user's repos are a set ordered by creation time (oldest first), so 91 // the user page paginates them the same way the home repos list does. 92 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, event.user_id)); 93 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 94 const user_repo_order_key = evt.orderKey(event_to_write.created_ts, event_id); 95 try user_repos.put(&user_repo_order_key); 96 } else { 97 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 98 const existing_repo = try DB.HashMap(.read_only).init(existing_repo_cursor); 99 const existing_repo_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 100 101 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_repo_event.user_id, existing_repo_event.name }); 102 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 103 104 // drop it from the ordered set using its recorded creation timestamp 105 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "repo-id-set")); 106 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 107 const order_key = evt.orderKey(existing_repo_event.created_ts, event_id); 108 _ = try repo_id_set.remove(&order_key); 109 110 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 111 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 112 113 const user_key = hash.hashInt(hash_kind, existing_repo_event.user_id); 114 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(user_key); 115 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 116 _ = try user_repos.remove(&order_key); 117 } 118 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 119 } 120 } 121 122 // a repo plus its event id (the id is the on-disk repo directory name, so the 123 // caller can locate the working repo under <server>/repos/<hex event id>). 124 pub const RepoWithId = struct { 125 repo: Self, 126 event_id: [evt.event_id_size]u8, 127 }; 128 129 // read a repo by its owner's name and repo name. the owner name is resolved to 130 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 131 // matching the /repo/username/reponame url. 132 pub fn readByOwnerAndName( 133 comptime DB: type, 134 comptime hash_kind: hash.HashKind, 135 haxy_moment: DB.HashMap(.read_only), 136 arena: *std.heap.ArenaAllocator, 137 owner_name: []const u8, 138 repo_name: []const u8, 139 ) !?RepoWithId { 140 // owner name -> user id 141 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 142 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 143 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 144 var user_id: [evt.event_id_size]u8 = undefined; 145 _ = try user_id_cursor.readBytes(&user_id); 146 147 // "user-id/repo-name" -> repo event id 148 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ user_id[0..], repo_name }); 149 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 150 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 151 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 152 var repo_id: [evt.event_id_size]u8 = undefined; 153 _ = try repo_id_cursor.readBytes(&repo_id); 154 155 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 156 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 157 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 158 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 159 return .{ .repo = try evt.read(@This(), DB, hash_kind, arena, repo_map), .event_id = repo_id }; 160 } 161