Skip to content

Commit 653ef7f

Browse files
committed
chore(lib/trie): insert inserts value byte slice
1 parent 9757382 commit 653ef7f

File tree

2 files changed

+75
-89
lines changed

2 files changed

+75
-89
lines changed

lib/trie/trie.go

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,19 @@ func (t *Trie) Put(keyLE, value []byte) {
300300
}
301301

302302
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)
309304
}
310305

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) {
316309
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+
}
319316
}
320317

321318
// 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) {
334331
}
335332
}
336333

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) {
339336
newParent = t.updateBranch(parentBranch, key, value)
340337

341338
if newParent.IsDirty() {
@@ -347,13 +344,12 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte,
347344
return newParent
348345
}
349346

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) {
354349
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
357353
parentLeaf.SetDirty(true)
358354
}
359355
return parentLeaf
@@ -371,7 +367,7 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
371367

372368
if len(key) == commonPrefixLength {
373369
// key is included in parent leaf key
374-
newBranchParent.Value = newValue
370+
newBranchParent.Value = value
375371

376372
if len(key) < len(parentLeafKey) {
377373
// 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,
384380
return newBranchParent
385381
}
386382

387-
value.SetKey(key[commonPrefixLength+1:])
388-
389383
if len(parentLeaf.Key) == commonPrefixLength {
390384
// the key of the parent leaf is at this new branch
391385
newBranchParent.Value = parentLeaf.Value
@@ -397,15 +391,21 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
397391
newBranchParent.Children[childIndex] = parentLeaf
398392
}
399393
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+
}
401400

402401
return newBranchParent
403402
}
404403

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) {
406405
if bytes.Equal(key, parentBranch.Key) {
407406
parentBranch.SetDirty(true)
408-
parentBranch.Value = value.GetValue()
407+
parentBranch.Generation = t.generation
408+
parentBranch.Value = value
409409
return parentBranch
410410
}
411411

@@ -419,7 +419,7 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (
419419
if child == nil {
420420
child = &node.Leaf{
421421
Key: remainingKey,
422-
Value: value.GetValue(),
422+
Value: value,
423423
Generation: t.generation,
424424
Dirty: true,
425425
}
@@ -430,6 +430,7 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (
430430

431431
parentBranch.Children[childIndex] = child
432432
parentBranch.SetDirty(true)
433+
parentBranch.Generation = t.generation
433434
return parentBranch
434435
}
435436

@@ -444,10 +445,14 @@ func (t *Trie) updateBranch(parentBranch *node.Branch, key []byte, value Node) (
444445

445446
oldParentIndex := parentBranch.Key[commonPrefixLength]
446447
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
448453

449454
if len(key) <= commonPrefixLength {
450-
newParentBranch.Value = value.(*node.Leaf).Value
455+
newParentBranch.Value = value
451456
} else {
452457
childIndex := key[commonPrefixLength]
453458
remainingKey := key[commonPrefixLength+1:]

0 commit comments

Comments
 (0)