tendermint/merkle/iavl_tree.go

200 lines
4.0 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 00:52:29 -07:00
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/db"
2014-10-06 00:15:37 -07:00
)
const defaultCacheCapacity = 1000 // TODO make configurable.
2014-06-24 17:28:40 -07:00
/*
Immutable AVL Tree (wraps the Node root)
This tree is not concurrency safe.
You must wrap your calls with your own mutex.
*/
type IAVLTree struct {
2014-10-11 00:52:29 -07:00
keyCodec Codec
valueCodec Codec
root *IAVLNode
// Cache
cache map[string]nodeElement
cacheSize int
queue *list.List
// Persistence
db DB
2014-06-24 17:28:40 -07:00
}
2014-10-11 00:52:29 -07:00
func NewIAVLTree(keyCodec, valueCodec Codec, cacheSize int, db DB) *IAVLTree {
return &IAVLTree{
2014-10-11 00:52:29 -07:00
keyCodec: keyCodec,
valueCodec: valueCodec,
root: nil,
cache: make(map[string]nodeElement),
cacheSize: cacheSize,
queue: list.New(),
db: db,
}
2014-06-24 17:28:40 -07:00
}
2014-10-11 00:52:29 -07:00
func LoadIAVLTreeFromHash(keyCodec, valueCodec Codec, cacheSize int, db DB, hash []byte) *IAVLTree {
t := NewIAVLTree(keyCodec, valueCodec, cacheSize, db)
t.root = t.getNode(hash)
return t
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 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 00:52:29 -07:00
t.root = t.getNode(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 00:52:29 -07:00
func (t *IAVLTree) Checkpoint() interface{} {
return t.root
2014-10-05 21:21:34 -07:00
}
2014-10-11 00:52:29 -07:00
func (t *IAVLTree) Restore(checkpoint interface{}) {
t.root = checkpoint.(*IAVLNode)
}
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 00:52:29 -07:00
func (t *IAVLTree) getNode(hash []byte) *IAVLNode {
2014-10-06 00:15:37 -07:00
// Check the cache.
2014-10-11 00:52:29 -07:00
nodeElem, ok := t.cache[string(hash)]
2014-10-06 00:15:37 -07:00
if ok {
// Already exists. Move to back of queue.
2014-10-11 00:52:29 -07:00
t.queue.MoveToBack(nodeElem.elem)
2014-10-06 00:15:37 -07:00
return nodeElem.node
} else {
// Doesn't exist, load.
2014-10-11 00:52:29 -07:00
buf := t.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 00:52:29 -07:00
t.cacheNode(node)
2014-10-06 00:15:37 -07:00
return node
2014-10-05 21:21:34 -07:00
}
}
2014-10-11 00:52:29 -07:00
func (t *IAVLTree) cacheNode(node *IAVLNode) {
// Create entry in cache and append to queue.
elem := t.queue.PushBack(node.hash)
t.cache[string(node.hash)] = nodeElement{node, elem}
// Maybe expire an item.
if t.queue.Len() > t.cacheSize {
hash := t.queue.Remove(t.queue.Front()).([]byte)
delete(t.cache, string(hash))
}
}
func (t *IAVLTree) saveNode(node *IAVLNode) {
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 00:52:29 -07:00
if _, ok := t.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 00:52:29 -07:00
t.db.Set(node.hash, buf.Bytes())
2014-10-05 21:21:34 -07:00
node.persisted = true
2014-10-11 00:52:29 -07:00
t.cacheNode(node)
2014-06-24 17:28:40 -07:00
}