diff options
Diffstat (limited to 'src/db.zig')
| -rw-r--r-- | src/db.zig | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -29,6 +29,7 @@ pub fn Db(comptime K: type, comptime V: type) type { pub const Iterator = struct { pub const Result = struct { key: K, val: V }; cursor: lmdb.Cursor, + dir: enum { Forward, Backward }, k: ?K, v: ?V, @@ -37,7 +38,11 @@ pub fn Db(comptime K: type, comptime V: type) type { const result = Result{ .key = self.k.?, .val = self.v.? }; var k = self.k.?; - self.v = self.cursor.get(&k, V, .Next) catch return null; + if (self.dir == .Forward) { + self.v = self.cursor.get(&k, V, .Next) catch return null; + } else { + self.v = self.cursor.get(&k, V, .Prev) catch return null; + } if (self.v != null) { self.k = k; } @@ -52,7 +57,14 @@ pub fn Db(comptime K: type, comptime V: type) type { var k: K = undefined; const v = try cursor.get(&k, V, .First); - return .{ .cursor = cursor, .k = k, .v = v }; + return .{ .cursor = cursor, .dir = .Forward, .k = k, .v = v }; + } + pub fn reverse_iterator(self: Self) !Iterator { + var cursor = try self.dbi.cursor(); + + var k: K = undefined; + const v = try cursor.get(&k, V, .Last); + return .{ .cursor = cursor, .dir = .Backward, .k = k, .v = v }; } }; } |
