╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const hash = xit.hash; 5 6 user_id: []const u8, 7 name: []const u8, 8 description: []const u8, 9 enable_issue: bool, 10 11 const Self = @This(); 12 13 pub const name_max_len = 32; 14 15 pub fn consume( 16 comptime DB: type, 17 comptime hash_kind: hash.HashKind, 18 haxy_moment: DB.HashMap(.read_write), 19 event_id: *const [evt.event_id_size]u8, 20 event_maybe: ?@This(), 21 arena: *std.heap.ArenaAllocator, 22 created_ts: u64, 23 ) !void { 24 const repo_key = hash.hashInt(hash_kind, event_id); 25 26 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 27 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 28 29 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 30 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 31 32 if (event_maybe) |event| { 33 if (event.name.len > name_max_len) return error.NameTooLong; 34 35 // the index is keyed by "user-id/repo-name": a repo is unique per 36 // owner, and the read side resolves the url's username to its user id. 37 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ event.user_id, event.name }); 38 39 // if this event_id already maps to a repo under a different key (its 40 // name or owner changed), drop the stale name->id entry first 41 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 42 if (existing_cursor_maybe) |existing_cursor| { 43 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 44 const existing_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 45 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_event.user_id, existing_event.name }); 46 if (!std.mem.eql(u8, existing_path, repo_path)) { 47 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 48 } 49 } 50 51 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 52 const repo = try DB.HashMap(.read_write).init(repo_cursor); 53 try evt.upsert(@This(), DB, hash_kind, repo, event); 54 55 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 56 57 // first time we've seen this repo: record its creation timestamp and add it 58 // to the ordered map the repos view paginates through 59 if (existing_cursor_maybe == null) { 60 try repo.put(hash.hashInt(hash_kind, "created-ts"), .{ .uint = created_ts }); 61 62 const timestamp_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "timestamp->repo-id")); 63 const timestamp_to_repo_id = try DB.SortedMap(.read_write).init(timestamp_to_repo_id_cursor); 64 const order_key = evt.orderKey(created_ts, event_id); 65 try timestamp_to_repo_id.put(&order_key, .{ .bytes = event_id }); 66 } 67 68 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 69 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 70 71 const user_repos_cursor = try user_id_to_repos.putCursor(hash.hashInt(hash_kind, event.user_id)); 72 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 73 try user_repos.put(repo_key, .{ .bytes = event_id }); 74 } else { 75 if (try event_id_to_repo.getCursor(repo_key)) |existing_repo_cursor| { 76 const existing_repo = try DB.HashMap(.read_only).init(existing_repo_cursor); 77 const existing_repo_event = try evt.read(@This(), DB, hash_kind, arena, existing_repo); 78 79 const existing_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ existing_repo_event.user_id, existing_repo_event.name }); 80 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 81 82 // drop it from the ordered map using its recorded creation timestamp 83 if (try existing_repo.getCursor(hash.hashInt(hash_kind, "created-ts"))) |created_ts_cursor| { 84 const stored_ts = try created_ts_cursor.readUint(); 85 const timestamp_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "timestamp->repo-id")); 86 const timestamp_to_repo_id = try DB.SortedMap(.read_write).init(timestamp_to_repo_id_cursor); 87 const order_key = evt.orderKey(stored_ts, event_id); 88 _ = try timestamp_to_repo_id.remove(&order_key); 89 } 90 91 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repos")); 92 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 93 94 const user_key = hash.hashInt(hash_kind, existing_repo_event.user_id); 95 const user_repos_cursor = try user_id_to_repos.putCursor(user_key); 96 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 97 _ = try user_repos.remove(repo_key); 98 } 99 if (!try event_id_to_repo.remove(repo_key)) return error.EventNotFound; 100 } 101 } 102 103 // a repo plus its event id (the id is the on-disk repo directory name, so the 104 // caller can locate the working repo under <server>/repos/<hex event id>). 105 pub const RepoWithId = struct { 106 repo: Self, 107 event_id: [evt.event_id_size]u8, 108 }; 109 110 // read a repo by its owner's name and repo name. the owner name is resolved to 111 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 112 // matching the /repo/username/reponame url. 113 pub fn readByOwnerAndName( 114 comptime DB: type, 115 comptime hash_kind: hash.HashKind, 116 haxy_moment: DB.HashMap(.read_only), 117 arena: *std.heap.ArenaAllocator, 118 owner_name: []const u8, 119 repo_name: []const u8, 120 ) !?RepoWithId { 121 // owner name -> user id 122 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 123 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 124 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 125 var user_id: [evt.event_id_size]u8 = undefined; 126 _ = try user_id_cursor.readBytes(&user_id); 127 128 // "user-id/repo-name" -> repo event id 129 const repo_path = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ user_id[0..], repo_name }); 130 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 131 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 132 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 133 var repo_id: [evt.event_id_size]u8 = undefined; 134 _ = try repo_id_cursor.readBytes(&repo_id); 135 136 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 137 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 138 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 139 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 140 return .{ .repo = try evt.read(@This(), DB, hash_kind, arena, repo_map), .event_id = repo_id }; 141 } 142