Skip to content

Commit b7cf50b

Browse files
committed
Renamed Vstore to vztor and made nmslib external dep
1 parent 2370e6f commit b7cf50b

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

build.zig

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ pub fn build(b: *std.Build) void {
99
// ------------------------------------------------------------
1010
// Dependencies
1111
// ------------------------------------------------------------
12-
const nmslib_dep = b.dependency("nmslib", .{});
12+
const nmslib_dep = b.dependency("nmslib", .{
13+
});
1314
const lmdbx_dep = b.dependency("lmdbx", .{
14-
.optimize = .Debug,
1515
});
1616

1717
// ------------------------------------------------------------
@@ -24,29 +24,33 @@ pub fn build(b: *std.Build) void {
2424
// ------------------------------------------------------------
2525
// Executable (fast debug build)
2626
// ------------------------------------------------------------
27-
const exe_root = b.createModule(.{
27+
const lib_root = b.createModule(.{
2828
.root_source_file = b.path("src/main.zig"),
2929
.target = target,
3030
.optimize = .Debug,
3131
});
3232

33-
const exe = b.addExecutable(.{
34-
.name = "VStore",
35-
.root_module = exe_root,
33+
// const nmslib_lib = nmslib_dep.artifact("nmslib");
34+
const nmslib_mod = nmslib_dep.module("nmslib");
35+
36+
const lib = b.addLibrary(.{
37+
.name = "Vztor",
38+
.linkage = .static,
39+
.root_module = lib_root,
3640
});
3741

38-
exe.root_module.addImport("build_opts", build_opts.createModule());
39-
exe.root_module.addImport("utils", b.createModule(.{
42+
lib.root_module.addImport("build_opts", build_opts.createModule());
43+
lib.root_module.addImport("utils", b.createModule(.{
4044
.root_source_file = b.path("src/utils.zig"),
4145
}));
4246

43-
exe.root_module.addImport("lmdbx", lmdbx_dep.module("lmdbx"));
44-
exe.root_module.addImport("nmslib", nmslib_dep.module("nmslib"));
47+
lib.root_module.addImport("lmdbx", lmdbx_dep.module("lmdbx"));
48+
lib.root_module.addImport("nmslib", nmslib_mod);
4549

46-
exe.linkLibCpp();
47-
b.installArtifact(exe);
4850

49-
const run_cmd = b.addRunArtifact(exe);
51+
b.installArtifact(lib);
52+
53+
const run_cmd = b.addRunArtifact(lib);
5054
const run_step = b.step("run", "Run VStore in debug mode");
5155
run_step.dependOn(&run_cmd.step);
5256

@@ -63,16 +67,14 @@ pub fn build(b: *std.Build) void {
6367
.root_module = test_root,
6468
});
6569

66-
// mirror imports so tests see same world as exe
70+
// mirror imports so tests see same world as lib
6771
tests.root_module.addImport("build_opts", build_opts.createModule());
6872
tests.root_module.addImport("utils", b.createModule(.{
6973
.root_source_file = b.path("src/utils.zig"),
7074
}));
7175
tests.root_module.addImport("lmdbx", lmdbx_dep.module("lmdbx"));
72-
tests.root_module.addImport("nmslib", nmslib_dep.module("nmslib"));
73-
74-
tests.linkLibCpp();
75-
76+
tests.root_module.addImport("nmslib", nmslib_mod);
77+
7678
const run_tests = b.addRunArtifact(tests);
7779
const test_step = b.step("test", "Run VStore tests");
7880
test_step.dependOn(&run_tests.step);

build.zig.zon

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// It is redundant to include "zig" in this name because it is already
88
// within the Zig package namespace.
9-
.name = .VStore,
9+
.name = .Vztor,
1010
// This is a [Semantic Version](https://semver.org/).
1111
// In a future version of Zig it will be used for package deduplication.
1212
.version = "0.0.0",
@@ -22,7 +22,7 @@
2222
// original project's identity. Thus it is recommended to leave the comment
2323
// on the following line intact, so that it shows up in code reviews that`
2424
// modify the field.
25-
.fingerprint = 0x93a1ca272d548e10, // Changing this has security and trust implications.
25+
.fingerprint = 0x7a652a7af8d35233, // Changing this has security and trust implications.
2626
// Tracks the earliest Zig version that the package considers to be a
2727
// supported use case.
2828
.minimum_zig_version = "0.16.0-dev.457+f90510b08",
@@ -32,9 +32,6 @@
3232
// Once all dependencies are fetched, `zig build` no longer requires
3333
// internet connectivity.
3434
.dependencies = .{
35-
.nmslib = .{
36-
.path = "../nmslib-zig", // path to your library project
37-
},
3835
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
3936
//.example = .{
4037
// // When updating this field to a new URL, be sure to delete the corresponding
@@ -68,6 +65,10 @@
6865
.url = "https://github.com/theseyan/lmdbx-zig/archive/refs/tags/v0.2.0.tar.gz",
6966
.hash = "lmdbx-0.1.2-WlIvzjFVAwDUzL6W5PcOnaxpazqXl9wEyx0K0H0Gv_9R",
7067
},
68+
.nmslib = .{
69+
.url = "https://github.com/B-R-P/NMSLIB-ZIG/archive/refs/heads/main.tar.gz",
70+
.hash = "nmslib-0.1.0-7XQ0QQJrDwBigBxLkzWxkEZJPBDCEI91mwm6fQtmEfu0",
71+
},
7172
},
7273
// Specifies the set of files and directories that are included in this package.
7374
// Only files and directories listed here are included in the `hash` that

src/main.zig

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const searchResult = struct {
1919
};
2020

2121

22-
pub const VStore = struct {
22+
pub const Vztor = struct {
2323
const Self = @This();
2424

2525
arena: std.heap.ArenaAllocator,
@@ -182,7 +182,7 @@ pub const VStore = struct {
182182
// Commit setup transaction; DB and txn handles go out of scope here
183183
try txn.commit();
184184

185-
// Store everything into VStore
185+
// Store everything into Vztor
186186
store.env = env;
187187
store.rnd = rnd;
188188
store.counter = counter;
@@ -289,7 +289,7 @@ pub const VStore = struct {
289289

290290
try txn.commit();
291291

292-
// Return keys that were allocated with stable_alloc (owned by VStore)
292+
// Return keys that were allocated with stable_alloc (owned by Vztor)
293293
return nnkeys;
294294
}
295295

@@ -346,7 +346,7 @@ pub const VStore = struct {
346346

347347
fn search(self: *Self, vector: nmslib.QueryPoint, k: usize) ![]searchResult {
348348

349-
// Stable allocator for returned results (owned by VStore)
349+
// Stable allocator for returned results (owned by Vztor)
350350
const stable_alloc = self.arena.allocator();
351351

352352
const knn_result = try self.index.knnQuery(vector, k);
@@ -426,7 +426,7 @@ pub fn main() !void {
426426
const dist_type = nmslib.DistType.Float;
427427
std.debug.assert(nmslib.isValidSpaceType(space_type));
428428

429-
var store = try VStore.init(allocator, db_path, space_type, vector_type, dist_type, max_readers);
429+
var store = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, max_readers);
430430
defer store.deinit() catch unreachable;
431431

432432

@@ -452,8 +452,8 @@ pub fn main() !void {
452452

453453

454454

455-
test "VStore: basic put/get/save and reload" {
456-
std.debug.print("[ ] VStore: basic put/get/save and reload\n", .{});
455+
test "Vztor: basic put/get/save and reload" {
456+
std.debug.print("[ ] Vztor: basic put/get/save and reload\n", .{});
457457

458458
const allocator = std.heap.page_allocator;
459459
const db_path = "testdb_vstore_zigtest";
@@ -462,7 +462,7 @@ test "VStore: basic put/get/save and reload" {
462462
const dist_type = nmslib.DistType.Float;
463463

464464
// Initialize the store
465-
var store = try VStore.init(allocator, db_path, space_type, vector_type, dist_type, 16);
465+
var store = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, 16);
466466

467467
// Prepare two sparse vectors and payloads
468468
const vectors = [_][]const nmslib.SparseElem{
@@ -498,7 +498,7 @@ test "VStore: basic put/get/save and reload" {
498498
try store.deinit();
499499

500500
// Re-open the store from the same path (index should be loaded from disk)
501-
var reopened = try VStore.init(allocator, db_path, space_type, vector_type, dist_type, 16);
501+
var reopened = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, 16);
502502

503503
// Re-fetch using the copied key (not owned by the arena)
504504
const got2 = try reopened.batchGet(key_copy);
@@ -512,21 +512,21 @@ test "VStore: basic put/get/save and reload" {
512512
const cwd = std.fs.cwd();
513513
cwd.deleteTree(db_path) catch {};
514514

515-
std.debug.print("[x] VStore: basic put/get/save and reload\n", .{});
515+
std.debug.print("[x] Vztor: basic put/get/save and reload\n", .{});
516516
}
517517

518518

519519

520-
test "VStore: batchPut returns unique keys on repeated insert" {
521-
std.debug.print("[ ] VStore: batchPut returns unique keys on repeated insert\n", .{});
520+
test "Vztor: batchPut returns unique keys on repeated insert" {
521+
std.debug.print("[ ] Vztor: batchPut returns unique keys on repeated insert\n", .{});
522522

523523
const allocator = std.heap.page_allocator;
524524
const db_path = "testdb_vstore_unique_keys";
525525
const space_type = "negdotprod_sparse";
526526
const vector_type = nmslib.DataType.SparseVector;
527527
const dist_type = nmslib.DistType.Float;
528528

529-
var store = try VStore.init(allocator, db_path, space_type, vector_type, dist_type, 16);
529+
var store = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, 16);
530530

531531
const vectors = [_][]const nmslib.SparseElem{
532532
&[_]nmslib.SparseElem{
@@ -557,21 +557,21 @@ test "VStore: batchPut returns unique keys on repeated insert" {
557557
const cwd = std.fs.cwd();
558558
cwd.deleteTree(db_path) catch {};
559559

560-
std.debug.print("[x] VStore: batchPut returns unique keys on repeated insert\n", .{});
560+
std.debug.print("[x] Vztor: batchPut returns unique keys on repeated insert\n", .{});
561561
}
562562

563563

564564

565-
test "VStore: batchGet returns KeyNotFound for missing key" {
566-
std.debug.print("[ ] VStore: batchGet returns KeyNotFound for missing key\n", .{});
565+
test "Vztor: batchGet returns KeyNotFound for missing key" {
566+
std.debug.print("[ ] Vztor: batchGet returns KeyNotFound for missing key\n", .{});
567567
const allocator = std.heap.page_allocator;
568568
const db_path = "testdb_vstore_missing_key";
569569
const space_type = "negdotprod_sparse";
570570
const vector_type = nmslib.DataType.SparseVector;
571571
const dist_type = nmslib.DistType.Float;
572572

573573
{
574-
var store = try VStore.init(allocator, db_path, space_type, vector_type, dist_type, 8);
574+
var store = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, 8);
575575

576576
// Attempt to get a key that does not exist
577577
const res = store.batchGet("no-such-key");
@@ -583,19 +583,19 @@ test "VStore: batchGet returns KeyNotFound for missing key" {
583583

584584
const cwd = std.fs.cwd();
585585
cwd.deleteTree(db_path) catch {};
586-
std.debug.print("[x] VStore: batchGet returns KeyNotFound for missing key\n", .{});
586+
std.debug.print("[x] Vztor: batchGet returns KeyNotFound for missing key\n", .{});
587587
}
588588

589589

590-
test "VStore: bulk 10 insert and retrieve" {
591-
std.debug.print("[ ] VStore: bulk 10 insert and retrieve\n", .{});
590+
test "Vztor: bulk 10 insert and retrieve" {
591+
std.debug.print("[ ] Vztor: bulk 10 insert and retrieve\n", .{});
592592
const allocator = std.heap.page_allocator;
593593
const db_path = "testdb_vstore_bulk10";
594594
const space_type = "negdotprod_sparse";
595595
const vector_type = nmslib.DataType.SparseVector;
596596
const dist_type = nmslib.DistType.Float;
597597

598-
var store = try VStore.init(allocator, db_path, space_type, vector_type, dist_type, 16);
598+
var store = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, 16);
599599
defer store.deinit() catch unreachable;
600600

601601
const vectors = [_][]const nmslib.SparseElem{
@@ -636,11 +636,11 @@ test "VStore: bulk 10 insert and retrieve" {
636636

637637
const cwd = std.fs.cwd();
638638
cwd.deleteTree(db_path) catch {};
639-
std.debug.print("[x] VStore: bulk 10 insert and retrieve\n", .{});
639+
std.debug.print("[x] Vztor: bulk 10 insert and retrieve\n", .{});
640640
}
641641

642-
test "VStore: init tolerates empty IDX directory" {
643-
std.debug.print("[ ] VStore: init tolerates empty IDX directory\n", .{});
642+
test "Vztor: init tolerates empty IDX directory" {
643+
std.debug.print("[ ] Vztor: init tolerates empty IDX directory\n", .{});
644644
const allocator = std.heap.page_allocator;
645645
const db_path = "testdb_vstore_empty_idx";
646646
const space_type = "negdotprod_sparse";
@@ -656,7 +656,7 @@ test "VStore: init tolerates empty IDX directory" {
656656
try cwd.makePath(db_path ++ "/IDX");
657657

658658
// This call used to fail when IDX existed but had no index.
659-
var store = try VStore.init(allocator, db_path, space_type, vector_type, dist_type, 16);
659+
var store = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, 16);
660660
defer store.deinit() catch unreachable;
661661

662662
// Store should be usable: do a simple put/get round-trip
@@ -672,5 +672,5 @@ test "VStore: init tolerates empty IDX directory" {
672672

673673
// Cleanup
674674
cwd.deleteTree(db_path) catch {};
675-
std.debug.print("[x] VStore: init tolerates empty IDX directory\n", .{});
675+
std.debug.print("[x] Vztor: init tolerates empty IDX directory\n", .{});
676676
}

0 commit comments

Comments
 (0)