╭ 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 10 // what the db stores: the event's data plus the commit-derived fields 11 pub const Record = struct { 12 event: Self, 13 created_ts: u64 = 0, // the commit timestamp of the event that first created this repo 14 15 // a repo's key in the name index. it's unique per owner, so the read side 16 // resolves the url's username to its user id first. 17 pub fn indexKey(self: Record, allocator: std.mem.Allocator) ![]const u8 { 18 return std.fmt.allocPrint(allocator, "{s}/{s}", .{ self.event.user_id, self.event.name }); 19 } 20 }; 21 22 const Self = @This(); 23 24 pub const name_max_len = 32; 25 26 // the moment keys `evt.merge` reads and writes for this kind 27 pub const record_map_key = "event-id->repo"; 28 pub const id_set_key = "repo-id-set"; 29 pub const name_index_key = "name->repo-id"; 30 pub const conflicts_key = "conflicted-repo-id->conflict"; 31 pub const id_to_field_to_oid_key = "repo-id->field->oid"; 32 33 pub fn validateName(name: []const u8) !void { 34 if (name.len == 0) return error.NameEmpty; 35 if (name.len > name_max_len) return error.NameTooLong; 36 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 37 38 for (name) |c| { 39 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 40 return error.InvalidName; 41 } 42 } 43 44 pub fn consume( 45 comptime DB: type, 46 comptime hash_kind: hash.HashKind, 47 haxy_moment: DB.HashMap(.read_write), 48 event_id: *const [evt.event_id_size]u8, 49 record_maybe: ?Record, 50 arena: *std.heap.ArenaAllocator, 51 // the commit this event came from, recorded against every field it 52 // changes. null from `merge`, which sets oids and conflicts itself. 53 event_oid: ?[]const u8, 54 ) !void { 55 const repo_key = hash.hashInt(hash_kind, event_id); 56 57 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 58 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 59 60 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 61 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 62 63 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 64 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 65 66 const repo_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 67 const repo_id_to_field_to_oid = try DB.HashMap(.read_write).init(repo_id_to_field_to_oid_cursor); 68 69 if (record_maybe) |record| { 70 try validateName(record.event.name); 71 72 var record_to_write = record; 73 74 const repo_path = try record.indexKey(arena.allocator()); 75 76 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 77 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 78 79 // if this event_id already maps to a repo under a different key (its 80 // name or owner changed), drop the stale index entries first 81 var existing_record_maybe: ?Record = null; 82 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 83 if (existing_cursor_maybe) |existing_cursor| { 84 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 85 const existing_record = try evt.read(Record, DB, hash_kind, arena, existing_repo); 86 existing_record_maybe = existing_record; 87 // updates preserve the original creation timestamp 88 record_to_write.created_ts = existing_record.created_ts; 89 90 // any event settles the conflict, since resolving in the ui may 91 // keep our own values and so change nothing to detect 92 if (event_oid != null) { 93 _ = try conflicts.remove(&evt.orderKeyDesc(existing_record.created_ts, event_id)); 94 } 95 const existing_path = try existing_record.indexKey(arena.allocator()); 96 if (!std.mem.eql(u8, existing_path, repo_path)) { 97 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 98 } 99 if (!std.mem.eql(u8, existing_record.event.user_id, record.event.user_id)) { 100 const old_user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, existing_record.event.user_id)); 101 const old_user_repos = try DB.SortedSet(.read_write).init(old_user_repos_cursor); 102 const order_key = evt.orderKeyDesc(existing_record.created_ts, event_id); 103 _ = try old_user_repos.remove(&order_key); 104 } 105 } 106 107 try evt.writeOid(Self, DB, hash_kind, repo_id_to_field_to_oid, repo_key, if (existing_record_maybe) |existing| existing.event else null, record.event, event_oid); 108 109 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 110 const repo = try DB.HashMap(.read_write).init(repo_cursor); 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