tendermint/merkle/iavl_tree.go

157 lines
2.8 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-10-05 21:21:34 -07:00
ndb IAVLNodeDB
2014-07-01 14:50:24 -07:00
root *IAVLNode
2014-06-24 17:28:40 -07:00
}
2014-10-05 21:21:34 -07:00
func NewIAVLTree(db DB) *IAVLTree {
return &IAVLTree{
2014-10-05 21:21:34 -07:00
ndb: NewIAVLNodeDB(db),
root: nil,
}
2014-06-24 17:28:40 -07:00
}
2014-10-05 21:21:34 -07:00
func LoadIAVLTreeFromHash(db DB, hash []byte) *IAVLTree {
ndb := NewIAVLNodeDB(db)
root := ndb.Get(hash)
if root == nil {
2014-08-10 16:35:08 -07:00
return nil
}
2014-10-05 21:21:34 -07:00
return &IAVLTree{ndb: ndb, root: root}
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
}
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
}
2014-10-05 21:21:34 -07:00
return t.root.has(t.ndb, 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-10-05 21:21:34 -07:00
t.root, updated = t.root.set(t.ndb, 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-10-05 21:21:34 -07:00
t.root.Save(t.ndb)
2014-08-10 16:35:08 -07:00
}
func (t *IAVLTree) SaveKey(key string) {
if t.root == nil {
return
2014-07-01 14:50:24 -07:00
}
hash, _ := t.root.HashWithCount()
2014-10-05 21:21:34 -07:00
t.root.Save(t.ndb)
t.ndb.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
}
2014-10-05 21:21:34 -07:00
return t.root.get(t.ndb, 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)
}
2014-10-05 21:21:34 -07:00
newRootHash, newRoot, _, value, err := t.root.remove(t.ndb, key)
2014-07-01 14:50:24 -07:00
if err != nil {
return nil, err
}
2014-10-05 21:21:34 -07:00
if newRoot == nil && newRootHash != nil {
t.root = t.ndb.Get(newRootHash)
} else {
t.root = newRoot
}
2014-07-01 14:50:24 -07:00
return value, nil
2014-06-24 17:28:40 -07:00
}
func (t *IAVLTree) Copy() Tree {
2014-10-05 21:21:34 -07:00
return &IAVLTree{ndb: t.ndb, root: t.root}
}
//-----------------------------------------------------------------------------
type IAVLNodeDB struct {
db DB
cache map[string]*IAVLNode
// XXX expire entries
}
func (ndb *IAVLNodeDB) Get(hash []byte) *IAVLNode {
buf := ndb.db.Get(hash)
r := bytes.NewReader(buf)
var n int64
var err error
node := ReadIAVLNode(r, &n, &err)
if err != nil {
panic(err)
}
node.persisted = true
ndb.cache[string(hash)] = node
return node
}
func (ndb *IAVLNodeDB) Save(node *IAVLNode) {
hash := node.hash
if hash != nil {
panic("Expected to find node.hash, but none found.")
}
buf := bytes.NewBuffer(nil)
_, err := self.WriteTo(buf)
if err != nil {
panic(err)
}
node.persisted = true
ndb.cache[string(hash)] = node
ndb.db.Set(hash, buf.Bytes())
2014-06-24 17:28:40 -07:00
}