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"
|
|
|
|
. "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
|
|
|
}
|
|
|
|
|
2014-10-11 00:52:29 -07:00
|
|
|
func NewIAVLTree(keyCodec, valueCodec Codec, cacheSize int, 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-09-03 19:21:19 -07:00
|
|
|
}
|
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.
|
|
|
|
func (t *IAVLTree) Copy() *IAVLTree {
|
|
|
|
if t.ndb != nil && !t.root.persisted {
|
|
|
|
panic("It is unsafe to Copy() an unpersisted tree.")
|
|
|
|
// 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.
|
|
|
|
// On the other hand, in-memory trees (ndb == nil)
|
|
|
|
// don't mutate
|
|
|
|
} else if t.ndb == nil && t.root.hash == nil {
|
|
|
|
panic("An in-memory IAVLTree must be hashed first")
|
|
|
|
// An in-memory IAVLTree is finalized when the hashes are
|
|
|
|
// calculated.
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -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)
|
2014-09-03 19:21:19 -07:00
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNodeDB(cacheSize int, db DB) *nodeDB {
|
|
|
|
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
|
|
|
}
|