-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMemorySegment.zig
165 lines (138 loc) · 4.57 KB
/
MemorySegment.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const std = @import("std");
const log = std.log;
const common = @import("common.zig");
const SearchResults = common.SearchResults;
const KeepOrDelete = common.KeepOrDelete;
const SegmentInfo = @import("segment.zig").SegmentInfo;
const Item = @import("segment.zig").Item;
const Change = @import("change.zig").Change;
const Deadline = @import("utils/Deadline.zig");
const SegmentMerger = @import("segment_merger.zig").SegmentMerger;
const Self = @This();
pub const Options = struct {};
allocator: std.mem.Allocator,
info: SegmentInfo = .{},
attributes: std.AutoHashMapUnmanaged(u64, u64) = .{},
docs: std.AutoHashMapUnmanaged(u32, bool) = .{},
items: std.ArrayListUnmanaged(Item) = .{},
frozen: bool = false,
pub fn init(allocator: std.mem.Allocator, opts: Options) Self {
_ = opts;
return .{
.allocator = allocator,
};
}
pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
_ = delete_file;
self.attributes.deinit(self.allocator);
self.docs.deinit(self.allocator);
self.items.deinit(self.allocator);
}
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !void {
var items = self.items.items;
for (sorted_hashes) |hash| {
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .id = 0 }, items, {}, Item.cmpByHash);
for (matches[0]..matches[1]) |i| {
try results.incr(items[i].id, self.info.version);
}
items = items[matches[1]..];
}
}
pub fn getSize(self: Self) usize {
return self.items.items.len;
}
pub fn build(self: *Self, changes: []const Change) !void {
var num_attributes: u32 = 0;
var num_docs: u32 = 0;
var num_items: usize = 0;
for (changes) |change| {
switch (change) {
.insert => |op| {
num_docs += 1;
num_items += op.hashes.len;
},
.delete => {
num_docs += 1;
},
.set_attribute => {
num_attributes += 1;
},
}
}
try self.attributes.ensureTotalCapacity(self.allocator, num_attributes);
try self.docs.ensureTotalCapacity(self.allocator, num_docs);
try self.items.ensureTotalCapacity(self.allocator, num_items);
var i = changes.len;
while (i > 0) {
i -= 1;
const change = changes[i];
switch (change) {
.insert => |op| {
const result = self.docs.getOrPutAssumeCapacity(op.id);
if (!result.found_existing) {
result.value_ptr.* = true;
var items = self.items.addManyAsSliceAssumeCapacity(op.hashes.len);
for (op.hashes, 0..) |hash, j| {
items[j] = .{ .hash = hash, .id = op.id };
}
}
},
.delete => |op| {
const result = self.docs.getOrPutAssumeCapacity(op.id);
if (!result.found_existing) {
result.value_ptr.* = false;
}
},
.set_attribute => |op| {
const result = self.attributes.getOrPutAssumeCapacity(op.key);
if (!result.found_existing) {
result.value_ptr.* = op.value;
}
},
}
}
std.sort.pdq(Item, self.items.items, {}, Item.cmp);
}
pub fn cleanup(self: *Self) void {
_ = self;
}
pub fn merge(self: *Self, merger: *SegmentMerger(Self)) !void {
std.debug.assert(self.allocator.ptr == merger.allocator.ptr);
self.info = merger.segment.info;
self.attributes.deinit(self.allocator);
self.attributes = merger.segment.attributes.move();
self.docs.deinit(self.allocator);
self.docs = merger.segment.docs.move();
self.items.clearRetainingCapacity();
try self.items.ensureTotalCapacity(self.allocator, merger.estimated_size);
while (true) {
const item = try merger.read() orelse break;
try self.items.append(self.allocator, item);
merger.advance();
}
}
pub fn reader(self: *const Self) Reader {
return .{
.segment = self,
.index = 0,
};
}
pub const Reader = struct {
segment: *const Self,
index: usize,
pub fn close(self: *Reader) void {
_ = self;
}
pub fn read(self: *Reader) !?Item {
if (self.index < self.segment.items.items.len) {
return self.segment.items.items[self.index];
} else {
return null;
}
}
pub fn advance(self: *Reader) void {
if (self.index < self.segment.items.items.len) {
self.index += 1;
}
}
};