Skip to content

Commit 80ebd63

Browse files
authored
Upgrade to Zig 0.14 (#49)
* Upgrade to Zig 0.14 * Fix tests * Dont need python here at all
1 parent bb60cb7 commit 80ebd63

13 files changed

+75
-60
lines changed

.github/workflows/build-legacy-proxy.yml

-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v4
1919

20-
- uses: actions/setup-python@v5
21-
with:
22-
python-version: '3.13'
23-
cache: 'pip'
24-
2520
- name: Log in to the Container registry
2621
if: github.event_name != 'pull_request'
2722
uses: docker/login-action@v3

build.zig.zon

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
.{
2-
.name = "fpindex",
2+
.name = .fpindex,
33
.version = "2.0.0",
4-
.minimum_zig_version = "0.13.0",
4+
.minimum_zig_version = "0.14.0",
5+
.fingerprint = 0x38793b7ca08220b7,
56
.dependencies = .{
67
.httpz = .{
7-
.url = "git+https://github.com/karlseguin/http.zig?ref=zig-0.13#7d2ddae87af9b110783085c0ea6b03985faa4584",
8-
.hash = "12208c1f2c5f730c4c03aabeb0632ade7e21914af03e6510311b449458198d0835d6",
8+
.url = "git+https://github.com/karlseguin/http.zig?ref=master#56258131ef4505543fef5484451867c13c5ff322",
9+
.hash = "httpz-0.0.0-PNVzrJSuBgDFvO7mtd2qDzaq8_hXIu1BqFuL1jwAV8Ac",
910
},
1011
.zul = .{
11-
.url = "git+https://github.com/karlseguin/zul.git#df74463d223810a720f37db9dac3b160efbb9d4b",
12-
.hash = "122012992f8502acdcb69ae0f35682850b5c57abe892b1c717bc6dba65d05653fa76",
12+
.url = "git+https://github.com/karlseguin/zul.git?ref=master#d9142c73aedc5698beba58b3fbf2bcfe69864778",
13+
.hash = "zul-0.0.0-1oDot0BCBwA9cUo5OOrPs5NGmvoM7sk1ztfbdfL7mh4P",
1314
},
1415
.metrics = .{
15-
.url = "git+https://github.com/karlseguin/metrics.zig?ref=zig-0.13#fcf9e94fa54a20f4954e9821801c32e44d407a2f",
16-
.hash = "12201776681f1e5ec6df7df30786e90771c5de564c941a309c73c4299c7864ddb4c3",
16+
.url = "git+https://github.com/karlseguin/metrics.zig?ref=master#cf2797bcb3aea7e5cdaf4de39c5550c70796e7b1",
17+
.hash = "122061f30077ef518dd435d397598ab3c45daa3d2c25e6b45383fb94d0bd2c3af1af",
1718
},
1819
.msgpack = .{
19-
.url = "git+https://github.com/lalinsky/msgpack.zig?ref=v0.1.0#d141ef4e1f585fecbbcdac9a9f85e41b5759182c",
20-
.hash = "1220cb5fbd418638a830cb3c8a47d95d766d5ec1904631a14cde18cad89047165404",
20+
.url = "git+https://github.com/lalinsky/msgpack.zig?ref=v0.2.0#964ed489cf663fd3d7b79fe328300a71d3ab12d5",
21+
.hash = "msgpack-0.1.0-ZOu9PHimAQCfeM27BhUPC1NY6ZXjMlL2_ZEkjWRDldrT",
2122
},
2223
},
2324
.paths = .{

src/FileSegment.zig

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ num_items: usize = 0,
3636
delete_in_deinit: bool = false,
3737

3838
mmaped_file: ?std.fs.File = null,
39-
mmaped_data: ?[]align(std.mem.page_size) u8 = null,
39+
mmaped_data: ?[]align(std.heap.page_size_min) u8 = null,
4040

4141
pub fn init(allocator: std.mem.Allocator, options: Options) Self {
4242
return Self{
@@ -72,6 +72,10 @@ pub fn getBlockData(self: Self, block: usize) []const u8 {
7272
return self.blocks[block * self.block_size .. (block + 1) * self.block_size];
7373
}
7474

75+
fn compareHashes(a: u32, b: u32) std.math.Order {
76+
return std.math.order(a, b);
77+
}
78+
7579
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults, deadline: Deadline) !void {
7680
var prev_block_no: usize = std.math.maxInt(usize);
7781
var prev_block_range_start: usize = 0;
@@ -89,7 +93,7 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults, d
8993
// We want to find hash=10, lowerBound returns block=3 (EOF), but block=2 could still contain hash=6, so we go one back.
9094

9195
for (sorted_hashes, 1..) |hash, i| {
92-
var block_no = std.sort.lowerBound(u32, hash, self.index.items[prev_block_range_start..], {}, std.sort.asc(u32)) + prev_block_range_start;
96+
var block_no = std.sort.lowerBound(u32, self.index.items[prev_block_range_start..], hash, compareHashes) + prev_block_range_start;
9397
if (block_no > 0) {
9498
block_no -= 1;
9599
}
@@ -103,7 +107,7 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults, d
103107
const block_data = self.getBlockData(block_no);
104108
try filefmt.readBlock(block_data, &block_items, self.min_doc_id);
105109
}
106-
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .id = 0 }, block_items.items, {}, Item.cmpByHash);
110+
const matches = std.sort.equalRange(Item, block_items.items, Item{ .hash = hash, .id = 0 }, Item.orderByHash);
107111
for (matches[0]..matches[1]) |j| {
108112
try results.incr(block_items.items[j].id, self.info.version);
109113
}

src/MemorySegment.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
4949
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults, deadline: Deadline) !void {
5050
var items = self.items.items;
5151
for (sorted_hashes) |hash| {
52-
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .id = 0 }, items, {}, Item.cmpByHash);
52+
const matches = std.sort.equalRange(Item, items, Item{ .hash = hash, .id = 0 }, Item.orderByHash);
5353
for (matches[0]..matches[1]) |i| {
5454
try results.incr(items[i].id, self.info.version);
5555
}
@@ -131,7 +131,7 @@ pub fn build(self: *Self, changes: []const Change) !void {
131131
}
132132
}
133133

134-
std.sort.pdq(Item, self.items.items, {}, Item.cmp);
134+
std.sort.pdq(Item, self.items.items, {}, Item.lessThan);
135135
}
136136

137137
pub fn cleanup(self: *Self) void {

src/Oplog.zig

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ const Self = @This();
1212
pub const FileInfo = struct {
1313
id: u64 = 0,
1414

15-
fn cmp(_: void, a: FileInfo, b: FileInfo) bool {
15+
fn lessThan(_: void, a: FileInfo, b: FileInfo) bool {
1616
return a.id < b.id;
1717
}
18+
19+
fn order(a: FileInfo, b: FileInfo) std.math.Order {
20+
return std.math.order(a.id, b.id);
21+
}
1822
};
1923

2024
allocator: std.mem.Allocator,
@@ -70,7 +74,7 @@ pub fn open(self: *Self, first_commit_id: u64, receiver: anytype, ctx: anytype)
7074
}
7175
}
7276

73-
std.sort.pdq(FileInfo, self.files.items, {}, FileInfo.cmp);
77+
std.sort.pdq(FileInfo, self.files.items, {}, FileInfo.lessThan);
7478

7579
try self.truncateNoLock(first_commit_id);
7680

@@ -177,9 +181,9 @@ fn getFile(self: *Self, commit_id: u64) !std.fs.File {
177181
}
178182

179183
fn truncateNoLock(self: *Self, commit_id: u64) !void {
180-
assert(std.sort.isSorted(FileInfo, self.files.items, {}, FileInfo.cmp));
184+
assert(std.sort.isSorted(FileInfo, self.files.items, {}, FileInfo.lessThan));
181185

182-
var pos = std.sort.lowerBound(FileInfo, FileInfo{ .id = commit_id }, self.files.items, {}, FileInfo.cmp);
186+
var pos = std.sort.lowerBound(FileInfo, self.files.items, FileInfo{ .id = commit_id }, FileInfo.order);
183187
if (pos > 0) {
184188
pos -= 1;
185189
}

src/filefmt.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub fn writeSegmentFile(dir: std.fs.Dir, reader: anytype) !void {
327327
.has_attributes = true,
328328
.has_docs = true,
329329
};
330-
try packer.write(SegmentFileHeader, header);
330+
try packer.write(header);
331331

332332
try packer.writeMap(segment.attributes);
333333
try packer.writeMap(segment.docs);
@@ -359,7 +359,7 @@ pub fn writeSegmentFile(dir: std.fs.Dir, reader: anytype) !void {
359359
.num_blocks = num_blocks,
360360
.checksum = crc.final(),
361361
};
362-
try packer.write(SegmentFileFooter, footer);
362+
try packer.write(footer);
363363

364364
try buffered_writer.flush();
365365

src/index_tests.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ const Deadline = @import("utils/Deadline.zig");
1010
const Index = @import("Index.zig");
1111

1212
fn generateRandomHashes(buf: []u32, seed: u64) []u32 {
13-
var prng = std.rand.DefaultPrng.init(seed);
13+
var prng = std.Random.DefaultPrng.init(seed);
1414
const rand = prng.random();
1515
for (buf) |*h| {
16-
h.* = std.rand.int(rand, u32);
16+
h.* = rand.int(u32);
1717
}
1818
return buf;
1919
}

src/main.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const MultiIndex = @import("MultiIndex.zig");
88
const server = @import("server.zig");
99
const metrics = @import("metrics.zig");
1010

11-
pub const std_options = .{
11+
pub const std_options = std.Options{
1212
.log_level = .debug,
1313
.logFn = logHandler,
1414
};

src/segment.zig

+10-4
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ pub const Item = packed struct(u64) {
5454
id: u32,
5555
hash: u32,
5656

57-
pub fn cmp(_: void, a: Item, b: Item) bool {
57+
pub fn lessThan(_: void, a: Item, b: Item) bool {
5858
const xa: u64 = @bitCast(a);
5959
const xb: u64 = @bitCast(b);
6060
return xa < xb;
6161
}
6262

63-
pub fn cmpByHash(_: void, a: Item, b: Item) bool {
64-
return a.hash < b.hash;
63+
pub fn order(a: Item, b: Item) std.math.Order {
64+
const xa: u64 = @bitCast(a);
65+
const xb: u64 = @bitCast(b);
66+
return std.math.order(xa, xb);
67+
}
68+
69+
pub fn orderByHash(a: Item, b: Item) std.math.Order {
70+
return std.math.order(a.hash, b.hash);
6571
}
6672
};
6773

@@ -93,7 +99,7 @@ test "Item array sort" {
9399
items[1] = Item{ .hash = 2, .id = 100 };
94100
items[2] = Item{ .hash = 1, .id = 300 };
95101

96-
std.sort.insertion(Item, items, {}, Item.cmp);
102+
std.sort.insertion(Item, items, {}, Item.lessThan);
97103

98104
try std.testing.expectEqualSlices(Item, &[_]Item{
99105
Item{ .hash = 1, .id = 300 },

src/segment_merge_policy.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ test "TieredMergePolicy" {
223223

224224
var last_id: u64 = 1;
225225

226-
var prng = std.rand.DefaultPrng.init(blk: {
226+
var prng = std.Random.DefaultPrng.init(blk: {
227227
var seed: u64 = undefined;
228228
try std.posix.getrandom(std.mem.asBytes(&seed));
229229
//seed = 16044660244849477186;

src/segment_merger.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub fn SegmentMerger(comptime Segment: type) type {
155155

156156
for (self.sources.items, 0..) |*source, i| {
157157
if (try source.read()) |item| {
158-
if (min_item == null or Item.cmp({}, item, min_item.?)) {
158+
if (min_item == null or Item.order(item, min_item.?) == .lt) {
159159
min_item = item;
160160
min_item_index = i;
161161
}

src/server.zig

+29-24
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ const metrics = @import("metrics.zig");
1616

1717
const Context = struct {
1818
indexes: *MultiIndex,
19+
20+
pub fn notFound(ctx: *Context, req: *httpz.Request, res: *httpz.Response) !void {
21+
return handleNotFound(ctx, req, res);
22+
}
23+
24+
pub fn uncaughtError(ctx: *Context, req: *httpz.Request, res: *httpz.Response, err: anyerror) void {
25+
return handleError(ctx, req, res, err);
26+
}
1927
};
2028

21-
const Server = httpz.ServerApp(*Context);
29+
const Server = httpz.Server(*Context);
2230

2331
var global_server: ?*Server = null;
2432

@@ -30,16 +38,16 @@ fn shutdown(_: c_int) callconv(.C) void {
3038
}
3139
}
3240

33-
fn installSignalHandlers(server: *Server) !void {
41+
fn installSignalHandlers(server: *Server) void {
3442
global_server = server;
3543

36-
try std.posix.sigaction(std.posix.SIG.INT, &.{
44+
std.posix.sigaction(std.posix.SIG.INT, &.{
3745
.handler = .{ .handler = shutdown },
3846
.mask = std.posix.empty_sigset,
3947
.flags = 0,
4048
}, null);
4149

42-
try std.posix.sigaction(std.posix.SIG.TERM, &.{
50+
std.posix.sigaction(std.posix.SIG.TERM, &.{
4351
.handler = .{ .handler = shutdown },
4452
.mask = std.posix.empty_sigset,
4553
.flags = 0,
@@ -67,35 +75,32 @@ pub fn run(allocator: std.mem.Allocator, indexes: *MultiIndex, address: []const
6775
var server = try Server.init(allocator, config, &ctx);
6876
defer server.deinit();
6977

70-
server.errorHandler(handleError);
71-
server.notFound(handleNotFound);
72-
73-
try installSignalHandlers(&server);
78+
installSignalHandlers(&server);
7479

75-
var router = server.router();
80+
var router = try server.router(.{});
7681

7782
// Monitoring API
78-
router.get("/_metrics", handleMetrics);
79-
router.get("/_health", handleHealth);
80-
router.get("/:index/_health", handleIndexHealth);
83+
router.get("/_metrics", handleMetrics, .{});
84+
router.get("/_health", handleHealth, .{});
85+
router.get("/:index/_health", handleIndexHealth, .{});
8186

8287
// Search API
83-
router.post("/:index/_search", handleSearch);
88+
router.post("/:index/_search", handleSearch, .{});
8489

8590
// Bulk API
86-
router.post("/:index/_update", handleUpdate);
91+
router.post("/:index/_update", handleUpdate, .{});
8792

8893
// Fingerprint API
89-
router.head("/:index/:id", handleHeadFingerprint);
90-
router.get("/:index/:id", handleGetFingerprint);
91-
router.put("/:index/:id", handlePutFingerprint);
92-
router.delete("/:index/:id", handleDeleteFingerprint);
94+
router.head("/:index/:id", handleHeadFingerprint, .{});
95+
router.get("/:index/:id", handleGetFingerprint, .{});
96+
router.put("/:index/:id", handlePutFingerprint, .{});
97+
router.delete("/:index/:id", handleDeleteFingerprint, .{});
9398

9499
// Index API
95-
router.head("/:index", handleHeadIndex);
96-
router.get("/:index", handleGetIndex);
97-
router.put("/:index", handlePutIndex);
98-
router.delete("/:index", handleDeleteIndex);
100+
router.head("/:index", handleHeadIndex, .{});
101+
router.get("/:index", handleGetIndex, .{});
102+
router.put("/:index", handlePutIndex, .{});
103+
router.delete("/:index", handleDeleteIndex, .{});
99104

100105
log.info("listening on {s}:{d}", .{ address, port });
101106
try server.listen();
@@ -453,8 +458,8 @@ const Attributes = struct {
453458
try packer.writeMapHeader(self.attributes.count());
454459
var iter = self.attributes.iterator();
455460
while (iter.next()) |entry| {
456-
try packer.write(@TypeOf(entry.key_ptr.*), entry.key_ptr.*);
457-
try packer.write(@TypeOf(entry.value_ptr.*), entry.value_ptr.*);
461+
try packer.write(entry.key_ptr.*);
462+
try packer.write(entry.value_ptr.*);
458463
}
459464
}
460465
};

src/utils/shared_ptr.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn RefCounter(comptime T: type) type {
2424
pub fn decr(self: *Self) bool {
2525
const prev_ref_count = self.refs.fetchSub(1, .monotonic);
2626
if (prev_ref_count == 1) {
27-
self.refs.fence(.acquire);
27+
_ = self.refs.load(.acquire);
2828
return true;
2929
}
3030
return false;

0 commit comments

Comments
 (0)