Skip to content

Commit

Permalink
radixdb: attach fresh prefix node to current
Browse files Browse the repository at this point in the history
Previously, nodes with newly discovered prefixes were attached to the
parent of the current node, which is incorrect. Instead, these nodes
should be attached to the current node. This commit also fixes an
incorrect suffix extraction from newNode.
  • Loading branch information
toru committed Oct 5, 2024
1 parent 7fdb4b3 commit e43ec34
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions radixdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ func (rdb *RadixDB) Insert(key []byte, value any) error {
// at the deepest level of the tree for the given key.
key = key[len(prefix):]
nextNode := current.findCompatibleChild(key)
newNode.key = newNode.key[len(prefix):]

if nextNode == nil {
newNode.key = key

if current == rdb.root {
// A root node with nil key means that it's an intermediate node
// with existing edges to child nodes.
Expand All @@ -142,7 +141,7 @@ func (rdb *RadixDB) Insert(key []byte, value any) error {
rdb.numNodes++
return nil
} else {
parent.children = append(parent.children, newNode)
current.children = append(current.children, newNode)
}

rdb.numNodes++
Expand All @@ -152,7 +151,6 @@ func (rdb *RadixDB) Insert(key []byte, value any) error {
// Reaching this point means that a compatible child was found.
// Update relevant iterators and continue traversing the tree until
// we reach a leaf node or no further nodes are available.
newNode.key = newNode.key[len(prefix):]
parent = current
current = nextNode
}
Expand Down

0 comments on commit e43ec34

Please sign in to comment.