╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2902 2903 return true; 2904 } 2905 2906 // 2907 // CommitIterator 2908 // 2909 // differs from xit's ObjectIterator in a few ways: 2910 // 2911 // 1. iterates parents first, rather than children first 2912 // 2. `next` only returns the object id, not the full Object 2913 // 3. stores its temporary state in a file on disk to avoid OOMs 2914 // 2915 2916 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2917 return struct { 2918 const DB = EventDB(repo_opts.hash); 2919 // a plain file is used for the scratch db because each write outside 2920 // a transaction is synced anyway, so buffering would gain nothing 2921 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2922 const db_name = "haxy-repo-events.db"; 2923 2924 repo_dir: std.Io.Dir, 2925 db_file: std.Io.File, 2926 db: *TempDB, 2927 // map of each object id to its children 2928 parent_to_children: TempDB.HashMap(.read_write), 2929 // tracking pending parents is necessary because a child can only 2930 // be returned after all of its unconsumed parents have been returned 2931 child_to_pending_parents: TempDB.HashMap(.read_write), 2932 // queue of object ids that are ready to be returned from `next` 2933 ready_queue: TempDB.ArrayList(.read_write), 2934 ready_queue_index: u64, 2935 // the object id at the tip of this branch 2936 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2937 // the object id furthest back in the history that hasn't been consumed 2938 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2939 2940 pub fn init( 2941 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2942 io: std.Io, 2943 allocator: std.mem.Allocator, 2944 consumed_object_ids: DB.HashMap(.read_only), 2945 ref: rf.Ref, 2946 ) !CommitIterator(repo_kind, repo_opts) { 2947 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2948 errdefer { 2949 db_file.close(io); 2950 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2951 } 2952 2953 // cursors point at the db, so it needs a stable address 2954 const db_ptr = try allocator.create(TempDB); 2955 errdefer allocator.destroy(db_ptr); 2956 // the db is scratch state that gets deleted after iteration, so 2957 // there is nothing worth fsyncing 2958 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2959 2960 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2961 2962 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2963 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2964 2965 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2966 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2967 2968 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2969 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2970 2971 var self = CommitIterator(repo_kind, repo_opts){ 2972 .repo_dir = state.core.repo_dir, 2973 .db_file = db_file, 2974 .db = db_ptr, 2975 .parent_to_children = parent_to_children, 2976 .child_to_pending_parents = child_to_pending_parents, 2977 .ready_queue = ready_queue, 2978 .ready_queue_index = 0, 2979 .newest_object_id = null, 2980 .oldest_object_id = null, 2981 }; 2982 errdefer self.deinit(io, allocator); 2983 2984 try self.collect(state, io, allocator, consumed_object_ids, ref); 2985 2986 return self; 2987 } 2988 2989 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2990 self.db_file.close(io); 2991 self.repo_dir.deleteFile(io, db_name) catch {}; 2992 allocator.destroy(self.db); 2993 } 2994 2995 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2996 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2997 2998 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2999 self.ready_queue_index += 1; 3000 3001 try self.enqueueChildren(&oid); 3002 return oid; 3003 } 3004 3005 fn collect( 3006 self: *CommitIterator(repo_kind, repo_opts), 3007 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 3008 io: std.Io, 3009 allocator: std.mem.Allocator, 3010 consumed_object_ids: DB.HashMap(.read_only), 3011 ref: rf.Ref, 3012 ) !void { 3013 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 3014 3015 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 3016 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 3017 var walk_queue_index: u64 = 0; 3018 3019 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 3020 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 3021 3022 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 3023 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3024 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 3025 try walk_queue.append(.{ .bytes = &head_oid }); 3026 3027 while (walk_queue_index < try walk_queue.count()) { 3028 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 3029 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 3030 3031 walk_queue_index += 1; 3032 3033 // if we've seen this object id, skip it 3034 if (null != try seen_object_ids.getCursor(oid_int)) { 3035 continue; 3036 } 3037 3038 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 3039 3040 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 3041 defer commit_object.deinit(); 3042 3043 // if this object id has already been consumed, skip it 3044 if (null != try consumed_object_ids.getCursor(oid_int)) { 3045 continue; 3046 } 3047 3048 if (self.newest_object_id == null) self.newest_object_id = oid; 3049 self.oldest_object_id = oid; 3050 3051 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 3052 var pending_parent_count: u64 = 0; 3053 for (parent_oids) |*parent_oid| { 3054 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3055 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 3056 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 3057 3058 // if this object id has already been consumed, skip it 3059 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 3060 continue; 3061 } 3062 3063 // if this object id hasn't already been seen, add it to the walk queue 3064 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 3065 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 3066 } 3067 3068 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 3069 const children = try TempDB.HashSet(.read_write).init(children_cursor); 3070 try children.put(oid_int, .{ .bytes = &oid }); 3071 pending_parent_count += 1; 3072 } 3073 3074 if (pending_parent_count == 0) { 3075 try self.ready_queue.append(.{ .bytes = &oid }); 3076 } else { 3077 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 3078 } 3079 } 3080 } 3081 3082 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 3083 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 3084 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 3085 const children = try TempDB.HashSet(.read_only).init(children_cursor); 3086 var children_iter = try children.iterator(); 3087 3088 while (try children_iter.next()) |*child_cursor| { 3089 const kv_pair = try child_cursor.readKeyValuePair(); 3090 const child_oid_int = kv_pair.hash; 3091 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 3092 const pending_parent_count = try pending_parent_cursor.readUint(); 3093 3094 if (pending_parent_count <= 1) { 3095 _ = try self.child_to_pending_parents.remove(child_oid_int); 3096 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 3097 try self.ready_queue.append(.{ .bytes = &child_oid }); 3098 } else { 3099 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 3100 } 3101 } 3102 } 3103 3104 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 3105 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 3106 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3107 _ = try oid_cursor.readBytes(&oid); 3108 return oid; 3109 } 3110 }; 3111 } 3112