2014-06-24 17:28:40 -07:00
|
|
|
package merkle
|
|
|
|
|
2014-06-25 21:37:20 -07:00
|
|
|
import (
|
2014-07-01 14:50:24 -07:00
|
|
|
. "github.com/tendermint/tendermint/binary"
|
2014-06-25 21:37:20 -07:00
|
|
|
)
|
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
const HASH_BYTE_SIZE int = 4 + 32
|
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-07-01 14:50:24 -07:00
|
|
|
db Db
|
|
|
|
root *IAVLNode
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAVLTree(db Db) *IAVLTree {
|
2014-07-01 14:50:24 -07:00
|
|
|
return &IAVLTree{db: db, root: nil}
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAVLTreeFromHash(db Db, hash ByteSlice) *IAVLTree {
|
2014-07-01 14:50:24 -07:00
|
|
|
root := &IAVLNode{
|
|
|
|
hash: hash,
|
|
|
|
flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
|
|
|
|
}
|
|
|
|
root.fill(db)
|
|
|
|
return &IAVLTree{db: db, root: root}
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Root() Node {
|
2014-07-01 14:50:24 -07:00
|
|
|
return t.root
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return t.root.Height()
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Has(key Key) bool {
|
2014-07-01 14:50:24 -07:00
|
|
|
if t.root == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return t.root.has(t.db, key)
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
2014-07-19 15:19:07 -07:00
|
|
|
func (t *IAVLTree) Set(key Key, value Value) (updated bool) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if t.root == nil {
|
|
|
|
t.root = NewIAVLNode(key, value)
|
|
|
|
return false
|
|
|
|
}
|
2014-07-19 15:19:07 -07:00
|
|
|
t.root, updated = t.root.set(t.db, key, value)
|
2014-07-01 14:50:24 -07:00
|
|
|
return updated
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Hash() (ByteSlice, uint64) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if t.root == nil {
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
return t.root.Hash()
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Save() {
|
2014-07-01 14:50:24 -07:00
|
|
|
if t.root == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.root.hash == nil {
|
|
|
|
t.root.Hash()
|
|
|
|
}
|
|
|
|
t.root.Save(t.db)
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Get(key Key) (value Value) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if t.root == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.root.get(t.db, key)
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Remove(key Key) (value Value, err error) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if t.root == nil {
|
|
|
|
return nil, NotFound(key)
|
|
|
|
}
|
|
|
|
newRoot, _, value, err := t.root.remove(t.db, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.root = newRoot
|
|
|
|
return value, nil
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Copy() Tree {
|
2014-07-01 14:50:24 -07:00
|
|
|
return &IAVLTree{db: t.db, root: t.root}
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Traverses all the nodes of the tree in prefix order.
|
|
|
|
// return true from cb to halt iteration.
|
|
|
|
// node.Height() == 0 if you just want a value node.
|
|
|
|
func (t *IAVLTree) Traverse(cb func(Node) bool) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if t.root == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.root.traverse(t.db, cb)
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IAVLTree) Values() <-chan Value {
|
2014-07-01 14:50:24 -07:00
|
|
|
root := t.root
|
|
|
|
ch := make(chan Value)
|
2014-07-02 16:03:04 -07:00
|
|
|
if root == nil {
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
2014-07-01 14:50:24 -07:00
|
|
|
go func() {
|
|
|
|
root.traverse(t.db, func(n Node) bool {
|
|
|
|
if n.Height() == 0 {
|
|
|
|
ch <- n.Value()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return ch
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|