Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
230 commits
Select commit Hold shift + click to select a range
f684e66
create develop :dog:
laohanlinux May 14, 2023
00713d2
add node.zig :dog:
laohanlinux May 14, 2023
6034a51
add node.zig :dog:
laohanlinux Jun 17, 2023
05996da
add node.zig :dog:
laohanlinux Jun 17, 2023
66ff65d
laohanlinux May 12, 2024
f039548
:cat:
May 13, 2024
dd44d34
:volcano:
laohanlinux May 13, 2024
6d36671
:cat:
May 14, 2024
11e5b4f
:cat:
May 14, 2024
1ec0d6d
:cat:
May 14, 2024
879ace3
:dog:
Jun 13, 2024
9496184
:dog:
Jun 13, 2024
b602fab
:dog:
Jun 13, 2024
5a9cae9
:dog: duck
Jun 13, 2024
5df9e8b
:dog:
Jun 13, 2024
0b27b55
:dog:
Jun 13, 2024
73e7e9c
:cat:
laohanlinux Jun 13, 2024
3754287
:dog:
Jun 14, 2024
74286a4
:cat:
laohanlinux Jun 16, 2024
1c57de8
:dog:
Jun 17, 2024
da0273f
TO_AQUAMAN Opz code
Jun 18, 2024
7099b14
:cat:
laohanlinux Jun 18, 2024
52f42ed
:dog:
Jun 19, 2024
81f4dbe
:cat:
Jun 21, 2024
c8d2ba9
:cat:
laohanlinux Jun 24, 2024
f80f0d1
:cat:
Jun 25, 2024
b40e8fa
:cat:
Jun 26, 2024
f314ec2
:cat:
Jun 26, 2024
0b126d7
:cat:
Jul 1, 2024
fcbfd64
:cat:
laohanlinux Jul 1, 2024
56962d0
:cat:
Jul 5, 2024
eeda27d
:cat:
laohanlinux Jul 7, 2024
3be346b
:cat:
laohanlinux Jul 8, 2024
fb90727
:cat
laohanlinux Jul 10, 2024
3fea77e
:cat:
Jul 11, 2024
bcfb47e
:cat:
laohanlinux Jul 11, 2024
06e4e1b
:cat:
laohanlinux Jul 14, 2024
86583da
:dog:
Jul 15, 2024
296803e
:dog:
Jul 16, 2024
0f8c090
:dog:
Jul 17, 2024
a4f960c
:dog:
Jul 18, 2024
c3b767d
:cat:
laohanlinux Jul 18, 2024
ee2f741
:cat:
laohanlinux Jul 18, 2024
95a514c
:dog:
Jul 19, 2024
dab8223
:cat:
laohanlinux Jul 21, 2024
5168741
:cat:
Jul 23, 2024
7ac424d
:car:
laohanlinux Jul 23, 2024
bc42c53
:cat:
Jul 24, 2024
df47dd0
:tiger:
laohanlinux Jul 24, 2024
b082a23
:dog:
Jul 25, 2024
2a50bae
:dog:
Jul 25, 2024
4648d1a
:dog:
Jul 26, 2024
c189615
:dog:
Jul 29, 2024
c69309f
:cat:
laohanlinux Jul 29, 2024
e373dfc
:cat:
Jul 30, 2024
aa4384e
:cat:
laohanlinux Jul 30, 2024
8ce4406
:cat:
Jul 31, 2024
2e65489
:dog:
Aug 1, 2024
6ff6bd5
:cat:
laohanlinux Aug 1, 2024
3dae50b
:cat:
Aug 4, 2024
e1f6b00
:cat:
laohanlinux Aug 5, 2024
4d0a91f
:dog:
Aug 8, 2024
7b23e99
:dog:
laohanlinux Aug 8, 2024
2826590
:dog:
Aug 9, 2024
fea276d
:dog:
Aug 12, 2024
87f3d5f
:cat:
Aug 12, 2024
01805df
:car:
laohanlinux Aug 12, 2024
b5384f7
:dog:
Aug 13, 2024
d515c87
:dog:
laohanlinux Aug 13, 2024
5626a28
:dog:
Aug 14, 2024
5887e1c
:dog:
laohanlinux Aug 15, 2024
711f2cb
:dog:
laohanlinux Aug 15, 2024
d26a5f2
:cat:
laohanlinux Aug 16, 2024
7a6e801
:dog:
laohanlinux Aug 16, 2024
d836539
:dog:
laohanlinux Aug 18, 2024
1fdeaaf
:dog:
laohanlinux Aug 19, 2024
2f9d031
:dog:
laohanlinux Aug 19, 2024
d6e8b46
:cat:
laohanlinux Aug 20, 2024
218de40
:dog:
laohanlinux Aug 21, 2024
527520f
:dog:
laohanlinux Aug 22, 2024
d4da787
:dog:
laohanlinux Sep 2, 2024
47b8924
:dog:
laohanlinux Sep 2, 2024
0006907
:cat:
laohanlinux Sep 3, 2024
de5e986
:dog: :dog:
laohanlinux Sep 3, 2024
2806814
:dog:
laohanlinux Sep 3, 2024
958a9c1
add BufStr
laohanlinux Sep 4, 2024
a285a55
:cat:
laohanlinux Sep 4, 2024
0ee7952
:dog:
laohanlinux Sep 4, 2024
4c7d4d1
:fish:
laohanlinux Sep 6, 2024
5a0259f
:cat: :fish:
laohanlinux Sep 7, 2024
183a3fc
:fish:
laohanlinux Sep 7, 2024
4071c68
:dog:
laohanlinux Sep 9, 2024
36e7cea
:cat:
laohanlinux Sep 9, 2024
1b2c709
:dog:
laohanlinux Sep 19, 2024
d156e23
:fix
laohanlinux Sep 19, 2024
bdee879
:cat:
laohanlinux Sep 23, 2024
e181d9d
:cat:
laohanlinux Sep 23, 2024
0858a24
:dog:
laohanlinux Sep 25, 2024
8370d60
:cat:
laohanlinux Sep 25, 2024
07b9521
:cat:
laohanlinux Sep 26, 2024
3d498dc
:disappointed:
laohanlinux Sep 26, 2024
18970d0
:dog:
laohanlinux Sep 27, 2024
ccdb664
:cat:
laohanlinux Sep 28, 2024
6a8f939
Code ..
laohanlinux Sep 29, 2024
d322b99
Code ..
laohanlinux Sep 29, 2024
528821c
Fix sort
laohanlinux Sep 29, 2024
c973ebe
Fix sort
laohanlinux Sep 29, 2024
02019d3
Opz code
laohanlinux Sep 29, 2024
07389c3
:cat:
laohanlinux Sep 29, 2024
c402774
:ab:
laohanlinux Sep 29, 2024
0ec42dc
:cat:
laohanlinux Sep 30, 2024
fe60d86
:cat:
laohanlinux Oct 3, 2024
a91eb51
:dog:
laohanlinux Oct 3, 2024
516712c
:dog:
laohanlinux Oct 3, 2024
88d85b2
:dog:
laohanlinux Oct 4, 2024
4f487b6
:dog:
laohanlinux Oct 4, 2024
03d89d0
:dog:
laohanlinux Oct 4, 2024
9701e1a
:dog:
laohanlinux Oct 5, 2024
1c4c345
:dog:
laohanlinux Oct 6, 2024
5623e9d
:dog:
laohanlinux Oct 6, 2024
8aac6a6
:dog:
laohanlinux Oct 8, 2024
6672506
:dog:
laohanlinux Oct 8, 2024
863c0b5
:cat:
laohanlinux Oct 8, 2024
250795b
:+1:
laohanlinux Oct 8, 2024
9bc83b6
:fish:
laohanlinux Oct 9, 2024
ff1cfd4
:dog:
laohanlinux Oct 9, 2024
49f8e5e
:dog:
laohanlinux Oct 10, 2024
6772428
:cat:
laohanlinux Oct 10, 2024
c7bfdf3
:bit:
laohanlinux Oct 10, 2024
58189ed
:dog:
laohanlinux Oct 10, 2024
50dd183
:cat:
laohanlinux Oct 11, 2024
4f992d8
:cat:
laohanlinux Oct 11, 2024
90723f8
:cat:
laohanlinux Oct 14, 2024
06a7c84
:cat:
laohanlinux Oct 14, 2024
72a7f45
:cat:
laohanlinux Oct 14, 2024
bdf32a1
:coffee:
laohanlinux Oct 14, 2024
84060e8
:dog:
laohanlinux Oct 15, 2024
a34efbf
:dog:
laohanlinux Oct 15, 2024
228b311
:cactus:
laohanlinux Oct 15, 2024
773dcd3
:dog:
laohanlinux Oct 16, 2024
127f3de
init
laohanlinux Oct 16, 2024
edbc6a4
init
laohanlinux Oct 16, 2024
108a6ea
:cow:
laohanlinux Oct 16, 2024
439cd8b
:cat:
laohanlinux Oct 17, 2024
fea450e
:football:
laohanlinux Oct 17, 2024
8a99fbf
:dog:
laohanlinux Oct 18, 2024
42d634b
:cat: add Cursor_QuickCheck
laohanlinux Oct 21, 2024
7b1a263
:cat: add Cursor_QuickCheck_Reverse
laohanlinux Oct 21, 2024
f7779f1
:cat: add Cursor_QuickCheck_BucketsOnly
laohanlinux Oct 21, 2024
deda2d8
:cat: add // Ensure that a Tx cursor can reverse iterate over subbuck…
laohanlinux Oct 21, 2024
7d014e6
:cat: add Cursor_QuickCheck_Reverse
laohanlinux Oct 21, 2024
8d4bd21
:cat: fix compiled
laohanlinux Oct 21, 2024
cd77886
update zig
laohanlinux Oct 21, 2024
e24f206
update zig
laohanlinux Oct 21, 2024
4926174
add test
laohanlinux Oct 21, 2024
a1b54c2
:cat: fix compiled
laohanlinux Oct 21, 2024
3fd3b64
Merge branch 'main' into develop
laohanlinux Oct 21, 2024
773cd88
Merge branch 'main' into develop
laohanlinux Oct 21, 2024
05f5c69
:+1:
laohanlinux Oct 21, 2024
8dfd3df
:dog:
laohanlinux Oct 22, 2024
6b06c97
:cat:
laohanlinux Oct 23, 2024
619e18c
init
laohanlinux Oct 24, 2024
e11f64a
:dog:
laohanlinux Oct 24, 2024
9b6f124
:dog:
laohanlinux Oct 25, 2024
fead59b
:dog: fix inline bucket root, Yes!!!
laohanlinux Oct 30, 2024
56d5318
:dog: ...
laohanlinux Oct 30, 2024
9df9d2d
:dog: fix inline bucket root, Yes!!!
laohanlinux Oct 31, 2024
b35ab38
:dog: fix inline bucket root, Yes!!!
laohanlinux Oct 31, 2024
42bd7a5
:dog: fix inline bucket root, Yes!!!
laohanlinux Oct 31, 2024
5aa8639
:dog:
laohanlinux Oct 31, 2024
796aa50
:dog:
laohanlinux Oct 31, 2024
aa124de
:dog:
laohanlinux Nov 1, 2024
c18a65f
:dog:
laohanlinux Nov 4, 2024
0b0816d
:fix: cache
laohanlinux Nov 4, 2024
9109ba1
:dog:
laohanlinux Nov 4, 2024
ef1642c
:dog:
laohanlinux Nov 4, 2024
996baf3
:dog:
laohanlinux Nov 4, 2024
96df3b0
:coffee:
laohanlinux Nov 6, 2024
df13ac1
add readme
laohanlinux Nov 8, 2024
6032bad
add readme
laohanlinux Nov 8, 2024
df56f26
:coffe:
laohanlinux Nov 8, 2024
c02deab
:coffe:
laohanlinux Nov 11, 2024
9a4fd34
fix mustCheck
laohanlinux Nov 11, 2024
5c71344
:hi:
laohanlinux Nov 12, 2024
6d3e38b
:hi:
laohanlinux Nov 13, 2024
12921c2
:hi: add log.zig
laohanlinux Nov 13, 2024
7ffa6f7
:hi:
laohanlinux Nov 14, 2024
02e80dc
add example
laohanlinux Nov 18, 2024
75fa455
add tx_test.zig
laohanlinux Nov 27, 2024
838ae24
:ok:
laohanlinux Nov 29, 2024
627f3fd
:cat:
laohanlinux Dec 4, 2024
914e922
:dog:
Dec 25, 2024
4415cf8
🐕
Dec 26, 2024
ddb71e7
🐟
Dec 26, 2024
0663e7c
:duck:
Jan 3, 2025
25a131e
:cat:
Jan 8, 2025
7579e56
👎
Jan 9, 2025
2fe83ee
:
Jan 9, 2025
53b357d
👎
Jan 9, 2025
b57e4db
:cat:
Jan 9, 2025
b64a6fc
:cat:
Jan 9, 2025
e36401a
:cat:
Jan 9, 2025
acd2034
:cat:
Jan 9, 2025
0df4c6e
:cat:
Jan 9, 2025
5f193dc
:cat:
Jan 9, 2025
0d7676b
:cat:
Jan 9, 2025
7b2da21
👎
Jan 13, 2025
4e8e5c6
🚗
Jan 13, 2025
f8a45db
Jan 13, 2025
007cfe7
:cat:
Jan 13, 2025
b6196e2
:cat:
Jan 13, 2025
e2211ea
:cat:
Jan 13, 2025
9c574d8
:cat:
Jan 13, 2025
91c3387
:cat:
Jan 13, 2025
a3d5ba2
:cat:
Jan 13, 2025
7b96ae7
:cat:
Jan 13, 2025
f2ae8cd
:cat:
Jan 14, 2025
7bc6e87
:cat:
Jan 14, 2025
18eb3b2
:cat:
Jan 14, 2025
03710e9
:cat:
Jan 14, 2025
9138c9b
:cat:
Jan 14, 2025
470c6d3
:cat:
Jan 14, 2025
9e3a77d
:cat:
Jan 14, 2025
cb737b0
stable
Jan 16, 2025
b70163c
stable
Jan 16, 2025
368c042
stable
Jan 16, 2025
8127e51
stable
Jan 16, 2025
750e862
Merge branch 'main' into align
Jan 16, 2025
3ec605e
👎
Jan 18, 2025
f15d12a
Opz code
Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
// internet connectivity.
.dependencies = .{
.@"boltdb-zig" = .{
.url = "git+https://github.com/laohanlinux/boltdb-zig.git/?ref=align#f2ae8cd9a8d4396fec3fbbed3adbd9a14e0fa1a3",
.hash = "12203cc7a802ae6cd01c6b856ff96ddfe26cb9612533ca116a0e30178d5251e173bd",
.url = "git+https://github.com/laohanlinux/boltdb-zig.git/?ref=align#3ec605eca6a9f85959f81900542fea59cc3eaef6",
.hash = "122056bd20aba7365380a500c315f4a03aa5ea2134685b11fa7e32e312344c28175c",
},
},
.paths = .{
Expand Down
34 changes: 1 addition & 33 deletions src/bucket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ const NodeSet = std.AutoHashMap(*Node, void);
// A set of aligned values that will be freed by the bucket.
pub const AutoFreeObject = struct {
isFreed: bool = false,
// A set of bytes that will be freed by the bucket.
autoFreeBytes: std.AutoHashMap(u64, []u8),
// A set of nodes that will be freed by the bucket.
// 1: Note, the bucket.nodes is not in the autoFreeObject, so we need to destroy it manually.
// But the bucket.rootNode is in the autoFreeObject, so we don't need to destroy it manually.
// Because the bucket.rootNode is a special node, it is the root node of the bucket.
// So, we need to destroy it manually.
// 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.
autoFreeNodes: NodeSet,
freePtrs: std.AutoArrayHashMap(u64, isize),
allocSize: usize = 0,

allocator: std.mem.Allocator,
Expand All @@ -37,40 +34,15 @@ pub const AutoFreeObject = struct {
return .{
.autoFreeNodes = NodeSet.init(allocator),
.allocator = allocator,
.freePtrs = std.AutoArrayHashMap(u64, isize).init(allocator),
.autoFreeBytes = std.AutoHashMap(u64, []u8).init(allocator),
};
}

/// Add a node to the auto free object.
pub fn addNode(self: *AutoFreeObject, node: *Node) void {
const key = node.key orelse "";
self.allocSize += node.size();
const gop = self.autoFreeNodes.getOrPut(node) catch unreachable;
const ptr = @intFromPtr(node);
// assert(gop.found_existing == false, "the node({}: 0x{x}, {d}) is already in the auto free nodes", .{ node.pgid, ptr, node.id });
if (gop.found_existing) {
std.log.debug("the node({s}, {}: 0x{x}, {d}) is already in the auto free nodes", .{ key, node.pgid, ptr, node.id });
}
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 });
}

/// Add a byte slice to the auto free object.
pub fn addAutoFreeBytes(self: *AutoFreeObject, value: []u8) void {
const ptr = @intFromPtr(value.ptr);
const got = self.autoFreeBytes.getOrPut(ptr) catch unreachable;
if (got.found_existing) {
// std.log.debug("the auto free bytes({}: 0x{x}) is already in the auto free bytes", .{ value.len, ptr });
} else {
got.value_ptr.* = value;
self.allocSize += value.len;
// std.log.info("add auto free bytes, size: {d}, ptr: 0x{x}, allocSize: {d}", .{ value.len, ptr, self.allocSize });
}
}

/// Get the alloc size.
pub fn getAllocSize(self: *AutoFreeObject) usize {
return self.allocSize;
assert(gop.found_existing == false, "the node({}: 0x{x}, {d}) is already in the auto free nodes", .{ node.pgid, ptr, node.id });
}

/// Deinit the auto free object.
Expand All @@ -80,14 +52,10 @@ pub const AutoFreeObject = struct {
{
var it = self.autoFreeNodes.keyIterator();
while (it.next()) |node| {
const ptr = @intFromPtr(node);
node.*.deinit();
self.freePtrs.put(ptr, 1) catch unreachable;
}
self.autoFreeNodes.deinit();
}

self.freePtrs.deinit();
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/cursor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ pub const Cursor = struct {
// // _ = printNodes;
// printNodes(n);
assert(n.inodes.items.len > 0, "the node is empty", .{});
var indexRef = n.searchInodes2(key);
var indexRef = n.searchInodes(key);
if (!indexRef.exact) {
indexRef.index -= 1;
}
Expand Down
85 changes: 7 additions & 78 deletions src/node.zig
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub const Node = struct {
pub fn del(self: *Self, key: []const u8) ?usize {
// std.log.debug("del key: {any} at node: {d}", .{ key, self.pgid });
// Find index of key.
const indexRef = self.searchInodes2(key);
const indexRef = self.searchInodes(key);
var inode = self.inodes.orderedRemove(indexRef.index);
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.? });
// free the inode
Expand Down Expand Up @@ -431,15 +431,14 @@ pub const Node = struct {
self.parent = Node.init(self.getAllocator());
self.parent.?.bucket = self.bucket;
self.parent.?.children.append(self) catch unreachable; // children also is you!
self.bucket.?.tx.?.autoFreeNodes.?.addNode(self.parent.?);
// self.bucket.?.tx.?.autoFreeNodes.?.addNode(self.parent.?);
}

// Create a new node and add it to the parent.
const next = Node.init(self.bucket.?.getAllocator());
next.bucket = self.bucket;
next.isLeaf = self.isLeaf;
next.parent = self.parent;
// TODO: maybe here is a bug
self.parent.?.children.append(next) catch unreachable;
next.inodes.ensureTotalCapacity(self.inodes.items.len - _splitIndex) catch unreachable;
next.inodes.appendSlice(self.inodes.items[_splitIndex..]) catch unreachable;
Expand Down Expand Up @@ -506,20 +505,18 @@ pub const Node = struct {
// Spill child nodes first. Child nodes can materialize sibling nodes in
// the case of split-merge so we cannot use a range loop. We have to check
// the children size on every loop iteration.
const lessFn = struct {
fn less(_: void, a: *Node, b: *Node) bool {
return std.mem.order(u8, a.key.?, b.key.?) == .lt;
}
}.less;
std.mem.sort(
*Node,
self.children.items,
{},
lessFn,
struct {
fn lessFn(_: void, a: *Node, b: *Node) bool {
return std.mem.order(u8, a.key.?, b.key.?) == .lt;
}
}.lessFn,
);
for (0..self.children.items.len) |i| {
const child = self.children.items[i];
// std.log.debug("spill child node: {d}, index: {d}", .{ child.pgid, i });
try child.spill();
}
// We no longer need the children list because it's only used for spilling tracking.
Expand All @@ -530,11 +527,9 @@ pub const Node = struct {
defer nodes.deinit();
// log.debug("pgid: {d}, nodeid: 0x{x}, nodes size: {d}, key: {s}", .{ self.pgid, self.nodePtrInt(), nodes.items.len, self.key orelse "empty" });
for (nodes.items) |node| {
// log.debug("spill node: {d}, count:{}, index: {d}", .{ node.pgid, nodes.items.len, i });
// Add node's page to the freelist if it's not new.
// (it is the first one, because split node from left to right!)
if (node.pgid > 0) {
// log.debug("free a page to freelist, pgid: {}", .{node.pgid});
try _db.freelist.free(_tx.meta.txid, _tx.getPage(node.pgid));
// reset the pgid to 0, so the node will be a new node.
node.pgid = 0;
Expand Down Expand Up @@ -562,21 +557,18 @@ pub const Node = struct {
// Update the statistics.
_tx.stats.spill += 1;
}
// self.safeCheck();
// If the root node split and created a new root then we need to spill that
// as well. We'll clear out the children to make sure it doesn't try to respill.
if (self.parent != null and self.parent.?.pgid == 0) {
self.children.clearAndFree();
return self.parent.?.spill();
}
// log.debug("Try to spill parent, pgid: {d}", .{self.pgid});
}

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

Expand Down Expand Up @@ -629,10 +620,7 @@ pub const Node = struct {
}
// 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 });
return;
} else {
// std.log.debug("the node parent is not null, so rebalance: {d}, parent: {d}", .{ self.pgid, self.parent.?.pgid });
}

// If node has no keys then just remove it.
if (self.numChildren() == 0) {
// remove self from parent reference.
Expand All @@ -643,13 +631,9 @@ pub const Node = struct {
const exists = self.bucket.?.nodes.?.remove(self.pgid);
assert(exists, "rebalance: node({d}) not found in nodes map", .{self.pgid});
// free reference page to db.
const oldPgid = self.pgid;
self.free();
// continue reblance parent.
self.parent.?.rebalance();
// destroy self.
const key = if (self.key == null) "" else self.key.?;
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 });
self.deinitAndDestroy();
return;
}
Expand Down Expand Up @@ -710,7 +694,6 @@ pub const Node = struct {
fn removeChild(self: *Self, target: *Node) void {
for (self.children.items, 0..) |child, i| {
if (child == target) {
// TODO mybey we should check the child is in the children list.
const childNode = self.children.orderedRemove(i);
assert(childNode.nodePtrInt() == target.nodePtrInt(), "the child is not in the children list", .{});
return;
Expand All @@ -723,20 +706,17 @@ pub const Node = struct {
pub fn dereference(self: *Self) void {
if (self.key != null) {
const cpKey = self.arenaAllocator.allocator().dupe(u8, self.key.?) catch unreachable;
// self.allocator.free(self.key.?);
self.key = cpKey;
assert(self.pgid == 0 or self.key != null and self.key.?.len > 0, "deference: zero-length node key on existing node", .{});
}

for (self.inodes.items) |*inode| {
const newKey = self.arenaAllocator.allocator().dupe(u8, inode.key.?) catch unreachable;
// self.allocator.free(inode.key.?);
inode.key = newKey;
assert(inode.key != null and inode.key.?.len > 0, "deference: zero-length inode key on existing node", .{});
// If the value is not null
if (inode.value) |value| {
const newValue = self.arenaAllocator.allocator().dupe(u8, value) catch unreachable;
// self.allocator.free(value);
inode.value = newValue;
assert(inode.value != null and inode.value.?.len > 0, "deference: zero-length inode value on existing node", .{});
}
Expand Down Expand Up @@ -766,64 +746,13 @@ pub const Node = struct {

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

/// binary search the key in the inodes
pub fn binarySearchInodes(self: *const Self, key: []const u8) ?usize {
const findFn = struct {
fn find(context: []const u8, item: INode) std.math.Order {
return std.mem.order(u8, context, item.key.?);
}
}.find;
return std.sort.binarySearch(INode, self.inodes.items, key, findFn);
}

/// Returns the index of the first element in `items` that is greater than or equal to `context`,
/// if no such element exists, returns `items.len`.
pub fn lowerBoundInodes(self: *const Self, key: []const u8) usize {
const lowerBoundFn = struct {
fn lower(context: []const u8, item: INode) std.math.Order {
return std.mem.order(u8, context, item.key.?);
}
}.lower;
return std.sort.lowerBound(INode, self.inodes.items, key, lowerBoundFn);
}

/// Returns the index of the first element in `items` that is greater than `context`,
/// if no such element exists, returns `items.len`.
pub fn upperBoundInodes(self: *const Self, key: []const u8) usize {
const upperBoundFn = struct {
fn upper(context: []const u8, item: INode) std.math.Order {
return std.mem.order(u8, context, item.key.?);
}
}.upper;
return std.sort.upperBound(INode, self.inodes.items, key, upperBoundFn);
}

/// 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
pub fn searchInodes(self: *const Self, key: []const u8) struct { index: usize, exact: bool } {
var left: usize = 0;
var right: usize = self.inodes.items.len;
while (left < right) {
const mid = left + (right - left) / 2;
const element = self.inodes.items[mid];
const cmp = std.mem.order(u8, element.key.?, key);
switch (cmp) {
.eq => return .{ .index = mid, .exact = true },
.lt => left = mid + 1,
.gt => right = mid,
}
}
return .{ .index = left, .exact = false };
}

/// 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
pub fn searchInodes2(self: *const Self, key: []const u8) struct { index: usize, exact: bool } {
var left: usize = 0;
var right: usize = self.inodes.items.len;

while (left < right) {
const mid = left + (right - left) / 2;
const cmp = std.mem.order(u8, key, self.inodes.items[mid].key.?);
Expand Down
40 changes: 20 additions & 20 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const std = @import("std");
const testing = std.testing;
export fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
std.testing.log_level = .debug;
std.log.warn("run test", .{});
try testing.expect(add(3, 7) == 10);
}
test {
_ = @import("cursor_test.zig");
_ = @import("node_test.zig");
_ = @import("bucket_test.zig");
_ = @import("tx_test.zig");
_ = @import("page.zig");
}
const std = @import("std");
const testing = std.testing;

export fn add(a: i32, b: i32) i32 {
return a + b;
}

test "basic add functionality" {
std.testing.log_level = .debug;
std.log.warn("run test", .{});
try testing.expect(add(3, 7) == 10);
}

test {
_ = @import("cursor_test.zig");
_ = @import("node_test.zig");
_ = @import("bucket_test.zig");
_ = @import("tx_test.zig");
_ = @import("page.zig");
}
Loading