You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package merkle
  2. import (
  3. . "github.com/tendermint/tendermint/binary"
  4. )
  5. const HASH_BYTE_SIZE int = 4 + 32
  6. /*
  7. Immutable AVL Tree (wraps the Node root)
  8. This tree is not concurrency safe.
  9. You must wrap your calls with your own mutex.
  10. */
  11. type IAVLTree struct {
  12. db Db
  13. root *IAVLNode
  14. }
  15. func NewIAVLTree(db Db) *IAVLTree {
  16. return &IAVLTree{db: db, root: nil}
  17. }
  18. func NewIAVLTreeFromHash(db Db, hash ByteSlice) *IAVLTree {
  19. root := &IAVLNode{
  20. hash: hash,
  21. flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
  22. }
  23. root.fill(db)
  24. return &IAVLTree{db: db, root: root}
  25. }
  26. func (t *IAVLTree) Root() Node {
  27. return t.root
  28. }
  29. func (t *IAVLTree) Size() uint64 {
  30. if t.root == nil {
  31. return 0
  32. }
  33. return t.root.Size()
  34. }
  35. func (t *IAVLTree) Height() uint8 {
  36. if t.root == nil {
  37. return 0
  38. }
  39. return t.root.Height()
  40. }
  41. func (t *IAVLTree) Has(key Key) bool {
  42. if t.root == nil {
  43. return false
  44. }
  45. return t.root.has(t.db, key)
  46. }
  47. func (t *IAVLTree) Put(key Key, value Value) (updated bool) {
  48. if t.root == nil {
  49. t.root = NewIAVLNode(key, value)
  50. return false
  51. }
  52. t.root, updated = t.root.put(t.db, key, value)
  53. return updated
  54. }
  55. func (t *IAVLTree) Hash() (ByteSlice, uint64) {
  56. if t.root == nil {
  57. return nil, 0
  58. }
  59. return t.root.Hash()
  60. }
  61. func (t *IAVLTree) Save() {
  62. if t.root == nil {
  63. return
  64. }
  65. if t.root.hash == nil {
  66. t.root.Hash()
  67. }
  68. t.root.Save(t.db)
  69. }
  70. func (t *IAVLTree) Get(key Key) (value Value) {
  71. if t.root == nil {
  72. return nil
  73. }
  74. return t.root.get(t.db, key)
  75. }
  76. func (t *IAVLTree) Remove(key Key) (value Value, err error) {
  77. if t.root == nil {
  78. return nil, NotFound(key)
  79. }
  80. newRoot, _, value, err := t.root.remove(t.db, key)
  81. if err != nil {
  82. return nil, err
  83. }
  84. t.root = newRoot
  85. return value, nil
  86. }
  87. func (t *IAVLTree) Copy() Tree {
  88. return &IAVLTree{db: t.db, root: t.root}
  89. }
  90. // Traverses all the nodes of the tree in prefix order.
  91. // return true from cb to halt iteration.
  92. // node.Height() == 0 if you just want a value node.
  93. func (t *IAVLTree) Traverse(cb func(Node) bool) {
  94. if t.root == nil {
  95. return
  96. }
  97. t.root.traverse(t.db, cb)
  98. }
  99. func (t *IAVLTree) Values() <-chan Value {
  100. root := t.root
  101. ch := make(chan Value)
  102. if root == nil {
  103. close(ch)
  104. return ch
  105. }
  106. go func() {
  107. root.traverse(t.db, func(n Node) bool {
  108. if n.Height() == 0 {
  109. ch <- n.Value()
  110. }
  111. return true
  112. })
  113. close(ch)
  114. }()
  115. return ch
  116. }