]> gitweb.ps.run Git - ziglmdb/blobdiff - src/db.zig
unify containers to allow for pagination
[ziglmdb] / src / db.zig
index 5d7a8242c83bf0b2f0a58b609d2dfbb9bb3a1bb2..8bf2f7f7da8c40a46413759e1be408b53c0c4411 100644 (file)
@@ -76,6 +76,7 @@ pub fn Set(comptime K: type) type {
         idx: ?Index = null,
 
         const Self = @This();
+        pub const Key = K;
         pub const Index = u64;
         pub const View = SetView(K);
 
@@ -183,6 +184,12 @@ pub fn SetView(comptime K: type) type {
 
             try self.dbi.del(self.item_idx(k));
         }
+        pub fn clear(self: *Self) !void {
+            var it = self.iterator();
+            while (it.next()) |i| {
+                try self.del(i.key);
+            }
+        }
         pub fn has(self: Self, k: K) !bool {
             return self.dbi.has(self.item_idx(k));
         }
@@ -194,7 +201,7 @@ pub fn SetView(comptime K: type) type {
             idx: ?K,
             dir: enum { Forward, Backward },
 
-            pub fn next(self: *Iterator) ?K {
+            pub fn next(self: *Iterator) ?struct { key: K } {
                 if (self.idx != null) {
                     const k = self.idx.?;
                     const item = self.sv.item_get(k) catch return null;
@@ -202,7 +209,7 @@ pub fn SetView(comptime K: type) type {
                         .Forward => item.next,
                         .Backward => item.prev,
                     };
-                    return k;
+                    return .{ .key = k };
                 } else {
                     return null;
                 }
@@ -230,6 +237,8 @@ pub fn List(comptime V: type) type {
 
         const Self = @This();
         pub const Index = u64;
+        pub const Key = u64;
+        pub const Val = V;
         pub const View = ListView(V);
 
         fn open_dbi(txn: lmdb.Txn) !lmdb.Dbi {
@@ -357,6 +366,12 @@ pub fn ListView(comptime V: type) type {
 
             try self.dbi.del(self.item_idx(k));
         }
+        pub fn clear(self: *Self) !void {
+            var it = self.iterator();
+            while (it.next()) |kv| {
+                try self.del(kv.key);
+            }
+        }
         pub fn len(self: Self) usize {
             return self.head.len;
         }
@@ -398,12 +413,14 @@ pub fn ListView(comptime V: type) type {
 
 pub fn SetList(comptime K: type, comptime V: type) type {
     return struct {
-        idx: ?Index = null,
-
         const Self = @This();
         pub const Index = u64;
+        pub const Key = K;
+        pub const Val = V;
         pub const View = SetListView(K, V);
 
+        idx: ?Index = null,
+
         fn open_dbi(txn: lmdb.Txn) !lmdb.Dbi {
             return try txn.dbi("SetList");
         }
@@ -513,6 +530,12 @@ pub fn SetListView(comptime K: type, comptime V: type) type {
 
             try self.dbi.del(self.item_idx(k));
         }
+        pub fn clear(self: *Self) !void {
+            var it = self.iterator();
+            while (it.next()) |kv| {
+                try self.del(kv.key);
+            }
+        }
         pub fn has(self: Self, k: K) !bool {
             return self.dbi.has(self.item_idx(k));
         }