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.

447 lines
8.7 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
  1. package merkle
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. . "github.com/tendermint/tendermint/binary"
  6. "io"
  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 {
  93. panic(err)
  94. }
  95. self.hash = hasher.Sum(nil)
  96. return self.hash, hashCount + 1
  97. }
  98. func (self *IAVLNode) Save(db Db) {
  99. if self.hash == nil {
  100. panic("savee.hash can't be nil")
  101. }
  102. if self.flags&IAVLNODE_FLAG_PERSISTED > 0 ||
  103. self.flags&IAVLNODE_FLAG_PLACEHOLDER > 0 {
  104. return
  105. }
  106. // children
  107. if self.height > 0 {
  108. self.left.Save(db)
  109. self.right.Save(db)
  110. }
  111. // save self
  112. buf := bytes.NewBuffer(nil)
  113. _, err := self.WriteTo(buf)
  114. if err != nil {
  115. panic(err)
  116. }
  117. db.Put([]byte(self.hash), buf.Bytes())
  118. self.flags |= IAVLNODE_FLAG_PERSISTED
  119. }
  120. func (self *IAVLNode) put(db Db, key Key, value Value) (_ *IAVLNode, updated bool) {
  121. if self.height == 0 {
  122. if key.Less(self.key) {
  123. return &IAVLNode{
  124. key: self.key,
  125. height: 1,
  126. size: 2,
  127. left: NewIAVLNode(key, value),
  128. right: self,
  129. }, false
  130. } else if self.key.Equals(key) {
  131. return NewIAVLNode(key, value), true
  132. } else {
  133. return &IAVLNode{
  134. key: key,
  135. height: 1,
  136. size: 2,
  137. left: self,
  138. right: NewIAVLNode(key, value),
  139. }, false
  140. }
  141. } else {
  142. self = self.Copy()
  143. if key.Less(self.key) {
  144. self.left, updated = self.leftFilled(db).put(db, key, value)
  145. } else {
  146. self.right, updated = self.rightFilled(db).put(db, key, value)
  147. }
  148. if updated {
  149. return self, updated
  150. } else {
  151. self.calcHeightAndSize(db)
  152. return self.balance(db), updated
  153. }
  154. }
  155. }
  156. // newKey: new leftmost leaf key for tree after successfully removing 'key' if changed.
  157. func (self *IAVLNode) remove(db Db, key Key) (newSelf *IAVLNode, newKey Key, value Value, err error) {
  158. if self.height == 0 {
  159. if self.key.Equals(key) {
  160. return nil, nil, self.value, nil
  161. } else {
  162. return self, nil, nil, NotFound(key)
  163. }
  164. } else {
  165. if key.Less(self.key) {
  166. var newLeft *IAVLNode
  167. newLeft, newKey, value, err = self.leftFilled(db).remove(db, key)
  168. if err != nil {
  169. return self, nil, value, err
  170. } else if newLeft == nil { // left node held value, was removed
  171. return self.right, self.key, value, nil
  172. }
  173. self = self.Copy()
  174. self.left = newLeft
  175. } else {
  176. var newRight *IAVLNode
  177. newRight, newKey, value, err = self.rightFilled(db).remove(db, key)
  178. if err != nil {
  179. return self, nil, value, err
  180. } else if newRight == nil { // right node held value, was removed
  181. return self.left, nil, value, nil
  182. }
  183. self = self.Copy()
  184. self.right = newRight
  185. if newKey != nil {
  186. self.key = newKey
  187. newKey = nil
  188. }
  189. }
  190. self.calcHeightAndSize(db)
  191. return self.balance(db), newKey, value, err
  192. }
  193. }
  194. func (self *IAVLNode) WriteTo(w io.Writer) (n int64, err error) {
  195. n, _, err = self.saveToCountHashes(w, true)
  196. return
  197. }
  198. func (self *IAVLNode) saveToCountHashes(w io.Writer, meta bool) (n int64, hashCount uint64, err error) {
  199. var _n int64
  200. if meta {
  201. // height & size
  202. _n, err = UInt8(self.height).WriteTo(w)
  203. if err != nil {
  204. return
  205. } else {
  206. n += _n
  207. }
  208. _n, err = UInt64(self.size).WriteTo(w)
  209. if err != nil {
  210. return
  211. } else {
  212. n += _n
  213. }
  214. // key
  215. _n, err = Byte(GetBinaryType(self.key)).WriteTo(w)
  216. if err != nil {
  217. return
  218. } else {
  219. n += _n
  220. }
  221. _n, err = self.key.WriteTo(w)
  222. if err != nil {
  223. return
  224. } else {
  225. n += _n
  226. }
  227. }
  228. if self.height == 0 {
  229. // value
  230. _n, err = Byte(GetBinaryType(self.value)).WriteTo(w)
  231. if err != nil {
  232. return
  233. } else {
  234. n += _n
  235. }
  236. if self.value != nil {
  237. _n, err = self.value.WriteTo(w)
  238. if err != nil {
  239. return
  240. } else {
  241. n += _n
  242. }
  243. }
  244. } else {
  245. // left
  246. leftHash, leftCount := self.left.Hash()
  247. hashCount += leftCount
  248. _n, err = leftHash.WriteTo(w)
  249. if err != nil {
  250. return
  251. } else {
  252. n += _n
  253. }
  254. // right
  255. rightHash, rightCount := self.right.Hash()
  256. hashCount += rightCount
  257. _n, err = rightHash.WriteTo(w)
  258. if err != nil {
  259. return
  260. } else {
  261. n += _n
  262. }
  263. }
  264. return
  265. }
  266. // Given a placeholder node which has only the hash set,
  267. // load the rest of the data from db.
  268. // Not threadsafe.
  269. func (self *IAVLNode) fill(db Db) {
  270. if self.hash == nil {
  271. panic("placeholder.hash can't be nil")
  272. }
  273. buf := db.Get(self.hash)
  274. r := bytes.NewReader(buf)
  275. // node header
  276. self.height = uint8(ReadUInt8(r))
  277. self.size = uint64(ReadUInt64(r))
  278. // key
  279. key := ReadBinary(r)
  280. self.key = key.(Key)
  281. if self.height == 0 {
  282. // value
  283. self.value = ReadBinary(r)
  284. } else {
  285. // left
  286. var leftHash ByteSlice
  287. leftHash = ReadByteSlice(r)
  288. self.left = &IAVLNode{
  289. hash: leftHash,
  290. flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
  291. }
  292. // right
  293. var rightHash ByteSlice
  294. rightHash = ReadByteSlice(r)
  295. self.right = &IAVLNode{
  296. hash: rightHash,
  297. flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
  298. }
  299. if r.Len() != 0 {
  300. panic("buf not all consumed")
  301. }
  302. }
  303. self.flags &= ^IAVLNODE_FLAG_PLACEHOLDER
  304. }
  305. func (self *IAVLNode) leftFilled(db Db) *IAVLNode {
  306. if self.left.flags&IAVLNODE_FLAG_PLACEHOLDER > 0 {
  307. self.left.fill(db)
  308. }
  309. return self.left
  310. }
  311. func (self *IAVLNode) rightFilled(db Db) *IAVLNode {
  312. if self.right.flags&IAVLNODE_FLAG_PLACEHOLDER > 0 {
  313. self.right.fill(db)
  314. }
  315. return self.right
  316. }
  317. func (self *IAVLNode) rotateRight(db Db) *IAVLNode {
  318. self = self.Copy()
  319. sl := self.leftFilled(db).Copy()
  320. slr := sl.right
  321. sl.right = self
  322. self.left = slr
  323. self.calcHeightAndSize(db)
  324. sl.calcHeightAndSize(db)
  325. return sl
  326. }
  327. func (self *IAVLNode) rotateLeft(db Db) *IAVLNode {
  328. self = self.Copy()
  329. sr := self.rightFilled(db).Copy()
  330. srl := sr.left
  331. sr.left = self
  332. self.right = srl
  333. self.calcHeightAndSize(db)
  334. sr.calcHeightAndSize(db)
  335. return sr
  336. }
  337. func (self *IAVLNode) calcHeightAndSize(db Db) {
  338. self.height = maxUint8(self.leftFilled(db).Height(), self.rightFilled(db).Height()) + 1
  339. self.size = self.leftFilled(db).Size() + self.rightFilled(db).Size()
  340. }
  341. func (self *IAVLNode) calcBalance(db Db) int {
  342. return int(self.leftFilled(db).Height()) - int(self.rightFilled(db).Height())
  343. }
  344. func (self *IAVLNode) balance(db Db) (newSelf *IAVLNode) {
  345. balance := self.calcBalance(db)
  346. if balance > 1 {
  347. if self.leftFilled(db).calcBalance(db) >= 0 {
  348. // Left Left Case
  349. return self.rotateRight(db)
  350. } else {
  351. // Left Right Case
  352. self = self.Copy()
  353. self.left = self.leftFilled(db).rotateLeft(db)
  354. //self.calcHeightAndSize()
  355. return self.rotateRight(db)
  356. }
  357. }
  358. if balance < -1 {
  359. if self.rightFilled(db).calcBalance(db) <= 0 {
  360. // Right Right Case
  361. return self.rotateLeft(db)
  362. } else {
  363. // Right Left Case
  364. self = self.Copy()
  365. self.right = self.rightFilled(db).rotateRight(db)
  366. //self.calcHeightAndSize()
  367. return self.rotateLeft(db)
  368. }
  369. }
  370. // Nothing changed
  371. return self
  372. }
  373. func (self *IAVLNode) lmd(db Db) *IAVLNode {
  374. if self.height == 0 {
  375. return self
  376. }
  377. return self.leftFilled(db).lmd(db)
  378. }
  379. func (self *IAVLNode) rmd(db Db) *IAVLNode {
  380. if self.height == 0 {
  381. return self
  382. }
  383. return self.rightFilled(db).rmd(db)
  384. }
  385. func (self *IAVLNode) traverse(db Db, cb func(Node) bool) bool {
  386. stop := cb(self)
  387. if stop {
  388. return stop
  389. }
  390. if self.height > 0 {
  391. stop = self.leftFilled(db).traverse(db, cb)
  392. if stop {
  393. return stop
  394. }
  395. stop = self.rightFilled(db).traverse(db, cb)
  396. if stop {
  397. return stop
  398. }
  399. }
  400. return false
  401. }