trie: ensure resolved nodes stay loaded

Commit 40cdcf1183 broke the optimisation which kept nodes resolved
during Get in the trie. The decoder assigned cache generation 0
unconditionally, causing resolved nodes to get flushed on Commit.

This commit fixes it and adds two tests.
This commit is contained in:
Felix Lange
2016-10-17 16:13:50 +02:00
parent 187d6a66a5
commit 177cab5fe7
6 changed files with 95 additions and 43 deletions

View File

@ -144,14 +144,15 @@ func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode
if err == nil && didResolve {
n = n.copy()
n.Val = newnode
n.flags.gen = t.cachegen
}
return value, n, didResolve, err
case *fullNode:
value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1)
if err == nil && didResolve {
n = n.copy()
n.flags.gen = t.cachegen
n.Children[key[pos]] = newnode
}
return value, n, didResolve, err
case hashNode:
@ -247,7 +248,8 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error
return false, n, err
}
n = n.copy()
n.Children[key[0]], n.flags.hash, n.flags.dirty = nn, nil, true
n.flags = t.newFlag()
n.Children[key[0]] = nn
return true, n, nil
case nil:
@ -331,7 +333,8 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
return false, n, err
}
n = n.copy()
n.Children[key[0]], n.flags.hash, n.flags.dirty = nn, nil, true
n.flags = t.newFlag()
n.Children[key[0]] = nn
// Check how many non-nil entries are left after deleting and
// reduce the full node to a short node if only one entry is
@ -427,7 +430,7 @@ func (t *Trie) resolveHash(n hashNode, prefix, suffix []byte) (node, error) {
SuffixLen: len(suffix),
}
}
dec := mustDecodeNode(n, enc)
dec := mustDecodeNode(n, enc, t.cachegen)
return dec, nil
}