From 12f1aea38d6f9dd9748a95b2d9ea813ae16277a7 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 20 Nov 2014 18:11:31 +0100 Subject: [PATCH] Fixed iterator for short nodes. In some cases the iterator didn't properly return the correct key because it didn't append fields to the reverse lookup. --- ptrie/iterator.go | 5 +++-- ptrie/iterator_test.go | 5 +++++ ptrie/trie_test.go | 3 +-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ptrie/iterator.go b/ptrie/iterator.go index c6d4f64a01..5714bdbc8a 100644 --- a/ptrie/iterator.go +++ b/ptrie/iterator.go @@ -65,9 +65,9 @@ func (self *Iterator) next(node Node, key []byte) []byte { } } else { cnode := node.Value() - skey := key[len(k):] var ret []byte + skey := key[len(k):] if trie.BeginsWith(key, k) { ret = self.next(cnode, skey) } else if bytes.Compare(k, key[:len(k)]) > 0 { @@ -93,7 +93,8 @@ func (self *Iterator) key(node Node) []byte { return k } else { - return self.key(node.Value()) + k := trie.RemTerm(node.Key()) + return append(k, self.key(node.Value())...) } case *FullNode: if node.Value() != nil { diff --git a/ptrie/iterator_test.go b/ptrie/iterator_test.go index 8921bb6708..acfc03d633 100644 --- a/ptrie/iterator_test.go +++ b/ptrie/iterator_test.go @@ -8,12 +8,17 @@ func TestIterator(t *testing.T) { {"do", "verb"}, {"ether", "wookiedoo"}, {"horse", "stallion"}, + {"shaman", "horse"}, + {"doge", "coin"}, + {"dog", "puppy"}, + {"somethingveryoddindeedthis is", "myothernodedata"}, } v := make(map[string]bool) for _, val := range vals { v[val.k] = false trie.UpdateString(val.k, val.v) } + trie.Commit() it := trie.Iterator() for it.Next() { diff --git a/ptrie/trie_test.go b/ptrie/trie_test.go index dfc89709d7..ba50f50a51 100644 --- a/ptrie/trie_test.go +++ b/ptrie/trie_test.go @@ -122,7 +122,6 @@ func TestEmptyValues(t *testing.T) { } func TestReplication(t *testing.T) { - t.Skip() trie := NewEmpty() vals := []struct{ k, v string }{ {"do", "verb"}, @@ -138,7 +137,7 @@ func TestReplication(t *testing.T) { for _, val := range vals { trie.UpdateString(val.k, val.v) } - trie.Hash() + trie.Commit() trie2 := New(trie.roothash, trie.cache.backend) if string(trie2.GetString("horse")) != "stallion" {