Skip to content

Commit 94a08e6

Browse files
committed
Add support for attributes in MemorySegment
1 parent e033ba6 commit 94a08e6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/MemorySegment.zig

+26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub const Options = struct {};
1919

2020
allocator: std.mem.Allocator,
2121
info: SegmentInfo = .{},
22+
attributes: std.StringHashMapUnmanaged([]const u8) = .{},
2223
docs: std.AutoHashMap(u32, bool),
2324
items: std.ArrayList(Item),
2425
frozen: bool = false,
@@ -34,6 +35,16 @@ pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
3435

3536
pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
3637
_ = delete_file;
38+
39+
{
40+
var iter = self.attributes.iterator();
41+
while (iter.next()) |entry| {
42+
self.allocator.free(entry.key_ptr.*);
43+
self.allocator.free(entry.value_ptr.*);
44+
}
45+
}
46+
self.attributes.deinit(self.allocator);
47+
3748
self.docs.deinit();
3849
self.items.deinit();
3950
}
@@ -54,6 +65,7 @@ pub fn getSize(self: Self) usize {
5465
}
5566

5667
pub fn build(self: *Self, changes: []const Change) !void {
68+
var num_attributes: u32 = 0;
5769
var num_docs: u32 = 0;
5870
var num_items: usize = 0;
5971
for (changes) |change| {
@@ -65,9 +77,13 @@ pub fn build(self: *Self, changes: []const Change) !void {
6577
.delete => {
6678
num_docs += 1;
6779
},
80+
.set_attribute => {
81+
num_attributes += 1;
82+
},
6883
}
6984
}
7085

86+
try self.attributes.ensureTotalCapacity(self.allocator, num_attributes);
7187
try self.docs.ensureTotalCapacity(num_docs);
7288
try self.items.ensureTotalCapacity(num_items);
7389

@@ -92,6 +108,16 @@ pub fn build(self: *Self, changes: []const Change) !void {
92108
result.value_ptr.* = false;
93109
}
94110
},
111+
.set_attribute => |op| {
112+
const result = self.attributes.getOrPutAssumeCapacity(op.name);
113+
if (!result.found_existing) {
114+
errdefer self.attributes.removeByPtr(result.key_ptr);
115+
result.key_ptr.* = try self.allocator.dupe(u8, op.name);
116+
errdefer self.allocator.free(result.key_ptr.*);
117+
result.value_ptr.* = try self.allocator.dupe(u8, op.value);
118+
errdefer self.allocator.free(result.value_ptr.*);
119+
}
120+
},
95121
}
96122
}
97123

src/change.zig

+10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ pub const Delete = struct {
1717
}
1818
};
1919

20+
pub const SetAttribute = struct {
21+
name: []const u8,
22+
value: []const u8,
23+
24+
pub fn msgpackFormat() msgpack.StructFormat {
25+
return .{ .as_map = .{ .key = .{ .field_name_prefix = 1 } } };
26+
}
27+
};
28+
2029
pub const Change = union(enum) {
2130
insert: Insert,
2231
delete: Delete,
32+
set_attribute: SetAttribute,
2333

2434
pub fn msgpackFormat() msgpack.UnionFormat {
2535
return .{ .as_map = .{ .key = .{ .field_name_prefix = 1 } } };

0 commit comments

Comments
 (0)