tendermint/merkle/iavl_tree.go

130 lines
2.2 KiB
Go
Raw Normal View History

2014-06-24 17:28:40 -07:00
package merkle
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 {
return &IAVLTree{
db: db,
root: nil,
}
2014-06-24 17:28:40 -07:00
}
// TODO rename to Load.
func NewIAVLTreeFromHash(db Db, hash []byte) *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
}
2014-08-10 16:35:08 -07:00
func NewIAVLTreeFromKey(db Db, key string) *IAVLTree {
hash := db.Get([]byte(key))
if hash == nil {
return nil
}
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) 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 []byte) 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
}
func (t *IAVLTree) Set(key []byte, value []byte) (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() []byte {
if t.root == nil {
return nil
}
hash, _ := t.root.HashWithCount()
return hash
}
func (t *IAVLTree) HashWithCount() ([]byte, uint64) {
2014-07-01 14:50:24 -07:00
if t.root == nil {
return nil, 0
}
return t.root.HashWithCount()
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
}
t.root.HashWithCount()
2014-08-10 16:35:08 -07:00
t.root.Save(t.db)
}
func (t *IAVLTree) SaveKey(key string) {
if t.root == nil {
return
2014-07-01 14:50:24 -07:00
}
hash, _ := t.root.HashWithCount()
2014-07-01 14:50:24 -07:00
t.root.Save(t.db)
2014-08-10 16:35:08 -07:00
t.db.Set([]byte(key), hash)
2014-06-24 17:28:40 -07:00
}
func (t *IAVLTree) Get(key []byte) (value []byte) {
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 []byte) (value []byte, 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
}