forked from zhangchiqing/merkle-patricia-trie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.go
346 lines (293 loc) · 8.6 KB
/
trie.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import "fmt"
type Trie struct {
root Node
}
func NewTrie() *Trie {
return &Trie{}
}
func (t *Trie) Hash() []byte {
if IsEmptyNode(t.root) {
return EmptyNodeHash
}
return t.root.Hash()
}
func (t *Trie) Get(key []byte) ([]byte, bool) {
node := t.root
nibbles := FromBytes(key)
for {
if IsEmptyNode(node) {
return nil, false
}
if leaf, ok := node.(*LeafNode); ok {
matched := PrefixMatchedLen(leaf.Path, nibbles)
if matched != len(leaf.Path) || matched != len(nibbles) {
return nil, false
}
return leaf.Value, true
}
if branch, ok := node.(*BranchNode); ok {
if len(nibbles) == 0 {
return branch.Value, branch.HasValue()
}
b, remaining := nibbles[0], nibbles[1:]
nibbles = remaining
node = branch.Branches[b]
continue
}
if ext, ok := node.(*ExtensionNode); ok {
matched := PrefixMatchedLen(ext.Path, nibbles)
// E 01020304
// 010203
if matched < len(ext.Path) {
return nil, false
}
nibbles = nibbles[matched:]
node = ext.Next
continue
}
panic("not found")
}
}
// Put adds a key value pair to the trie
// In general, the rule is:
// - When stopped at an EmptyNode, replace it with a new LeafNode with the remaining path.
// - When stopped at a LeafNode, convert it to an ExtensionNode and add a new branch and a new LeafNode.
// - When stopped at an ExtensionNode, convert it to another ExtensionNode with shorter path and create a new BranchNode points to the ExtensionNode.
/*func (t *Trie) Put(key []byte, value []byte) {
// need to use pointer, so that I can update root in place without
// keeping trace of the parent node
node := &t.root
nibbles := FromBytes(key)
for {
if IsEmptyNode(*node) {
leaf := NewLeafNodeFromNibbles(nibbles, value)
*node = leaf
return
}
if leaf, ok := (*node).(*LeafNode); ok {
matched := PrefixMatchedLen(leaf.Path, nibbles)
// if all matched, update value even if the value are equal
if matched == len(nibbles) && matched == len(leaf.Path) {
newLeaf := NewLeafNodeFromNibbles(leaf.Path, value)
*node = newLeaf
return
}
branch := NewBranchNode()
// if matched some nibbles, check if matches either all remaining nibbles
// or all leaf nibbles
if matched == len(leaf.Path) {
branch.SetValue(leaf.Value)
}
if matched == len(nibbles) {
branch.SetValue(value)
}
// if there is matched nibbles, an extension node will be created
if matched > 0 {
// create an extension node for the shared nibbles
ext := NewExtensionNode(leaf.Path[:matched], branch)
*node = ext
} else {
// when there no matched nibble, there is no need to keep the extension node
*node = branch
}
if matched < len(leaf.Path) {
// have dismatched
// L 01020304 hello
// + 010203 world
// 01020304, 0, 4
branchNibble, leafNibbles := leaf.Path[matched], leaf.Path[matched+1:]
newLeaf := NewLeafNodeFromNibbles(leafNibbles, leaf.Value) // not :matched+1
branch.SetBranch(branchNibble, newLeaf)
}
if matched < len(nibbles) {
// L 01020304 hello
// + 010203040 world
// L 01020304 hello
// + 010203040506 world
branchNibble, leafNibbles := nibbles[matched], nibbles[matched+1:]
newLeaf := NewLeafNodeFromNibbles(leafNibbles, value)
branch.SetBranch(branchNibble, newLeaf)
}
return
}
if branch, ok := (*node).(*BranchNode); ok {
if len(nibbles) == 0 {
branch.SetValue(value)
return
}
b, remaining := nibbles[0], nibbles[1:]
nibbles = remaining
node = &branch.Branches[b]
continue
}
// E 01020304
// B 0 hello
// L 506 world
// + 010203 good
if ext, ok := (*node).(*ExtensionNode); ok {
matched := PrefixMatchedLen(ext.Path, nibbles)
if matched < len(ext.Path) {
// E 01020304
// + 010203 good
extNibbles, branchNibble, extRemainingnibbles := ext.Path[:matched], ext.Path[matched], ext.Path[matched+1:]
branch := NewBranchNode()
if len(extRemainingnibbles) == 0 {
// E 0102030
// + 010203 good
branch.SetBranch(branchNibble, ext.Next)
} else {
// E 01020304
// + 010203 good
newExt := NewExtensionNode(extRemainingnibbles, ext.Next)
branch.SetBranch(branchNibble, newExt)
}
if matched < len(nibbles) {
nodeBranchNibble, nodeLeafNibbles := nibbles[matched], nibbles[matched+1:]
remainingLeaf := NewLeafNodeFromNibbles(nodeLeafNibbles, value)
branch.SetBranch(nodeBranchNibble, remainingLeaf)
} else if matched == len(nibbles) {
branch.SetValue(value)
} else {
panic(fmt.Sprintf("too many matched (%v > %v)", matched, len(nibbles)))
}
// if there is no shared extension nibbles any more, then we don't need the extension node
// any more
// E 01020304
// + 1234 good
if len(extNibbles) == 0 {
*node = branch
} else {
// otherwise create a new extension node
*node = NewExtensionNode(extNibbles, branch)
}
return
}
nibbles = nibbles[matched:]
node = &ext.Next
continue
}
panic("unknown type")
}
}
*/
func (t *Trie) Put(key []byte, value []byte) {
node := &t.root
nibbles := FromBytes(key)
if IsEmptyNode(*node) {
leaf := NewLeafNodeFromNibbles(nibbles, value)
*node = leaf
return
}
for {
switch n := (*node).(type) {
case *LeafNode:
t.handleLeafNodePut(n, nibbles, value, node)
return
case *BranchNode:
t.handleBranchNodePut(n, nibbles, value, node)
continue
case *ExtensionNode:
t.handleExtensionNodePut(n, nibbles, value, node)
continue
default:
panic("unknown type")
}
}
}
func (t *Trie) handleLeafNodePut(leaf *LeafNode, nibbles []Nibble, value []byte, node *Node) {
matched := PrefixMatchedLen(leaf.Path, nibbles)
// if all matched, update value even if the value are equal
if matched == len(nibbles) && matched == len(leaf.Path) {
newLeaf := NewLeafNodeFromNibbles(leaf.Path, value)
*node = newLeaf
return
}
branch := NewBranchNode()
// if matched some nibbles, check if matches either all remaining nibbles
// or all leaf nibbles
if matched == len(leaf.Path) {
branch.SetValue(leaf.Value)
}
if matched == len(nibbles) {
branch.SetValue(value)
}
// if there is matched nibbles, an extension node will be created
if matched > 0 {
// create an extension node for the shared nibbles
ext := NewExtensionNode(leaf.Path[:matched], branch)
*node = ext
} else {
// when there no matched nibble, there is no need to keep the extension node
*node = branch
}
if matched < len(leaf.Path) {
// have dismatched
// L 01020304 hello
// + 010203 world
// 01020304, 0, 4
branchNibble, leafNibbles := leaf.Path[matched], leaf.Path[matched+1:]
newLeaf := NewLeafNodeFromNibbles(leafNibbles, leaf.Value) // not :matched+1
branch.SetBranch(branchNibble, newLeaf)
}
if matched < len(nibbles) {
// L 01020304 hello
// + 010203040 world
// L 01020304 hello
// + 010203040506 world
branchNibble, leafNibbles := nibbles[matched], nibbles[matched+1:]
newLeaf := NewLeafNodeFromNibbles(leafNibbles, value)
branch.SetBranch(branchNibble, newLeaf)
}
return
}
func (t *Trie) handleBranchNodePut(branch *BranchNode, nibbles []Nibble, value []byte, node *Node) {
if len(nibbles) == 0 {
branch.SetValue(value)
return
}
b, remaining := nibbles[0], nibbles[1:]
nibbles = remaining
node = &branch.Branches[b]
}
func (t *Trie) handleExtensionNodePut(ext *ExtensionNode, nibbles []Nibble, value []byte, node *Node) {
matched := PrefixMatchedLen(ext.Path, nibbles)
if matched < len(ext.Path) {
// E 01020304
// + 010203 good
extNibbles, branchNibble, extRemainingnibbles := ext.Path[:matched], ext.Path[matched], ext.Path[matched+1:]
branch := NewBranchNode()
if len(extRemainingnibbles) == 0 {
// E 0102030
// + 010203 good
branch.SetBranch(branchNibble, ext.Next)
} else {
// E 01020304
// + 010203 good
newExt := NewExtensionNode(extRemainingnibbles, ext.Next)
branch.SetBranch(branchNibble, newExt)
}
if matched < len(nibbles) {
nodeBranchNibble, nodeLeafNibbles := nibbles[matched], nibbles[matched+1:]
remainingLeaf := NewLeafNodeFromNibbles(nodeLeafNibbles, value)
branch.SetBranch(nodeBranchNibble, remainingLeaf)
} else if matched == len(nibbles) {
branch.SetValue(value)
} else {
panic(fmt.Sprintf("too many matched (%v > %v)", matched, len(nibbles)))
}
// if there is no shared extension nibbles any more, then we don't need the extension node
// any more
// E 01020304
// + 1234 good
if len(extNibbles) == 0 {
*node = branch
} else {
// otherwise create a new extension node
*node = NewExtensionNode(extNibbles, branch)
}
return
}
nibbles = nibbles[matched:]
node = &ext.Next
}