Added caching and database interface to trie

* Reimplemented caching for trie
* Reimplemented resetting and persisting trie
This commit is contained in:
obscuren 2014-11-19 16:21:28 +01:00
parent e70529a977
commit 0f460ad26e
6 changed files with 135 additions and 39 deletions

View File

@ -23,6 +23,10 @@ func (db *MemDatabase) Put(key []byte, value []byte) {
db.db[string(key)] = value db.db[string(key)] = value
} }
func (db *MemDatabase) Set(key []byte, value []byte) {
db.Put(key, value)
}
func (db *MemDatabase) Get(key []byte) ([]byte, error) { func (db *MemDatabase) Get(key []byte) ([]byte, error) {
return db.db[string(key)], nil return db.db[string(key)], nil
} }

View File

@ -18,7 +18,14 @@ func (self *FullNode) Branches() []Node {
return self.nodes[:16] return self.nodes[:16]
} }
func (self *FullNode) Copy() Node { return self } func (self *FullNode) Copy() Node {
nnode := NewFullNode(self.trie)
for i, node := range self.nodes {
nnode.nodes[i] = node
}
return nnode
}
// Returns the length of non-nil nodes // Returns the length of non-nil nodes
func (self *FullNode) Len() (amount int) { func (self *FullNode) Len() (amount int) {

View File

@ -17,7 +17,7 @@ func (self *ShortNode) Value() Node {
return self.value return self.value
} }
func (self *ShortNode) Dirty() bool { return true } func (self *ShortNode) Dirty() bool { return true }
func (self *ShortNode) Copy() Node { return self } func (self *ShortNode) Copy() Node { return NewShortNode(self.trie, self.key, self.value) }
func (self *ShortNode) RlpData() interface{} { func (self *ShortNode) RlpData() interface{} {
return []interface{}{self.key, self.value.Hash()} return []interface{}{self.key, self.value.Hash()}

View File

@ -2,6 +2,7 @@ package ptrie
import ( import (
"bytes" "bytes"
"container/list"
"sync" "sync"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -14,33 +15,61 @@ type Backend interface {
Set([]byte, []byte) Set([]byte, []byte)
} }
type Cache map[string][]byte type Cache struct {
store map[string][]byte
func (self Cache) Get(key []byte) []byte { backend Backend
return self[string(key)]
} }
func (self Cache) Set(key []byte, data []byte) {
self[string(key)] = data func NewCache(backend Backend) *Cache {
return &Cache{make(map[string][]byte), backend}
}
func (self *Cache) Get(key []byte) []byte {
data := self.store[string(key)]
if data == nil {
data = self.backend.Get(key)
}
return data
}
func (self *Cache) Set(key []byte, data []byte) {
self.store[string(key)] = data
}
func (self *Cache) Flush() {
for k, v := range self.store {
self.backend.Set([]byte(k), v)
}
// This will eventually grow too large. We'd could
// do a make limit on storage and push out not-so-popular nodes.
//self.Reset()
}
func (self *Cache) Reset() {
self.store = make(map[string][]byte)
} }
type Trie struct { type Trie struct {
mu sync.Mutex mu sync.Mutex
root Node root Node
roothash []byte roothash []byte
backend Backend cache *Cache
}
func NewEmpty() *Trie { revisions *list.List
return &Trie{sync.Mutex{}, nil, nil, make(Cache)}
} }
func New(root []byte, backend Backend) *Trie { func New(root []byte, backend Backend) *Trie {
trie := &Trie{} trie := &Trie{}
trie.revisions = list.New()
trie.roothash = root trie.roothash = root
trie.backend = backend trie.cache = NewCache(backend)
value := ethutil.NewValueFromBytes(trie.backend.Get(root)) if root != nil {
trie.root = trie.mknode(value) value := ethutil.NewValueFromBytes(trie.cache.Get(root))
trie.root = trie.mknode(value)
}
return trie return trie
} }
@ -64,10 +93,28 @@ func (self *Trie) Hash() []byte {
hash = crypto.Sha3(ethutil.Encode(self.root)) hash = crypto.Sha3(ethutil.Encode(self.root))
} }
self.roothash = hash if !bytes.Equal(hash, self.roothash) {
self.revisions.PushBack(self.roothash)
self.roothash = hash
}
return hash return hash
} }
func (self *Trie) Commit() {
// Hash first
self.Hash()
self.cache.Flush()
}
func (self *Trie) Reset() {
self.cache.Reset()
revision := self.revisions.Remove(self.revisions.Back()).([]byte)
self.roothash = revision
value := ethutil.NewValueFromBytes(self.cache.Get(self.roothash))
self.root = self.mknode(value)
}
func (self *Trie) UpdateString(key, value string) Node { return self.Update([]byte(key), []byte(value)) } func (self *Trie) UpdateString(key, value string) Node { return self.Update([]byte(key), []byte(value)) }
func (self *Trie) Update(key, value []byte) Node { func (self *Trie) Update(key, value []byte) Node {
@ -272,7 +319,7 @@ func (self *Trie) mknode(value *ethutil.Value) Node {
func (self *Trie) trans(node Node) Node { func (self *Trie) trans(node Node) Node {
switch node := node.(type) { switch node := node.(type) {
case *HashNode: case *HashNode:
value := ethutil.NewValueFromBytes(self.backend.Get(node.key)) value := ethutil.NewValueFromBytes(self.cache.Get(node.key))
return self.mknode(value) return self.mknode(value)
default: default:
return node return node
@ -283,7 +330,7 @@ func (self *Trie) store(node Node) interface{} {
data := ethutil.Encode(node) data := ethutil.Encode(node)
if len(data) >= 32 { if len(data) >= 32 {
key := crypto.Sha3(data) key := crypto.Sha3(data)
self.backend.Set(key, data) self.cache.Set(key, data)
return key return key
} }

View File

@ -8,6 +8,16 @@ import (
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
) )
type Db map[string][]byte
func (self Db) Get(k []byte) []byte { return self[string(k)] }
func (self Db) Set(k, v []byte) { self[string(k)] = v }
// Used for testing
func NewEmpty() *Trie {
return New(nil, make(Db))
}
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
trie := NewEmpty() trie := NewEmpty()
@ -91,7 +101,7 @@ func TestReplication(t *testing.T) {
} }
trie.Hash() trie.Hash()
trie2 := New(trie.roothash, trie.backend) trie2 := New(trie.roothash, trie.cache)
if string(trie2.GetString("horse")) != "stallion" { if string(trie2.GetString("horse")) != "stallion" {
t.Error("expected to have harse => stallion") t.Error("expected to have harse => stallion")
} }
@ -104,6 +114,53 @@ func TestReplication(t *testing.T) {
} }
func TestReset(t *testing.T) {
trie := NewEmpty()
vals := []struct{ k, v string }{
{"do", "verb"},
{"ether", "wookiedoo"},
{"horse", "stallion"},
}
for _, val := range vals {
trie.UpdateString(val.k, val.v)
}
trie.Commit()
before := ethutil.CopyBytes(trie.roothash)
trie.UpdateString("should", "revert")
trie.Hash()
// Should have no effect
trie.Hash()
trie.Hash()
// ###
trie.Reset()
after := ethutil.CopyBytes(trie.roothash)
if !bytes.Equal(before, after) {
t.Errorf("expected roots to be equal. %x - %x", before, after)
}
}
// Not actual test
func TestOutput(t *testing.T) {
t.Skip()
base := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
trie := NewEmpty()
for i := 0; i < 50; i++ {
trie.UpdateString(fmt.Sprintf("%s%d", base, i), "valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
}
trie.Hash()
fmt.Println("############################## FULL ################################")
fmt.Println(trie.root)
trie2 := New(trie.roothash, trie.cache)
trie2.GetString(base + "20")
fmt.Println("############################## SMALL ################################")
fmt.Println(trie2.root)
}
func BenchmarkGets(b *testing.B) { func BenchmarkGets(b *testing.B) {
trie := NewEmpty() trie := NewEmpty()
vals := []struct{ k, v string }{ vals := []struct{ k, v string }{
@ -136,22 +193,3 @@ func BenchmarkUpdate(b *testing.B) {
} }
trie.Hash() trie.Hash()
} }
// Not actual test
func TestOutput(t *testing.T) {
t.Skip()
base := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
trie := NewEmpty()
for i := 0; i < 50; i++ {
trie.UpdateString(fmt.Sprintf("%s%d", base, i), "valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
}
trie.Hash()
fmt.Println("############################## FULL ################################")
fmt.Println(trie.root)
trie2 := New(trie.roothash, trie.backend)
trie2.GetString(base + "20")
fmt.Println("############################## SMALL ################################")
fmt.Println(trie2.root)
}

View File

@ -8,6 +8,6 @@ type ValueNode struct {
func (self *ValueNode) Value() Node { return self } // Best not to call :-) func (self *ValueNode) Value() Node { return self } // Best not to call :-)
func (self *ValueNode) Val() []byte { return self.data } func (self *ValueNode) Val() []byte { return self.data }
func (self *ValueNode) Dirty() bool { return true } func (self *ValueNode) Dirty() bool { return true }
func (self *ValueNode) Copy() Node { return self } func (self *ValueNode) Copy() Node { return &ValueNode{self.trie, self.data} }
func (self *ValueNode) RlpData() interface{} { return self.data } func (self *ValueNode) RlpData() interface{} { return self.data }
func (self *ValueNode) Hash() interface{} { return self.data } func (self *ValueNode) Hash() interface{} { return self.data }