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.

161 lines
2.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package db
  2. import (
  3. "fmt"
  4. "sync"
  5. )
  6. func init() {
  7. registerDBCreator(MemDBBackendStr, func(name string, dir string) (DB, error) {
  8. return NewMemDB(), nil
  9. }, false)
  10. }
  11. type MemDB struct {
  12. mtx sync.Mutex
  13. db map[string][]byte
  14. }
  15. func NewMemDB() *MemDB {
  16. database := &MemDB{db: make(map[string][]byte)}
  17. return database
  18. }
  19. func (db *MemDB) Get(key []byte) []byte {
  20. db.mtx.Lock()
  21. defer db.mtx.Unlock()
  22. return db.db[string(key)]
  23. }
  24. func (db *MemDB) Set(key []byte, value []byte) {
  25. db.mtx.Lock()
  26. defer db.mtx.Unlock()
  27. db.db[string(key)] = value
  28. }
  29. func (db *MemDB) SetSync(key []byte, value []byte) {
  30. db.mtx.Lock()
  31. defer db.mtx.Unlock()
  32. db.db[string(key)] = value
  33. }
  34. func (db *MemDB) Delete(key []byte) {
  35. db.mtx.Lock()
  36. defer db.mtx.Unlock()
  37. delete(db.db, string(key))
  38. }
  39. func (db *MemDB) DeleteSync(key []byte) {
  40. db.mtx.Lock()
  41. defer db.mtx.Unlock()
  42. delete(db.db, string(key))
  43. }
  44. func (db *MemDB) Close() {
  45. // Close is a noop since for an in-memory
  46. // database, we don't have a destination
  47. // to flush contents to nor do we want
  48. // any data loss on invoking Close()
  49. // See the discussion in https://github.com/tendermint/tmlibs/pull/56
  50. }
  51. func (db *MemDB) Print() {
  52. db.mtx.Lock()
  53. defer db.mtx.Unlock()
  54. for key, value := range db.db {
  55. fmt.Printf("[%X]:\t[%X]\n", []byte(key), value)
  56. }
  57. }
  58. func (db *MemDB) Stats() map[string]string {
  59. stats := make(map[string]string)
  60. stats["database.type"] = "memDB"
  61. return stats
  62. }
  63. type memDBIterator struct {
  64. last int
  65. keys []string
  66. db *MemDB
  67. }
  68. func newMemDBIterator() *memDBIterator {
  69. return &memDBIterator{}
  70. }
  71. func (it *memDBIterator) Next() bool {
  72. if it.last >= len(it.keys)-1 {
  73. return false
  74. }
  75. it.last++
  76. return true
  77. }
  78. func (it *memDBIterator) Key() []byte {
  79. return []byte(it.keys[it.last])
  80. }
  81. func (it *memDBIterator) Value() []byte {
  82. return it.db.Get(it.Key())
  83. }
  84. func (db *MemDB) Iterator() Iterator {
  85. it := newMemDBIterator()
  86. it.db = db
  87. it.last = -1
  88. db.mtx.Lock()
  89. defer db.mtx.Unlock()
  90. // unfortunately we need a copy of all of the keys
  91. for key, _ := range db.db {
  92. it.keys = append(it.keys, key)
  93. }
  94. return it
  95. }
  96. func (db *MemDB) NewBatch() Batch {
  97. return &memDBBatch{db, nil}
  98. }
  99. //--------------------------------------------------------------------------------
  100. type memDBBatch struct {
  101. db *MemDB
  102. ops []operation
  103. }
  104. type opType int
  105. const (
  106. opTypeSet = 1
  107. opTypeDelete = 2
  108. )
  109. type operation struct {
  110. opType
  111. key []byte
  112. value []byte
  113. }
  114. func (mBatch *memDBBatch) Set(key, value []byte) {
  115. mBatch.ops = append(mBatch.ops, operation{opTypeSet, key, value})
  116. }
  117. func (mBatch *memDBBatch) Delete(key []byte) {
  118. mBatch.ops = append(mBatch.ops, operation{opTypeDelete, key, nil})
  119. }
  120. func (mBatch *memDBBatch) Write() {
  121. mBatch.db.mtx.Lock()
  122. defer mBatch.db.mtx.Unlock()
  123. for _, op := range mBatch.ops {
  124. if op.opType == opTypeSet {
  125. mBatch.db.db[string(op.key)] = op.value
  126. } else if op.opType == opTypeDelete {
  127. delete(mBatch.db.db, string(op.key))
  128. }
  129. }
  130. }