Skip to content

Commit 4efbe21

Browse files
committed
Keep attributes at u64->u64
1 parent 45c39c4 commit 4efbe21

5 files changed

+27
-31
lines changed

src/FileSegment.zig

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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;
1211

1312
const filefmt = @import("filefmt.zig");
1413

@@ -21,7 +20,7 @@ pub const Options = struct {
2120
allocator: std.mem.Allocator,
2221
dir: std.fs.Dir,
2322
info: SegmentInfo = .{},
24-
attributes: std.StringHashMapUnmanaged([]const u8) = .{},
23+
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
2524
docs: std.AutoHashMap(u32, bool),
2625
index: std.ArrayList(u32),
2726
block_size: usize = 0,
@@ -32,8 +31,6 @@ delete_in_deinit: bool = false,
3231

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

35-
usingnamespace SegmentBase(Self);
36-
3734
pub fn init(allocator: std.mem.Allocator, options: Options) Self {
3835
return Self{
3936
.allocator = allocator,
@@ -45,7 +42,7 @@ pub fn init(allocator: std.mem.Allocator, options: Options) Self {
4542
}
4643

4744
pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
48-
self.deinitAttributes(self.allocator);
45+
self.attributes.deinit(self.allocator);
4946
self.docs.deinit();
5047
self.index.deinit();
5148

src/MemorySegment.zig

+9-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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;
109

1110
const Change = @import("change.zig").Change;
1211

@@ -20,13 +19,11 @@ pub const Options = struct {};
2019

2120
allocator: std.mem.Allocator,
2221
info: SegmentInfo = .{},
23-
attributes: std.StringHashMapUnmanaged([]const u8) = .{},
22+
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
2423
docs: std.AutoHashMap(u32, bool),
2524
items: std.ArrayList(Item),
2625
frozen: bool = false,
2726

28-
usingnamespace SegmentBase(Self);
29-
3027
pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
3128
_ = opts;
3229
return .{
@@ -39,7 +36,7 @@ pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
3936
pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
4037
_ = delete_file;
4138

42-
self.deinitAttributes(self.allocator);
39+
self.attributes.deinit(self.allocator);
4340
self.docs.deinit();
4441
self.items.deinit();
4542
}
@@ -104,13 +101,9 @@ pub fn build(self: *Self, changes: []const Change) !void {
104101
}
105102
},
106103
.set_attribute => |op| {
107-
const result = self.attributes.getOrPutAssumeCapacity(op.name);
104+
const result = self.attributes.getOrPutAssumeCapacity(op.key);
108105
if (!result.found_existing) {
109-
errdefer self.attributes.removeByPtr(result.key_ptr);
110-
result.key_ptr.* = try self.allocator.dupe(u8, op.name);
111-
errdefer self.allocator.free(result.key_ptr.*);
112-
result.value_ptr.* = try self.allocator.dupe(u8, op.value);
113-
errdefer self.allocator.free(result.value_ptr.*);
106+
result.value_ptr.* = op.value;
114107
}
115108
},
116109
}
@@ -124,8 +117,13 @@ pub fn cleanup(self: *Self) void {
124117
}
125118

126119
pub fn merge(self: *Self, merger: *SegmentMerger(Self)) !void {
120+
std.debug.assert(self.allocator.ptr == merger.allocator.ptr);
121+
127122
self.info = merger.segment.info;
128123

124+
self.attributes.deinit(self.allocator);
125+
self.attributes = merger.segment.attributes.move();
126+
129127
self.docs.deinit();
130128
self.docs = merger.segment.docs.move();
131129

src/change.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub const Delete = struct {
1818
};
1919

2020
pub const SetAttribute = struct {
21-
name: []const u8,
22-
value: []const u8,
21+
key: u64,
22+
value: u64,
2323

2424
pub fn msgpackFormat() msgpack.StructFormat {
2525
return .{ .as_map = .{ .key = .{ .field_name_prefix = 1 } } };

src/segment.zig

-13
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,3 @@ 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-
}

src/segment_merger.zig

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const SharedPtr = @import("utils/shared_ptr.zig").SharedPtr;
77

88
pub const MergedSegmentInfo = struct {
99
info: SegmentInfo = .{},
10+
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
1011
docs: std.AutoHashMap(u32, bool),
1112
};
1213

@@ -76,16 +77,29 @@ pub fn SegmentMerger(comptime Segment: type) type {
7677
return error.NoSources;
7778
}
7879

80+
var total_attributes: u32 = 0;
7981
var total_docs: u32 = 0;
8082
for (sources, 0..) |source, i| {
8183
if (i == 0) {
8284
self.segment.info = source.reader.segment.info;
8385
} else {
8486
self.segment.info = SegmentInfo.merge(self.segment.info, source.reader.segment.info);
8587
}
88+
total_attributes += source.reader.segment.attributes.count();
8689
total_docs += source.reader.segment.docs.count();
8790
}
8891

92+
try self.segment.attributes.ensureTotalCapacity(self.allocator, total_attributes);
93+
for (sources) |*source| {
94+
const segment = source.reader.segment;
95+
var iter = segment.attributes.iterator();
96+
while (iter.next()) |entry| {
97+
const key = entry.key_ptr.*;
98+
const value = entry.value_ptr.*;
99+
self.segment.attributes.putAssumeCapacity(key, value);
100+
}
101+
}
102+
89103
try self.segment.docs.ensureTotalCapacity(total_docs);
90104
for (sources) |*source| {
91105
const segment = source.reader.segment;

0 commit comments

Comments
 (0)