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.

216 lines
4.8 KiB

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