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.

232 lines
5.7 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, cmn.Cyan(_fmt("%X", key)), cmn.Blue(_fmt("%X", value)))
  29. }()
  30. value = ddb.db.Get(key)
  31. return
  32. }
  33. // Implements DB.
  34. func (ddb debugDB) Has(key []byte) (has bool) {
  35. defer func() {
  36. fmt.Printf("%v.Has(%v) %v\n", ddb.label, cmn.Cyan(_fmt("%X", key)), 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, cmn.Cyan(_fmt("%X", key)), cmn.Yellow(_fmt("%X", value)))
  43. ddb.db.Set(key, value)
  44. }
  45. // Implements DB.
  46. func (ddb debugDB) SetSync(key []byte, value []byte) {
  47. fmt.Printf("%v.SetSync(%v, %v)\n", ddb.label, cmn.Cyan(_fmt("%X", key)), cmn.Yellow(_fmt("%X", value)))
  48. ddb.db.SetSync(key, value)
  49. }
  50. // Implements atomicSetDeleter.
  51. func (ddb debugDB) SetNoLock(key []byte, value []byte) {
  52. fmt.Printf("%v.SetNoLock(%v, %v)\n", ddb.label, cmn.Cyan(_fmt("%X", key)), cmn.Yellow(_fmt("%X", value)))
  53. ddb.db.(atomicSetDeleter).SetNoLock(key, value)
  54. }
  55. // Implements atomicSetDeleter.
  56. func (ddb debugDB) SetNoLockSync(key []byte, value []byte) {
  57. fmt.Printf("%v.SetNoLockSync(%v, %v)\n", ddb.label, cmn.Cyan(_fmt("%X", key)), cmn.Yellow(_fmt("%X", value)))
  58. ddb.db.(atomicSetDeleter).SetNoLockSync(key, value)
  59. }
  60. // Implements DB.
  61. func (ddb debugDB) Delete(key []byte) {
  62. fmt.Printf("%v.Delete(%v)\n", ddb.label, cmn.Red(_fmt("%X", key)))
  63. ddb.db.Delete(key)
  64. }
  65. // Implements DB.
  66. func (ddb debugDB) DeleteSync(key []byte) {
  67. fmt.Printf("%v.DeleteSync(%v)\n", ddb.label, cmn.Red(_fmt("%X", key)))
  68. ddb.db.DeleteSync(key)
  69. }
  70. // Implements atomicSetDeleter.
  71. func (ddb debugDB) DeleteNoLock(key []byte) {
  72. fmt.Printf("%v.DeleteNoLock(%v)\n", ddb.label, cmn.Red(_fmt("%X", key)))
  73. ddb.db.(atomicSetDeleter).DeleteNoLock(key)
  74. }
  75. // Implements atomicSetDeleter.
  76. func (ddb debugDB) DeleteNoLockSync(key []byte) {
  77. fmt.Printf("%v.DeleteNoLockSync(%v)\n", ddb.label, cmn.Red(_fmt("%X", key)))
  78. ddb.db.(atomicSetDeleter).DeleteNoLockSync(key)
  79. }
  80. // Implements DB.
  81. func (ddb debugDB) Iterator(start, end []byte) Iterator {
  82. fmt.Printf("%v.Iterator(%v, %v)\n", ddb.label, cmn.Cyan(_fmt("%X", start)), cmn.Blue(_fmt("%X", end)))
  83. return NewDebugIterator(ddb.label, ddb.db.Iterator(start, end))
  84. }
  85. // Implements DB.
  86. func (ddb debugDB) ReverseIterator(start, end []byte) Iterator {
  87. fmt.Printf("%v.ReverseIterator(%v, %v)\n", ddb.label, cmn.Cyan(_fmt("%X", start)), cmn.Blue(_fmt("%X", end)))
  88. return NewDebugIterator(ddb.label, ddb.db.ReverseIterator(start, end))
  89. }
  90. // Implements DB.
  91. // Panics if the underlying db is not an
  92. // atomicSetDeleter.
  93. func (ddb debugDB) NewBatch() Batch {
  94. fmt.Printf("%v.NewBatch()\n", ddb.label)
  95. return NewDebugBatch(ddb.label, ddb.db.NewBatch())
  96. }
  97. // Implements DB.
  98. func (ddb debugDB) Close() {
  99. fmt.Printf("%v.Close()\n", ddb.label)
  100. ddb.db.Close()
  101. }
  102. // Implements DB.
  103. func (ddb debugDB) Print() {
  104. ddb.db.Print()
  105. }
  106. // Implements DB.
  107. func (ddb debugDB) Stats() map[string]string {
  108. return ddb.db.Stats()
  109. }
  110. //----------------------------------------
  111. // debugIterator
  112. type debugIterator struct {
  113. label string
  114. itr Iterator
  115. }
  116. // For printing all operationgs to the console for debugging.
  117. func NewDebugIterator(label string, itr Iterator) debugIterator {
  118. return debugIterator{
  119. label: label,
  120. itr: itr,
  121. }
  122. }
  123. // Implements Iterator.
  124. func (ditr debugIterator) Domain() (start []byte, end []byte) {
  125. defer func() {
  126. fmt.Printf("%v.itr.Domain() (%X,%X)\n", ditr.label, start, end)
  127. }()
  128. start, end = ditr.itr.Domain()
  129. return
  130. }
  131. // Implements Iterator.
  132. func (ditr debugIterator) Valid() (ok bool) {
  133. defer func() {
  134. fmt.Printf("%v.itr.Valid() %v\n", ditr.label, ok)
  135. }()
  136. ok = ditr.itr.Valid()
  137. return
  138. }
  139. // Implements Iterator.
  140. func (ditr debugIterator) Next() {
  141. fmt.Printf("%v.itr.Next()\n", ditr.label)
  142. ditr.itr.Next()
  143. }
  144. // Implements Iterator.
  145. func (ditr debugIterator) Key() (key []byte) {
  146. fmt.Printf("%v.itr.Key() %v\n", ditr.label, cmn.Cyan(_fmt("%X", key)))
  147. key = ditr.itr.Key()
  148. return
  149. }
  150. // Implements Iterator.
  151. func (ditr debugIterator) Value() (value []byte) {
  152. fmt.Printf("%v.itr.Value() %v\n", ditr.label, cmn.Blue(_fmt("%X", value)))
  153. value = ditr.itr.Value()
  154. return
  155. }
  156. // Implements Iterator.
  157. func (ditr debugIterator) Close() {
  158. fmt.Printf("%v.itr.Close()\n", ditr.label)
  159. ditr.itr.Close()
  160. }
  161. //----------------------------------------
  162. // debugBatch
  163. type debugBatch struct {
  164. label string
  165. bch Batch
  166. }
  167. // For printing all operationgs to the console for debugging.
  168. func NewDebugBatch(label string, bch Batch) debugBatch {
  169. return debugBatch{
  170. label: label,
  171. bch: bch,
  172. }
  173. }
  174. // Implements Batch.
  175. func (dbch debugBatch) Set(key, value []byte) {
  176. fmt.Printf("%v.batch.Set(%v, %v)\n", dbch.label, cmn.Cyan(_fmt("%X", key)), cmn.Yellow(_fmt("%X", value)))
  177. dbch.bch.Set(key, value)
  178. }
  179. // Implements Batch.
  180. func (dbch debugBatch) Delete(key []byte) {
  181. fmt.Printf("%v.batch.Delete(%v)\n", dbch.label, cmn.Red(_fmt("%X", key)))
  182. dbch.bch.Delete(key)
  183. }
  184. // Implements Batch.
  185. func (dbch debugBatch) Write() {
  186. fmt.Printf("%v.batch.Write()\n", dbch.label)
  187. dbch.bch.Write()
  188. }
  189. // Implements Batch.
  190. func (dbch debugBatch) WriteSync() {
  191. fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
  192. dbch.bch.WriteSync()
  193. }