Skip to content

Commit

Permalink
chore(lib/trie): insert inserts value byte slice
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Feb 15, 2022
1 parent f908e42 commit 2ed131b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 89 deletions.
65 changes: 35 additions & 30 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,22 +323,19 @@ func (t *Trie) Put(keyLE, value []byte) {
}

func (t *Trie) put(key, value []byte) {
nodeToInsert := &node.Leaf{
Value: value,
Generation: t.generation,
Dirty: true,
}
t.root = t.insert(t.root, key, nodeToInsert)
t.root = t.insert(t.root, key, value)
}

// insert attempts to insert a key with value into the trie
func (t *Trie) insert(parent Node, key []byte, value Node) (newParent Node) {
// TODO change value node to be value []byte?
value.SetGeneration(t.generation) // just in case it's not set by the caller.

// insert inserts a value in the trie at the key specified.
// It may create one or more new nodes or update an existing node.
func (t *Trie) insert(parent Node, key, value []byte) (newParent Node) {
if parent == nil {
value.SetKey(key)
return value
return &node.Leaf{
Key: key,
Value: value,
Generation: t.generation,
Dirty: true,
}
}

// TODO ensure all values have dirty set to true
Expand All @@ -354,8 +351,8 @@ func (t *Trie) insert(parent Node, key []byte, value Node) (newParent Node) {
}
}

func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte,
value Node) (newParent Node) {
func (t *Trie) insertInBranch(parentBranch *node.Branch, key,
value []byte) (newParent Node) {
newParent = t.updateBranch(parentBranch, key, value)

if newParent.IsDirty() {
Expand All @@ -367,13 +364,12 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte,
return newParent
}

func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
value Node) (newParent Node) {
newValue := value.(*node.Leaf).Value

func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key,
value []byte) (newParent Node) {
if bytes.Equal(parentLeaf.Key, key) {
if !bytes.Equal(newValue, parentLeaf.Value) {
parentLeaf.Value = newValue
if !bytes.Equal(value, parentLeaf.Value) {
parentLeaf.Value = value
parentLeaf.Generation = t.generation
parentLeaf.SetDirty(true)
}
return parentLeaf
Expand All @@ -391,7 +387,7 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,

if len(key) == commonPrefixLength {
// key is included in parent leaf key
newBranchParent.Value = newValue
newBranchParent.Value = value

if len(key) < len(parentLeafKey) {
// Move the current leaf parent as a child to the new branch.
Expand All @@ -404,8 +400,6 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
return newBranchParent
}

value.SetKey(key[commonPrefixLength+1:])

if len(parentLeaf.Key) == commonPrefixLength {
// the key of the parent leaf is at this new branch
newBranchParent.Value = parentLeaf.Value
Expand All @@ -417,15 +411,21 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
newBranchParent.Children[childIndex] = parentLeaf
}
childIndex := key[commonPrefixLength]
newBranchParent.Children[childIndex] = value
newBranchParent.Children[childIndex] = &node.Leaf{
Key: key[commonPrefixLength+1:],
Value: value,
Generation: t.generation,
Dirty: true,
}

return newBranchParent
}

func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (newParent Node) {
func (t *Trie) updateBranch(parentBranch *node.Branch, key, value []byte) (newParent Node) {
if bytes.Equal(key, parentBranch.Key) {
parentBranch.SetDirty(true)
parentBranch.Value = value.GetValue()
parentBranch.Generation = t.generation
parentBranch.Value = value
return parentBranch
}

Expand All @@ -439,7 +439,7 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (
if child == nil {
child = &node.Leaf{
Key: remainingKey,
Value: value.GetValue(),
Value: value,
Generation: t.generation,
Dirty: true,
}
Expand All @@ -450,6 +450,7 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (

parentBranch.Children[childIndex] = child
parentBranch.SetDirty(true)
parentBranch.Generation = t.generation
return parentBranch
}

Expand All @@ -464,10 +465,14 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (

oldParentIndex := parentBranch.Key[commonPrefixLength]
remainingOldParentKey := parentBranch.Key[commonPrefixLength+1:]
newParentBranch.Children[oldParentIndex] = t.insert(nil, remainingOldParentKey, parentBranch)

parentBranch.Dirty = true
parentBranch.Key = remainingOldParentKey
parentBranch.Generation = t.generation
newParentBranch.Children[oldParentIndex] = parentBranch

if len(key) <= commonPrefixLength {
newParentBranch.Value = value.(*node.Leaf).Value
newParentBranch.Value = value
} else {
childIndex := key[commonPrefixLength]
remainingKey := key[commonPrefixLength+1:]
Expand Down
Loading

0 comments on commit 2ed131b

Please sign in to comment.