Skip to content

Commit 0eacba3

Browse files
authored
chore(lib/trie): insert inserts value byte slice instead of node (#2296)
1 parent 9ac6642 commit 0eacba3

File tree

2 files changed

+70
-87
lines changed

2 files changed

+70
-87
lines changed

lib/trie/trie.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -306,22 +306,19 @@ func (t *Trie) Put(keyLE, value []byte) {
306306
}
307307

308308
func (t *Trie) put(key, value []byte) {
309-
nodeToInsert := &node.Leaf{
310-
Value: value,
311-
Generation: t.generation,
312-
Dirty: true,
313-
}
314-
t.root = t.insert(t.root, key, nodeToInsert)
309+
t.root = t.insert(t.root, key, value)
315310
}
316311

317-
// insert attempts to insert a key with value into the trie
318-
func (t *Trie) insert(parent Node, key []byte, value Node) (newParent Node) {
319-
// TODO change value node to be value []byte?
320-
value.SetGeneration(t.generation) // just in case it's not set by the caller.
321-
312+
// insert inserts a value in the trie at the key specified.
313+
// It may create one or more new nodes or update an existing node.
314+
func (t *Trie) insert(parent Node, key, value []byte) (newParent Node) {
322315
if parent == nil {
323-
value.SetKey(key)
324-
return value
316+
return &node.Leaf{
317+
Key: key,
318+
Value: value,
319+
Generation: t.generation,
320+
Dirty: true,
321+
}
325322
}
326323

327324
// TODO ensure all values have dirty set to true
@@ -340,13 +337,12 @@ func (t *Trie) insert(parent Node, key []byte, value Node) (newParent Node) {
340337
}
341338
}
342339

343-
func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
344-
value Node) (newParent Node) {
345-
newValue := value.(*node.Leaf).Value
346-
340+
func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key,
341+
value []byte) (newParent Node) {
347342
if bytes.Equal(parentLeaf.Key, key) {
348-
if !bytes.Equal(newValue, parentLeaf.Value) {
349-
parentLeaf.Value = newValue
343+
if !bytes.Equal(value, parentLeaf.Value) {
344+
parentLeaf.Value = value
345+
parentLeaf.Generation = t.generation
350346
parentLeaf.SetDirty(true)
351347
}
352348
return parentLeaf
@@ -364,7 +360,7 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
364360

365361
if len(key) == commonPrefixLength {
366362
// key is included in parent leaf key
367-
newBranchParent.Value = newValue
363+
newBranchParent.Value = value
368364

369365
if len(key) < len(parentLeafKey) {
370366
// Move the current leaf parent as a child to the new branch.
@@ -377,8 +373,6 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
377373
return newBranchParent
378374
}
379375

380-
value.SetKey(key[commonPrefixLength+1:])
381-
382376
if len(parentLeaf.Key) == commonPrefixLength {
383377
// the key of the parent leaf is at this new branch
384378
newBranchParent.Value = parentLeaf.Value
@@ -390,15 +384,21 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key []byte,
390384
newBranchParent.Children[childIndex] = parentLeaf
391385
}
392386
childIndex := key[commonPrefixLength]
393-
newBranchParent.Children[childIndex] = value
387+
newBranchParent.Children[childIndex] = &node.Leaf{
388+
Key: key[commonPrefixLength+1:],
389+
Value: value,
390+
Generation: t.generation,
391+
Dirty: true,
392+
}
394393

395394
return newBranchParent
396395
}
397396

398-
func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte, value Node) (newParent Node) {
397+
func (t *Trie) insertInBranch(parentBranch *node.Branch, key, value []byte) (newParent Node) {
399398
if bytes.Equal(key, parentBranch.Key) {
400399
parentBranch.SetDirty(true)
401-
parentBranch.Value = value.GetValue()
400+
parentBranch.Generation = t.generation
401+
parentBranch.Value = value
402402
return parentBranch
403403
}
404404

@@ -412,7 +412,7 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte, value Node)
412412
if child == nil {
413413
child = &node.Leaf{
414414
Key: remainingKey,
415-
Value: value.GetValue(),
415+
Value: value,
416416
Generation: t.generation,
417417
Dirty: true,
418418
}
@@ -423,6 +423,7 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte, value Node)
423423

424424
parentBranch.Children[childIndex] = child
425425
parentBranch.SetDirty(true)
426+
parentBranch.Generation = t.generation
426427
return parentBranch
427428
}
428429

@@ -438,10 +439,14 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key []byte, value Node)
438439

439440
oldParentIndex := parentBranch.Key[commonPrefixLength]
440441
remainingOldParentKey := parentBranch.Key[commonPrefixLength+1:]
441-
newParentBranch.Children[oldParentIndex] = t.insert(nil, remainingOldParentKey, parentBranch)
442+
443+
parentBranch.Dirty = true
444+
parentBranch.Key = remainingOldParentKey
445+
parentBranch.Generation = t.generation
446+
newParentBranch.Children[oldParentIndex] = parentBranch
442447

443448
if len(key) <= commonPrefixLength {
444-
newParentBranch.Value = value.(*node.Leaf).Value
449+
newParentBranch.Value = value
445450
} else {
446451
childIndex := key[commonPrefixLength]
447452
remainingKey := key[commonPrefixLength+1:]

lib/trie/trie_test.go

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,18 +1020,20 @@ func Test_Trie_insert(t *testing.T) {
10201020
trie Trie
10211021
parent Node
10221022
key []byte
1023-
value Node
1023+
value []byte
10241024
newNode Node
10251025
}{
10261026
"nil parent": {
10271027
trie: Trie{
10281028
generation: 1,
10291029
},
10301030
key: []byte{1},
1031-
value: &node.Leaf{},
1031+
value: []byte("leaf"),
10321032
newNode: &node.Leaf{
10331033
Key: []byte{1},
1034+
Value: []byte("leaf"),
10341035
Generation: 1,
1036+
Dirty: true,
10351037
},
10361038
},
10371039
"branch parent": {
@@ -1046,10 +1048,8 @@ func Test_Trie_insert(t *testing.T) {
10461048
&node.Leaf{Key: []byte{2}},
10471049
},
10481050
},
1049-
key: []byte{1, 0},
1050-
value: &node.Leaf{
1051-
Value: []byte("leaf"),
1052-
},
1051+
key: []byte{1, 0},
1052+
value: []byte("leaf"),
10531053
newNode: &node.Branch{
10541054
Key: []byte{1},
10551055
Value: []byte("branch"),
@@ -1074,10 +1074,8 @@ func Test_Trie_insert(t *testing.T) {
10741074
Key: []byte{1},
10751075
Value: []byte("original leaf"),
10761076
},
1077-
key: []byte{1},
1078-
value: &node.Leaf{
1079-
Value: []byte("new leaf"),
1080-
},
1077+
key: []byte{1},
1078+
value: []byte("new leaf"),
10811079
newNode: &node.Leaf{
10821080
Key: []byte{1},
10831081
Value: []byte("new leaf"),
@@ -1093,10 +1091,8 @@ func Test_Trie_insert(t *testing.T) {
10931091
Key: []byte{1},
10941092
Value: []byte("same"),
10951093
},
1096-
key: []byte{1},
1097-
value: &node.Leaf{
1098-
Value: []byte("same"),
1099-
},
1094+
key: []byte{1},
1095+
value: []byte("same"),
11001096
newNode: &node.Leaf{
11011097
Key: []byte{1},
11021098
Value: []byte("same"),
@@ -1111,10 +1107,8 @@ func Test_Trie_insert(t *testing.T) {
11111107
Key: []byte{1},
11121108
Value: []byte("original leaf"),
11131109
},
1114-
key: []byte{1, 0},
1115-
value: &node.Leaf{
1116-
Value: []byte("leaf"),
1117-
},
1110+
key: []byte{1, 0},
1111+
value: []byte("leaf"),
11181112
newNode: &node.Branch{
11191113
Key: []byte{1},
11201114
Value: []byte("original leaf"),
@@ -1125,6 +1119,7 @@ func Test_Trie_insert(t *testing.T) {
11251119
Key: []byte{},
11261120
Value: []byte("leaf"),
11271121
Generation: 1,
1122+
Dirty: true,
11281123
},
11291124
},
11301125
},
@@ -1137,10 +1132,8 @@ func Test_Trie_insert(t *testing.T) {
11371132
Key: []byte{1, 2},
11381133
Value: []byte("original leaf"),
11391134
},
1140-
key: []byte{2, 3},
1141-
value: &node.Leaf{
1142-
Value: []byte("leaf"),
1143-
},
1135+
key: []byte{2, 3},
1136+
value: []byte("leaf"),
11441137
newNode: &node.Branch{
11451138
Key: []byte{},
11461139
Dirty: true,
@@ -1157,6 +1150,7 @@ func Test_Trie_insert(t *testing.T) {
11571150
Key: []byte{3},
11581151
Value: []byte("leaf"),
11591152
Generation: 1,
1153+
Dirty: true,
11601154
},
11611155
},
11621156
},
@@ -1168,10 +1162,8 @@ func Test_Trie_insert(t *testing.T) {
11681162
parent: &node.Leaf{
11691163
Key: []byte{1},
11701164
},
1171-
key: []byte{1},
1172-
value: &node.Leaf{
1173-
Value: []byte("leaf"),
1174-
},
1165+
key: []byte{1},
1166+
value: []byte("leaf"),
11751167
newNode: &node.Leaf{
11761168
Key: []byte{1},
11771169
Value: []byte("leaf"),
@@ -1186,10 +1178,8 @@ func Test_Trie_insert(t *testing.T) {
11861178
parent: &node.Leaf{
11871179
Key: []byte{1, 2},
11881180
},
1189-
key: []byte{1},
1190-
value: &node.Leaf{
1191-
Value: []byte("leaf"),
1192-
},
1181+
key: []byte{1},
1182+
value: []byte("leaf"),
11931183
newNode: &node.Branch{
11941184
Key: []byte{1},
11951185
Value: []byte("leaf"),
@@ -1229,7 +1219,7 @@ func Test_Trie_insertInBranch(t *testing.T) {
12291219
testCases := map[string]struct {
12301220
parent *node.Branch
12311221
key []byte
1232-
value Node
1222+
value []byte
12331223
newNode Node
12341224
}{
12351225
"update with branch": {
@@ -1240,10 +1230,8 @@ func Test_Trie_insertInBranch(t *testing.T) {
12401230
&node.Leaf{Key: []byte{1}},
12411231
},
12421232
},
1243-
key: []byte{2},
1244-
value: &node.Branch{
1245-
Value: []byte("new"),
1246-
},
1233+
key: []byte{2},
1234+
value: []byte("new"),
12471235
newNode: &node.Branch{
12481236
Key: []byte{2},
12491237
Value: []byte("new"),
@@ -1261,10 +1249,8 @@ func Test_Trie_insertInBranch(t *testing.T) {
12611249
&node.Leaf{Key: []byte{1}},
12621250
},
12631251
},
1264-
key: []byte{2},
1265-
value: &node.Leaf{
1266-
Value: []byte("new"),
1267-
},
1252+
key: []byte{2},
1253+
value: []byte("new"),
12681254
newNode: &node.Branch{
12691255
Key: []byte{2},
12701256
Value: []byte("new"),
@@ -1282,10 +1268,8 @@ func Test_Trie_insertInBranch(t *testing.T) {
12821268
&node.Leaf{Key: []byte{1}},
12831269
},
12841270
},
1285-
key: []byte{2, 3, 4, 5},
1286-
value: &node.Leaf{
1287-
Value: []byte{6},
1288-
},
1271+
key: []byte{2, 3, 4, 5},
1272+
value: []byte{6},
12891273
newNode: &node.Branch{
12901274
Key: []byte{2},
12911275
Value: []byte{5},
@@ -1315,10 +1299,8 @@ func Test_Trie_insertInBranch(t *testing.T) {
13151299
},
13161300
},
13171301
},
1318-
key: []byte{2, 3, 4, 5, 6},
1319-
value: &node.Leaf{
1320-
Value: []byte{6},
1321-
},
1302+
key: []byte{2, 3, 4, 5, 6},
1303+
value: []byte{6},
13221304
newNode: &node.Branch{
13231305
Key: []byte{2},
13241306
Value: []byte{5},
@@ -1349,10 +1331,8 @@ func Test_Trie_insertInBranch(t *testing.T) {
13491331
&node.Leaf{Key: []byte{1}},
13501332
},
13511333
},
1352-
key: []byte{2, 4, 5, 6},
1353-
value: &node.Leaf{
1354-
Value: []byte{6},
1355-
},
1334+
key: []byte{2, 4, 5, 6},
1335+
value: []byte{6},
13561336
newNode: &node.Branch{
13571337
Key: []byte{2},
13581338
Dirty: true,
@@ -1369,6 +1349,7 @@ func Test_Trie_insertInBranch(t *testing.T) {
13691349
&node.Leaf{
13701350
Key: []byte{5, 6},
13711351
Value: []byte{6},
1352+
Dirty: true,
13721353
},
13731354
},
13741355
},
@@ -1381,10 +1362,8 @@ func Test_Trie_insertInBranch(t *testing.T) {
13811362
&node.Leaf{Key: []byte{1}},
13821363
},
13831364
},
1384-
key: []byte{3},
1385-
value: &node.Leaf{
1386-
Value: []byte{6},
1387-
},
1365+
key: []byte{3},
1366+
value: []byte{6},
13881367
newNode: &node.Branch{
13891368
Key: []byte{},
13901369
Dirty: true,
@@ -1401,6 +1380,7 @@ func Test_Trie_insertInBranch(t *testing.T) {
14011380
&node.Leaf{
14021381
Key: []byte{},
14031382
Value: []byte{6},
1383+
Dirty: true,
14041384
},
14051385
},
14061386
},
@@ -1413,10 +1393,8 @@ func Test_Trie_insertInBranch(t *testing.T) {
14131393
&node.Leaf{Key: []byte{1}},
14141394
},
14151395
},
1416-
key: []byte{},
1417-
value: &node.Leaf{
1418-
Value: []byte{6},
1419-
},
1396+
key: []byte{},
1397+
value: []byte{6},
14201398
newNode: &node.Branch{
14211399
Key: []byte{},
14221400
Value: []byte{6},

0 commit comments

Comments
 (0)