@@ -300,22 +300,19 @@ func (t *Trie) Put(keyLE, value []byte) {
300
300
}
301
301
302
302
func (t * Trie ) put (key , value []byte ) {
303
- nodeToInsert := & node.Leaf {
304
- Value : value ,
305
- Generation : t .generation ,
306
- Dirty : true ,
307
- }
308
- t .root = t .insert (t .root , key , nodeToInsert )
303
+ t .root = t .insert (t .root , key , value )
309
304
}
310
305
311
- // insert attempts to insert a key with value into the trie
312
- func (t * Trie ) insert (parent Node , key []byte , value Node ) (newParent Node ) {
313
- // TODO change value node to be value []byte?
314
- value .SetGeneration (t .generation ) // just in case it's not set by the caller.
315
-
306
+ // insert inserts a value in the trie at the key specified.
307
+ // It may create one or more new nodes or update an existing node.
308
+ func (t * Trie ) insert (parent Node , key , value []byte ) (newParent Node ) {
316
309
if parent == nil {
317
- value .SetKey (key )
318
- return value
310
+ return & node.Leaf {
311
+ Key : key ,
312
+ Value : value ,
313
+ Generation : t .generation ,
314
+ Dirty : true ,
315
+ }
319
316
}
320
317
321
318
// TODO ensure all values have dirty set to true
@@ -334,8 +331,8 @@ func (t *Trie) insert(parent Node, key []byte, value Node) (newParent Node) {
334
331
}
335
332
}
336
333
337
- func (t * Trie ) insertInBranch (parentBranch * node.Branch , key [] byte ,
338
- value Node ) (newParent Node ) {
334
+ func (t * Trie ) insertInBranch (parentBranch * node.Branch , key ,
335
+ value [] byte ) (newParent Node ) {
339
336
newParent = t .updateBranch (parentBranch , key , value )
340
337
341
338
if newParent .IsDirty () {
@@ -347,13 +344,12 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte,
347
344
return newParent
348
345
}
349
346
350
- func (t * Trie ) insertInLeaf (parentLeaf * node.Leaf , key []byte ,
351
- value Node ) (newParent Node ) {
352
- newValue := value .(* node.Leaf ).Value
353
-
347
+ func (t * Trie ) insertInLeaf (parentLeaf * node.Leaf , key ,
348
+ value []byte ) (newParent Node ) {
354
349
if bytes .Equal (parentLeaf .Key , key ) {
355
- if ! bytes .Equal (newValue , parentLeaf .Value ) {
356
- parentLeaf .Value = newValue
350
+ if ! bytes .Equal (value , parentLeaf .Value ) {
351
+ parentLeaf .Value = value
352
+ parentLeaf .Generation = t .generation
357
353
parentLeaf .SetDirty (true )
358
354
}
359
355
return parentLeaf
@@ -371,7 +367,7 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
371
367
372
368
if len (key ) == commonPrefixLength {
373
369
// key is included in parent leaf key
374
- newBranchParent .Value = newValue
370
+ newBranchParent .Value = value
375
371
376
372
if len (key ) < len (parentLeafKey ) {
377
373
// Move the current leaf parent as a child to the new branch.
@@ -384,8 +380,6 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
384
380
return newBranchParent
385
381
}
386
382
387
- value .SetKey (key [commonPrefixLength + 1 :])
388
-
389
383
if len (parentLeaf .Key ) == commonPrefixLength {
390
384
// the key of the parent leaf is at this new branch
391
385
newBranchParent .Value = parentLeaf .Value
@@ -397,15 +391,21 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
397
391
newBranchParent .Children [childIndex ] = parentLeaf
398
392
}
399
393
childIndex := key [commonPrefixLength ]
400
- newBranchParent .Children [childIndex ] = value
394
+ newBranchParent .Children [childIndex ] = & node.Leaf {
395
+ Key : key [commonPrefixLength + 1 :],
396
+ Value : value ,
397
+ Generation : t .generation ,
398
+ Dirty : true ,
399
+ }
401
400
402
401
return newBranchParent
403
402
}
404
403
405
- func (t * Trie ) updateBranch (parentBranch * node.Branch , key [] byte , value Node ) (newParent Node ) {
404
+ func (t * Trie ) updateBranch (parentBranch * node.Branch , key , value [] byte ) (newParent Node ) {
406
405
if bytes .Equal (key , parentBranch .Key ) {
407
406
parentBranch .SetDirty (true )
408
- parentBranch .Value = value .GetValue ()
407
+ parentBranch .Generation = t .generation
408
+ parentBranch .Value = value
409
409
return parentBranch
410
410
}
411
411
@@ -419,7 +419,7 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (
419
419
if child == nil {
420
420
child = & node.Leaf {
421
421
Key : remainingKey ,
422
- Value : value . GetValue () ,
422
+ Value : value ,
423
423
Generation : t .generation ,
424
424
Dirty : true ,
425
425
}
@@ -430,6 +430,7 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (
430
430
431
431
parentBranch .Children [childIndex ] = child
432
432
parentBranch .SetDirty (true )
433
+ parentBranch .Generation = t .generation
433
434
return parentBranch
434
435
}
435
436
@@ -444,10 +445,14 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (
444
445
445
446
oldParentIndex := parentBranch .Key [commonPrefixLength ]
446
447
remainingOldParentKey := parentBranch .Key [commonPrefixLength + 1 :]
447
- newParentBranch .Children [oldParentIndex ] = t .insert (nil , remainingOldParentKey , parentBranch )
448
+
449
+ parentBranch .Dirty = true
450
+ parentBranch .Key = remainingOldParentKey
451
+ parentBranch .Generation = t .generation
452
+ newParentBranch .Children [oldParentIndex ] = parentBranch
448
453
449
454
if len (key ) <= commonPrefixLength {
450
- newParentBranch .Value = value .( * node. Leaf ). Value
455
+ newParentBranch .Value = value
451
456
} else {
452
457
childIndex := key [commonPrefixLength ]
453
458
remainingKey := key [commonPrefixLength + 1 :]
0 commit comments