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