Skip to content

Commit a960149

Browse files
authored
Remove dirty code (#4)
1 parent f20d0ab commit a960149

5 files changed

Lines changed: 31 additions & 134 deletions

File tree

example/build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
// internet connectivity.
2525
.dependencies = .{
2626
.@"boltdb-zig" = .{
27-
.url = "git+https://github.com/laohanlinux/boltdb-zig.git/?ref=align#f2ae8cd9a8d4396fec3fbbed3adbd9a14e0fa1a3",
28-
.hash = "12203cc7a802ae6cd01c6b856ff96ddfe26cb9612533ca116a0e30178d5251e173bd",
27+
.url = "git+https://github.com/laohanlinux/boltdb-zig.git/?ref=align#3ec605eca6a9f85959f81900542fea59cc3eaef6",
28+
.hash = "122056bd20aba7365380a500c315f4a03aa5ea2134685b11fa7e32e312344c28175c",
2929
},
3030
},
3131
.paths = .{

src/bucket.zig

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ const NodeSet = std.AutoHashMap(*Node, void);
1919
// A set of aligned values that will be freed by the bucket.
2020
pub const AutoFreeObject = struct {
2121
isFreed: bool = false,
22-
// A set of bytes that will be freed by the bucket.
23-
autoFreeBytes: std.AutoHashMap(u64, []u8),
2422
// A set of nodes that will be freed by the bucket.
2523
// 1: Note, the bucket.nodes is not in the autoFreeObject, so we need to destroy it manually.
2624
// But the bucket.rootNode is in the autoFreeObject, so we don't need to destroy it manually.
2725
// Because the bucket.rootNode is a special node, it is the root node of the bucket.
2826
// So, we need to destroy it manually.
2927
// 2: the nodes of autoFreeNodes is a new node that created after tx.commit(Copy on Write), their are is a spill node, a snapshot node, a new node.
3028
autoFreeNodes: NodeSet,
31-
freePtrs: std.AutoArrayHashMap(u64, isize),
3229
allocSize: usize = 0,
3330

3431
allocator: std.mem.Allocator,
@@ -37,40 +34,15 @@ pub const AutoFreeObject = struct {
3734
return .{
3835
.autoFreeNodes = NodeSet.init(allocator),
3936
.allocator = allocator,
40-
.freePtrs = std.AutoArrayHashMap(u64, isize).init(allocator),
41-
.autoFreeBytes = std.AutoHashMap(u64, []u8).init(allocator),
4237
};
4338
}
4439

4540
/// Add a node to the auto free object.
4641
pub fn addNode(self: *AutoFreeObject, node: *Node) void {
47-
const key = node.key orelse "";
4842
self.allocSize += node.size();
4943
const gop = self.autoFreeNodes.getOrPut(node) catch unreachable;
5044
const ptr = @intFromPtr(node);
51-
// assert(gop.found_existing == false, "the node({}: 0x{x}, {d}) is already in the auto free nodes", .{ node.pgid, ptr, node.id });
52-
if (gop.found_existing) {
53-
std.log.debug("the node({s}, {}: 0x{x}, {d}) is already in the auto free nodes", .{ key, node.pgid, ptr, node.id });
54-
}
55-
std.log.info("add node to the auto free nodes, key: {s}, pgid: {d}, ptr: 0x{x}, id: {d}, allocSize: {d}", .{ key, node.pgid, ptr, node.id, self.allocSize });
56-
}
57-
58-
/// Add a byte slice to the auto free object.
59-
pub fn addAutoFreeBytes(self: *AutoFreeObject, value: []u8) void {
60-
const ptr = @intFromPtr(value.ptr);
61-
const got = self.autoFreeBytes.getOrPut(ptr) catch unreachable;
62-
if (got.found_existing) {
63-
// std.log.debug("the auto free bytes({}: 0x{x}) is already in the auto free bytes", .{ value.len, ptr });
64-
} else {
65-
got.value_ptr.* = value;
66-
self.allocSize += value.len;
67-
// std.log.info("add auto free bytes, size: {d}, ptr: 0x{x}, allocSize: {d}", .{ value.len, ptr, self.allocSize });
68-
}
69-
}
70-
71-
/// Get the alloc size.
72-
pub fn getAllocSize(self: *AutoFreeObject) usize {
73-
return self.allocSize;
45+
assert(gop.found_existing == false, "the node({}: 0x{x}, {d}) is already in the auto free nodes", .{ node.pgid, ptr, node.id });
7446
}
7547

7648
/// Deinit the auto free object.
@@ -80,14 +52,10 @@ pub const AutoFreeObject = struct {
8052
{
8153
var it = self.autoFreeNodes.keyIterator();
8254
while (it.next()) |node| {
83-
const ptr = @intFromPtr(node);
8455
node.*.deinit();
85-
self.freePtrs.put(ptr, 1) catch unreachable;
8656
}
8757
self.autoFreeNodes.deinit();
8858
}
89-
90-
self.freePtrs.deinit();
9159
}
9260
};
9361

src/cursor.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pub const Cursor = struct {
372372
// // _ = printNodes;
373373
// printNodes(n);
374374
assert(n.inodes.items.len > 0, "the node is empty", .{});
375-
var indexRef = n.searchInodes2(key);
375+
var indexRef = n.searchInodes(key);
376376
if (!indexRef.exact) {
377377
indexRef.index -= 1;
378378
}

src/node.zig

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub const Node = struct {
253253
pub fn del(self: *Self, key: []const u8) ?usize {
254254
// std.log.debug("del key: {any} at node: {d}", .{ key, self.pgid });
255255
// Find index of key.
256-
const indexRef = self.searchInodes2(key);
256+
const indexRef = self.searchInodes(key);
257257
var inode = self.inodes.orderedRemove(indexRef.index);
258258
assert(indexRef.exact, "the key is not found, key: {s}, index: {d}, node len: {d}, node key: {s}", .{ key, indexRef.index, self.inodes.items.len, inode.key.? });
259259
// free the inode
@@ -431,15 +431,14 @@ pub const Node = struct {
431431
self.parent = Node.init(self.getAllocator());
432432
self.parent.?.bucket = self.bucket;
433433
self.parent.?.children.append(self) catch unreachable; // children also is you!
434-
self.bucket.?.tx.?.autoFreeNodes.?.addNode(self.parent.?);
434+
// self.bucket.?.tx.?.autoFreeNodes.?.addNode(self.parent.?);
435435
}
436436

437437
// Create a new node and add it to the parent.
438438
const next = Node.init(self.bucket.?.getAllocator());
439439
next.bucket = self.bucket;
440440
next.isLeaf = self.isLeaf;
441441
next.parent = self.parent;
442-
// TODO: maybe here is a bug
443442
self.parent.?.children.append(next) catch unreachable;
444443
next.inodes.ensureTotalCapacity(self.inodes.items.len - _splitIndex) catch unreachable;
445444
next.inodes.appendSlice(self.inodes.items[_splitIndex..]) catch unreachable;
@@ -506,20 +505,18 @@ pub const Node = struct {
506505
// Spill child nodes first. Child nodes can materialize sibling nodes in
507506
// the case of split-merge so we cannot use a range loop. We have to check
508507
// the children size on every loop iteration.
509-
const lessFn = struct {
510-
fn less(_: void, a: *Node, b: *Node) bool {
511-
return std.mem.order(u8, a.key.?, b.key.?) == .lt;
512-
}
513-
}.less;
514508
std.mem.sort(
515509
*Node,
516510
self.children.items,
517511
{},
518-
lessFn,
512+
struct {
513+
fn lessFn(_: void, a: *Node, b: *Node) bool {
514+
return std.mem.order(u8, a.key.?, b.key.?) == .lt;
515+
}
516+
}.lessFn,
519517
);
520518
for (0..self.children.items.len) |i| {
521519
const child = self.children.items[i];
522-
// std.log.debug("spill child node: {d}, index: {d}", .{ child.pgid, i });
523520
try child.spill();
524521
}
525522
// We no longer need the children list because it's only used for spilling tracking.
@@ -530,11 +527,9 @@ pub const Node = struct {
530527
defer nodes.deinit();
531528
// log.debug("pgid: {d}, nodeid: 0x{x}, nodes size: {d}, key: {s}", .{ self.pgid, self.nodePtrInt(), nodes.items.len, self.key orelse "empty" });
532529
for (nodes.items) |node| {
533-
// log.debug("spill node: {d}, count:{}, index: {d}", .{ node.pgid, nodes.items.len, i });
534530
// Add node's page to the freelist if it's not new.
535531
// (it is the first one, because split node from left to right!)
536532
if (node.pgid > 0) {
537-
// log.debug("free a page to freelist, pgid: {}", .{node.pgid});
538533
try _db.freelist.free(_tx.meta.txid, _tx.getPage(node.pgid));
539534
// reset the pgid to 0, so the node will be a new node.
540535
node.pgid = 0;
@@ -562,21 +557,18 @@ pub const Node = struct {
562557
// Update the statistics.
563558
_tx.stats.spill += 1;
564559
}
565-
// self.safeCheck();
566560
// If the root node split and created a new root then we need to spill that
567561
// as well. We'll clear out the children to make sure it doesn't try to respill.
568562
if (self.parent != null and self.parent.?.pgid == 0) {
569563
self.children.clearAndFree();
570564
return self.parent.?.spill();
571565
}
572-
// log.debug("Try to spill parent, pgid: {d}", .{self.pgid});
573566
}
574567

575568
/// Attempts to combine the node with sibling nodes if the node fill
576569
/// size is below a threshold or if there are not enough keys.
577570
pub fn rebalance(self: *Self) void {
578571
if (!self.unbalance) {
579-
// std.log.debug("i has rebalance, pgid: {}", .{self.pgid});
580572
return;
581573
}
582574
self.unbalance = false;
@@ -588,7 +580,6 @@ pub const Node = struct {
588580
// Ignore if node is above threshold (25%) and has enough keys.
589581
const threshold = self.bucket.?.tx.?.db.?.pageSize / 4;
590582
if (self.size() > threshold and self.inodes.items.len > self.minKeys()) {
591-
// std.log.debug("the node size is too large, so don't rebalance: {d}", .{self.pgid});
592583
return;
593584
}
594585

@@ -629,10 +620,7 @@ pub const Node = struct {
629620
}
630621
// std.log.debug("nothing need to rebalance at root: {d}, key={s}, isLeaf: {}, inodes len: {d}", .{ self.pgid, self.key orelse "empty", self.isLeaf, self.inodes.items.len });
631622
return;
632-
} else {
633-
// std.log.debug("the node parent is not null, so rebalance: {d}, parent: {d}", .{ self.pgid, self.parent.?.pgid });
634623
}
635-
636624
// If node has no keys then just remove it.
637625
if (self.numChildren() == 0) {
638626
// remove self from parent reference.
@@ -643,13 +631,9 @@ pub const Node = struct {
643631
const exists = self.bucket.?.nodes.?.remove(self.pgid);
644632
assert(exists, "rebalance: node({d}) not found in nodes map", .{self.pgid});
645633
// free reference page to db.
646-
const oldPgid = self.pgid;
647634
self.free();
648635
// continue reblance parent.
649636
self.parent.?.rebalance();
650-
// destroy self.
651-
const key = if (self.key == null) "" else self.key.?;
652-
std.log.info("destroy self, key={any}, id: {d}, pgid: {d}, ptr: 0x{x}, keyPtr: 0x{x}, parentPtr: 0x{x}, parentInodesLen: {d}", .{ key, self.id, oldPgid, self.nodePtrInt(), @intFromPtr(self.key.?.ptr), self.parent.?.nodePtrInt(), self.parent.?.inodes.items.len });
653637
self.deinitAndDestroy();
654638
return;
655639
}
@@ -710,7 +694,6 @@ pub const Node = struct {
710694
fn removeChild(self: *Self, target: *Node) void {
711695
for (self.children.items, 0..) |child, i| {
712696
if (child == target) {
713-
// TODO mybey we should check the child is in the children list.
714697
const childNode = self.children.orderedRemove(i);
715698
assert(childNode.nodePtrInt() == target.nodePtrInt(), "the child is not in the children list", .{});
716699
return;
@@ -723,20 +706,17 @@ pub const Node = struct {
723706
pub fn dereference(self: *Self) void {
724707
if (self.key != null) {
725708
const cpKey = self.arenaAllocator.allocator().dupe(u8, self.key.?) catch unreachable;
726-
// self.allocator.free(self.key.?);
727709
self.key = cpKey;
728710
assert(self.pgid == 0 or self.key != null and self.key.?.len > 0, "deference: zero-length node key on existing node", .{});
729711
}
730712

731713
for (self.inodes.items) |*inode| {
732714
const newKey = self.arenaAllocator.allocator().dupe(u8, inode.key.?) catch unreachable;
733-
// self.allocator.free(inode.key.?);
734715
inode.key = newKey;
735716
assert(inode.key != null and inode.key.?.len > 0, "deference: zero-length inode key on existing node", .{});
736717
// If the value is not null
737718
if (inode.value) |value| {
738719
const newValue = self.arenaAllocator.allocator().dupe(u8, value) catch unreachable;
739-
// self.allocator.free(value);
740720
inode.value = newValue;
741721
assert(inode.value != null and inode.value.?.len > 0, "deference: zero-length inode value on existing node", .{});
742722
}
@@ -766,64 +746,13 @@ pub const Node = struct {
766746

767747
/// get the allocator of the node
768748
pub fn getAllocator(self: *Self) std.mem.Allocator {
769-
// std.log.err("arena allocator capacity: {d}", .{self.arenaAllocator.queryCapacity()});
770749
return self.arenaAllocator.allocator();
771750
}
772751

773-
/// binary search the key in the inodes
774-
pub fn binarySearchInodes(self: *const Self, key: []const u8) ?usize {
775-
const findFn = struct {
776-
fn find(context: []const u8, item: INode) std.math.Order {
777-
return std.mem.order(u8, context, item.key.?);
778-
}
779-
}.find;
780-
return std.sort.binarySearch(INode, self.inodes.items, key, findFn);
781-
}
782-
783-
/// Returns the index of the first element in `items` that is greater than or equal to `context`,
784-
/// if no such element exists, returns `items.len`.
785-
pub fn lowerBoundInodes(self: *const Self, key: []const u8) usize {
786-
const lowerBoundFn = struct {
787-
fn lower(context: []const u8, item: INode) std.math.Order {
788-
return std.mem.order(u8, context, item.key.?);
789-
}
790-
}.lower;
791-
return std.sort.lowerBound(INode, self.inodes.items, key, lowerBoundFn);
792-
}
793-
794-
/// Returns the index of the first element in `items` that is greater than `context`,
795-
/// if no such element exists, returns `items.len`.
796-
pub fn upperBoundInodes(self: *const Self, key: []const u8) usize {
797-
const upperBoundFn = struct {
798-
fn upper(context: []const u8, item: INode) std.math.Order {
799-
return std.mem.order(u8, context, item.key.?);
800-
}
801-
}.upper;
802-
return std.sort.upperBound(INode, self.inodes.items, key, upperBoundFn);
803-
}
804-
805752
/// search the key in the inodes, if found, return the index and exact, if not found, return the position of the first element that is greater than the key
806753
pub fn searchInodes(self: *const Self, key: []const u8) struct { index: usize, exact: bool } {
807754
var left: usize = 0;
808755
var right: usize = self.inodes.items.len;
809-
while (left < right) {
810-
const mid = left + (right - left) / 2;
811-
const element = self.inodes.items[mid];
812-
const cmp = std.mem.order(u8, element.key.?, key);
813-
switch (cmp) {
814-
.eq => return .{ .index = mid, .exact = true },
815-
.lt => left = mid + 1,
816-
.gt => right = mid,
817-
}
818-
}
819-
return .{ .index = left, .exact = false };
820-
}
821-
822-
/// search the key in the inodes, if found, return the index and exact, if not found, return the position of the first element that is greater than the key
823-
pub fn searchInodes2(self: *const Self, key: []const u8) struct { index: usize, exact: bool } {
824-
var left: usize = 0;
825-
var right: usize = self.inodes.items.len;
826-
827756
while (left < right) {
828757
const mid = left + (right - left) / 2;
829758
const cmp = std.mem.order(u8, key, self.inodes.items[mid].key.?);

src/root.zig

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
const std = @import("std");
2-
const testing = std.testing;
3-
4-
export fn add(a: i32, b: i32) i32 {
5-
return a + b;
6-
}
7-
8-
test "basic add functionality" {
9-
std.testing.log_level = .debug;
10-
std.log.warn("run test", .{});
11-
try testing.expect(add(3, 7) == 10);
12-
}
13-
14-
test {
15-
_ = @import("cursor_test.zig");
16-
_ = @import("node_test.zig");
17-
_ = @import("bucket_test.zig");
18-
_ = @import("tx_test.zig");
19-
_ = @import("page.zig");
20-
}
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
export fn add(a: i32, b: i32) i32 {
5+
return a + b;
6+
}
7+
8+
test "basic add functionality" {
9+
std.testing.log_level = .debug;
10+
std.log.warn("run test", .{});
11+
try testing.expect(add(3, 7) == 10);
12+
}
13+
14+
test {
15+
_ = @import("cursor_test.zig");
16+
_ = @import("node_test.zig");
17+
_ = @import("bucket_test.zig");
18+
_ = @import("tx_test.zig");
19+
_ = @import("page.zig");
20+
}

0 commit comments

Comments
 (0)