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