╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Attachment.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
133 { 134 const DB = evt.EventDB(repo_opts.hash); 135 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 136 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 137 const haxy_moment = (if (event_db_maybe) |*event_db| 138 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 139 else if (repo_kind == .git) 140 return error.ParentNotFound 141 else 142 evt.currentMoment(repo_opts, repo)) catch return error.ParentNotFound; 143 if (!try parentExists(DB, repo_opts.hash, haxy_moment, &parent_id_bytes)) return error.ParentNotFound; 144 } 145 146 var id_bytes: [evt.event_id_size]u8 = undefined; 147 io.random(&id_bytes); 148 const event_id = std.fmt.bytesToHex(id_bytes, .lower); 149 try evt.consume(host, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 150 .id = event_id, 151 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 152 .author = author, 153 .tree_entries = &.{.{ .blob = blob }}, 154 .event = .{ .attach = .{ .parent_id = parent_id.* } }, 155 }}); 156 return event_id; 157 } 158 159 // read an attachment by event id, or null if the id isn't a known attachment. 160 // field byte slices are allocated in `arena`. 161 pub fn readById( 162 comptime DB: type, 163 comptime hash_kind: hash.HashKind, 164 haxy_moment: DB.HashMap(.read_only), 165 arena: *std.heap.ArenaAllocator, 166 attachment_id: []const u8, 167 ) !?Record { 168 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 169 const records = try DB.HashMap(.read_only).init(records_cursor); 170 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, attachment_id)) orelse return null; 171 const record_map = try DB.HashMap(.read_only).init(record_cursor); 172 return try evt.read(Record, DB, hash_kind, arena, record_map); 173 } 174