Skip to content

Commit 45c39c4

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

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/FileSegment.zig

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const KeepOrDelete = common.KeepOrDelete;
88

99
const Item = @import("segment.zig").Item;
1010
const SegmentInfo = @import("segment.zig").SegmentInfo;
11+
const SegmentBase = @import("segment.zig").SegmentBase;
1112

1213
const filefmt = @import("filefmt.zig");
1314

@@ -20,6 +21,7 @@ pub const Options = struct {
2021
allocator: std.mem.Allocator,
2122
dir: std.fs.Dir,
2223
info: SegmentInfo = .{},
24+
attributes: std.StringHashMapUnmanaged([]const u8) = .{},
2325
docs: std.AutoHashMap(u32, bool),
2426
index: std.ArrayList(u32),
2527
block_size: usize = 0,
@@ -30,6 +32,8 @@ delete_in_deinit: bool = false,
3032

3133
raw_data: ?[]align(std.mem.page_size) u8 = null,
3234

35+
usingnamespace SegmentBase(Self);
36+
3337
pub fn init(allocator: std.mem.Allocator, options: Options) Self {
3438
return Self{
3539
.allocator = allocator,
@@ -41,6 +45,7 @@ pub fn init(allocator: std.mem.Allocator, options: Options) Self {
4145
}
4246

4347
pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
48+
self.deinitAttributes(self.allocator);
4449
self.docs.deinit();
4550
self.index.deinit();
4651

src/MemorySegment.zig

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const SearchResults = common.SearchResults;
66
const KeepOrDelete = common.KeepOrDelete;
77
const SegmentInfo = @import("segment.zig").SegmentInfo;
88
const Item = @import("segment.zig").Item;
9+
const SegmentBase = @import("segment.zig").SegmentBase;
910

1011
const Change = @import("change.zig").Change;
1112

@@ -24,6 +25,8 @@ docs: std.AutoHashMap(u32, bool),
2425
items: std.ArrayList(Item),
2526
frozen: bool = false,
2627

28+
usingnamespace SegmentBase(Self);
29+
2730
pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
2831
_ = opts;
2932
return .{
@@ -36,15 +39,7 @@ pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
3639
pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
3740
_ = delete_file;
3841

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-
42+
self.deinitAttributes(self.allocator);
4843
self.docs.deinit();
4944
self.items.deinit();
5045
}

src/segment.zig

+13
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,16 @@ test "Item array sort" {
9797
Item{ .hash = 2, .id = 200 },
9898
}, items);
9999
}
100+
101+
pub fn SegmentBase(comptime T: type) type {
102+
return struct {
103+
pub fn deinitAttributes(self: *T, allocator: std.mem.Allocator) void {
104+
var iter = self.attributes.iterator();
105+
while (iter.next()) |entry| {
106+
allocator.free(entry.key_ptr.*);
107+
allocator.free(entry.value_ptr.*);
108+
}
109+
self.attributes.deinit(allocator);
110+
}
111+
};
112+
}

0 commit comments

Comments
 (0)