2014-05-19 20:46:41 -07:00
|
|
|
package merkle
|
|
|
|
|
|
|
|
import (
|
2014-07-01 14:50:24 -07:00
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
. "github.com/tendermint/tendermint/binary"
|
|
|
|
"io"
|
2014-05-19 20:46:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Node
|
|
|
|
|
|
|
|
type IAVLNode struct {
|
2014-10-05 21:21:34 -07:00
|
|
|
key []byte
|
|
|
|
value []byte
|
|
|
|
size uint64
|
|
|
|
height uint8
|
|
|
|
hash []byte
|
|
|
|
leftHash []byte
|
|
|
|
rightHash []byte
|
|
|
|
persisted bool
|
|
|
|
|
|
|
|
// May or may not be persisted nodes, but they'll get cleared
|
|
|
|
// when this this node is saved.
|
|
|
|
leftCached *IAVLNode
|
|
|
|
rightCached *IAVLNode
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -07:00
|
|
|
func NewIAVLNode(key []byte, value []byte) *IAVLNode {
|
2014-07-01 14:50:24 -07:00
|
|
|
return &IAVLNode{
|
2014-10-05 21:21:34 -07:00
|
|
|
key: key,
|
|
|
|
value: value,
|
|
|
|
size: 1,
|
|
|
|
persisted: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadIAVLNode(r io.Reader, n *int64, err *error) *IAVLNode {
|
|
|
|
node := &IAVLNode{}
|
|
|
|
|
|
|
|
// node header & key
|
|
|
|
node.height = ReadUInt8(r, &n, &err)
|
|
|
|
node.size = ReadUInt64(r, &n, &err)
|
|
|
|
node.key = ReadByteSlice(r, &n, &err)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// node value or children.
|
|
|
|
if node.height == 0 {
|
|
|
|
// value
|
|
|
|
node.value = ReadByteSlice(r, &n, &err)
|
|
|
|
} else {
|
|
|
|
// left
|
|
|
|
node.leftHash = ReadByteSlice(r, &n, &err)
|
|
|
|
// right
|
|
|
|
node.rightHash = ReadByteSlice(r, &n, &err)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return node
|
2014-05-27 22:31:47 -07:00
|
|
|
}
|
|
|
|
|
2014-05-21 21:48:41 -07:00
|
|
|
func (self *IAVLNode) Copy() *IAVLNode {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.height == 0 {
|
|
|
|
panic("Why are you copying a value node?")
|
|
|
|
}
|
|
|
|
return &IAVLNode{
|
2014-10-05 21:21:34 -07:00
|
|
|
key: self.key,
|
|
|
|
size: self.size,
|
|
|
|
height: self.height,
|
|
|
|
hash: nil, // Going to be mutated anyways.
|
|
|
|
leftHash: self.leftHash,
|
|
|
|
rightHash: self.rightHash,
|
|
|
|
persisted: self.persisted,
|
|
|
|
leftCached: self.leftCached,
|
|
|
|
rightCached: self.rightCached,
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-05-21 21:48:41 -07:00
|
|
|
func (self *IAVLNode) Size() uint64 {
|
2014-07-01 14:50:24 -07:00
|
|
|
return self.size
|
2014-05-21 16:24:50 -07:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:08:49 -07:00
|
|
|
func (self *IAVLNode) Height() uint8 {
|
2014-07-01 14:50:24 -07:00
|
|
|
return self.height
|
2014-05-22 18:08:49 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) has(ndb *IAVLNodeDB, key []byte) (has bool) {
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Equal(self.key, key) {
|
2014-07-01 14:50:24 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if self.height == 0 {
|
|
|
|
return false
|
|
|
|
} else {
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Compare(key, self.key) == -1 {
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.getLeft(ndb).has(ndb, key)
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.getRight(ndb).has(ndb, key)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) get(ndb *IAVLNodeDB, key []byte) (value []byte) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.height == 0 {
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Equal(self.key, key) {
|
2014-07-01 14:50:24 -07:00
|
|
|
return self.value
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Compare(key, self.key) == -1 {
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.getLeft(ndb).get(ndb, key)
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.getRight(ndb).get(ndb, key)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -07:00
|
|
|
func (self *IAVLNode) HashWithCount() ([]byte, uint64) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.hash != nil {
|
|
|
|
return self.hash, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
hasher := sha256.New()
|
2014-08-10 16:35:08 -07:00
|
|
|
_, hashCount, err := self.saveToCountHashes(hasher)
|
2014-07-01 14:50:24 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
self.hash = hasher.Sum(nil)
|
|
|
|
|
|
|
|
return self.hash, hashCount + 1
|
2014-05-22 18:08:49 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) Save(ndb *IAVLNodeDB) []byte {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.hash == nil {
|
2014-10-05 21:21:34 -07:00
|
|
|
hash, _ := self.HashWithCount()
|
|
|
|
self.hash = hash
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
if self.persisted {
|
|
|
|
return self.hash
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// children
|
2014-10-05 21:21:34 -07:00
|
|
|
if self.leftCached != nil {
|
|
|
|
self.leftHash = self.leftCached.Save(ndb)
|
|
|
|
self.leftCached = nil
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
if self.rightCached != nil {
|
|
|
|
self.rightHash = self.rightCached.Save(ndb)
|
|
|
|
self.rightCached = nil
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
// save self
|
|
|
|
ndb.Save(self)
|
|
|
|
return self.hash
|
2014-05-23 17:49:28 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) set(ndb *IAVLNodeDB, key []byte, value []byte) (_ *IAVLNode, updated bool) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.height == 0 {
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Compare(key, self.key) == -1 {
|
2014-07-01 14:50:24 -07:00
|
|
|
return &IAVLNode{
|
2014-10-05 21:21:34 -07:00
|
|
|
key: self.key,
|
|
|
|
height: 1,
|
|
|
|
size: 2,
|
|
|
|
leftCached: NewIAVLNode(key, value),
|
|
|
|
rightCached: self,
|
2014-07-01 14:50:24 -07:00
|
|
|
}, false
|
2014-09-03 19:21:19 -07:00
|
|
|
} else if bytes.Equal(self.key, key) {
|
2014-07-01 14:50:24 -07:00
|
|
|
return NewIAVLNode(key, value), true
|
|
|
|
} else {
|
|
|
|
return &IAVLNode{
|
2014-10-05 21:21:34 -07:00
|
|
|
key: key,
|
|
|
|
height: 1,
|
|
|
|
size: 2,
|
|
|
|
leftCached: self,
|
|
|
|
rightCached: NewIAVLNode(key, value),
|
2014-07-01 14:50:24 -07:00
|
|
|
}, false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self = self.Copy()
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Compare(key, self.key) == -1 {
|
2014-10-05 21:21:34 -07:00
|
|
|
self.leftCached, updated = self.getLeft(ndb).set(ndb, key, value)
|
|
|
|
self.leftHash = nil
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
2014-10-05 21:21:34 -07:00
|
|
|
self.rightCached, updated = self.getRight(ndb).set(ndb, key, value)
|
|
|
|
self.rightHash = nil
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
if updated {
|
|
|
|
return self, updated
|
|
|
|
} else {
|
2014-10-05 21:21:34 -07:00
|
|
|
self.calcHeightAndSize(ndb)
|
|
|
|
return self.balance(ndb), updated
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-27 22:31:47 -07:00
|
|
|
}
|
2014-05-21 16:24:50 -07:00
|
|
|
|
2014-05-27 22:31:47 -07:00
|
|
|
// newKey: new leftmost leaf key for tree after successfully removing 'key' if changed.
|
2014-10-05 21:21:34 -07:00
|
|
|
// only one of newSelfHash or newSelf is returned.
|
|
|
|
func (self *IAVLNode) remove(ndb *IAVLNodeDB, key []byte) (newSelfHash []byte, newSelf *IAVLNode, newKey []byte, value []byte, err error) {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.height == 0 {
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Equal(self.key, key) {
|
2014-10-05 21:21:34 -07:00
|
|
|
return nil, nil, nil, self.value, nil
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
2014-10-05 21:21:34 -07:00
|
|
|
return nil, self, nil, nil, NotFound(key)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-03 19:21:19 -07:00
|
|
|
if bytes.Compare(key, self.key) == -1 {
|
2014-10-05 21:21:34 -07:00
|
|
|
var newLeftHash []byte
|
2014-07-01 14:50:24 -07:00
|
|
|
var newLeft *IAVLNode
|
2014-10-05 21:21:34 -07:00
|
|
|
newLeftHash, newLeft, newKey, value, err = self.getLeft(ndb).remove(ndb, key)
|
2014-07-01 14:50:24 -07:00
|
|
|
if err != nil {
|
2014-10-05 21:21:34 -07:00
|
|
|
return nil, self, nil, value, err
|
|
|
|
} else if newLeftHash == nil && newLeft == nil { // left node held value, was removed
|
|
|
|
return self.rightHash, self.rightCached, self.key, value, nil
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
self = self.Copy()
|
2014-10-05 21:21:34 -07:00
|
|
|
self.leftHash, self.leftCached = newLeftHash, newLeft
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
2014-10-05 21:21:34 -07:00
|
|
|
var newRightHash []byte
|
2014-07-01 14:50:24 -07:00
|
|
|
var newRight *IAVLNode
|
2014-10-05 21:21:34 -07:00
|
|
|
newRightHash, newRight, newKey, value, err = self.getRight(ndb).remove(ndb, key)
|
2014-07-01 14:50:24 -07:00
|
|
|
if err != nil {
|
2014-10-05 21:21:34 -07:00
|
|
|
return nil, self, nil, value, err
|
|
|
|
} else if newRightHash == nil && newRight == nil { // right node held value, was removed
|
|
|
|
return self.leftHash, self.leftCached, nil, value, nil
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
self = self.Copy()
|
2014-10-05 21:21:34 -07:00
|
|
|
self.rightHash, self.rightCached = newRightHash, newRight
|
2014-07-01 14:50:24 -07:00
|
|
|
if newKey != nil {
|
|
|
|
self.key = newKey
|
|
|
|
newKey = nil
|
|
|
|
}
|
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
self.calcHeightAndSize(ndb)
|
|
|
|
return nil, self.balance(ndb), newKey, value, err
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-05-22 18:08:49 -07:00
|
|
|
}
|
|
|
|
|
2014-06-03 17:03:29 -07:00
|
|
|
func (self *IAVLNode) WriteTo(w io.Writer) (n int64, err error) {
|
2014-08-10 16:35:08 -07:00
|
|
|
n, _, err = self.saveToCountHashes(w)
|
2014-07-01 14:50:24 -07:00
|
|
|
return
|
2014-05-22 18:08:49 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
func (self *IAVLNode) saveToCountHashes(w io.Writer) (n int64, hashCount uint64, err error) {
|
2014-09-03 19:21:19 -07:00
|
|
|
// height & size & key
|
|
|
|
WriteUInt8(w, self.height, &n, &err)
|
|
|
|
WriteUInt64(w, self.size, &n, &err)
|
|
|
|
WriteByteSlice(w, self.key, &n, &err)
|
2014-08-10 16:35:08 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 16:35:08 -07:00
|
|
|
// value or children
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.height == 0 {
|
|
|
|
// value
|
2014-09-03 19:21:19 -07:00
|
|
|
WriteByteSlice(w, self.value, &n, &err)
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
|
|
|
// left
|
2014-10-05 21:21:34 -07:00
|
|
|
if self.leftCached != nil {
|
|
|
|
leftHash, leftCount := self.left.HashWithCount()
|
|
|
|
self.leftHash = leftHash
|
|
|
|
hashCount += leftCount
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
WriteByteSlice(w, self.leftHash, &n, &err)
|
2014-07-01 14:50:24 -07:00
|
|
|
// right
|
2014-10-05 21:21:34 -07:00
|
|
|
if self.rightCached != nil {
|
|
|
|
rightHash, rightCount := self.right.HashWithCount()
|
|
|
|
self.rightHash = rightHash
|
|
|
|
hashCount += rightCount
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
WriteByteSlice(w, self.rightHash, &n, &err)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
return
|
2014-05-23 17:49:28 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) getLeft(ndb *IAVLNodeDB) *IAVLNode {
|
|
|
|
if self.leftCached != nil {
|
|
|
|
return self.leftCached
|
|
|
|
} else {
|
|
|
|
return ndb.Get(leftHash)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-05-22 18:08:49 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) getRight(ndb *IAVLNodeDB) *IAVLNode {
|
|
|
|
if self.rightCached != nil {
|
|
|
|
return self.rightCached
|
|
|
|
} else {
|
|
|
|
return ndb.Get(rightHash)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-05-21 16:24:50 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) rotateRight(ndb *IAVLNodeDB) *IAVLNode {
|
2014-07-01 14:50:24 -07:00
|
|
|
self = self.Copy()
|
2014-10-05 21:21:34 -07:00
|
|
|
sl := self.getLeft(ndb).Copy()
|
2014-05-20 16:33:10 -07:00
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
slrHash, slrCached := sl.rightHash, sl.rightCached
|
|
|
|
sl.rightHash, sl.rightCached = nil, self
|
|
|
|
self.leftHash, self.leftCached = slrHash, slrCached
|
2014-05-20 16:33:10 -07:00
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
self.calcHeightAndSize(ndb)
|
|
|
|
sl.calcHeightAndSize(ndb)
|
2014-05-20 16:33:10 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
return sl
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) rotateLeft(ndb *IAVLNodeDB) *IAVLNode {
|
2014-07-01 14:50:24 -07:00
|
|
|
self = self.Copy()
|
2014-10-05 21:21:34 -07:00
|
|
|
sr := self.getRight(ndb).Copy()
|
2014-05-20 16:33:10 -07:00
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
srlHash, srlCached := sr.leftHash, sr.leftCached
|
|
|
|
sr.leftHash, sr.leftCached = nil, self
|
|
|
|
self.rightHash, self.rightCached = srlHash, srlCached
|
2014-05-20 16:33:10 -07:00
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
self.calcHeightAndSize(ndb)
|
|
|
|
sr.calcHeightAndSize(ndb)
|
2014-05-20 16:33:10 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
return sr
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) calcHeightAndSize(ndb *IAVLNodeDB) {
|
|
|
|
self.height = maxUint8(self.getLeft(ndb).Height(), self.getRight(ndb).Height()) + 1
|
|
|
|
self.size = self.getLeft(ndb).Size() + self.getRight(ndb).Size()
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) calcBalance(ndb *IAVLNodeDB) int {
|
|
|
|
return int(self.getLeft(ndb).Height()) - int(self.getRight(ndb).Height())
|
2014-05-20 16:33:10 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) balance(ndb *IAVLNodeDB) (newSelf *IAVLNode) {
|
|
|
|
balance := self.calcBalance(ndb)
|
2014-07-01 14:50:24 -07:00
|
|
|
if balance > 1 {
|
2014-10-05 21:21:34 -07:00
|
|
|
if self.getLeft(ndb).calcBalance(ndb) >= 0 {
|
2014-07-01 14:50:24 -07:00
|
|
|
// Left Left Case
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.rotateRight(ndb)
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
|
|
|
// Left Right Case
|
|
|
|
self = self.Copy()
|
2014-10-05 21:21:34 -07:00
|
|
|
self.leftHash, self.leftCached = nil, self.getLeft(ndb).rotateLeft(ndb)
|
2014-07-01 14:50:24 -07:00
|
|
|
//self.calcHeightAndSize()
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.rotateRight(ndb)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if balance < -1 {
|
2014-10-05 21:21:34 -07:00
|
|
|
if self.getRight(ndb).calcBalance(ndb) <= 0 {
|
2014-07-01 14:50:24 -07:00
|
|
|
// Right Right Case
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.rotateLeft(ndb)
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
|
|
|
// Right Left Case
|
|
|
|
self = self.Copy()
|
2014-10-05 21:21:34 -07:00
|
|
|
self.rightHash, self.rightCached = nil, self.getRight(ndb).rotateRight(ndb)
|
2014-07-01 14:50:24 -07:00
|
|
|
//self.calcHeightAndSize()
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.rotateLeft(ndb)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Nothing changed
|
|
|
|
return self
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
// Only used in testing...
|
|
|
|
func (self *IAVLNode) lmd(ndb *IAVLNodeDB) *IAVLNode {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.height == 0 {
|
|
|
|
return self
|
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.getLeft(ndb).lmd(ndb)
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
// Only used in testing...
|
|
|
|
func (self *IAVLNode) rmd(ndb *IAVLNodeDB) *IAVLNode {
|
2014-07-01 14:50:24 -07:00
|
|
|
if self.height == 0 {
|
|
|
|
return self
|
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
return self.getRight(ndb).rmd(ndb)
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:21:34 -07:00
|
|
|
func (self *IAVLNode) traverse(ndb *IAVLNodeDB, cb func(*IAVLNode) bool) bool {
|
2014-07-01 14:50:24 -07:00
|
|
|
stop := cb(self)
|
|
|
|
if stop {
|
|
|
|
return stop
|
|
|
|
}
|
|
|
|
if self.height > 0 {
|
2014-10-05 21:21:34 -07:00
|
|
|
stop = self.getLeft(ndb).traverse(ndb, cb)
|
2014-07-01 14:50:24 -07:00
|
|
|
if stop {
|
|
|
|
return stop
|
|
|
|
}
|
2014-10-05 21:21:34 -07:00
|
|
|
stop = self.getRight(ndb).traverse(ndb, cb)
|
2014-07-01 14:50:24 -07:00
|
|
|
if stop {
|
|
|
|
return stop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|