aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/trie.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-02-17 03:32:56 +0800
committerobscuren <geffobscura@gmail.com>2014-02-17 03:32:56 +0800
commite4a6ee3d7f8a3876387f837f6fec6822bd97951e (patch)
tree8e43b7fc4a7cc42978bfa972d9506179834570a8 /ethutil/trie.go
parent7264044122d2ceef413667ad8473746a40a44782 (diff)
downloaddexon-e4a6ee3d7f8a3876387f837f6fec6822bd97951e.tar.gz
dexon-e4a6ee3d7f8a3876387f837f6fec6822bd97951e.tar.zst
dexon-e4a6ee3d7f8a3876387f837f6fec6822bd97951e.zip
Added dirty tracking on the cache
Diffstat (limited to 'ethutil/trie.go')
-rw-r--r--ethutil/trie.go17
1 files changed, 15 insertions, 2 deletions
diff --git a/ethutil/trie.go b/ethutil/trie.go
index 0a3f73136..7140a1b36 100644
--- a/ethutil/trie.go
+++ b/ethutil/trie.go
@@ -20,8 +20,9 @@ func (n *Node) Copy() *Node {
}
type Cache struct {
- nodes map[string]*Node
- db Database
+ nodes map[string]*Node
+ db Database
+ IsDirty bool
}
func NewCache(db Database) *Cache {
@@ -36,6 +37,7 @@ func (cache *Cache) Put(v interface{}) interface{} {
sha := Sha3Bin(enc)
cache.nodes[string(sha)] = NewNode(sha, value, true)
+ cache.IsDirty = true
return sha
}
@@ -60,12 +62,18 @@ func (cache *Cache) Get(key []byte) *Value {
}
func (cache *Cache) Commit() {
+ // Don't try to commit if it isn't dirty
+ if !cache.IsDirty {
+ return
+ }
+
for key, node := range cache.nodes {
if node.Dirty {
cache.db.Put([]byte(key), node.Value.Encode())
node.Dirty = false
}
}
+ cache.IsDirty = false
// If the nodes grows beyond the 200 entries we simple empty it
// FIXME come up with something better
@@ -80,6 +88,7 @@ func (cache *Cache) Undo() {
delete(cache.nodes, key)
}
}
+ cache.IsDirty = false
}
// A (modified) Radix Trie implementation. The Trie implements
@@ -103,6 +112,10 @@ func (t *Trie) Sync() {
t.cache.Commit()
}
+func (t *Trie) Undo() {
+ t.cache.Undo()
+}
+
/*
* Public (query) interface functions
*/