╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
87 // the moment keys `evt.merge` reads and writes for this kind 88 pub const merge_policy: evt.MergePolicy = .target_wins; 89 pub const record_map_key = "event-id->repo"; 90 pub const all_id_set_key = "repo-id-set"; 91 pub const name_index_key = "name->repo-id"; 92 93 pub fn validateName(name: []const u8) !void { 94 if (name.len == 0) return error.NameEmpty; 95 if (name.len > name_max_len) return error.NameTooLong; 96 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 97 98 for (name) |c| { 99 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 100 return error.InvalidName; 101 } 102 } 103 104 pub fn consume( 105 comptime DB: type, 106 comptime hash_kind: hash.HashKind, 107 haxy_moment: DB.HashMap(.read_write), 108 event_id: *const [evt.event_id_size]u8, 109 record_maybe: ?Record, 110 arena: *std.heap.ArenaAllocator, 111 _: ?[]const u8, 112 ) !void { 113 const repo_key = hash.hashInt(hash_kind, event_id); 114 115 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 116 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 117 118 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 119 const name_to_repo_id = try DB.SortedMap(.read_write).init(name_to_repo_id_cursor); 120 121 var existing_record_maybe: ?Record = null; 122 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 123 if (existing_cursor_maybe) |existing_cursor| { 124 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 125 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 126 } 127 128 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 129 130 if (!record_to_write.removed) try validateName(record_to_write.event.name); 131 132 if (existing_record_maybe) |existing_record| { 133 // updates preserve the original creation metadata 134 record_to_write.created_order = existing_record.created_order; 135 136 // drop the old active index; active values are re-added below 137 if (!existing_record.removed) { 138 _ = try name_to_repo_id.remove(existing_record.event.name); 139 } 140 } 141 142 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 143 const repo = try DB.HashMap(.read_write).init(repo_cursor); 144 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 145 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, existing_record_maybe, record_to_write); 146 147 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 148 149 // the id set retains removed records so merges can carry removals 150 if (existing_cursor_maybe == null) { 151 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 152 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 153 try repo_id_set.put(&order_key); 154 } 155 156 if (!record_to_write.removed) { 157 try name_to_repo_id.put(record_to_write.event.name, .{ .bytes = event_id }); 158 } 159 } 160 161 // a repo plus its event id (the id is the on-disk repo directory name, so the 162 // caller can locate the working repo under <server>/repos/<hex event id>). 163 pub const RepoWithId = struct { 164 repo: Record, 165 event_id: [evt.event_id_size]u8, 166 }; 167 168 // a repo as one viewer sees it. without a role, the viewer may not see it. 169 pub const RepoWithRole = struct { 170 repo: Record, 171 event_id: [evt.event_id_size]u8, 172 role: ?Role, 173 }; 174 175 // read a repo by its name from its owner's user repo 176 pub fn readByName( 177 comptime DB: type, 178 comptime hash_kind: hash.HashKind, 179 user_moment: DB.HashMap(.read_only), 180 arena: *std.heap.ArenaAllocator, 181 name: []const u8, 182 ) !?RepoWithId { 183 const name_to_repo_id_cursor = try user_moment.getCursor(hash.hashInt(hash_kind, name_index_key)) orelse return null; 184 const name_to_repo_id = try DB.SortedMap(.read_only).init(name_to_repo_id_cursor); 185 const repo_id_cursor = try name_to_repo_id.getCursor(name) orelse return null; 186 var repo_id: [evt.event_id_size]u8 = undefined; 187 _ = try repo_id_cursor.readBytes(&repo_id); 188 const repo = (try readById(DB, hash_kind, user_moment, arena, &repo_id)) orelse return null; 189 return .{ .repo = repo, .event_id = repo_id }; 190 } 191 192 pub fn readById( 193 comptime DB: type, 194 comptime hash_kind: hash.HashKind, 195 haxy_moment: DB.HashMap(.read_only), 196 arena: *std.heap.ArenaAllocator, 197 id: []const u8, 198 ) !?Record { 199 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 200 const records = try DB.HashMap(.read_only).init(records_cursor); 201 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 202 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 203 } 204