╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Attachment.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
101 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .attach, existing_record_maybe, record_to_write); 102 103 if (existing_cursor_maybe == null) { 104 const attachment_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 105 const attachment_id_set = try DB.SortedSet(.read_write).init(attachment_id_set_cursor); 106 try attachment_id_set.put(&evt.orderKeyDesc(record_to_write.created_order, event_id)); 107 } 108 109 // removed attachments stay in the parent's set; the views skip them 110 const parent_attachments_cursor = try parent_id_to_attachment_id_set.putCursor(hash.hashInt(hash_kind, &record_to_write.event.parent_id)); 111 const parent_attachments = try DB.SortedSet(.read_write).init(parent_attachments_cursor); 112 try parent_attachments.put(&evt.orderKey(record_to_write.created_order, event_id)); 113 } 114 115 // attach `blob` to a parent event and return the event id. `repo` must be 116 // writable. 117 pub fn create( 118 host: evt.Host, 119 comptime repo_kind: rp.RepoKind, 120 comptime repo_opts: rp.RepoOpts(repo_kind), 121 io: std.Io, 122 allocator: std.mem.Allocator, 123 repo: *rp.Repo(repo_kind, repo_opts), 124 parent_id: *const [evt.event_id_size * 2]u8, 125 blob: evt.Blob, 126 author: evt.CommitAuthor, 127 ) ![evt.event_id_size * 2]u8 { 128 if (!nameValid(blob.name)) return error.InvalidFields; 129 if (blob.size == 0 or blob.size > max_size) return error.InvalidFields; 130 131 const parent_id_bytes = evt.parseEventId(parent_id) catch return error.InvalidFields; 132 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