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.

405 lines
10 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
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
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. import (
  3. . "github.com/tendermint/tendermint/binary"
  4. "bytes"
  5. "io"
  6. "crypto/sha256"
  7. )
  8. // Node
  9. type IAVLNode struct {
  10. key Key
  11. value Value
  12. size uint64
  13. height uint8
  14. hash ByteSlice
  15. left *IAVLNode
  16. right *IAVLNode
  17. // volatile
  18. flags byte
  19. }
  20. const (
  21. IAVLNODE_FLAG_PERSISTED = byte(0x01)
  22. IAVLNODE_FLAG_PLACEHOLDER = byte(0x02)
  23. )
  24. func NewIAVLNode(key Key, value Value) *IAVLNode {
  25. return &IAVLNode{
  26. key: key,
  27. value: value,
  28. size: 1,
  29. }
  30. }
  31. func (self *IAVLNode) Copy() *IAVLNode {
  32. if self.height == 0 {
  33. panic("Why are you copying a value node?")
  34. }
  35. return &IAVLNode{
  36. key: self.key,
  37. size: self.size,
  38. height: self.height,
  39. left: self.left,
  40. right: self.right,
  41. hash: nil,
  42. flags: byte(0),
  43. }
  44. }
  45. func (self *IAVLNode) Key() Key {
  46. return self.key
  47. }
  48. func (self *IAVLNode) Value() Value {
  49. return self.value
  50. }
  51. func (self *IAVLNode) Size() uint64 {
  52. return self.size
  53. }
  54. func (self *IAVLNode) Height() uint8 {
  55. return self.height
  56. }
  57. func (self *IAVLNode) has(db Db, key Key) (has bool) {
  58. if self.key.Equals(key) {
  59. return true
  60. }
  61. if self.height == 0 {
  62. return false
  63. } else {
  64. if key.Less(self.key) {
  65. return self.leftFilled(db).has(db, key)
  66. } else {
  67. return self.rightFilled(db).has(db, key)
  68. }
  69. }
  70. }
  71. func (self *IAVLNode) get(db Db, key Key) (value Value) {
  72. if self.height == 0 {
  73. if self.key.Equals(key) {
  74. return self.value
  75. } else {
  76. return nil
  77. }
  78. } else {
  79. if key.Less(self.key) {
  80. return self.leftFilled(db).get(db, key)
  81. } else {
  82. return self.rightFilled(db).get(db, key)
  83. }
  84. }
  85. }
  86. func (self *IAVLNode) Hash() (ByteSlice, uint64) {
  87. if self.hash != nil {
  88. return self.hash, 0
  89. }
  90. hasher := sha256.New()
  91. _, hashCount, err := self.saveToCountHashes(hasher, false)
  92. if err != nil { panic(err) }
  93. self.hash = hasher.Sum(nil)
  94. return self.hash, hashCount+1
  95. }
  96. func (self *IAVLNode) Save(db Db) {
  97. if self.hash == nil {
  98. panic("savee.hash can't be nil")
  99. }
  100. if self.flags & IAVLNODE_FLAG_PERSISTED > 0 ||
  101. self.flags & IAVLNODE_FLAG_PLACEHOLDER > 0 {
  102. return
  103. }
  104. // children
  105. if self.height > 0 {
  106. self.left.Save(db)
  107. self.right.Save(db)
  108. }
  109. // save self
  110. buf := bytes.NewBuffer(nil)
  111. _, err := self.WriteTo(buf)
  112. if err != nil { panic(err) }
  113. db.Put([]byte(self.hash), buf.Bytes())
  114. self.flags |= IAVLNODE_FLAG_PERSISTED
  115. }
  116. func (self *IAVLNode) put(db Db, key Key, value Value) (_ *IAVLNode, updated bool) {
  117. if self.height == 0 {
  118. if key.Less(self.key) {
  119. return &IAVLNode{
  120. key: self.key,
  121. height: 1,
  122. size: 2,
  123. left: NewIAVLNode(key, value),
  124. right: self,
  125. }, false
  126. } else if self.key.Equals(key) {
  127. return NewIAVLNode(key, value), true
  128. } else {
  129. return &IAVLNode{
  130. key: key,
  131. height: 1,
  132. size: 2,
  133. left: self,
  134. right: NewIAVLNode(key, value),
  135. }, false
  136. }
  137. } else {
  138. self = self.Copy()
  139. if key.Less(self.key) {
  140. self.left, updated = self.leftFilled(db).put(db, key, value)
  141. } else {
  142. self.right, updated = self.rightFilled(db).put(db, key, value)
  143. }
  144. if updated {
  145. return self, updated
  146. } else {
  147. self.calcHeightAndSize(db)
  148. return self.balance(db), updated
  149. }
  150. }
  151. }
  152. // newKey: new leftmost leaf key for tree after successfully removing 'key' if changed.
  153. func (self *IAVLNode) remove(db Db, key Key) (newSelf *IAVLNode, newKey Key, value Value, err error) {
  154. if self.height == 0 {
  155. if self.key.Equals(key) {
  156. return nil, nil, self.value, nil
  157. } else {
  158. return self, nil, nil, NotFound(key)
  159. }
  160. } else {
  161. if key.Less(self.key) {
  162. var newLeft *IAVLNode
  163. newLeft, newKey, value, err = self.leftFilled(db).remove(db, key)
  164. if err != nil {
  165. return self, nil, value, err
  166. } else if newLeft == nil { // left node held value, was removed
  167. return self.right, self.key, value, nil
  168. }
  169. self = self.Copy()
  170. self.left = newLeft
  171. } else {
  172. var newRight *IAVLNode
  173. newRight, newKey, value, err = self.rightFilled(db).remove(db, key)
  174. if err != nil {
  175. return self, nil, value, err
  176. } else if newRight == nil { // right node held value, was removed
  177. return self.left, nil, value, nil
  178. }
  179. self = self.Copy()
  180. self.right = newRight
  181. if newKey != nil {
  182. self.key = newKey
  183. newKey = nil
  184. }
  185. }
  186. self.calcHeightAndSize(db)
  187. return self.balance(db), newKey, value, err
  188. }
  189. }
  190. func (self *IAVLNode) WriteTo(w io.Writer) (n int64, err error) {
  191. n, _, err = self.saveToCountHashes(w, true)
  192. return
  193. }
  194. func (self *IAVLNode) saveToCountHashes(w io.Writer, meta bool) (n int64, hashCount uint64, err error) {
  195. var _n int64
  196. if meta {
  197. // height & size
  198. _n, err = UInt8(self.height).WriteTo(w)
  199. if err != nil { return } else { n += _n }
  200. _n, err = UInt64(self.size).WriteTo(w)
  201. if err != nil { return } else { n += _n }
  202. // key
  203. _n, err = Byte(GetBinaryType(self.key)).WriteTo(w)
  204. if err != nil { return } else { n += _n }
  205. _n, err = self.key.WriteTo(w)
  206. if err != nil { return } else { n += _n }
  207. }
  208. if self.height == 0 {
  209. // value
  210. _n, err = Byte(GetBinaryType(self.value)).WriteTo(w)
  211. if err != nil { return } else { n += _n }
  212. if self.value != nil {
  213. _n, err = self.value.WriteTo(w)
  214. if err != nil { return } else { n += _n }
  215. }
  216. } else {
  217. // left
  218. leftHash, leftCount := self.left.Hash()
  219. hashCount += leftCount
  220. _n, err = leftHash.WriteTo(w)
  221. if err != nil { return } else { n += _n }
  222. // right
  223. rightHash, rightCount := self.right.Hash()
  224. hashCount += rightCount
  225. _n, err = rightHash.WriteTo(w)
  226. if err != nil { return } else { n += _n }
  227. }
  228. return
  229. }
  230. // Given a placeholder node which has only the hash set,
  231. // load the rest of the data from db.
  232. // Not threadsafe.
  233. func (self *IAVLNode) fill(db Db) {
  234. if self.hash == nil {
  235. panic("placeholder.hash can't be nil")
  236. }
  237. buf := db.Get(self.hash)
  238. r := bytes.NewReader(buf)
  239. // node header
  240. self.height = uint8(ReadUInt8(r))
  241. self.size = uint64(ReadUInt64(r))
  242. // key
  243. key := ReadBinary(r)
  244. self.key = key.(Key)
  245. if self.height == 0 {
  246. // value
  247. self.value = ReadBinary(r)
  248. } else {
  249. // left
  250. var leftHash ByteSlice
  251. leftHash = ReadByteSlice(r)
  252. self.left = &IAVLNode{
  253. hash: leftHash,
  254. flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
  255. }
  256. // right
  257. var rightHash ByteSlice
  258. rightHash = ReadByteSlice(r)
  259. self.right = &IAVLNode{
  260. hash: rightHash,
  261. flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
  262. }
  263. if r.Len() != 0 {
  264. panic("buf not all consumed")
  265. }
  266. }
  267. self.flags &= ^IAVLNODE_FLAG_PLACEHOLDER
  268. }
  269. func (self *IAVLNode) leftFilled(db Db) *IAVLNode {
  270. if self.left.flags & IAVLNODE_FLAG_PLACEHOLDER > 0 {
  271. self.left.fill(db)
  272. }
  273. return self.left
  274. }
  275. func (self *IAVLNode) rightFilled(db Db) *IAVLNode {
  276. if self.right.flags & IAVLNODE_FLAG_PLACEHOLDER > 0 {
  277. self.right.fill(db)
  278. }
  279. return self.right
  280. }
  281. func (self *IAVLNode) rotateRight(db Db) *IAVLNode {
  282. self = self.Copy()
  283. sl := self.leftFilled(db).Copy()
  284. slr := sl.right
  285. sl.right = self
  286. self.left = slr
  287. self.calcHeightAndSize(db)
  288. sl.calcHeightAndSize(db)
  289. return sl
  290. }
  291. func (self *IAVLNode) rotateLeft(db Db) *IAVLNode {
  292. self = self.Copy()
  293. sr := self.rightFilled(db).Copy()
  294. srl := sr.left
  295. sr.left = self
  296. self.right = srl
  297. self.calcHeightAndSize(db)
  298. sr.calcHeightAndSize(db)
  299. return sr
  300. }
  301. func (self *IAVLNode) calcHeightAndSize(db Db) {
  302. self.height = maxUint8(self.leftFilled(db).Height(), self.rightFilled(db).Height()) + 1
  303. self.size = self.leftFilled(db).Size() + self.rightFilled(db).Size()
  304. }
  305. func (self *IAVLNode) calcBalance(db Db) int {
  306. return int(self.leftFilled(db).Height()) - int(self.rightFilled(db).Height())
  307. }
  308. func (self *IAVLNode) balance(db Db) (newSelf *IAVLNode) {
  309. balance := self.calcBalance(db)
  310. if (balance > 1) {
  311. if (self.leftFilled(db).calcBalance(db) >= 0) {
  312. // Left Left Case
  313. return self.rotateRight(db)
  314. } else {
  315. // Left Right Case
  316. self = self.Copy()
  317. self.left = self.leftFilled(db).rotateLeft(db)
  318. //self.calcHeightAndSize()
  319. return self.rotateRight(db)
  320. }
  321. }
  322. if (balance < -1) {
  323. if (self.rightFilled(db).calcBalance(db) <= 0) {
  324. // Right Right Case
  325. return self.rotateLeft(db)
  326. } else {
  327. // Right Left Case
  328. self = self.Copy()
  329. self.right = self.rightFilled(db).rotateRight(db)
  330. //self.calcHeightAndSize()
  331. return self.rotateLeft(db)
  332. }
  333. }
  334. // Nothing changed
  335. return self
  336. }
  337. func (self *IAVLNode) lmd(db Db) (*IAVLNode) {
  338. if self.height == 0 {
  339. return self
  340. }
  341. return self.leftFilled(db).lmd(db)
  342. }
  343. func (self *IAVLNode) rmd(db Db) (*IAVLNode) {
  344. if self.height == 0 {
  345. return self
  346. }
  347. return self.rightFilled(db).rmd(db)
  348. }
  349. func (self *IAVLNode) traverse(db Db, cb func(Node)bool) bool {
  350. stop := cb(self)
  351. if stop { return stop }
  352. if self.height > 0 {
  353. stop = self.leftFilled(db).traverse(db, cb)
  354. if stop { return stop }
  355. stop = self.rightFilled(db).traverse(db, cb)
  356. if stop { return stop }
  357. }
  358. return false
  359. }