tendermint/merkle/iavl_tree.go

254 lines
5.4 KiB
Go
Raw Normal View History

2014-06-24 17:28:40 -07:00
package merkle
2014-10-06 00:15:37 -07:00
import (
"bytes"
"container/list"
2014-10-11 20:39:13 -07:00
"sync"
2014-10-11 00:52:29 -07:00
. "github.com/tendermint/tendermint/binary"
db_ "github.com/tendermint/tendermint/db"
2014-10-06 00:15:37 -07:00
)
2014-06-24 17:28:40 -07:00
/*
Immutable AVL Tree (wraps the Node root)
2014-10-11 20:39:13 -07:00
This tree is not goroutine safe.
2014-06-24 17:28:40 -07:00
*/
type IAVLTree struct {
2014-10-11 00:52:29 -07:00
keyCodec Codec
valueCodec Codec
root *IAVLNode
2014-10-11 20:39:13 -07:00
ndb *nodeDB
2014-06-24 17:28:40 -07:00
}
func NewIAVLTree(keyCodec, valueCodec Codec, cacheSize int, db db_.DB) *IAVLTree {
2014-10-11 20:39:13 -07:00
if db == nil {
// In-memory IAVLTree
return &IAVLTree{
keyCodec: keyCodec,
valueCodec: valueCodec,
}
} else {
// Persistent IAVLTree
return &IAVLTree{
keyCodec: keyCodec,
valueCodec: valueCodec,
ndb: newNodeDB(cacheSize, db),
}
}
2014-06-24 17:28:40 -07:00
}
2014-10-11 20:39:13 -07:00
// The returned tree and the original tree are goroutine independent.
// That is, they can each run in their own goroutine.
2014-10-11 21:27:58 -07:00
func (t *IAVLTree) Copy() Tree {
if t.root == nil {
return &IAVLTree{
keyCodec: t.keyCodec,
valueCodec: t.valueCodec,
root: nil,
ndb: t.ndb,
}
}
2014-10-11 20:39:13 -07:00
if t.ndb != nil && !t.root.persisted {
// Saving a tree finalizes all the nodes.
// It sets all the hashes recursively,
// clears all the leftNode/rightNode values recursively,
// and all the .persisted flags get set.
panic("It is unsafe to Copy() an unpersisted tree.")
2014-10-11 20:39:13 -07:00
} else if t.ndb == nil && t.root.hash == nil {
// An in-memory IAVLTree is finalized when the hashes are
// calculated.
t.root.hashWithCount(t)
2014-10-11 20:39:13 -07:00
}
return &IAVLTree{
keyCodec: t.keyCodec,
valueCodec: t.valueCodec,
root: t.root,
ndb: t.ndb,
}
2014-08-10 16:35:08 -07:00
}
2014-06-24 17:28:40 -07:00
func (t *IAVLTree) Size() uint64 {
2014-07-01 14:50:24 -07:00
if t.root == nil {
return 0
}
2014-10-11 00:52:29 -07:00
return t.root.size
2014-06-24 17:28:40 -07:00
}
func (t *IAVLTree) Height() uint8 {
2014-07-01 14:50:24 -07:00
if t.root == nil {
return 0
}
2014-10-11 00:52:29 -07:00
return t.root.height
2014-06-24 17:28:40 -07:00
}
2014-10-11 00:52:29 -07:00
func (t *IAVLTree) Has(key interface{}) bool {
2014-07-01 14:50:24 -07:00
if t.root == nil {
return false
}
2014-10-11 00:52:29 -07:00
return t.root.has(t, key)
2014-06-24 17:28:40 -07:00
}
2014-10-11 00:52:29 -07:00
func (t *IAVLTree) Set(key interface{}, value interface{}) (updated bool) {
2014-07-01 14:50:24 -07:00
if t.root == nil {
t.root = NewIAVLNode(key, value)
return false
}
2014-10-11 00:52:29 -07:00
t.root, updated = t.root.set(t, key, value)
2014-07-01 14:50:24 -07:00
return updated
2014-06-24 17:28:40 -07:00
}
func (t *IAVLTree) Hash() []byte {
if t.root == nil {
return nil
}
2014-10-11 00:52:29 -07:00
hash, _ := t.root.hashWithCount(t)
return hash
}
func (t *IAVLTree) HashWithCount() ([]byte, uint64) {
2014-07-01 14:50:24 -07:00
if t.root == nil {
return nil, 0
}
2014-10-11 00:52:29 -07:00
return t.root.hashWithCount(t)
2014-06-24 17:28:40 -07:00
}
2014-10-06 13:55:56 -07:00
func (t *IAVLTree) Save() []byte {
2014-07-01 14:50:24 -07:00
if t.root == nil {
2014-10-06 13:55:56 -07:00
return nil
2014-07-01 14:50:24 -07:00
}
2014-10-11 00:52:29 -07:00
return t.root.save(t)
2014-08-10 16:35:08 -07:00
}
2014-10-11 20:39:13 -07:00
func (t *IAVLTree) Load(hash []byte) {
2014-12-17 01:37:13 -08:00
if len(hash) == 0 {
panic("IAVLTree.Load() hash was nil")
}
2014-10-11 20:39:13 -07:00
t.root = t.ndb.GetNode(t, hash)
}
2014-10-11 00:52:29 -07:00
func (t *IAVLTree) Get(key interface{}) (index uint64, value interface{}) {
2014-07-01 14:50:24 -07:00
if t.root == nil {
2014-10-11 00:52:29 -07:00
return 0, nil
2014-07-01 14:50:24 -07:00
}
2014-10-11 00:52:29 -07:00
return t.root.get(t, key)
2014-06-24 17:28:40 -07:00
}
2014-10-11 00:52:29 -07:00
func (t *IAVLTree) GetByIndex(index uint64) (key interface{}, value interface{}) {
2014-07-01 14:50:24 -07:00
if t.root == nil {
2014-10-11 00:52:29 -07:00
return nil, nil
2014-07-01 14:50:24 -07:00
}
2014-10-11 00:52:29 -07:00
return t.root.getByIndex(t, index)
}
func (t *IAVLTree) Remove(key interface{}) (value interface{}, removed bool) {
if t.root == nil {
return nil, false
}
newRootHash, newRoot, _, value, removed := t.root.remove(t, key)
if !removed {
return nil, false
2014-07-01 14:50:24 -07:00
}
2014-10-05 21:21:34 -07:00
if newRoot == nil && newRootHash != nil {
2014-10-11 20:39:13 -07:00
t.root = t.ndb.GetNode(t, newRootHash)
2014-10-05 21:21:34 -07:00
} else {
t.root = newRoot
}
2014-10-11 00:52:29 -07:00
return value, true
2014-06-24 17:28:40 -07:00
}
2014-10-11 20:39:13 -07:00
func (t *IAVLTree) Iterate(fn func(key interface{}, value interface{}) bool) (stopped bool) {
2014-10-12 21:14:10 -07:00
if t.root == nil {
return false
}
2014-10-11 20:39:13 -07:00
return t.root.traverse(t, func(node *IAVLNode) bool {
if node.height == 0 {
return fn(node.key, node.value)
} else {
return false
}
})
2014-10-05 21:21:34 -07:00
}
2014-10-11 20:39:13 -07:00
//-----------------------------------------------------------------------------
2014-10-05 21:21:34 -07:00
2014-10-06 00:15:37 -07:00
type nodeElement struct {
node *IAVLNode
elem *list.Element
}
2014-10-11 20:39:13 -07:00
type nodeDB struct {
mtx sync.Mutex
cache map[string]nodeElement
cacheSize int
cacheQueue *list.List
db db_.DB
2014-10-11 20:39:13 -07:00
}
func newNodeDB(cacheSize int, db db_.DB) *nodeDB {
2014-10-11 20:39:13 -07:00
return &nodeDB{
cache: make(map[string]nodeElement),
cacheSize: cacheSize,
cacheQueue: list.New(),
db: db,
}
}
func (ndb *nodeDB) GetNode(t *IAVLTree, hash []byte) *IAVLNode {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
2014-10-06 00:15:37 -07:00
// Check the cache.
2014-10-11 20:39:13 -07:00
nodeElem, ok := ndb.cache[string(hash)]
2014-10-06 00:15:37 -07:00
if ok {
2014-10-11 20:39:13 -07:00
// Already exists. Move to back of cacheQueue.
ndb.cacheQueue.MoveToBack(nodeElem.elem)
2014-10-06 00:15:37 -07:00
return nodeElem.node
} else {
// Doesn't exist, load.
2014-10-11 20:39:13 -07:00
buf := ndb.db.Get(hash)
2014-10-06 00:15:37 -07:00
r := bytes.NewReader(buf)
var n int64
var err error
2014-10-11 00:52:29 -07:00
node := ReadIAVLNode(t, r, &n, &err)
2014-10-06 00:15:37 -07:00
if err != nil {
panic(err)
}
node.persisted = true
2014-10-11 20:39:13 -07:00
ndb.cacheNode(node)
2014-10-06 00:15:37 -07:00
return node
2014-10-05 21:21:34 -07:00
}
}
2014-10-11 20:39:13 -07:00
func (ndb *nodeDB) SaveNode(t *IAVLTree, node *IAVLNode) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
2014-10-06 00:15:37 -07:00
if node.hash == nil {
2014-10-05 21:21:34 -07:00
panic("Expected to find node.hash, but none found.")
}
2014-10-06 00:15:37 -07:00
if node.persisted {
panic("Shouldn't be calling save on an already persisted node.")
}
2014-10-11 20:39:13 -07:00
if _, ok := ndb.cache[string(node.hash)]; ok {
2014-10-06 00:15:37 -07:00
panic("Shouldn't be calling save on an already cached node.")
}
// Save node bytes to db
2014-10-05 21:21:34 -07:00
buf := bytes.NewBuffer(nil)
2014-10-11 00:52:29 -07:00
_, _, err := node.writeToCountHashes(t, buf)
2014-10-05 21:21:34 -07:00
if err != nil {
panic(err)
}
2014-10-11 20:39:13 -07:00
ndb.db.Set(node.hash, buf.Bytes())
2014-10-05 21:21:34 -07:00
node.persisted = true
2014-10-11 20:39:13 -07:00
ndb.cacheNode(node)
}
func (ndb *nodeDB) cacheNode(node *IAVLNode) {
// Create entry in cache and append to cacheQueue.
elem := ndb.cacheQueue.PushBack(node.hash)
ndb.cache[string(node.hash)] = nodeElement{node, elem}
// Maybe expire an item.
if ndb.cacheQueue.Len() > ndb.cacheSize {
hash := ndb.cacheQueue.Remove(ndb.cacheQueue.Front()).([]byte)
delete(ndb.cache, string(hash))
}
2014-06-24 17:28:40 -07:00
}