Skip to content

Commit a5cb5ba

Browse files
committed
Renamed names for consistency
1 parent fefa93d commit a5cb5ba

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/main.zig

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const utils = @import("utils");
77

88

99

10-
const getResult = struct {
10+
const GetResult = struct {
1111
vector: ?nmslib.DataPoint,
1212
data: []const u8,
1313
};
1414

15-
const searchResult = struct {
15+
const SearchResult = struct {
1616
key: []const u8,
1717
data: ?[]const u8,
1818
distance: f32
@@ -25,7 +25,7 @@ pub const Vztor = struct {
2525
arena: std.heap.ArenaAllocator,
2626
env: lmdbx.Environment,
2727
index: nmslib.Index,
28-
indexPath: []const u8,
28+
index_path: []const u8,
2929
counter: u32,
3030
rnd: std.Random,
3131
seed: u64,
@@ -142,10 +142,10 @@ pub const Vztor = struct {
142142
var seed: u64 = undefined;
143143
var counter: u32 = undefined;
144144
var rnd: std.Random = undefined;
145-
if (try db_metadata.get("seed")) |strSeed| {
146-
const strCounter = try db_metadata.get("random_counter") orelse "0";
147-
counter = try std.fmt.parseInt(u32, strCounter, 10);
148-
seed = try std.fmt.parseInt(u64, strSeed, 10);
145+
if (try db_metadata.get("seed")) |str_seed| {
146+
const str_counter = try db_metadata.get("random_counter") orelse "0";
147+
counter = try std.fmt.parseInt(u32, str_counter, 10);
148+
seed = try std.fmt.parseInt(u64, str_seed, 10);
149149
var prng = std.Random.DefaultPrng.init(seed);
150150
rnd = prng.random();
151151
for (0..counter) |_| {
@@ -156,10 +156,10 @@ pub const Vztor = struct {
156156
var prng = std.Random.DefaultPrng.init(seed);
157157
rnd = prng.random();
158158
counter = 0;
159-
const strSeed = try std.fmt.allocPrint(arena_allocator, "{d}", .{ seed });
160-
const strCounter = try std.fmt.allocPrint(arena_allocator, "{d}", .{ counter });
161-
try db_metadata.set("seed", strSeed, .Create);
162-
try db_metadata.set("random_counter", strCounter, .Create);
159+
const str_seed = try std.fmt.allocPrint(arena_allocator, "{d}", .{ seed });
160+
const str_counter = try std.fmt.allocPrint(arena_allocator, "{d}", .{ counter });
161+
try db_metadata.set("seed", str_seed, .Create);
162+
try db_metadata.set("random_counter", str_counter, .Create);
163163
}
164164

165165
// HNSW / index parameters (kept on stack; index gets a copy of what it needs)
@@ -190,7 +190,7 @@ pub const Vztor = struct {
190190
store.index = index;
191191

192192
// Full path to the index base file: "<db_path>/IDX/index"
193-
store.indexPath = try std.fs.path.join(arena_allocator, &.{ db_path, "IDX", "index" });
193+
store.index_path = try std.fs.path.join(arena_allocator, &.{ db_path, "IDX", "index" });
194194

195195
return store;
196196
}
@@ -205,8 +205,8 @@ pub const Vztor = struct {
205205
errdefer txn.abort() catch unreachable;
206206

207207
const db_metadata = try txn.database("metadata", .{});
208-
const counterStr = try std.fmt.allocPrint(self.arena.allocator(), "{d}", .{ self.counter });
209-
try db_metadata.set("random_counter", counterStr, .Update);
208+
const counter_str = try std.fmt.allocPrint(self.arena.allocator(), "{d}", .{ self.counter });
209+
try db_metadata.set("random_counter", counter_str, .Update);
210210

211211
// Commit exactly once on the success path
212212
try txn.commit();
@@ -258,14 +258,14 @@ pub const Vztor = struct {
258258
}
259259
}
260260

261-
// numKeys is used by nmslib; allocate from stable allocator
262-
var numKeys = try stable_alloc.alloc(i32, input_length);
261+
// num_keys is used by nmslib; allocate from stable allocator
262+
var num_keys = try stable_alloc.alloc(i32, input_length);
263263
for (0..input_length) |i| {
264-
numKeys[i] = utils.stringToId32(nnkeys[i], self.seed);
264+
num_keys[i] = utils.stringToId32(nnkeys[i], self.seed);
265265
}
266266

267267
const start_idx = self.index.dataQty();
268-
try self.index.addSparseBatch(vector, numKeys);
268+
try self.index.addSparseBatch(vector, num_keys);
269269

270270
const txn = try lmdbx.Transaction.init(self.env, .{ .mode = .ReadWrite });
271271
errdefer txn.abort() catch unreachable;
@@ -277,14 +277,14 @@ pub const Vztor = struct {
277277
for (0..input_length) |i| {
278278
// Numeric index key -> string
279279
var numeric_buf: [32]u8 align(4) = undefined;
280-
const numericSKey = try std.fmt.bufPrint(&numeric_buf, "{d}", .{ numKeys[i] });
280+
const numeric_skey = try std.fmt.bufPrint(&numeric_buf, "{d}", .{ num_keys[i] });
281281

282282
// Build "pos:key" using the same stable allocator.
283283
// LMDB copies the value, so we don't need to free key_pos manually.
284284
const key_pos = try utils.key_pos_gen(stable_alloc, nnkeys[i], start_idx + i);
285285

286286
try db_data.set(nnkeys[i], data[i], .Create);
287-
try db_index_to_key.set(numericSKey, key_pos, .Create);
287+
try db_index_to_key.set(numeric_skey, key_pos, .Create);
288288
}
289289

290290
try txn.commit();
@@ -296,15 +296,15 @@ pub const Vztor = struct {
296296

297297

298298

299-
fn batchGet(self: *Self, key: []const u8) !getResult {
299+
fn batchGet(self: *Self, key: []const u8) !GetResult {
300300
// Stable allocator for returned/copied values that must outlive this call
301301
const stable_alloc = self.arena.allocator();
302302

303-
const numericKey = utils.stringToId32(key, self.seed);
303+
const numeric_key = utils.stringToId32(key, self.seed);
304304

305305
// Use a small stack buffer for printing the numeric key (no heap alloc)
306306
var num_buf: [32]u8 align(4) = undefined;
307-
const numberSkey = try std.fmt.bufPrint(&num_buf, "{d}", .{ numericKey });
307+
const number_skey = try std.fmt.bufPrint(&num_buf, "{d}", .{ numeric_key });
308308

309309
const txn = try lmdbx.Transaction.init(self.env, .{ .mode = .ReadOnly });
310310
defer txn.abort() catch unreachable;
@@ -314,7 +314,7 @@ pub const Vztor = struct {
314314

315315
// Lookups -- handle optional/null returns explicitly
316316
const data_raw = try db_data.get(key) orelse return error.KeyNotFound;
317-
const key_pos_raw = (try db_index_to_key.get(numberSkey)) orelse return error.KeyNotFound;
317+
const key_pos_raw = (try db_index_to_key.get(number_skey)) orelse return error.KeyNotFound;
318318

319319
const d = try utils.key_pos_parse(key_pos_raw);
320320
std.debug.assert(std.mem.eql(u8, key, d.key));
@@ -335,7 +335,7 @@ pub const Vztor = struct {
335335
// Copy data into stable allocator so it remains valid after txn ends
336336
const data_copy = try stable_alloc.dupe(u8, data_raw);
337337

338-
return getResult{
338+
return GetResult{
339339
.vector = maybe_vector,
340340
.data = data_copy,
341341
};
@@ -344,15 +344,15 @@ pub const Vztor = struct {
344344

345345

346346

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

349349
// 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);
353353

354354
// Allocate the result array from stable allocator (returned to caller)
355-
const final_result = try stable_alloc.alloc(searchResult, knn_result.used);
355+
const final_result = try stable_alloc.alloc(SearchResult, knn_result.used);
356356

357357
const txn = try lmdbx.Transaction.init(self.env, .{ .mode = .ReadOnly });
358358
defer txn.abort() catch unreachable;
@@ -367,10 +367,10 @@ pub const Vztor = struct {
367367
const id = knn_result.ids[i];
368368
const distance = knn_result.distances[i];
369369

370-
const numericKeySlice = try std.fmt.bufPrint(&num_buf, "{d}", .{ id });
370+
const numeric_keySlice = try std.fmt.bufPrint(&num_buf, "{d}", .{ id });
371371

372372
// Handle possible missing entries explicitly
373-
const key_pos_raw = (try db_index_to_key.get(numericKeySlice)) orelse return error.KeyNotFound;
373+
const key_pos_raw = (try db_index_to_key.get(numeric_keySlice)) orelse return error.KeyNotFound;
374374
const d = try utils.key_pos_parse(key_pos_raw);
375375

376376
const data_raw = try db_data.get(d.key) orelse return error.KeyNotFound;
@@ -379,7 +379,7 @@ pub const Vztor = struct {
379379
const key_copy = try stable_alloc.dupe(u8, d.key);
380380
const data_copy = try stable_alloc.dupe(u8, data_raw);
381381

382-
final_result[i] = searchResult{
382+
final_result[i] = SearchResult{
383383
.key = key_copy,
384384
.data = data_copy,
385385
.distance = distance,
@@ -394,11 +394,11 @@ pub const Vztor = struct {
394394
const cwd = std.fs.cwd();
395395

396396
// Ensure the *directory* containing the index exists *before* saving
397-
const idx_dir = std.fs.path.dirname(self.indexPath) orelse ".";
397+
const idx_dir = std.fs.path.dirname(self.index_path) orelse ".";
398398
try cwd.makePath(idx_dir);
399399

400400
// Save index to disk using NMSLIB (to "<db_path>/IDX/index")
401-
try self.index.save(self.indexPath, true);
401+
try self.index.save(self.index_path, true);
402402

403403
// Flush LMDB environment to disk
404404
try self.env.sync();
@@ -690,17 +690,17 @@ test "Vztor: search returns correct nearest neighbors" {
690690
var store = try Vztor.init(allocator, db_path, space_type, vector_type, dist_type, 16);
691691
defer store.deinit() catch {};
692692

693-
const vecA = [_]nmslib.SparseElem{ .{ .id = 1, .value = 1.0 } };
694-
const vecB = [_]nmslib.SparseElem{ .{ .id = 1, .value = 0.5 } };
695-
const vecC = [_]nmslib.SparseElem{ .{ .id = 2, .value = 1.0 } };
693+
const vec_a = [_]nmslib.SparseElem{ .{ .id = 1, .value = 1.0 } };
694+
const vec_b = [_]nmslib.SparseElem{ .{ .id = 1, .value = 0.5 } };
695+
const vec_c = [_]nmslib.SparseElem{ .{ .id = 2, .value = 1.0 } };
696696

697-
const vectors = [_][]const nmslib.SparseElem{ &vecA, &vecB, &vecC };
697+
const vectors = [_][]const nmslib.SparseElem{ &vec_a, &vec_b, &vec_c };
698698
const payloads = [_][]const u8{ "A", "B", "C" };
699699

700700
const keys = try store.batchPut(&vectors, &payloads, null);
701701
try std.testing.expect(keys.len == 3);
702702

703-
const query: nmslib.QueryPoint = .{ .SparseVector = &vecA };
703+
const query: nmslib.QueryPoint = .{ .SparseVector = &vec_a };
704704

705705
const results = try store.search(query, 3);
706706
try std.testing.expect(results.len >= 1);

0 commit comments

Comments
 (0)