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.

257 lines
6.1 KiB

  1. package db
  2. import (
  3. "fmt"
  4. "sync"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. )
  7. //----------------------------------------
  8. // debugDB
  9. type debugDB struct {
  10. label string
  11. db DB
  12. }
  13. // For printing all operationgs to the console for debugging.
  14. func NewDebugDB(label string, db DB) debugDB {
  15. return debugDB{
  16. label: label,
  17. db: db,
  18. }
  19. }
  20. // Implements atomicSetDeleter.
  21. func (ddb debugDB) Mutex() *sync.Mutex { return nil }
  22. // Implements DB.
  23. func (ddb debugDB) Get(key []byte) (value []byte) {
  24. defer func() {
  25. fmt.Printf("%v.Get(%v) %v\n", ddb.label,
  26. cmn.ColoredBytes(key, cmn.Cyan, cmn.Blue),
  27. cmn.ColoredBytes(value, cmn.Green, cmn.Blue))
  28. }()
  29. value = ddb.db.Get(key)
  30. return
  31. }
  32. // Implements DB.
  33. func (ddb debugDB) Has(key []byte) (has bool) {
  34. defer func() {
  35. fmt.Printf("%v.Has(%v) %v\n", ddb.label,
  36. cmn.ColoredBytes(key, cmn.Cyan, cmn.Blue), has)
  37. }()
  38. return ddb.db.Has(key)
  39. }
  40. // Implements DB.
  41. func (ddb debugDB) Set(key []byte, value []byte) {
  42. fmt.Printf("%v.Set(%v, %v)\n", ddb.label,
  43. cmn.ColoredBytes(key, cmn.Yellow, cmn.Blue),
  44. cmn.ColoredBytes(value, cmn.Green, cmn.Blue))
  45. ddb.db.Set(key, value)
  46. }
  47. // Implements DB.
  48. func (ddb debugDB) SetSync(key []byte, value []byte) {
  49. fmt.Printf("%v.SetSync(%v, %v)\n", ddb.label,
  50. cmn.ColoredBytes(key, cmn.Yellow, cmn.Blue),
  51. cmn.ColoredBytes(value, cmn.Green, cmn.Blue))
  52. ddb.db.SetSync(key, value)
  53. }
  54. // Implements atomicSetDeleter.
  55. func (ddb debugDB) SetNoLock(key []byte, value []byte) {
  56. fmt.Printf("%v.SetNoLock(%v, %v)\n", ddb.label,
  57. cmn.ColoredBytes(key, cmn.Yellow, cmn.Blue),
  58. cmn.ColoredBytes(value, cmn.Green, cmn.Blue))
  59. ddb.db.(atomicSetDeleter).SetNoLock(key, value)
  60. }
  61. // Implements atomicSetDeleter.
  62. func (ddb debugDB) SetNoLockSync(key []byte, value []byte) {
  63. fmt.Printf("%v.SetNoLockSync(%v, %v)\n", ddb.label,
  64. cmn.ColoredBytes(key, cmn.Yellow, cmn.Blue),
  65. cmn.ColoredBytes(value, cmn.Green, cmn.Blue))
  66. ddb.db.(atomicSetDeleter).SetNoLockSync(key, value)
  67. }
  68. // Implements DB.
  69. func (ddb debugDB) Delete(key []byte) {
  70. fmt.Printf("%v.Delete(%v)\n", ddb.label,
  71. cmn.ColoredBytes(key, cmn.Red, cmn.Yellow))
  72. ddb.db.Delete(key)
  73. }
  74. // Implements DB.
  75. func (ddb debugDB) DeleteSync(key []byte) {
  76. fmt.Printf("%v.DeleteSync(%v)\n", ddb.label,
  77. cmn.ColoredBytes(key, cmn.Red, cmn.Yellow))
  78. ddb.db.DeleteSync(key)
  79. }
  80. // Implements atomicSetDeleter.
  81. func (ddb debugDB) DeleteNoLock(key []byte) {
  82. fmt.Printf("%v.DeleteNoLock(%v)\n", ddb.label,
  83. cmn.ColoredBytes(key, cmn.Red, cmn.Yellow))
  84. ddb.db.(atomicSetDeleter).DeleteNoLock(key)
  85. }
  86. // Implements atomicSetDeleter.
  87. func (ddb debugDB) DeleteNoLockSync(key []byte) {
  88. fmt.Printf("%v.DeleteNoLockSync(%v)\n", ddb.label,
  89. cmn.ColoredBytes(key, cmn.Red, cmn.Yellow))
  90. ddb.db.(atomicSetDeleter).DeleteNoLockSync(key)
  91. }
  92. // Implements DB.
  93. func (ddb debugDB) Iterator(start, end []byte) Iterator {
  94. fmt.Printf("%v.Iterator(%v, %v)\n", ddb.label,
  95. cmn.ColoredBytes(start, cmn.Cyan, cmn.Blue),
  96. cmn.ColoredBytes(end, cmn.Cyan, cmn.Blue))
  97. return NewDebugIterator(ddb.label, ddb.db.Iterator(start, end))
  98. }
  99. // Implements DB.
  100. func (ddb debugDB) ReverseIterator(start, end []byte) Iterator {
  101. fmt.Printf("%v.ReverseIterator(%v, %v)\n", ddb.label,
  102. cmn.ColoredBytes(start, cmn.Cyan, cmn.Blue),
  103. cmn.ColoredBytes(end, cmn.Cyan, cmn.Blue))
  104. return NewDebugIterator(ddb.label, ddb.db.ReverseIterator(start, end))
  105. }
  106. // Implements DB.
  107. // Panics if the underlying db is not an
  108. // atomicSetDeleter.
  109. func (ddb debugDB) NewBatch() Batch {
  110. fmt.Printf("%v.NewBatch()\n", ddb.label)
  111. return NewDebugBatch(ddb.label, ddb.db.NewBatch())
  112. }
  113. // Implements DB.
  114. func (ddb debugDB) Close() {
  115. fmt.Printf("%v.Close()\n", ddb.label)
  116. ddb.db.Close()
  117. }
  118. // Implements DB.
  119. func (ddb debugDB) Print() {
  120. ddb.db.Print()
  121. }
  122. // Implements DB.
  123. func (ddb debugDB) Stats() map[string]string {
  124. return ddb.db.Stats()
  125. }
  126. //----------------------------------------
  127. // debugIterator
  128. type debugIterator struct {
  129. label string
  130. itr Iterator
  131. }
  132. // For printing all operationgs to the console for debugging.
  133. func NewDebugIterator(label string, itr Iterator) debugIterator {
  134. return debugIterator{
  135. label: label,
  136. itr: itr,
  137. }
  138. }
  139. // Implements Iterator.
  140. func (ditr debugIterator) Domain() (start []byte, end []byte) {
  141. defer func() {
  142. fmt.Printf("%v.itr.Domain() (%X,%X)\n", ditr.label, start, end)
  143. }()
  144. start, end = ditr.itr.Domain()
  145. return
  146. }
  147. // Implements Iterator.
  148. func (ditr debugIterator) Valid() (ok bool) {
  149. defer func() {
  150. fmt.Printf("%v.itr.Valid() %v\n", ditr.label, ok)
  151. }()
  152. ok = ditr.itr.Valid()
  153. return
  154. }
  155. // Implements Iterator.
  156. func (ditr debugIterator) Next() {
  157. fmt.Printf("%v.itr.Next()\n", ditr.label)
  158. ditr.itr.Next()
  159. }
  160. // Implements Iterator.
  161. func (ditr debugIterator) Key() (key []byte) {
  162. key = ditr.itr.Key()
  163. fmt.Printf("%v.itr.Key() %v\n", ditr.label,
  164. cmn.ColoredBytes(key, cmn.Cyan, cmn.Blue))
  165. return
  166. }
  167. // Implements Iterator.
  168. func (ditr debugIterator) Value() (value []byte) {
  169. value = ditr.itr.Value()
  170. fmt.Printf("%v.itr.Value() %v\n", ditr.label,
  171. cmn.ColoredBytes(value, cmn.Green, cmn.Blue))
  172. return
  173. }
  174. // Implements Iterator.
  175. func (ditr debugIterator) Close() {
  176. fmt.Printf("%v.itr.Close()\n", ditr.label)
  177. ditr.itr.Close()
  178. }
  179. //----------------------------------------
  180. // debugBatch
  181. type debugBatch struct {
  182. label string
  183. bch Batch
  184. }
  185. // For printing all operationgs to the console for debugging.
  186. func NewDebugBatch(label string, bch Batch) debugBatch {
  187. return debugBatch{
  188. label: label,
  189. bch: bch,
  190. }
  191. }
  192. // Implements Batch.
  193. func (dbch debugBatch) Set(key, value []byte) {
  194. fmt.Printf("%v.batch.Set(%v, %v)\n", dbch.label,
  195. cmn.ColoredBytes(key, cmn.Yellow, cmn.Blue),
  196. cmn.ColoredBytes(value, cmn.Green, cmn.Blue))
  197. dbch.bch.Set(key, value)
  198. }
  199. // Implements Batch.
  200. func (dbch debugBatch) Delete(key []byte) {
  201. fmt.Printf("%v.batch.Delete(%v)\n", dbch.label,
  202. cmn.ColoredBytes(key, cmn.Red, cmn.Yellow))
  203. dbch.bch.Delete(key)
  204. }
  205. // Implements Batch.
  206. func (dbch debugBatch) Write() {
  207. fmt.Printf("%v.batch.Write()\n", dbch.label)
  208. dbch.bch.Write()
  209. }
  210. // Implements Batch.
  211. func (dbch debugBatch) WriteSync() {
  212. fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
  213. dbch.bch.WriteSync()
  214. }
  215. // Implements Batch.
  216. func (dbch debugBatch) Close() {
  217. dbch.bch.Close()
  218. }