Skip to content

Commit

Permalink
Rename doc_id to id
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Oct 13, 2024
1 parent f089709 commit d36e4fc
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 51 deletions.
14 changes: 7 additions & 7 deletions src/InMemoryIndex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn update(self: *Self, changes: []const Change) !void {
result.value_ptr.* = true;
var items = try node.data.items.addManyAsSlice(op.hashes.len);
for (op.hashes, 0..) |hash, j| {
items[j] = .{ .hash = hash, .doc_id = op.id };
items[j] = .{ .hash = hash, .id = op.id };
}
}
},
Expand Down Expand Up @@ -119,11 +119,11 @@ pub fn update(self: *Self, changes: []const Change) !void {
}
}

fn hasNewerVersion(self: *Self, doc_id: u32, version: u32) bool {
fn hasNewerVersion(self: *Self, id: u32, version: u32) bool {
var it = self.segments.last;
while (it) |node| : (it = node.prev) {
if (node.data.version > version) {
if (node.data.docs.contains(doc_id)) {
if (node.data.docs.contains(id)) {
return true;
}
} else {
Expand Down Expand Up @@ -234,7 +234,7 @@ fn prepareMerge(self: *Self) !?Merge {
}

for (segment.items.items) |item| {
if (!skip_docs.contains(item.doc_id)) {
if (!skip_docs.contains(item.id)) {
try node.data.items.append(item);
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ test "insert and search" {

const result = results.get(1);
try std.testing.expect(result != null);
try std.testing.expectEqual(1, result.?.doc_id);
try std.testing.expectEqual(1, result.?.id);
try std.testing.expectEqual(3, result.?.score);
try std.testing.expect(result.?.version != 0);
}
Expand Down Expand Up @@ -387,7 +387,7 @@ test "insert, partial update and search" {

const result = results.get(1);
try std.testing.expect(result != null);
try std.testing.expectEqual(1, result.?.doc_id);
try std.testing.expectEqual(1, result.?.id);
try std.testing.expectEqual(2, result.?.score);
try std.testing.expect(result.?.version != 0);
}
Expand Down Expand Up @@ -437,7 +437,7 @@ test "insert, full update (multiple times) and search" {

const result = results.get(i % 10);
try std.testing.expect(result != null);
try std.testing.expectEqual(1, result.?.doc_id);
try std.testing.expectEqual(1, result.?.id);
try std.testing.expectEqual(3, result.?.score);
try std.testing.expect(result.?.version != 0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/InMemorySegment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub fn search(self: *Self, hashes: []const u32, results: *SearchResults) !void {
assert(std.sort.isSorted(u32, hashes, {}, std.sort.asc(u32)));
var items = self.items.items;
for (hashes) |hash| {
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .doc_id = 0 }, items, {}, Item.cmpByHash);
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].doc_id, self.version);
try results.incr(items[i].id, self.version);
}
items = items[matches[1]..];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test "insert and search" {

const result = results.get(1);
try std.testing.expect(result != null);
try std.testing.expectEqual(1, result.?.doc_id);
try std.testing.expectEqual(1, result.?.id);
try std.testing.expectEqual(3, result.?.score);
try std.testing.expect(result.?.version != 0);
}
4 changes: 2 additions & 2 deletions src/Segment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub fn search(self: *Self, hashes: []const u32, results: *SearchResults) !void {
const block_data = self.getBlockData(block_no);
try filefmt.readBlock(block_data, &block_items);
}
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .doc_id = 0 }, block_items.items, {}, Item.cmpByHash);
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .id = 0 }, block_items.items, {}, Item.cmpByHash);
for (matches[0]..matches[1]) |i| {
try results.incr(block_items.items[i].doc_id, self.version);
try results.incr(block_items.items[i].id, self.version);
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const testing = std.testing;

pub const Item = packed struct(u64) {
doc_id: u32,
id: u32,
hash: u32,

pub fn cmp(_: void, a: Item, b: Item) bool {
Expand All @@ -19,11 +19,11 @@ pub const Item = packed struct(u64) {
test "Item binary" {
try testing.expectEqual(8, @sizeOf(Item));
try testing.expectEqual(64, @bitSizeOf(Item));
try testing.expectEqual(0, @bitOffsetOf(Item, "doc_id"));
try testing.expectEqual(0, @bitOffsetOf(Item, "id"));
try testing.expectEqual(32, @bitOffsetOf(Item, "hash"));

const item1 = Item{ .hash = 1, .doc_id = 2 };
const item2 = Item{ .hash = 2, .doc_id = 1 };
const item1 = Item{ .hash = 1, .id = 2 };
const item2 = Item{ .hash = 2, .id = 1 };

const x1: u64 = @bitCast(item1);
const x2: u64 = @bitCast(item2);
Expand All @@ -36,26 +36,26 @@ test "Item array sort" {
var items = try testing.allocator.alloc(Item, 3);
defer testing.allocator.free(items);

items[0] = Item{ .hash = 2, .doc_id = 200 };
items[1] = Item{ .hash = 2, .doc_id = 100 };
items[2] = Item{ .hash = 1, .doc_id = 300 };
items[0] = Item{ .hash = 2, .id = 200 };
items[1] = Item{ .hash = 2, .id = 100 };
items[2] = Item{ .hash = 1, .id = 300 };

std.sort.insertion(Item, items, {}, Item.cmp);

try testing.expectEqualSlices(Item, &[_]Item{
Item{ .hash = 1, .doc_id = 300 },
Item{ .hash = 2, .doc_id = 100 },
Item{ .hash = 2, .doc_id = 200 },
Item{ .hash = 1, .id = 300 },
Item{ .hash = 2, .id = 100 },
Item{ .hash = 2, .id = 200 },
}, items);
}

pub const SearchResult = struct {
doc_id: u32,
id: u32,
score: u32,
version: u32,

pub fn cmp(_: void, a: SearchResult, b: SearchResult) bool {
return a.score > b.score or (a.score == b.score and a.doc_id > b.doc_id);
return a.score > b.score or (a.score == b.score and a.id > b.id);
}
};

Expand All @@ -74,10 +74,10 @@ pub const SearchResults = struct {
self.results.deinit();
}

pub fn incr(self: *SearchResults, doc_id: u32, version: u32) !void {
const r = try self.results.getOrPut(doc_id);
pub fn incr(self: *SearchResults, id: u32, version: u32) !void {
const r = try self.results.getOrPut(id);
if (!r.found_existing or r.value_ptr.version < version) {
r.value_ptr.doc_id = doc_id;
r.value_ptr.id = id;
r.value_ptr.score = 1;
r.value_ptr.version = version;
} else if (r.value_ptr.version == version) {
Expand All @@ -89,8 +89,8 @@ pub const SearchResults = struct {
return self.results.count();
}

pub fn get(self: *SearchResults, doc_id: u32) ?SearchResult {
return self.results.get(doc_id);
pub fn get(self: *SearchResults, id: u32) ?SearchResult {
return self.results.get(id);
}

pub fn sort(self: *SearchResults) void {
Expand Down Expand Up @@ -119,8 +119,8 @@ test "sort search results" {
results.sort();

try testing.expectEqualSlices(SearchResult, &[_]SearchResult{
SearchResult{ .doc_id = 2, .score = 2, .version = 1 },
SearchResult{ .doc_id = 1, .score = 1, .version = 1 },
SearchResult{ .id = 2, .score = 2, .version = 1 },
SearchResult{ .id = 1, .score = 1, .version = 1 },
}, results.values());
}

Expand Down
38 changes: 19 additions & 19 deletions src/filefmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ pub fn readFirstItemFromBlock(data: []const u8) !Item {
var ptr: usize = 2;
const hash = readVarint32(data[ptr..]);
ptr += hash.size;
const doc_id = readVarint32(data[ptr..]);
ptr += doc_id.size;
return Item{ .hash = hash.value, .doc_id = doc_id.value };
const id = readVarint32(data[ptr..]);
ptr += id.size;
return Item{ .hash = hash.value, .id = id.value };
}

pub fn readBlock(data: []const u8, items: *std.ArrayList(Item)) !void {
Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn readBlock(data: []const u8, items: *std.ArrayList(Item)) !void {
lastHash += diffHash.value;
lastDocId = if (diffHash.value > 0) diffDocId.value else lastDocId + diffDocId.value;

try items.append(.{ .hash = lastHash, .doc_id = lastDocId });
try items.append(.{ .hash = lastHash, .id = lastDocId });

if (items.items.len >= numItems) {
break;
Expand All @@ -147,10 +147,10 @@ pub fn writeBlock(data: []u8, items: []const Item) !usize {
var lastDocId: u32 = 0;

for (items) |item| {
assert(item.hash > lastHash or (item.hash == lastHash and item.doc_id >= lastDocId));
assert(item.hash > lastHash or (item.hash == lastHash and item.id >= lastDocId));

const diffHash = item.hash - lastHash;
const diffDocId = if (diffHash > 0) item.doc_id else item.doc_id - lastDocId;
const diffDocId = if (diffHash > 0) item.id else item.id - lastDocId;

if (ptr + varint32Size(diffHash) + varint32Size(diffDocId) > data.len) {
break;
Expand All @@ -161,7 +161,7 @@ pub fn writeBlock(data: []u8, items: []const Item) !usize {
numItems += 1;

lastHash = item.hash;
lastDocId = item.doc_id;
lastDocId = item.id;
}

std.mem.writeInt(u16, data[0..2], numItems, .little);
Expand All @@ -174,11 +174,11 @@ test "writeBlock/readBlock/readFirstItemFromBlock" {
var items = std.ArrayList(Item).init(std.testing.allocator);
defer items.deinit();

try items.append(.{ .hash = 1, .doc_id = 1 });
try items.append(.{ .hash = 2, .doc_id = 1 });
try items.append(.{ .hash = 3, .doc_id = 1 });
try items.append(.{ .hash = 3, .doc_id = 2 });
try items.append(.{ .hash = 4, .doc_id = 1 });
try items.append(.{ .hash = 1, .id = 1 });
try items.append(.{ .hash = 2, .id = 1 });
try items.append(.{ .hash = 3, .id = 1 });
try items.append(.{ .hash = 3, .id = 2 });
try items.append(.{ .hash = 4, .id = 1 });

const blockSize = 1024;
var blockData: [blockSize]u8 = undefined;
Expand All @@ -192,11 +192,11 @@ test "writeBlock/readBlock/readFirstItemFromBlock" {
try testing.expectEqualSlices(
Item,
&[_]Item{
.{ .hash = 1, .doc_id = 1 },
.{ .hash = 2, .doc_id = 1 },
.{ .hash = 3, .doc_id = 1 },
.{ .hash = 3, .doc_id = 2 },
.{ .hash = 4, .doc_id = 1 },
.{ .hash = 1, .id = 1 },
.{ .hash = 2, .id = 1 },
.{ .hash = 3, .id = 1 },
.{ .hash = 3, .id = 2 },
.{ .hash = 4, .id = 1 },
},
items.items,
);
Expand Down Expand Up @@ -369,8 +369,8 @@ test "writeFile/readFile" {

in_memory_segment.version = 1;
try in_memory_segment.docs.put(1, true);
try in_memory_segment.items.append(Item{ .hash = 1, .doc_id = 1 });
try in_memory_segment.items.append(Item{ .hash = 2, .doc_id = 1 });
try in_memory_segment.items.append(Item{ .hash = 1, .id = 1 });
try in_memory_segment.items.append(Item{ .hash = 2, .id = 1 });

in_memory_segment.ensureSorted();

Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn main() !void {
try index.search(&[_]u32{ 1, 2, 3 }, &results, .{});

for (results.values()) |result| {
std.debug.print("doc_id: {}, score: {}, version: {}\n", .{ result.doc_id, result.score, result.version });
std.debug.print("id: {}, score: {}, version: {}\n", .{ result.id, result.score, result.version });
}
}

Expand Down

0 comments on commit d36e4fc

Please sign in to comment.