Skip to content

Commit

Permalink
change Merge to Update
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Dec 9, 2024
1 parent c22edd2 commit 1ce1836
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions core/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@ func (n *Node) String() string {
return fmt.Sprintf("Node{Value: %s, Left: %s, Right: %s, LeftHash: %s, RightHash: %s}", n.Value, n.Left, n.Right, n.LeftHash, n.RightHash)
}

// Merge combines the fields of two nodes if they are not nil.
// If a field is nil in the current node, it is updated with the corresponding field from the other node.
// If a field is not nil in both nodes, the fields must be equal, otherwise an error is returned.
// Update the receiver with non-nil fields from the `other` Node.
// If a field is non-nil in both Nodes, they must be equal, or an error is returned.
//
// This method modifies the receiver in-place and returns an error if any field conflicts are detected.
//
//nolint:gocyclo
func (n *Node) Merge(other *Node) error {
func (n *Node) Update(other *Node) error {
// Compare Value if both exist
if n.Value != nil && other.Value != nil {
if !n.Value.Equal(other.Value) {
Expand Down
10 changes: 5 additions & 5 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ func (sn *StorageNode) String() string {
return fmt.Sprintf("StorageNode{key: %s, node: %s}", sn.key, sn.node)
}

func (sn *StorageNode) Merge(other *StorageNode) error {
func (sn *StorageNode) Update(other *StorageNode) error {
if sn.key != nil && other.key != nil && !sn.key.Equal(NilKey) && !other.key.Equal(NilKey) {
if !sn.key.Equal(other.key) {
return fmt.Errorf("keys do not match: %s != %s", sn.key, other.key)
}
} else if other.key != nil && !other.key.Equal(NilKey) {
sn.key = other.key
}
return sn.node.Merge(other.node)
return sn.node.Update(other.node)
}

func NewStorageNode(key *Key, node *Node) *StorageNode {
Expand Down Expand Up @@ -182,10 +182,10 @@ func (s *StorageNodeSet) Put(key Key, node *StorageNode) error {
return fmt.Errorf("cannot put nil node")
}

// If key exists, merge the nodes
// If key exists, update the node
if existingNode, exists := s.set.Get(key); exists {
if err := existingNode.Merge(node); err != nil {
return fmt.Errorf("failed to merge nodes for key %v: %w", key, err)
if err := existingNode.Update(node); err != nil {
return fmt.Errorf("failed to update node for key %v: %w", key, err)
}
return nil
}
Expand Down

0 comments on commit 1ce1836

Please sign in to comment.