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.

129 lines
2.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package merkle
  2. const HASH_BYTE_SIZE int = 4 + 32
  3. /*
  4. Immutable AVL Tree (wraps the Node root)
  5. This tree is not concurrency safe.
  6. You must wrap your calls with your own mutex.
  7. */
  8. type IAVLTree struct {
  9. db Db
  10. root *IAVLNode
  11. }
  12. func NewIAVLTree(db Db) *IAVLTree {
  13. return &IAVLTree{
  14. db: db,
  15. root: nil,
  16. }
  17. }
  18. // TODO rename to Load.
  19. func NewIAVLTreeFromHash(db Db, hash []byte) *IAVLTree {
  20. root := &IAVLNode{
  21. hash: hash,
  22. flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
  23. }
  24. root.fill(db)
  25. return &IAVLTree{db: db, root: root}
  26. }
  27. func NewIAVLTreeFromKey(db Db, key string) *IAVLTree {
  28. hash := db.Get([]byte(key))
  29. if hash == nil {
  30. return nil
  31. }
  32. root := &IAVLNode{
  33. hash: hash,
  34. flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
  35. }
  36. root.fill(db)
  37. return &IAVLTree{db: db, root: root}
  38. }
  39. func (t *IAVLTree) Size() uint64 {
  40. if t.root == nil {
  41. return 0
  42. }
  43. return t.root.Size()
  44. }
  45. func (t *IAVLTree) Height() uint8 {
  46. if t.root == nil {
  47. return 0
  48. }
  49. return t.root.Height()
  50. }
  51. func (t *IAVLTree) Has(key []byte) bool {
  52. if t.root == nil {
  53. return false
  54. }
  55. return t.root.has(t.db, key)
  56. }
  57. func (t *IAVLTree) Set(key []byte, value []byte) (updated bool) {
  58. if t.root == nil {
  59. t.root = NewIAVLNode(key, value)
  60. return false
  61. }
  62. t.root, updated = t.root.set(t.db, key, value)
  63. return updated
  64. }
  65. func (t *IAVLTree) Hash() []byte {
  66. if t.root == nil {
  67. return nil
  68. }
  69. hash, _ := t.root.HashWithCount()
  70. return hash
  71. }
  72. func (t *IAVLTree) HashWithCount() ([]byte, uint64) {
  73. if t.root == nil {
  74. return nil, 0
  75. }
  76. return t.root.HashWithCount()
  77. }
  78. func (t *IAVLTree) Save() {
  79. if t.root == nil {
  80. return
  81. }
  82. t.root.HashWithCount()
  83. t.root.Save(t.db)
  84. }
  85. func (t *IAVLTree) SaveKey(key string) {
  86. if t.root == nil {
  87. return
  88. }
  89. hash, _ := t.root.HashWithCount()
  90. t.root.Save(t.db)
  91. t.db.Set([]byte(key), hash)
  92. }
  93. func (t *IAVLTree) Get(key []byte) (value []byte) {
  94. if t.root == nil {
  95. return nil
  96. }
  97. return t.root.get(t.db, key)
  98. }
  99. func (t *IAVLTree) Remove(key []byte) (value []byte, err error) {
  100. if t.root == nil {
  101. return nil, NotFound(key)
  102. }
  103. newRoot, _, value, err := t.root.remove(t.db, key)
  104. if err != nil {
  105. return nil, err
  106. }
  107. t.root = newRoot
  108. return value, nil
  109. }
  110. func (t *IAVLTree) Copy() Tree {
  111. return &IAVLTree{db: t.db, root: t.root}
  112. }