Skip to content

Commit 5bc422c

Browse files
committed
logs
1 parent 290e421 commit 5bc422c

File tree

6 files changed

+208
-169
lines changed

6 files changed

+208
-169
lines changed

secretary/errors.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,27 @@ var (
3434
ErrorInvalidBatchIncrement = errors.New("Batch Increment must be between 110 and 200")
3535
ErrorInvalidCollectionName = errors.New("Collection name is not valid, should be a-z 0-9 and with >4 & <30 characters")
3636

37+
// File I/O
3738
ErrorFileNotAligned = func(fileInfo os.FileInfo) error {
3839
return fmt.Errorf("Error : File %s not aligned", fileInfo.Name())
3940
}
40-
4141
ErrorReadingDataAtOffset = func(offset int64, err error) error {
4242
return fmt.Errorf("Error reading data at offset %d: %v", offset, err)
4343
}
44-
4544
ErrorWritingDataAtOffset = func(offset int64, err error) error {
4645
return fmt.Errorf("Error writing data at offset %d: %v", offset, err)
4746
}
48-
4947
ErrorAllocatingBatch = func(err error) error {
5048
return fmt.Errorf("Error allocating batch: %v", err)
5149
}
52-
5350
ErrorFileStat = func(err error) error {
5451
return fmt.Errorf("Error file stat: %v", err)
5552
}
56-
5753
ErrorDataExceedBatchSize = func(len int, batchSize uint32, offset int64) error {
5854
return fmt.Errorf("Error: Data size %d exceeds batch size %d at offset %d", len, batchSize, offset)
5955
}
6056

57+
// Pointer links
6158
ErrorParentNotKnowChild = func(child *Node) error {
6259
return fmt.Errorf("Parent[%d] doesnt know Child[%d]", child.parent.NodeID, child.NodeID)
6360
}

secretary/node.go

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ func (tree *BTree) buildSortedLeafNodes(sortedRecords []*Record) []*Node {
410410

411411
// Recursively build internal nodes
412412
func (tree *BTree) buildInternalNodes(children []*Node) *Node {
413-
fmt.Println(len(children))
414413
if len(children) == 1 {
415414
return children[0] // Root node
416415
}
@@ -597,7 +596,7 @@ func (tree *BTree) Delete(key []byte) error {
597596
leaf.records = append(leaf.records[:index], leaf.records[index+1:]...)
598597
// leaf.NumKeys--
599598

600-
fmt.Print("\nDeleting ", string(key), " index:", index, " found:", found, "\n")
599+
utils.Log(map[string]any{"Deleting": string(key), "index": index, "found": found})
601600

602601
// Handle underflow
603602
tree.handleUnderflow(leaf)
@@ -614,7 +613,7 @@ func (tree *BTree) handleUnderflow(node *Node) {
614613
return // No underflow
615614
}
616615

617-
fmt.Println("handleUnderflow", node.NodeID, utils.ArrayToStrings(node.Keys))
616+
utils.Log(map[string]any{"handleUnderflow": node.NodeID, "node.keys": utils.ArrayToStrings(node.Keys)})
618617

619618
// Check if the node is the root
620619
if node == tree.root {
@@ -638,14 +637,24 @@ func (tree *BTree) handleUnderflow(node *Node) {
638637
// leftSibling := node.prev
639638
leftSibling := parent.children[pos-1]
640639

641-
fmt.Printf("**** Try to borrow from leftSibling(%d) len(leftSibling.Keys)[%d] > minKeys[%d] %v\n", leftSibling.NodeID, len(leftSibling.Keys), minKeys, len(leftSibling.Keys) > minKeys)
640+
utils.Log(map[string]any{
641+
"**** Try to borrow from leftSibling": leftSibling.NodeID,
642+
"len(leftSibling.Keys)": len(leftSibling.Keys),
643+
"minKeys": minKeys,
644+
"len(leftSibling.Keys) > minKeys)": len(leftSibling.Keys) > minKeys,
645+
})
642646

643647
if len(leftSibling.Keys) > minKeys {
644648
// Borrow key from left sibling
645649
blen := len(leftSibling.Keys) - 1
646650
borrowedKey := leftSibling.Keys[blen]
647651
leftSibling.Keys = leftSibling.Keys[:blen]
648-
fmt.Printf("**** Borrow from leftSibling[%d] -> leftSibling.Keys %v : BorrowedKey : %s\n", leftSibling.NodeID, utils.ArrayToStrings(leftSibling.Keys), string(borrowedKey))
652+
653+
utils.Log(map[string]any{
654+
"**** Borrow from leftSibling": leftSibling.NodeID,
655+
"leftSibling.Keys": utils.ArrayToStrings(leftSibling.Keys),
656+
"borrowedKey": string(borrowedKey),
657+
})
649658

650659
node.Keys = append([][]byte{borrowedKey}, node.Keys...)
651660

@@ -654,14 +663,23 @@ func (tree *BTree) handleUnderflow(node *Node) {
654663
if leftSibling.children == nil {
655664
rlen := len(leftSibling.records) - 1
656665
borrowedRecord := leftSibling.records[rlen]
657-
fmt.Println("BorrowedRecord : ", rlen, string(borrowedKey), string(borrowedRecord.Value))
666+
667+
utils.Log(map[string]any{
668+
"BorrowedRecord": rlen,
669+
"string(borrowedKey)": string(borrowedKey),
670+
"string(borrowedRecord.Value)": string(borrowedRecord.Value),
671+
})
658672

659673
leftSibling.records = leftSibling.records[:rlen]
660674
node.records = append([]*Record{borrowedRecord}, node.records...)
661675
} else {
662676
clen := len(leftSibling.children) - 1
663677
borrowedChild := leftSibling.children[clen]
664-
fmt.Println("BorrowedChild : ", clen, string(borrowedKey))
678+
679+
utils.Log(map[string]any{
680+
"BorrowedChild": borrowedChild.NodeID,
681+
"borrowedKey": string(borrowedKey),
682+
})
665683

666684
leftSibling.children = leftSibling.children[:clen]
667685
node.children = append([]*Node{borrowedChild}, node.children...)
@@ -674,7 +692,11 @@ func (tree *BTree) handleUnderflow(node *Node) {
674692
// recursiveFixInternalNodeChildLinks(parent)
675693
recursiveFixInternalNodeChildLinks(node)
676694

677-
fmt.Println("Pos:", pos, "BorrowedKey:", string(borrowedKey), " parent.keys", utils.ArrayToStrings(parent.Keys))
695+
utils.Log(map[string]any{
696+
"Pos": pos,
697+
"BorrowedKey": string(borrowedKey),
698+
"parent.keys": utils.ArrayToStrings(parent.Keys),
699+
})
678700

679701
return
680702
}
@@ -685,14 +707,24 @@ func (tree *BTree) handleUnderflow(node *Node) {
685707
rightSibling := parent.children[pos+1]
686708
// rightSibling := node.next
687709

688-
fmt.Printf("**** Try to borrow from rightSibling(%d) len(rightSibling.Keys)[%d] > minKeys[%d] %v\n", rightSibling.NodeID, len(rightSibling.Keys), minKeys, len(rightSibling.Keys) > minKeys)
710+
utils.Log(map[string]any{
711+
"**** Try to borrow from rightSibling": rightSibling.NodeID,
712+
"len(rightSibling.Keys)": len(rightSibling.Keys),
713+
"minKeys": minKeys,
714+
"len(rightSibling.Keys) > minKeys": len(rightSibling.Keys) > minKeys,
715+
})
689716

690717
if len(rightSibling.Keys) > minKeys {
691718
// Borrow key from right sibling
692719

693720
borrowedKey := rightSibling.Keys[0]
694721
rightSibling.Keys = rightSibling.Keys[1:]
695-
fmt.Printf("**** Borrow from rightSibling[%d] -> rightSibling.Keys : %v BorrowedKey : %s\n", rightSibling.NodeID, utils.ArrayToStrings(rightSibling.Keys), string(borrowedKey))
722+
723+
utils.Log(map[string]any{
724+
"**** Borrow from rightSibling ": rightSibling.NodeID,
725+
"rightSibling.Keys": utils.ArrayToStrings(rightSibling.Keys),
726+
"BorrowedKey": string(borrowedKey),
727+
})
696728

697729
node.Keys = append(node.Keys, borrowedKey)
698730

@@ -701,13 +733,15 @@ func (tree *BTree) handleUnderflow(node *Node) {
701733

702734
if rightSibling.children == nil {
703735
borrowedRecord := rightSibling.records[0]
704-
fmt.Println("BorrowedRecord : ", string(borrowedKey), string(borrowedRecord.Value))
736+
737+
utils.Log(map[string]any{"BorrowedRecord": borrowedRecord})
705738

706739
rightSibling.records = rightSibling.records[1:]
707740
node.records = append(node.records, borrowedRecord)
708741
} else {
709742
borrowedChild := rightSibling.children[0]
710-
fmt.Println("BorrowedChild : ", string(borrowedKey))
743+
744+
utils.Log(map[string]any{"BorrowedChild": borrowedChild})
711745

712746
rightSibling.children = rightSibling.children[1:]
713747
node.children = append(node.children, borrowedChild)
@@ -720,7 +754,11 @@ func (tree *BTree) handleUnderflow(node *Node) {
720754
// recursiveFixInternalNodeChildLinks(parent)
721755
recursiveFixInternalNodeChildLinks(node)
722756

723-
fmt.Println("Pos:", pos, "BorrowedKey:", string(borrowedKey), " parent.keys", utils.ArrayToStrings(parent.Keys))
757+
utils.Log(map[string]any{
758+
"Pos:": pos,
759+
"BorrowedKey:": string(borrowedKey),
760+
"parent.keys": utils.ArrayToStrings(parent.Keys),
761+
})
724762

725763
return
726764
}
@@ -731,7 +769,15 @@ func (tree *BTree) handleUnderflow(node *Node) {
731769
leftSibling := parent.children[pos-1]
732770
// leftSibling := node.prev
733771

734-
fmt.Println("**** Merge with left sibling -> Pos:", pos, " Parent ", parent.NodeID, " parent.keys", utils.ArrayToStrings(parent.Keys), " NodeID ", node.NodeID, utils.ArrayToStrings(node.Keys), " leftSiblingID ", leftSibling.NodeID, utils.ArrayToStrings(leftSibling.Keys))
772+
utils.Log(map[string]any{
773+
"**** Merge with left sibling -> Pos": pos,
774+
"Parent": parent.NodeID,
775+
"parent.keys": utils.ArrayToStrings(parent.Keys),
776+
"NodeID ": node.NodeID,
777+
"node.Keys": utils.ArrayToStrings(node.Keys),
778+
"leftSiblingID": leftSibling.NodeID,
779+
"leftSibling.Keys": utils.ArrayToStrings(leftSibling.Keys),
780+
})
735781

736782
leftSibling.Keys = append(leftSibling.Keys, node.Keys...)
737783

@@ -751,7 +797,15 @@ func (tree *BTree) handleUnderflow(node *Node) {
751797
node.next.prev = leftSibling
752798
}
753799

754-
fmt.Println("**** Merge with left sibling -> Pos:", pos, " Parent ", parent.NodeID, " parent.keys", utils.ArrayToStrings(parent.Keys), " NodeID ", node.NodeID, utils.ArrayToStrings(node.Keys), " leftSiblingID ", leftSibling.NodeID, utils.ArrayToStrings(leftSibling.Keys))
800+
utils.Log(map[string]any{
801+
"**** Merge with left sibling -> Pos": pos,
802+
"Parent": parent.NodeID,
803+
"parent.keys": utils.ArrayToStrings(parent.Keys),
804+
"NodeID ": node.NodeID,
805+
"node.Keys": utils.ArrayToStrings(node.Keys),
806+
"leftSiblingID": leftSibling.NodeID,
807+
"leftSibling.Keys": utils.ArrayToStrings(leftSibling.Keys),
808+
})
755809

756810
// recursiveFixInternalNodeChildLinks(parent)
757811
recursiveFixInternalNodeChildLinks(leftSibling)
@@ -763,7 +817,15 @@ func (tree *BTree) handleUnderflow(node *Node) {
763817
rightSibling := parent.children[pos+1]
764818
// rightSibling := node.next
765819

766-
fmt.Println("**** Merge right sibling -> Pos:", pos, " Parent ", parent.NodeID, " parent.keys", utils.ArrayToStrings(parent.Keys), " NodeID ", node.NodeID, utils.ArrayToStrings(node.Keys), " rightSiblingID ", rightSibling.NodeID, utils.ArrayToStrings(rightSibling.Keys))
820+
utils.Log(map[string]any{
821+
"**** Merge right sibling -> Pos": pos,
822+
"Parent": parent.NodeID,
823+
"parent.keys": utils.ArrayToStrings(parent.Keys),
824+
"NodeID": node.NodeID,
825+
"node.Keys": utils.ArrayToStrings(node.Keys),
826+
"rightSiblingID": rightSibling.NodeID,
827+
"rightSibling.Keys": utils.ArrayToStrings(rightSibling.Keys),
828+
})
767829

768830
node.Keys = append(node.Keys, rightSibling.Keys...)
769831

@@ -783,7 +845,15 @@ func (tree *BTree) handleUnderflow(node *Node) {
783845
rightSibling.next.prev = rightSibling.prev
784846
}
785847

786-
fmt.Println("**** Merge right sibling -> Pos:", pos, " Parent ", parent.NodeID, " parent.keys", utils.ArrayToStrings(parent.Keys), " NodeID ", node.NodeID, utils.ArrayToStrings(node.Keys), " rightSiblingID ", rightSibling.NodeID, utils.ArrayToStrings(rightSibling.Keys))
848+
utils.Log(map[string]any{
849+
"**** Merge right sibling -> Pos": pos,
850+
"Parent": parent.NodeID,
851+
"parent.keys": utils.ArrayToStrings(parent.Keys),
852+
"NodeID": node.NodeID,
853+
"node.Keys": utils.ArrayToStrings(node.Keys),
854+
"rightSiblingID": rightSibling.NodeID,
855+
"rightSibling.Keys": utils.ArrayToStrings(rightSibling.Keys),
856+
})
787857

788858
// recursiveFixInternalNodeChildLinks(parent)
789859
recursiveFixInternalNodeChildLinks(node)
@@ -802,7 +872,12 @@ func fixInternalNodeChildLinks(node *Node) {
802872
}
803873
child.parent = node
804874
}
805-
fmt.Println("FixInternalNodeChildLinks", node.NodeID, "len(parent.children)", len(node.children), utils.ArrayToStrings(node.Keys))
875+
876+
utils.Log(map[string]any{
877+
"FixInternalNodeChildLinks": node.NodeID,
878+
"len(node.children)": len(node.children),
879+
"node.keys": utils.ArrayToStrings(node.Keys),
880+
})
806881
}
807882
}
808883

@@ -908,7 +983,7 @@ func (tree *BTree) SerializeNodeJSON(node *Node, height int) ([]byte, error) {
908983

909984
func (tree *BTree) PrintNode(node *Node, height int) {
910985
t, err := tree.SerializeNodeJSON(node, height)
911-
fmt.Println(string(t), err)
986+
utils.Log(string(t), err)
912987
}
913988

914989
// SerializeTreeJSON converts the entire BTree into a JSON structure

0 commit comments

Comments
 (0)