File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -969,6 +969,10 @@ func (t *Trie) deleteBranch(branch *Node, key []byte) (
969
969
}
970
970
971
971
commonPrefixLength := lenCommonPrefix (branch .Key , key )
972
+ keyDoesNotExist := commonPrefixLength == len (key )
973
+ if keyDoesNotExist {
974
+ return branch , false , 0
975
+ }
972
976
childIndex := key [commonPrefixLength ]
973
977
childKey := key [commonPrefixLength + 1 :]
974
978
child := branch .Children [childIndex ]
Original file line number Diff line number Diff line change @@ -3573,6 +3573,40 @@ func Test_Trie_delete(t *testing.T) {
3573
3573
},
3574
3574
updated : true ,
3575
3575
},
3576
+ "handle nonexistent key (no op)" : {
3577
+ trie : Trie {
3578
+ generation : 1 ,
3579
+ },
3580
+ parent : & Node {
3581
+ Key : []byte {1 , 0 , 2 , 3 },
3582
+ Descendants : 1 ,
3583
+ Children : padRightChildren ([]* Node {
3584
+ { // full key 1, 0, 2
3585
+ Key : []byte {2 },
3586
+ Value : []byte {1 },
3587
+ },
3588
+ { // full key 1, 1, 2
3589
+ Key : []byte {2 },
3590
+ Value : []byte {2 },
3591
+ },
3592
+ }),
3593
+ },
3594
+ key : []byte {1 , 0 , 2 },
3595
+ newParent : & Node {
3596
+ Key : []byte {1 , 0 , 2 , 3 },
3597
+ Descendants : 1 ,
3598
+ Children : padRightChildren ([]* Node {
3599
+ { // full key 1, 0, 2
3600
+ Key : []byte {2 },
3601
+ Value : []byte {1 },
3602
+ },
3603
+ { // full key 1, 1, 2
3604
+ Key : []byte {2 },
3605
+ Value : []byte {2 },
3606
+ },
3607
+ }),
3608
+ },
3609
+ },
3576
3610
}
3577
3611
3578
3612
for name , testCase := range testCases {
You can’t perform that action at this time.
0 commit comments