added ability to iterate over all indexes and use index.Iterate() instead of ReadDir() to walk over the graph
Signed-off-by: Roman Strashkin <roman.strashkin@gmail.com>
This commit is contained in:
parent
3314761f62
commit
1d2e175c37
2 changed files with 33 additions and 0 deletions
|
@ -108,3 +108,13 @@ func (idx *TruncIndex) Get(s string) (string, error) {
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("no such id: %s", s)
|
return "", fmt.Errorf("no such id: %s", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iterates over all stored IDs, and passes each of them to the given handler
|
||||||
|
func (idx *TruncIndex) Iterate(handler func(id string)) {
|
||||||
|
idx.RLock()
|
||||||
|
defer idx.RUnlock()
|
||||||
|
idx.trie.Visit(func(prefix patricia.Prefix, item patricia.Item) error {
|
||||||
|
handler(string(prefix))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -96,6 +96,29 @@ func TestTruncIndex(t *testing.T) {
|
||||||
assertIndexGet(t, index, id[:7], id, false)
|
assertIndexGet(t, index, id[:7], id, false)
|
||||||
assertIndexGet(t, index, id[:15], id, false)
|
assertIndexGet(t, index, id[:15], id, false)
|
||||||
assertIndexGet(t, index, id, id, false)
|
assertIndexGet(t, index, id, id, false)
|
||||||
|
|
||||||
|
assertIndexIterate(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertIndexIterate(t *testing.T) {
|
||||||
|
ids := []string{
|
||||||
|
"19b36c2c326ccc11e726eee6ee78a0baf166ef96",
|
||||||
|
"28b36c2c326ccc11e726eee6ee78a0baf166ef96",
|
||||||
|
"37b36c2c326ccc11e726eee6ee78a0baf166ef96",
|
||||||
|
"46b36c2c326ccc11e726eee6ee78a0baf166ef96",
|
||||||
|
}
|
||||||
|
|
||||||
|
index := NewTruncIndex(ids)
|
||||||
|
|
||||||
|
index.Iterate(func(targetId string) {
|
||||||
|
for _, id := range ids {
|
||||||
|
if targetId == id {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Fatalf("An unknown ID '%s'", targetId)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult string, expectError bool) {
|
func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult string, expectError bool) {
|
||||||
|
|
Loading…
Reference in a new issue