Skip to content

Commit 35e2d1c

Browse files
committed
Fix that Ctrie iterator did not return entries from tNodes.
Fixes #171
1 parent 01ff43b commit 35e2d1c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

trie/ctrie/ctrie.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2015 Workiva, LLC
2+
Copyright 2015-2017 Workiva, LLC and others.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -405,6 +405,8 @@ func (c *Ctrie) traverse(i *iNode, ch chan<- *Entry, cancel <-chan struct{}) err
405405
return errCanceled
406406
}
407407
}
408+
case main.tNode != nil:
409+
ch <- main.tNode.Entry
408410
}
409411
return nil
410412
}

trie/ctrie/ctrie_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,24 @@ func TestIterator(t *testing.T) {
341341
assert.False(ok)
342342
}
343343

344+
// TestIteratorCoversTNodes reproduces the scenario of a bug where tNodes weren't being traversed.
345+
func TestIteratorCoversTNodes(t *testing.T) {
346+
assert := assert.New(t)
347+
ctrie := New(mockHashFactory)
348+
// Add a pair of keys that collide (because we're using the mock hash).
349+
ctrie.Insert([]byte("a"), true)
350+
ctrie.Insert([]byte("b"), true)
351+
// Delete one key, leaving exactly one sNode in the cNode. This will
352+
// trigger creation of a tNode.
353+
ctrie.Remove([]byte("b"))
354+
seenKeys := map[string]bool{}
355+
for entry := range ctrie.Iterator(nil) {
356+
seenKeys[string(entry.Key)] = true
357+
}
358+
assert.Contains(seenKeys, "a", "Iterator did not return 'a'.")
359+
assert.Len(seenKeys, 1)
360+
}
361+
344362
func TestSize(t *testing.T) {
345363
ctrie := New(nil)
346364
for i := 0; i < 10; i++ {

0 commit comments

Comments
 (0)