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.

256 lines
6.1 KiB

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