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:
Roman Strashkin
2015-07-07 22:13:28 +03:00
parent 46aea60fb0
commit cc955ae73c
9 changed files with 63 additions and 62 deletions
+10
View File
@@ -108,3 +108,13 @@ func (idx *TruncIndex) Get(s string) (string, error) {
}
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
})
}
+23
View File
@@ -96,6 +96,29 @@ func TestTruncIndex(t *testing.T) {
assertIndexGet(t, index, id[:7], id, false)
assertIndexGet(t, index, id[:15], 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) {