╭ 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 deleted: bool = false, 14 created_ts: u64 = 0, // the commit timestamp of the event that first created this repo 15 16 // a repo's key in the name index. it's unique per owner, so the read side 17 // resolves the url's username to its user id first. 18 pub fn indexKey(self: Record, allocator: std.mem.Allocator) ![]const u8 { 19 return std.fmt.allocPrint(allocator, "{s}/{s}", .{ self.event.user_id, self.event.name }); 20 } 21 }; 22 23 const Self = @This(); 24 25 pub const name_max_len = 32; 26 27 // the moment keys `evt.merge` reads and writes for this kind 28 pub const record_map_key = "event-id->repo"; 29 pub const id_set_key = "repo-id-set"; 30 pub const name_index_key = "name->repo-id"; 31 pub const conflicts_key = "conflicted-repo-id->conflict"; 32 pub const id_to_field_to_oid_key = "repo-id->field->oid"; 33 34 pub fn validateName(name: []const u8) !void { 35 if (name.len == 0) return error.NameEmpty; 36 if (name.len > name_max_len) return error.NameTooLong; 37 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 38 39 for (name) |c| { 40 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 41 return error.InvalidName; 42 } 43 } 44 45 pub fn consume( 46 comptime DB: type, 47 comptime hash_kind: hash.HashKind, 48 haxy_moment: DB.HashMap(.read_write), 49 event_id: *const [evt.event_id_size]u8, 50 record_maybe: ?Record, 51 arena: *std.heap.ArenaAllocator, 52 // the commit this event came from, recorded against every field it 53 // changes. null from `merge`, which sets oids and conflicts itself. 54 event_oid: ?[]const u8, 55 ) !void { 56 const repo_key = hash.hashInt(hash_kind, event_id); 57 58 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 59 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 60 61 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 62 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 63 64 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 65 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 66 67 const repo_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 68 const repo_id_to_field_to_oid = try DB.HashMap(.read_write).init(repo_id_to_field_to_oid_cursor); 69 70 var existing_record_maybe: ?Record = null; 71 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 72 if (existing_cursor_maybe) |existing_cursor| { 73 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 74 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 75 } 76 77 var record_to_write = if (record_maybe) |record| 78 record 79 else blk: { 80 var record = existing_record_maybe orelse return error.EventNotFound; 81 record.deleted = true; 82 break :blk record; 83 }; 84 85 if (!record_to_write.deleted) try validateName(record_to_write.event.name); 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 if (existing_record_maybe) |existing_record| { 91 // updates preserve the original creation timestamp 92 record_to_write.created_ts = existing_record.created_ts; 93 const order_key = evt.orderKeyDesc(existing_record.created_ts, event_id); 94 95 // drop the old active indexes; active values are re-added below 96 if (!existing_record.deleted) { 97 const existing_path = try existing_record.indexKey(arena.allocator()); 98 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 99 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 _ = try old_user_repos.remove(&order_key); 103 } 104 105 // any event settles the conflict, since resolving in the ui may 106 // keep our own values and so change nothing to detect 107 if (event_oid != null or record_to_write.deleted) { 108 _ = try conflicts.remove(&order_key); 109 } 110 } 111 112 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_to_write.event, event_oid); 113 114 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 115 const repo = try DB.HashMap(.read_write).init(repo_cursor); 116 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 117 118 const order_key = evt.orderKeyDesc(record_to_write.created_ts, event_id); 119 120 // the id set retains tombstones so merges can carry deletions 121 if (existing_cursor_maybe == null) { 122 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 123 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 124 try repo_id_set.put(&order_key); 125 } 126 127 if (!record_to_write.deleted) { 128 const repo_path = try record_to_write.indexKey(arena.allocator()); 129 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 130 131 // each user's repos are ordered by creation time, newest first 132 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, record_to_write.event.user_id)); 133 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 134 try user_repos.put(&order_key); 135 } 136 } 137 138 // a repo plus its event id (the id is the on-disk repo directory name, so the 139 // caller can locate the working repo under <server>/repos/<hex event id>). 140 pub const RepoWithId = struct { 141 repo: Record, 142 event_id: [evt.event_id_size]u8, 143 }; 144 145 // read a repo by its owner's name and repo name. the owner name is resolved to 146 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 147 // matching the /repo/username/reponame url. 148 pub fn readByOwnerAndName( 149 comptime DB: type, 150 comptime hash_kind: hash.HashKind, 151 haxy_moment: DB.HashMap(.read_only), 152 arena: *std.heap.ArenaAllocator, 153 owner_name: []const u8, 154 repo_name: []const u8, 155 ) !?RepoWithId { 156 // owner name -> user id 157 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 158 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 159 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 160 var user_id: [evt.event_id_size]u8 = undefined; 161 _ = try user_id_cursor.readBytes(&user_id); 162 163 // "user-id/repo-name" -> repo event id 164 const repo_path = try (Record{ .event = .{ .user_id = user_id[0..], .name = repo_name, .description = "" } }).indexKey(arena.allocator()); 165 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 166 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 167 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 168 var repo_id: [evt.event_id_size]u8 = undefined; 169 _ = try repo_id_cursor.readBytes(&repo_id); 170 171 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 172 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 173 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 174 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 175 return .{ .repo = try evt.read(Record, DB, hash_kind, arena, repo_map), .event_id = repo_id }; 176 } 177