╭ 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 read_access: Access = .private, 10 write_access: Access = .private, 11 read_user_ids: []const u8 = "", 12 write_user_ids: []const u8 = "", 13 14 // what the db stores: the event's data plus the commit-derived fields 15 pub const Record = struct { 16 event: Self, 17 removed: bool = false, 18 created_order: u64 = 0, 19 updated_order: u64 = 0, 20 21 // a repo's key in the name index. it's unique per owner, so the read side 22 // resolves the url's username to its user id first. 23 pub fn indexKey(self: Record, allocator: std.mem.Allocator) ![]const u8 { 24 return std.fmt.allocPrint(allocator, "{s}/{s}", .{ self.event.user_id, self.event.name }); 25 } 26 }; 27 28 const Self = @This(); 29 30 pub const Access = enum { 31 private, 32 public, 33 }; 34 35 pub const name_max_len = 32; 36 37 // the moment keys `evt.merge` reads and writes for this kind 38 pub const merge_policy: evt.MergePolicy = .target_wins; 39 pub const record_map_key = "event-id->repo"; 40 pub const id_set_key = "repo-id-set"; 41 pub const name_index_key = "name->repo-id"; 42 43 pub fn validateName(name: []const u8) !void { 44 if (name.len == 0) return error.NameEmpty; 45 if (name.len > name_max_len) return error.NameTooLong; 46 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 47 48 for (name) |c| { 49 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 50 return error.InvalidName; 51 } 52 } 53 54 fn validateUserIds(user_ids: []const u8) !void { 55 var lines = std.mem.tokenizeScalar(u8, user_ids, '\n'); 56 while (lines.next()) |id| _ = try evt.parseEventId(id); 57 } 58 59 pub fn consume( 60 comptime DB: type, 61 comptime hash_kind: hash.HashKind, 62 haxy_moment: DB.HashMap(.read_write), 63 event_id: *const [evt.event_id_size]u8, 64 record_maybe: ?Record, 65 arena: *std.heap.ArenaAllocator, 66 _: ?[]const u8, 67 ) !void { 68 const repo_key = hash.hashInt(hash_kind, event_id); 69 70 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 71 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 72 73 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "name->repo-id")); 74 const name_to_repo_id = try DB.HashMap(.read_write).init(name_to_repo_id_cursor); 75 76 var existing_record_maybe: ?Record = null; 77 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 78 if (existing_cursor_maybe) |existing_cursor| { 79 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 80 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 81 } 82 83 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 84 85 if (!record_to_write.removed) { 86 try validateName(record_to_write.event.name); 87 try validateUserIds(record_to_write.event.read_user_ids); 88 try validateUserIds(record_to_write.event.write_user_ids); 89 } 90 91 const user_id_to_repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "user-id->repo-id-set")); 92 const user_id_to_repo_id_set = try DB.HashMap(.read_write).init(user_id_to_repo_id_set_cursor); 93 94 if (existing_record_maybe) |existing_record| { 95 // updates preserve the original creation metadata 96 record_to_write.created_order = existing_record.created_order; 97 const order_key = evt.orderKeyDesc(existing_record.created_order, event_id); 98 99 // drop the old active indexes; active values are re-added below 100 if (!existing_record.removed) { 101 const existing_path = try existing_record.indexKey(arena.allocator()); 102 _ = try name_to_repo_id.remove(hash.hashInt(hash_kind, existing_path)); 103 104 const old_user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, existing_record.event.user_id)); 105 const old_user_repos = try DB.SortedSet(.read_write).init(old_user_repos_cursor); 106 _ = try old_user_repos.remove(&order_key); 107 } 108 } 109 110 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 111 const repo = try DB.HashMap(.read_write).init(repo_cursor); 112 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 113 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, existing_record_maybe, record_to_write); 114 115 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 116 117 // the id set retains removed records so merges can carry removals 118 if (existing_cursor_maybe == null) { 119 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 120 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 121 try repo_id_set.put(&order_key); 122 } 123 124 if (!record_to_write.removed) { 125 const repo_path = try record_to_write.indexKey(arena.allocator()); 126 try name_to_repo_id.put(hash.hashInt(hash_kind, repo_path), .{ .bytes = event_id }); 127 128 // each user's repos are ordered by creation time, newest first 129 const user_repos_cursor = try user_id_to_repo_id_set.putCursor(hash.hashInt(hash_kind, record_to_write.event.user_id)); 130 const user_repos = try DB.SortedSet(.read_write).init(user_repos_cursor); 131 try user_repos.put(&order_key); 132 } 133 } 134 135 // a repo plus its event id (the id is the on-disk repo directory name, so the 136 // caller can locate the working repo under <server>/repos/<hex event id>). 137 pub const RepoWithId = struct { 138 repo: Record, 139 event_id: [evt.event_id_size]u8, 140 }; 141 142 // read a repo by its owner's name and repo name. the owner name is resolved to 143 // a user id via name->user-id (the repo index is keyed by "user-id/repo-name"), 144 // matching the /repo/username/reponame url. 145 pub fn readByOwnerAndName( 146 comptime DB: type, 147 comptime hash_kind: hash.HashKind, 148 haxy_moment: DB.HashMap(.read_only), 149 arena: *std.heap.ArenaAllocator, 150 owner_name: []const u8, 151 repo_name: []const u8, 152 ) !?RepoWithId { 153 // owner name -> user id 154 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return null; 155 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 156 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, owner_name)) orelse return null; 157 var user_id: [evt.event_id_size]u8 = undefined; 158 _ = try user_id_cursor.readBytes(&user_id); 159 160 // "user-id/repo-name" -> repo event id 161 const repo_path = try (Record{ .event = .{ .user_id = user_id[0..], .name = repo_name, .description = "" } }).indexKey(arena.allocator()); 162 const name_to_repo_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->repo-id")) orelse return null; 163 const name_to_repo_id = try DB.HashMap(.read_only).init(name_to_repo_id_cursor); 164 const repo_id_cursor = try name_to_repo_id.getCursor(hash.hashInt(hash_kind, repo_path)) orelse return null; 165 var repo_id: [evt.event_id_size]u8 = undefined; 166 _ = try repo_id_cursor.readBytes(&repo_id); 167 168 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return null; 169 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 170 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &repo_id)) orelse return null; 171 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 172 return .{ .repo = try evt.read(Record, DB, hash_kind, arena, repo_map), .event_id = repo_id }; 173 } 174 175 pub fn readById( 176 comptime DB: type, 177 comptime hash_kind: hash.HashKind, 178 haxy_moment: DB.HashMap(.read_only), 179 arena: *std.heap.ArenaAllocator, 180 id: []const u8, 181 ) !?Record { 182 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 183 const records = try DB.HashMap(.read_only).init(records_cursor); 184 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 185 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 186 } 187