@@ -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 .? );
0 commit comments