1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
const std = @import("std");
const lmdb = @import("lmdb");
const PRNG_SEED = 0;
pub fn Db(comptime K: type, comptime V: type) type {
return struct {
const Self = @This();
dbi: lmdb.Dbi,
pub fn init(txn: lmdb.Txn, name: [:0]const u8) !Self {
return .{
.dbi = try txn.dbi(name),
};
}
pub fn put(self: Self, k: K, v: V) !void {
try self.dbi.put(k, v);
}
pub fn get(self: Self, k: K) !V {
return try self.dbi.get(k, V);
}
pub fn del(self: Self, k: K) !void {
try self.dbi.del(k);
}
pub fn has(self: Self, k: K) !bool {
return try self.dbi.has(k);
}
pub const Iterator = struct {
cursor: lmdb.Cursor,
k: ?K,
v: ?V,
pub fn next(self: *Iterator) ?struct { key: K, val: V } {
if (self.k != null and self.v != null) {
const result = .{ .key = self.k.?, .val = self.v.? };
var k = self.k.?;
self.v = self.cursor.get(&k, V, .Next) catch return null;
if (self.v != null) {
self.k = k;
}
return result;
} else {
return null;
}
}
};
pub fn iterator(self: Self) !Iterator {
var cursor = try self.dbi.cursor();
var k: K = undefined;
const v = try cursor.get(&k, V, .First);
return .{ .cursor = cursor, .k = k, .v = v };
}
};
}
pub const Prng = struct {
var prng = std.Random.DefaultPrng.init(PRNG_SEED);
pub fn gen(dbi: lmdb.Dbi, comptime T: type) !T {
var buf: [@sizeOf(T)]u8 = undefined;
// TODO: limit loop
while (true) {
prng.fill(&buf);
const t = std.mem.bytesToValue(T, &buf);
if (!try dbi.has(t)) {
return t;
}
}
}
};
fn SetListBase(comptime K: type, comptime V: type) type {
return struct {
const Self = @This();
pub const Index = u64;
pub const Key = K;
pub const Val = V;
pub const View = SetListViewBase(K, V);
idx: ?Index = null,
fn open_dbi(txn: lmdb.Txn) !lmdb.Dbi {
return try txn.dbi("SetList");
}
pub fn init(txn: lmdb.Txn) !Self {
const head = View.Head{};
const dbi = try open_dbi(txn);
const idx = try Prng.gen(dbi, Index);
try dbi.put(idx, head);
return .{ .idx = idx };
}
pub fn open(self: Self, txn: lmdb.Txn) !View {
// create new head
if (self.idx == null) {
return error.NotInitialized;
}
// get head from dbi
const dbi = try open_dbi(txn);
const head = try dbi.get(self.idx.?, View.Head);
return .{
.dbi = dbi,
.idx = self.idx.?,
.head = head,
};
}
};
}
fn SetListViewBase(comptime K: type, comptime V: type) type {
return struct {
const Self = @This();
pub const ItemIndex = struct { SetListBase(K, V).Index, Key };
pub const Key = K;
pub const Val = V;
pub const Head = struct {
len: usize = 0,
first: ?K = null,
last: ?K = null,
};
pub const Item = struct {
next: ?K = null,
prev: ?K = null,
data: V,
};
dbi: lmdb.Dbi,
idx: SetListBase(K, V).Index,
head: Head,
fn item_idx(self: Self, k: K) ItemIndex {
return .{ self.idx, k };
}
fn item_get(self: Self, k: K) !Item {
return try self.dbi.get(self.item_idx(k), Item);
}
fn item_put(self: Self, k: K, item: Item) !void {
try self.dbi.put(self.item_idx(k), item);
}
fn head_update(self: Self) !void {
try self.dbi.put(self.idx, self.head);
}
pub fn del(self: *Self, k: K) !void {
const item = try self.item_get(k);
if (item.prev != null) {
var prev = try self.item_get(item.prev.?);
prev.next = item.next;
try self.item_put(item.prev.?, prev);
}
if (item.next != null) {
var next = try self.item_get(item.next.?);
next.prev = item.prev;
try self.item_put(item.next.?, next);
}
if (self.head.first == k) self.head.first = item.next;
if (self.head.last == k) self.head.last = item.prev;
self.head.len -= 1;
try self.head_update();
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;
}
pub fn append(self: *Self, key: Key, val: Val) !void {
if (self.head.len == 0) {
const item = Item{ .data = val };
try self.item_put(key, item);
self.head.len = 1;
self.head.first = key;
self.head.last = key;
try self.head_update();
} else {
const prev_idx = self.head.last.?;
var prev = try self.item_get(prev_idx);
const item = Item{ .prev = prev_idx, .data = val };
try self.item_put(key, item);
prev.next = key;
try self.item_put(prev_idx, prev);
self.head.last = key;
self.head.len += 1;
try self.head_update();
}
}
pub fn get(self: Self, key: Key) !Val {
const item = try self.item_get(key);
return item.data;
}
pub fn has(self: Self, key: Key) !bool {
return self.dbi.has(self.item_idx(key));
}
pub const Iterator = struct {
pub const Result = ?struct { key: K, val: V };
slv: SetListViewBase(K, V),
idx: ?K,
dir: enum { Forward, Backward },
pub fn next(self: *Iterator) Result {
if (self.idx != null) {
const k = self.idx.?;
const item = self.slv.item_get(k) catch return null;
self.idx = switch (self.dir) {
.Forward => item.next,
.Backward => item.prev,
};
return .{ .key = k, .val = item.data };
} else {
return null;
}
}
};
pub fn iterator(self: Self) Iterator {
return .{
.slv = self,
.idx = self.head.first,
.dir = .Forward,
};
}
pub fn reverse_iterator(self: Self) Iterator {
return .{
.slv = self,
.idx = self.head.last,
.dir = .Backward,
};
}
};
}
pub fn Set(comptime K: type) type {
return struct {
pub const Key = K;
pub const Val = void;
pub const Base = SetListBase(Key, Val);
pub const View = struct {
const ViewBase = SetListViewBase(Key, Val);
base: ViewBase,
pub fn del(self: *@This(), key: Key) !void {
try self.base.del(key);
}
pub fn clear(self: *@This()) !void {
try self.base.clear();
}
pub fn len(self: @This()) usize {
return self.base.len();
}
pub fn append(self: *@This(), key: Key) !void {
try self.base.append(key, {});
}
pub fn has(self: @This(), key: Key) !bool {
return try self.base.has(key);
}
pub fn iterator(self: @This()) ViewBase.Iterator {
return self.base.iterator();
}
pub fn reverse_iterator(self: @This()) ViewBase.Iterator {
return self.base.reverse_iterator();
}
};
base: Base,
pub fn init(txn: lmdb.Txn) !@This() {
return .{ .base = try Base.init(txn) };
}
pub fn open(self: @This(), txn: lmdb.Txn) !View {
return .{ .base = try self.base.open(txn) };
}
};
}
pub fn List(comptime V: type) type {
return struct {
pub const Key = u64;
pub const Val = V;
pub const Base = SetListBase(Key, Val);
pub const View = struct {
const ViewBase = SetListViewBase(Key, Val);
base: ViewBase,
fn gen(self: @This()) !Key {
// TODO: limit loop
while (true) {
const key = try Prng.gen(self.base.dbi, Key);
if (!try self.base.dbi.has(self.base.item_idx(key))) {
return key;
}
}
}
pub fn del(self: *@This(), key: Key) !void {
try self.base.del(key);
}
pub fn clear(self: *@This()) !void {
try self.base.clear();
}
pub fn len(self: @This()) usize {
return self.base.len();
}
pub fn append(self: *@This(), val: Val) !Key {
const key = try self.gen();
try self.base.append(key, val);
return key;
}
pub fn get(self: @This(), key: Key) !Val {
return try self.base.get(key);
}
pub fn has(self: @This(), key: Key) !bool {
return try self.base.has(key);
}
pub fn iterator(self: @This()) ViewBase.Iterator {
return self.base.iterator();
}
pub fn reverse_iterator(self: @This()) ViewBase.Iterator {
return self.base.reverse_iterator();
}
};
base: Base,
pub fn init(txn: lmdb.Txn) !@This() {
return .{ .base = try Base.init(txn) };
}
pub fn open(self: @This(), txn: lmdb.Txn) !View {
return .{ .base = try self.base.open(txn) };
}
};
}
pub fn SetList(comptime K: type, comptime V: type) type {
return struct {
pub const Key = K;
pub const Val = V;
pub const Base = SetListBase(Key, Val);
pub const View = struct {
const ViewBase = SetListViewBase(Key, Val);
base: ViewBase,
pub fn del(self: *@This(), key: Key) !void {
try self.base.del(key);
}
pub fn clear(self: *@This()) !void {
try self.base.clear();
}
pub fn len(self: @This()) usize {
return self.base.len();
}
pub fn append(self: *@This(), key: Key, val: Val) !void {
try self.base.append(key, val);
}
pub fn get(self: @This(), key: Key) !Val {
return try self.base.get(key);
}
pub fn has(self: @This(), key: Key) !bool {
return try self.base.has(key);
}
pub fn iterator(self: @This()) ViewBase.Iterator {
return self.base.iterator();
}
pub fn reverse_iterator(self: @This()) ViewBase.Iterator {
return self.base.reverse_iterator();
}
};
base: Base,
pub fn init(txn: lmdb.Txn) !@This() {
return .{ .base = try Base.init(txn) };
}
pub fn open(self: @This(), txn: lmdb.Txn) !View {
return .{ .base = try self.base.open(txn) };
}
};
}
const DB_SIZE = 1024 * 1024 * 1;
test "db" {
const env = try lmdb.Env.open("db", DB_SIZE);
defer env.close();
const txn = try env.txn();
defer txn.commit() catch {};
var db = try Db(u32, u32).init(txn, "123");
var n: u32 = 456;
if (try db.has(123)) {
n = try db.get(123);
n += 1;
}
try db.put(123, n);
std.debug.print("n: {}\n", .{n});
}
// test "list" {
// const env = try lmdb.Env.open("db", DB_SIZE);
// defer env.close();
// const txn = try env.txn();
// defer txn.commit();
// const db = List.init(txn, "b", u32);
// }
test "set" {
var env = try lmdb.Env.open("db", 1024 * 1024 * 1);
// env.sync();
defer env.close();
var txn = try env.txn();
defer txn.commit() catch {};
var dbi = try txn.dbi("abc");
const A = struct {
ml: Set(usize),
};
var a: A = undefined;
const a_idx: u64 = 27;
if (try dbi.has(a_idx)) {
a = try dbi.get(a_idx, A);
} else {
a = A{ .ml = try Set(usize).init(txn) };
try dbi.put(a_idx, a);
}
var ml = try a.ml.open(txn);
const len = ml.len();
std.debug.print("{}\n", .{len});
try ml.append(len);
std.debug.print("{}\n", .{try ml.has(len)});
var it = ml.iterator();
while (it.next()) |i| {
std.debug.print("{}\n", .{i});
}
}
test "list" {
var env = try lmdb.Env.open("db", 1024 * 1024 * 1);
// env.sync();
defer env.close();
var txn = try env.txn();
defer txn.commit() catch {};
var dbi = try txn.dbi("def");
const A = struct {
ml: List(usize),
};
var a: A = undefined;
const a_idx: u64 = 27;
if (try dbi.has(a_idx)) {
a = try dbi.get(a_idx, A);
} else {
a = A{ .ml = try List(usize).init(txn) };
try dbi.put(a_idx, a);
}
var ml = try a.ml.open(txn);
const len = ml.len();
std.debug.print("{}\n", .{len});
const newest = try ml.append(len * 10);
std.debug.print("{}: {}\n", .{ newest, try ml.get(newest) });
var it = ml.iterator();
while (it.next()) |i| {
std.debug.print("{}: {}\n", .{ i.key, i.val });
}
}
test "setlist" {
var env = try lmdb.Env.open("db", 1024 * 1024 * 1);
// env.sync();
defer env.close();
var txn = try env.txn();
defer txn.commit() catch {};
var dbi = try txn.dbi("ghi");
const A = struct {
ml: SetList(usize, usize),
};
var a: A = undefined;
const a_idx: u64 = 27;
if (try dbi.has(a_idx)) {
a = try dbi.get(a_idx, A);
} else {
a = A{ .ml = try SetList(usize, usize).init(txn) };
try dbi.put(a_idx, a);
}
var ml = try a.ml.open(txn);
const len = ml.len();
std.debug.print("{}\n", .{len});
try ml.append(len, len * 10);
std.debug.print("{}\n", .{try ml.get(len)});
var it = ml.iterator();
while (it.next()) |i| {
std.debug.print("{}: {}\n", .{ i.key, i.val });
}
}
|