mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-31 20:21:56 +00:00
IAVLTree supports Codec.
This commit is contained in:
@@ -3,6 +3,9 @@ package merkle
|
||||
import (
|
||||
"bytes"
|
||||
"container/list"
|
||||
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/db"
|
||||
)
|
||||
|
||||
const defaultCacheCapacity = 1000 // TODO make configurable.
|
||||
@@ -14,53 +17,64 @@ This tree is not concurrency safe.
|
||||
You must wrap your calls with your own mutex.
|
||||
*/
|
||||
type IAVLTree struct {
|
||||
ndb *IAVLNodeDB
|
||||
root *IAVLNode
|
||||
keyCodec Codec
|
||||
valueCodec Codec
|
||||
root *IAVLNode
|
||||
|
||||
// Cache
|
||||
cache map[string]nodeElement
|
||||
cacheSize int
|
||||
queue *list.List
|
||||
|
||||
// Persistence
|
||||
db DB
|
||||
}
|
||||
|
||||
func NewIAVLTree(db DB) *IAVLTree {
|
||||
func NewIAVLTree(keyCodec, valueCodec Codec, cacheSize int, db DB) *IAVLTree {
|
||||
return &IAVLTree{
|
||||
ndb: NewIAVLNodeDB(defaultCacheCapacity, db),
|
||||
root: nil,
|
||||
keyCodec: keyCodec,
|
||||
valueCodec: valueCodec,
|
||||
root: nil,
|
||||
cache: make(map[string]nodeElement),
|
||||
cacheSize: cacheSize,
|
||||
queue: list.New(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func LoadIAVLTreeFromHash(db DB, hash []byte) *IAVLTree {
|
||||
ndb := NewIAVLNodeDB(defaultCacheCapacity, db)
|
||||
root := ndb.Get(hash)
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
return &IAVLTree{ndb: ndb, root: root}
|
||||
func LoadIAVLTreeFromHash(keyCodec, valueCodec Codec, cacheSize int, db DB, hash []byte) *IAVLTree {
|
||||
t := NewIAVLTree(keyCodec, valueCodec, cacheSize, db)
|
||||
t.root = t.getNode(hash)
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Size() uint64 {
|
||||
if t.root == nil {
|
||||
return 0
|
||||
}
|
||||
return t.root.Size()
|
||||
return t.root.size
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Height() uint8 {
|
||||
if t.root == nil {
|
||||
return 0
|
||||
}
|
||||
return t.root.Height()
|
||||
return t.root.height
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Has(key []byte) bool {
|
||||
func (t *IAVLTree) Has(key interface{}) bool {
|
||||
if t.root == nil {
|
||||
return false
|
||||
}
|
||||
return t.root.has(t.ndb, key)
|
||||
return t.root.has(t, key)
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Set(key []byte, value []byte) (updated bool) {
|
||||
func (t *IAVLTree) Set(key interface{}, value interface{}) (updated bool) {
|
||||
if t.root == nil {
|
||||
t.root = NewIAVLNode(key, value)
|
||||
return false
|
||||
}
|
||||
t.root, updated = t.root.set(t.ndb, key, value)
|
||||
t.root, updated = t.root.set(t, key, value)
|
||||
return updated
|
||||
}
|
||||
|
||||
@@ -68,7 +82,7 @@ func (t *IAVLTree) Hash() []byte {
|
||||
if t.root == nil {
|
||||
return nil
|
||||
}
|
||||
hash, _ := t.root.HashWithCount()
|
||||
hash, _ := t.root.hashWithCount(t)
|
||||
return hash
|
||||
}
|
||||
|
||||
@@ -76,117 +90,110 @@ func (t *IAVLTree) HashWithCount() ([]byte, uint64) {
|
||||
if t.root == nil {
|
||||
return nil, 0
|
||||
}
|
||||
return t.root.HashWithCount()
|
||||
return t.root.hashWithCount(t)
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Save() []byte {
|
||||
if t.root == nil {
|
||||
return nil
|
||||
}
|
||||
return t.root.Save(t.ndb)
|
||||
return t.root.save(t)
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Get(key []byte) (value []byte) {
|
||||
func (t *IAVLTree) Get(key interface{}) (index uint64, value interface{}) {
|
||||
if t.root == nil {
|
||||
return nil
|
||||
return 0, nil
|
||||
}
|
||||
return t.root.get(t.ndb, key)
|
||||
return t.root.get(t, key)
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Remove(key []byte) (value []byte, err error) {
|
||||
func (t *IAVLTree) GetByIndex(index uint64) (key interface{}, value interface{}) {
|
||||
if t.root == nil {
|
||||
return nil, NotFound(key)
|
||||
return nil, nil
|
||||
}
|
||||
newRootHash, newRoot, _, value, err := t.root.remove(t.ndb, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
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
|
||||
}
|
||||
if newRoot == nil && newRootHash != nil {
|
||||
t.root = t.ndb.Get(newRootHash)
|
||||
t.root = t.getNode(newRootHash)
|
||||
} else {
|
||||
t.root = newRoot
|
||||
}
|
||||
return value, nil
|
||||
return value, true
|
||||
}
|
||||
|
||||
func (t *IAVLTree) Copy() Tree {
|
||||
return &IAVLTree{ndb: t.ndb, root: t.root}
|
||||
func (t *IAVLTree) Checkpoint() interface{} {
|
||||
return t.root
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
func (t *IAVLTree) Restore(checkpoint interface{}) {
|
||||
t.root = checkpoint.(*IAVLNode)
|
||||
}
|
||||
|
||||
type nodeElement struct {
|
||||
node *IAVLNode
|
||||
elem *list.Element
|
||||
}
|
||||
|
||||
type IAVLNodeDB struct {
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
||||
func (ndb *IAVLNodeDB) Get(hash []byte) *IAVLNode {
|
||||
func (t *IAVLTree) getNode(hash []byte) *IAVLNode {
|
||||
// Check the cache.
|
||||
nodeElem, ok := ndb.cache[string(hash)]
|
||||
nodeElem, ok := t.cache[string(hash)]
|
||||
if ok {
|
||||
// Already exists. Move to back of queue.
|
||||
ndb.queue.MoveToBack(nodeElem.elem)
|
||||
t.queue.MoveToBack(nodeElem.elem)
|
||||
return nodeElem.node
|
||||
} else {
|
||||
// Doesn't exist, load.
|
||||
buf := ndb.db.Get(hash)
|
||||
buf := t.db.Get(hash)
|
||||
r := bytes.NewReader(buf)
|
||||
var n int64
|
||||
var err error
|
||||
node := ReadIAVLNode(r, &n, &err)
|
||||
node := ReadIAVLNode(t, r, &n, &err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
node.persisted = true
|
||||
ndb.cacheNode(node)
|
||||
t.cacheNode(node)
|
||||
return node
|
||||
}
|
||||
}
|
||||
|
||||
func (ndb *IAVLNodeDB) Save(node *IAVLNode) {
|
||||
func (t *IAVLTree) cacheNode(node *IAVLNode) {
|
||||
// Create entry in cache and append to queue.
|
||||
elem := t.queue.PushBack(node.hash)
|
||||
t.cache[string(node.hash)] = nodeElement{node, elem}
|
||||
// Maybe expire an item.
|
||||
if t.queue.Len() > t.cacheSize {
|
||||
hash := t.queue.Remove(t.queue.Front()).([]byte)
|
||||
delete(t.cache, string(hash))
|
||||
}
|
||||
}
|
||||
|
||||
func (t *IAVLTree) saveNode(node *IAVLNode) {
|
||||
if node.hash == nil {
|
||||
panic("Expected to find node.hash, but none found.")
|
||||
}
|
||||
if node.persisted {
|
||||
panic("Shouldn't be calling save on an already persisted node.")
|
||||
}
|
||||
if _, ok := ndb.cache[string(node.hash)]; ok {
|
||||
if _, ok := t.cache[string(node.hash)]; ok {
|
||||
panic("Shouldn't be calling save on an already cached node.")
|
||||
}
|
||||
// Save node bytes to db
|
||||
buf := bytes.NewBuffer(nil)
|
||||
_, err := node.WriteTo(buf)
|
||||
_, _, err := node.writeToCountHashes(t, buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ndb.db.Set(node.hash, buf.Bytes())
|
||||
t.db.Set(node.hash, buf.Bytes())
|
||||
node.persisted = true
|
||||
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))
|
||||
}
|
||||
t.cacheNode(node)
|
||||
}
|
||||
|
Reference in New Issue
Block a user