Skip to content

Commit

Permalink
feat(ledger): get all the tests working for lmdb and add some more
Browse files Browse the repository at this point in the history
  • Loading branch information
dnut committed Nov 1, 2024
1 parent 92f60c9 commit 8b1a7f7
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 109 deletions.
16 changes: 12 additions & 4 deletions src/ledger/database/hashmap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,17 @@ pub fn SharedHashMapDB(comptime column_families: []const ColumnFamily) type {
const cf_index, const key = delete_ix;
self.maps[cf_index].delete(self.allocator, key);
},
.delete_range => {
// TODO: also add to database tests
@panic("not implemented");
.delete_range => |delete_range_ix| {
const cf_index, const start, const end = delete_range_ix;
const keys, _ = self.maps[cf_index].map.range(start, end);
const to_delete = try batch.allocator.alloc([]const u8, keys.len);
defer batch.allocator.free(to_delete);
for (keys, 0..) |key, i| {
to_delete[i] = key;
}
for (to_delete) |delete_key| {
self.maps[cf_index].delete(self.allocator, delete_key);
}
},
}
}
Expand Down Expand Up @@ -266,7 +274,7 @@ pub fn SharedHashMapDB(comptime column_families: []const ColumnFamily) type {
const end_bytes = try key_serializer.serializeAlloc(self.allocator, end);
errdefer self.allocator.free(end_bytes);
const cf_index = cf.find(column_families);
self.instructions.append(
try self.instructions.append(
self.allocator,
.{ .delete_range = .{ cf_index, start_bytes, end_bytes } },
);
Expand Down
164 changes: 164 additions & 0 deletions src/ledger/database/interface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -547,5 +547,169 @@ fn tests(comptime Impl: fn ([]const ColumnFamily) type) type {

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator forward start before all"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

try db.put(cf1, 40, .{ .hello = 44 });
try db.put(cf1, 10, .{ .hello = 111 });
try db.put(cf1, 30, .{ .hello = 33 });
try db.put(cf1, 20, .{ .hello = 222 });

var iter = try db.iterator(cf1, .forward, 5);
defer iter.deinit();

var next = (try iter.next()).?;
try std.testing.expectEqual(10, next[0]);
try std.testing.expectEqual(Value1{ .hello = 111 }, next[1]);
next = (try iter.next()).?;
try std.testing.expectEqual(20, next[0]);
try std.testing.expectEqual(Value1{ .hello = 222 }, next[1]);
next = (try iter.next()).?;
try std.testing.expectEqual(30, next[0]);
try std.testing.expectEqual(Value1{ .hello = 33 }, next[1]);
next = (try iter.next()).?;
try std.testing.expectEqual(40, next[0]);
try std.testing.expectEqual(Value1{ .hello = 44 }, next[1]);

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator forward start after all"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

try db.put(cf1, 40, .{ .hello = 44 });
try db.put(cf1, 10, .{ .hello = 111 });
try db.put(cf1, 30, .{ .hello = 33 });
try db.put(cf1, 20, .{ .hello = 222 });

var iter = try db.iterator(cf1, .forward, 50);
defer iter.deinit();

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator reverse start before all"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

try db.put(cf1, 40, .{ .hello = 44 });
try db.put(cf1, 10, .{ .hello = 111 });
try db.put(cf1, 30, .{ .hello = 33 });
try db.put(cf1, 20, .{ .hello = 222 });

var iter = try db.iterator(cf1, .reverse, 50);
defer iter.deinit();

var next = (try iter.next()).?;
try std.testing.expectEqual(40, next[0]);
try std.testing.expectEqual(Value1{ .hello = 44 }, next[1]);
next = (try iter.next()).?;
try std.testing.expectEqual(30, next[0]);
try std.testing.expectEqual(Value1{ .hello = 33 }, next[1]);
next = (try iter.next()).?;
try std.testing.expectEqual(20, next[0]);
try std.testing.expectEqual(Value1{ .hello = 222 }, next[1]);
next = (try iter.next()).?;
try std.testing.expectEqual(10, next[0]);
try std.testing.expectEqual(Value1{ .hello = 111 }, next[1]);

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator reverse start after all"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

try db.put(cf1, 40, .{ .hello = 44 });
try db.put(cf1, 10, .{ .hello = 111 });
try db.put(cf1, 30, .{ .hello = 33 });
try db.put(cf1, 20, .{ .hello = 222 });

var iter = try db.iterator(cf1, .reverse, 5);
defer iter.deinit();

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator forward empty"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

var iter = try db.iterator(cf1, .forward, 1);
defer iter.deinit();

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator reverse empty"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

var iter = try db.iterator(cf1, .reverse, 1);
defer iter.deinit();

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator forward empty with null start"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

var iter = try db.iterator(cf1, .forward, null);
defer iter.deinit();

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"iterator reverse empty with null start"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

var iter = try db.iterator(cf1, .reverse, null);
defer iter.deinit();

try std.testing.expectEqual(null, try iter.next());
}

pub fn @"WriteBatch.deleteRange"() !void {
const path = test_dir ++ @src().fn_name;
try ledger.tests.freshDir(path);
var db = try DB.open(allocator, logger, path);
defer db.deinit();

try db.put(cf1, 40, .{ .hello = 44 });
try db.put(cf1, 10, .{ .hello = 111 });
try db.put(cf1, 30, .{ .hello = 33 });
try db.put(cf1, 20, .{ .hello = 222 });

var batch = try db.initWriteBatch();
defer batch.deinit();
try batch.deleteRange(cf1, 0, 100);
try db.commit(batch);

try std.testing.expectEqual(null, try db.get(allocator, cf1, 10));
try std.testing.expectEqual(null, try db.get(allocator, cf1, 20));
try std.testing.expectEqual(null, try db.get(allocator, cf1, 30));
try std.testing.expectEqual(null, try db.get(allocator, cf1, 40));
}
};
}
Loading

0 comments on commit 8b1a7f7

Please sign in to comment.