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
|
|
|
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/binary"
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
|
|
|
dbm "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 {
|
2015-01-14 20:34:53 -08:00
|
|
|
keyCodec binary.Codec
|
|
|
|
valueCodec binary.Codec
|
2014-10-11 00:52:29 -07:00
|
|
|
root *IAVLNode
|
2014-10-11 20:39:13 -07:00
|
|
|
ndb *nodeDB
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
func NewIAVLTree(keyCodec, valueCodec binary.Codec, cacheSize int, db dbm.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.
|
2014-10-11 21:27:58 -07:00
|
|
|
func (t *IAVLTree) Copy() Tree {
|
2014-10-13 20:07:26 -07:00
|
|
|
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.
|
2015-07-19 23:42:52 +00:00
|
|
|
PanicSanity("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.
|
2014-10-13 20:07:26 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (t *IAVLTree) Size() int {
|
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
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (t *IAVLTree) Height() int8 {
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (t *IAVLTree) HashWithCount() ([]byte, int) {
|
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
|
|
|
}
|
|
|
|
|
2015-03-20 20:50:38 -07:00
|
|
|
// Sets the root node by reading from db.
|
|
|
|
// If the hash is empty, then sets root to nil.
|
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 {
|
2015-03-20 20:50:38 -07:00
|
|
|
t.root = nil
|
|
|
|
} else {
|
|
|
|
t.root = t.ndb.GetNode(t, hash)
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
2014-10-11 20:39:13 -07:00
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (t *IAVLTree) Get(key interface{}) (index int, 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
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (t *IAVLTree) GetByIndex(index int) (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
|
2015-01-14 20:34:53 -08:00
|
|
|
db dbm.DB
|
2014-10-11 20:39:13 -07:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
func newNodeDB(cacheSize int, db dbm.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)
|
2015-01-10 05:41:50 -08:00
|
|
|
if len(buf) == 0 {
|
2015-01-14 20:34:53 -08:00
|
|
|
ndb.db.(*dbm.LevelDB).Print()
|
2015-07-19 23:42:52 +00:00
|
|
|
PanicSanity(Fmt("Value missing for key %X", hash))
|
2015-01-10 05:41:50 -08:00
|
|
|
}
|
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 {
|
2015-07-19 23:42:52 +00:00
|
|
|
PanicCrisis(Fmt("Error reading IAVLNode. bytes: %X error: %v", buf, err))
|
2014-10-06 00:15:37 -07:00
|
|
|
}
|
2015-05-24 14:19:46 -07:00
|
|
|
node.hash = hash
|
2014-10-06 00:15:37 -07:00
|
|
|
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 {
|
2015-07-19 23:42:52 +00:00
|
|
|
PanicSanity("Expected to find node.hash, but none found.")
|
2014-10-05 21:21:34 -07:00
|
|
|
}
|
2014-10-06 00:15:37 -07:00
|
|
|
if node.persisted {
|
2015-07-19 23:42:52 +00:00
|
|
|
PanicSanity("Shouldn't be calling save on an already persisted node.")
|
2014-10-06 00:15:37 -07:00
|
|
|
}
|
2015-04-17 17:43:45 -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.")
|
2015-04-17 17:43:45 -07:00
|
|
|
}*/
|
2014-10-06 00:15:37 -07:00
|
|
|
// Save node bytes to db
|
2014-10-05 21:21:34 -07:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2015-05-24 14:19:46 -07:00
|
|
|
_, err := node.writePersistBytes(t, buf)
|
2014-10-05 21:21:34 -07:00
|
|
|
if err != nil {
|
2015-07-19 23:42:52 +00:00
|
|
|
PanicCrisis(err)
|
2014-10-05 21:21:34 -07:00
|
|
|
}
|
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
|
|
|
}
|