Skip to content

Commit

Permalink
radixdb: add node.findCompatibleChild()
Browse files Browse the repository at this point in the history
  • Loading branch information
toru committed Oct 4, 2024
1 parent 7097efd commit 1689a3d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions radixdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ func (rdb *RadixDB) Len() uint64 {
return rdb.numNodes
}

// findCompatibleChild searches through the child nodes of the receiver node.
// It returns the first child node that shares a common prefix. If no child is
// found, the function returns nil.
func (node node) findCompatibleChild(key []byte) *node {
for _, child := range node.children {
prefix := longestCommonPrefix(child.key, key)

if len(prefix) > 0 {
return child
}
}

return nil
}

// longestCommonPrefix compares the two given byte slices, and returns the
// longest common prefix. It ensures memory-safety by establishing an index
// boundary based on the length of the shorter byte slice.
Expand Down
29 changes: 29 additions & 0 deletions radixdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,32 @@ func TestLongestCommonPrefix(t *testing.T) {
}
}
}

func TestFindCompatibleChild(t *testing.T) {
root := &node{
children: []*node{
{key: []byte("apple")},
{key: []byte("banana")},
{key: []byte("citron")},
},
}

tests := []struct {
key []byte
expected []byte
}{
{[]byte("apple"), []byte("apple")},
{[]byte("applet"), []byte("apple")},
{[]byte("bandage"), []byte("banana")},
{[]byte("coconut"), []byte("citron")},
{[]byte("durian"), nil},
{[]byte("orange"), nil},
}

for _, test := range tests {
child := root.findCompatibleChild([]byte(test.key))
if (child == nil && test.expected != nil) || (child != nil && !bytes.Equal(child.key, test.expected)) {
t.Errorf("findCompatibleChild(%q): got:%q, want:%q", test.key, child.key, test.expected)
}
}
}

0 comments on commit 1689a3d

Please sign in to comment.