-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathIndexReader.zig
86 lines (69 loc) · 2.45 KB
/
IndexReader.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
const std = @import("std");
const Self = @This();
const Deadline = @import("utils/Deadline.zig");
const SearchResults = @import("common.zig").SearchResults;
const SharedPtr = @import("utils/shared_ptr.zig").SharedPtr;
const DocInfo = @import("common.zig").DocInfo;
const SegmentList = @import("segment_list.zig").SegmentList;
const FileSegment = @import("FileSegment.zig");
const FileSegmentList = SegmentList(FileSegment);
const MemorySegment = @import("MemorySegment.zig");
const MemorySegmentList = SegmentList(MemorySegment);
const segment_lists = [_][]const u8{
"file_segments",
"memory_segments",
};
file_segments: SharedPtr(FileSegmentList),
memory_segments: SharedPtr(MemorySegmentList),
pub fn search(self: *Self, hashes: []const u32, allocator: std.mem.Allocator, deadline: Deadline) !SearchResults {
const sorted_hashes = try allocator.dupe(u32, hashes);
defer allocator.free(sorted_hashes);
std.sort.pdq(u32, sorted_hashes, {}, std.sort.asc(u32));
var results = SearchResults.init(allocator);
errdefer results.deinit();
inline for (segment_lists) |n| {
const segments = @field(self, n);
try segments.value.search(sorted_hashes, &results, deadline);
}
results.sort();
return results;
}
pub fn getDocInfo(self: *Self, doc_id: u32) !?DocInfo {
// TODO optimize, read from the end
var result: ?DocInfo = null;
inline for (segment_lists) |n| {
const segments = @field(self, n);
if (segments.value.getDocInfo(doc_id)) |res| {
result = res;
}
}
if (result) |res| {
if (!res.deleted) {
return res;
}
}
return null;
}
pub fn getVersion(self: *Self) u64 {
if (self.memory_segments.value.getLast()) |node| {
return node.value.info.version;
}
if (self.file_segments.value.getLast()) |node| {
return node.value.info.version;
}
return 0;
}
pub fn getAttributes(self: *Self, allocator: std.mem.Allocator) !std.StringHashMapUnmanaged(u64) {
var attributes: std.StringHashMapUnmanaged(u64) = .{};
errdefer attributes.deinit(allocator);
inline for (segment_lists) |n| {
const segments = @field(self, n);
for (segments.value.nodes.items) |node| {
var iter = node.value.attributes.iterator();
while (iter.next()) |entry| {
try attributes.put(allocator, entry.key_ptr.*, entry.value_ptr.*);
}
}
}
return attributes;
}