2014-06-24 17:28:40 -07:00
|
|
|
package merkle
|
|
|
|
|
2014-10-06 00:15:37 -07:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"container/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultCacheCapacity = 1000 // TODO make configurable.
|
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-06 00:15:37 -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 {
|
2014-09-03 19:21:19 -07:00
|
|
|
return &IAVLTree{
|
2014-10-06 00:15:37 -07:00
|
|
|
ndb: NewIAVLNodeDB(defaultCacheCapacity, db),
|
2014-09-03 19:21:19 -07:00
|
|
|
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 {
|
2014-10-06 00:15:37 -07:00
|
|
|
ndb := NewIAVLNodeDB(defaultCacheCapacity, db)
|
2014-10-05 21:21:34 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -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
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -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
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -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
|
|
|
|
}
|
2014-09-03 19:21:19 -07:00
|
|
|
return t.root.HashWithCount()
|
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-06 13:55:56 -07:00
|
|
|
return t.root.Save(t.ndb)
|
2014-08-10 16:35:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -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
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -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}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-06 00:15:37 -07:00
|
|
|
type nodeElement struct {
|
|
|
|
node *IAVLNode
|
|
|
|
elem *list.Element
|
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
type IAVLNodeDB struct {
|
2014-10-06 00:15:37 -07:00
|
|
|
capacity int
|
|
|
|
db DB
|
|
|
|
cache map[string]nodeElement
|
|
|
|
queue *list.List
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAVLNodeDB(capacity int, db DB) *IAVLNodeDB {
|
|
|
|
return &IAVLNodeDB{
|
|
|
|
capacity: capacity,
|
|
|
|
db: db,
|
|
|
|
cache: make(map[string]nodeElement),
|
|
|
|
queue: list.New(),
|
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ndb *IAVLNodeDB) Get(hash []byte) *IAVLNode {
|
2014-10-06 00:15:37 -07:00
|
|
|
// Check the cache.
|
|
|
|
nodeElem, ok := ndb.cache[string(hash)]
|
|
|
|
if ok {
|
|
|
|
// Already exists. Move to back of queue.
|
|
|
|
ndb.queue.MoveToBack(nodeElem.elem)
|
|
|
|
return nodeElem.node
|
|
|
|
} else {
|
|
|
|
// Doesn't exist, load.
|
|
|
|
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.cacheNode(node)
|
|
|
|
return node
|
2014-10-05 21:21:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ndb *IAVLNodeDB) Save(node *IAVLNode) {
|
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.")
|
|
|
|
}
|
|
|
|
if _, ok := ndb.cache[string(node.hash)]; ok {
|
|
|
|
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-06 00:15:37 -07:00
|
|
|
_, err := node.WriteTo(buf)
|
2014-10-05 21:21:34 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-06 00:15:37 -07:00
|
|
|
ndb.db.Set(node.hash, buf.Bytes())
|
2014-10-05 21:21:34 -07:00
|
|
|
node.persisted = true
|
2014-10-06 00:15:37 -07:00
|
|
|
ndb.cacheNode(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ndb *IAVLNodeDB) cacheNode(node *IAVLNode) {
|
|
|
|
// Create entry in cache and append to queue.
|
|
|
|
elem := ndb.queue.PushBack(node.hash)
|
|
|
|
ndb.cache[string(node.hash)] = nodeElement{node, elem}
|
|
|
|
// Maybe expire an item.
|
|
|
|
if ndb.queue.Len() > ndb.capacity {
|
|
|
|
hash := ndb.queue.Remove(ndb.queue.Front()).([]byte)
|
|
|
|
delete(ndb.cache, string(hash))
|
|
|
|
}
|
2014-06-24 17:28:40 -07:00
|
|
|
}
|