╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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