-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
147 lines (122 loc) · 3.18 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package PromethoniXTrie
func (trie *PromethoniXTrie) delete(root Hash, route Route) (Hash, error) {
if len(root) == 0 {
return nil, ErrNotFound
}
// fetch sub-trie root node
rootNode, err := trie.fetchNode(root)
if err != nil {
return nil, err
}
switch node := rootNode.(type) {
case *BranchNode:
return trie.deleteBranch(node, route)
case *ExtensionNode:
return trie.deleteExtension(node, route)
case *LeafNode:
return trie.deleteLeaf(node, route)
default:
return nil, ErrUnexpected
}
}
func (trie *PromethoniXTrie) deleteBranch(node *BranchNode, route Route) (Hash, error) {
nextHash, nextRoute, err := node.NextRoute(route)
if err != nil {
return nil, err
}
newHash, err := trie.delete(nextHash, nextRoute)
if err != nil {
return nil, err
}
node.Hashes[route[0]] = newHash
// remove empty branch node
branchLength := node.Length()
if branchLength == 0 {
return nil, nil
}
if branchLength == 1 {
return trie.deleteSingleBranch(node)
}
err = trie.commitNode(node)
return node.Hash, err
}
func (trie *PromethoniXTrie) deleteSingleBranch(rootNode *BranchNode) (Hash, error) {
for idx := range rootNode.Hashes {
if len(rootNode.Hashes[idx]) != 0 {
childNode, err := trie.fetchNode(rootNode.Hashes[idx])
if err != nil {
return nil, err
}
switch child := childNode.(type) {
case *BranchNode:
extNode := NewExtensionNode()
extNode.Path = Hash{byte(idx)}
extNode.NextHash = rootNode.Hashes[idx]
err = trie.commitNode(extNode)
return extNode.Hash, err
case *ExtensionNode:
child.Path = append(Hash{byte(idx)}, child.Path...)
err = trie.commitNode(child)
return child.Hash, err
case *LeafNode: // branch->leaf-->leaf
child.Path = append(Hash{byte(idx)}, child.Path...)
err = trie.commitNode(child)
return child.Hash, err
default:
return nil, ErrUnexpected
}
}
}
return nil, nil
}
func (trie *PromethoniXTrie) deleteExtension(node *ExtensionNode, route Route) (Hash, error) {
matchLen := prefixLen(node.Path, route)
if matchLen != len(node.Path) {
return nil, ErrNotFound
}
childHash, err := trie.delete(node.NextHash, route[matchLen:])
if err != nil {
return nil, err
}
// remove empty ext node
if childHash == nil {
return nil, nil
}
// child hash
var newHash Hash
newHash, err = trie.deleteSingleExtension(node, childHash)
if err != nil {
return nil, err
}
if newHash != nil {
return newHash, nil
}
node.NextHash = childHash
err = trie.commitNode(node)
return node.Hash, err
}
func (trie *PromethoniXTrie) deleteSingleExtension(node *ExtensionNode, hash Hash) (Hash, error) {
childNode, err := trie.fetchNode(hash)
if err != nil {
return nil, err
}
switch child := childNode.(type) {
case *ExtensionNode:
child.Path = append(node.Path, child.Path...)
err = trie.commitNode(child)
return child.Hash, err
case *LeafNode:
child.Path = append(node.Path, child.Path...)
err = trie.commitNode(child)
return child.Hash, err
default:
return nil, nil
}
}
func (trie *PromethoniXTrie) deleteLeaf(node *LeafNode, route Route) (Hash, error) {
matchLen := prefixLen(node.Path, route)
if matchLen != len(node.Path) {
return nil, ErrNotFound
}
return nil, nil
}