╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
951 return; 952 } 953 } 954 955 try map.put(key, .{ .uint = value }); 956 }, 957 .signed => { 958 if (try map.getCursor(key)) |value_cursor| { 959 if (try value_cursor.readInt() == value) { 960 return; 961 } 962 } 963 964 try map.put(key, .{ .int = value }); 965 }, 966 }, 967 .bool => { 968 const bytes = if (value) "true" else "false"; 969 if (try map.getCursor(key)) |value_cursor| { 970 var buffer: [5]u8 = undefined; 971 const existing = try value_cursor.readBytesObject(&buffer); 972 if (existing.format_tag) |format_tag| { 973 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 974 return; 975 } 976 } 977 } 978 979 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 980 }, 981 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 982 } 983 } 984 985 fn upsertBytes( 986 comptime DB: type, 987 comptime hash_kind: hash.HashKind, 988 map: DB.HashMap(.read_write), 989 key: hash.HashInt(hash_kind), 990 value: []const u8, 991 ) !void { 992 var existing_cursor_maybe = try map.getCursor(key); 993 if (existing_cursor_maybe) |*existing_cursor| { 994 if (try bytesEqual(DB, existing_cursor, value)) { 995 return; 996 } 997 } 998 999 var value_cursor = try map.putCursor(key); 1000 var write_buffer: [1024]u8 = undefined; 1001 var writer = try value_cursor.writer(&write_buffer); 1002 try writer.interface.writeAll(value); 1003 try writer.finish(); 1004 } 1005 1006 fn bytesEqual( 1007 comptime DB: type, 1008 cursor: *DB.Cursor(.read_only), 1009 value: []const u8, 1010 ) !bool { 1011 var read_buffer: [1024]u8 = undefined; 1012 var reader = try cursor.reader(&read_buffer); 1013 if (reader.size != value.len) { 1014 return false; 1015 } 1016 1017 var chunk_buffer: [1024]u8 = undefined; 1018 var offset: usize = 0; 1019 while (offset < value.len) { 1020 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1021 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1022 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1023 return false; 1024 } 1025 offset += chunk_len; 1026 } 1027 1028 return true; 1029 } 1030 1031 // 1032 // CommitIterator 1033 // 1034 // differs from xit's ObjectIterator in a few ways: 1035 // 1036 // 1. iterates parents first, rather than children first 1037 // 2. `next` only returns the object id, not the full Object 1038 // 3. stores its temporary state in a file on disk to avoid OOMs 1039 // 1040 1041 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1042 return struct { 1043 const DB = EventDB(repo_opts.hash); 1044 const db_name = "haxy-repo-events.db"; 1045 1046 repo_dir: std.Io.Dir, 1047 db_file: std.Io.File, 1048 db: *DB, 1049 // map of each object id to its children 1050 parent_to_children: DB.HashMap(.read_write), 1051 // tracking pending parents is necessary because a child can only 1052 // be returned after all of its unconsumed parents have been returned 1053 child_to_pending_parents: DB.HashMap(.read_write), 1054 // queue of object ids that are ready to be returned from `next` 1055 ready_queue: DB.ArrayList(.read_write), 1056 ready_queue_index: u64, 1057 // the object id at the tip of this branch 1058 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1059 // the object id furthest back in the history that hasn't been consumed 1060 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1061 1062 pub fn init( 1063 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1064 io: std.Io, 1065 allocator: std.mem.Allocator, 1066 consumed_object_ids: DB.HashMap(.read_only), 1067 ref: rf.Ref, 1068 ) !CommitIterator(repo_kind, repo_opts) { 1069 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1070 errdefer { 1071 db_file.close(io); 1072 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1073 } 1074 1075 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1076 errdefer allocator.destroy(buffer_ptr); 1077 1078 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1079 errdefer buffer_ptr.deinit(); 1080 1081 const db_ptr = try allocator.create(DB); 1082 errdefer allocator.destroy(db_ptr); 1083 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr }); 1084 1085 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1086 1087 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1088 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1089 1090 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1091 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1092 1093 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1094 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1095 1096 var self = CommitIterator(repo_kind, repo_opts){ 1097 .repo_dir = state.core.repo_dir, 1098 .db_file = db_file, 1099 .db = db_ptr, 1100 .parent_to_children = parent_to_children, 1101 .child_to_pending_parents = child_to_pending_parents, 1102 .ready_queue = ready_queue, 1103 .ready_queue_index = 0, 1104 .newest_object_id = null, 1105 .oldest_object_id = null, 1106 }; 1107 errdefer self.deinit(io, allocator); 1108 1109 try self.collect(state, io, allocator, consumed_object_ids, ref); 1110 1111 return self; 1112 } 1113 1114 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1115 self.db_file.close(io); 1116 self.db.core.memory.buffer.deinit(); 1117 allocator.destroy(self.db.core.memory.buffer); 1118 self.repo_dir.deleteFile(io, db_name) catch {}; 1119 allocator.destroy(self.db); 1120 } 1121 1122 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1123 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1124 1125 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1126 self.ready_queue_index += 1; 1127 1128 try self.enqueueChildren(&oid); 1129 return oid; 1130 } 1131 1132 fn collect( 1133 self: *CommitIterator(repo_kind, repo_opts), 1134 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1135 io: std.Io, 1136 allocator: std.mem.Allocator, 1137 consumed_object_ids: DB.HashMap(.read_only), 1138 ref: rf.Ref, 1139 ) !void { 1140 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1141 1142 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1143 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1144 var walk_queue_index: u64 = 0; 1145 1146 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1147 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1148 1149 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1150 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1151 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1152 try walk_queue.append(.{ .bytes = &head_oid }); 1153 1154 while (walk_queue_index < try walk_queue.count()) { 1155 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1156 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1157 1158 walk_queue_index += 1; 1159 1160 // if we've seen this object id, skip it 1161 if (null != try seen_object_ids.getCursor(oid_int)) { 1162 continue; 1163 } 1164 1165 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1166 1167 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1168 defer commit_object.deinit(); 1169 1170 // if this object id has already been consumed, skip it 1171 if (null != try consumed_object_ids.getCursor(oid_int)) { 1172 continue; 1173 } 1174 1175 if (self.newest_object_id == null) self.newest_object_id = oid; 1176 self.oldest_object_id = oid; 1177 1178 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1179 var pending_parent_count: u64 = 0; 1180 for (parent_oids) |*parent_oid| { 1181 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1182 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1183 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1184 1185 // if this object id has already been consumed, skip it 1186 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1187 continue; 1188 } 1189 1190 // if this object id hasn't already been seen, add it to the walk queue 1191 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1192 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1193 } 1194 1195 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1196 const children = try DB.HashSet(.read_write).init(children_cursor); 1197 try children.put(oid_int, .{ .bytes = &oid }); 1198 pending_parent_count += 1; 1199 } 1200 1201 if (pending_parent_count == 0) { 1202 try self.ready_queue.append(.{ .bytes = &oid }); 1203 } else { 1204 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1205 } 1206 } 1207 } 1208 1209 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1210 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1211 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1212 const children = try DB.HashSet(.read_only).init(children_cursor); 1213 var children_iter = try children.iterator(); 1214 1215 while (try children_iter.next()) |*child_cursor| { 1216 const kv_pair = try child_cursor.readKeyValuePair(); 1217 const child_oid_int = kv_pair.hash; 1218 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1219 const pending_parent_count = try pending_parent_cursor.readUint(); 1220 1221 if (pending_parent_count <= 1) { 1222 _ = try self.child_to_pending_parents.remove(child_oid_int); 1223 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1224 try self.ready_queue.append(.{ .bytes = &child_oid }); 1225 } else { 1226 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1227 } 1228 } 1229 } 1230 1231 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1232 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1233 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1234 _ = try oid_cursor.readBytes(&oid); 1235 return oid; 1236 } 1237 }; 1238 } 1239