memorydb.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package memorydb implements the key-value database layer based on memory maps.
  17. package memorydb
  18. import (
  19. "errors"
  20. "sort"
  21. "strings"
  22. "sync"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. )
  26. var (
  27. // errMemorydbClosed is returned if a memory database was already closed at the
  28. // invocation of a data access operation.
  29. errMemorydbClosed = errors.New("database closed")
  30. // errMemorydbNotFound is returned if a key is requested that is not found in
  31. // the provided memory database.
  32. errMemorydbNotFound = errors.New("not found")
  33. )
  34. // MemoryDatabase is an ephemeral key-value store. Apart from basic data storage
  35. // functionality it also supports batch writes and iterating over the keyspace in
  36. // binary-alphabetical order.
  37. type MemoryDatabase struct {
  38. db map[string][]byte
  39. lock sync.RWMutex
  40. }
  41. // New returns a wrapped map with all the required database interface methods
  42. // implemented.
  43. func New() *MemoryDatabase {
  44. return &MemoryDatabase{
  45. db: make(map[string][]byte),
  46. }
  47. }
  48. // NewWithCap returns a wrapped map pre-allocated to the provided capcity with
  49. // all the required database interface methods implemented.
  50. func NewWithCap(size int) *MemoryDatabase {
  51. return &MemoryDatabase{
  52. db: make(map[string][]byte, size),
  53. }
  54. }
  55. // Close deallocates the internal map and ensures any consecutive data access op
  56. // failes with an error.
  57. func (db *MemoryDatabase) Close() error {
  58. db.lock.Lock()
  59. defer db.lock.Unlock()
  60. db.db = nil
  61. return nil
  62. }
  63. // Has retrieves if a key is present in the key-value store.
  64. func (db *MemoryDatabase) Has(key []byte) (bool, error) {
  65. db.lock.RLock()
  66. defer db.lock.RUnlock()
  67. if db.db == nil {
  68. return false, errMemorydbClosed
  69. }
  70. _, ok := db.db[string(key)]
  71. return ok, nil
  72. }
  73. // Get retrieves the given key if it's present in the key-value store.
  74. func (db *MemoryDatabase) Get(key []byte) ([]byte, error) {
  75. db.lock.RLock()
  76. defer db.lock.RUnlock()
  77. if db.db == nil {
  78. return nil, errMemorydbClosed
  79. }
  80. if entry, ok := db.db[string(key)]; ok {
  81. return common.CopyBytes(entry), nil
  82. }
  83. return nil, errMemorydbNotFound
  84. }
  85. // Put inserts the given value into the key-value store.
  86. func (db *MemoryDatabase) Put(key []byte, value []byte) error {
  87. db.lock.Lock()
  88. defer db.lock.Unlock()
  89. if db.db == nil {
  90. return errMemorydbClosed
  91. }
  92. db.db[string(key)] = common.CopyBytes(value)
  93. return nil
  94. }
  95. // Delete removes the key from the key-value store.
  96. func (db *MemoryDatabase) Delete(key []byte) error {
  97. db.lock.Lock()
  98. defer db.lock.Unlock()
  99. if db.db == nil {
  100. return errMemorydbClosed
  101. }
  102. delete(db.db, string(key))
  103. return nil
  104. }
  105. // NewBatch creates a write-only key-value store that buffers changes to its host
  106. // database until a final write is called.
  107. func (db *MemoryDatabase) NewBatch() ethdb.Batch {
  108. return &memoryBatch{
  109. db: db,
  110. }
  111. }
  112. // NewIterator creates a binary-alphabetical iterator over the entire keyspace
  113. // contained within the memory database.
  114. func (db *MemoryDatabase) NewIterator() ethdb.Iterator {
  115. return db.NewIteratorWithPrefix(nil)
  116. }
  117. // NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
  118. // of database content with a particular key prefix.
  119. func (db *MemoryDatabase) NewIteratorWithPrefix(prefix []byte) ethdb.Iterator {
  120. db.lock.RLock()
  121. defer db.lock.RUnlock()
  122. var (
  123. pr = string(prefix)
  124. keys = make([]string, 0, len(db.db))
  125. values = make([][]byte, 0, len(db.db))
  126. )
  127. // Collect the keys from the memory database corresponding to the given prefix
  128. for key := range db.db {
  129. if strings.HasPrefix(key, pr) {
  130. keys = append(keys, key)
  131. }
  132. }
  133. // Sort the items and retrieve the associated values
  134. sort.Strings(keys)
  135. for _, key := range keys {
  136. values = append(values, db.db[key])
  137. }
  138. return &memoryIterator{
  139. keys: keys,
  140. values: values,
  141. }
  142. }
  143. // Stat returns a particular internal stat of the database.
  144. func (db *MemoryDatabase) Stat(property string) (string, error) {
  145. return "", errors.New("unknown property")
  146. }
  147. // Compact is not supported on a memory database.
  148. func (db *MemoryDatabase) Compact(start []byte, limit []byte) error {
  149. return errors.New("unsupported operation")
  150. }
  151. // Len returns the number of entries currently present in the memory database.
  152. //
  153. // Note, this method is only used for testing (i.e. not public in general) and
  154. // does not have explicit checks for closed-ness to allow simpler testing code.
  155. func (db *MemoryDatabase) Len() int {
  156. db.lock.RLock()
  157. defer db.lock.RUnlock()
  158. return len(db.db)
  159. }
  160. // keyvalue is a key-value tuple tagged with a deletion field to allow creating
  161. // memory-database write batches.
  162. type keyvalue struct {
  163. key []byte
  164. value []byte
  165. delete bool
  166. }
  167. // memoryBatch is a write-only memory batch that commits changes to its host
  168. // database when Write is called. A batch cannot be used concurrently.
  169. type memoryBatch struct {
  170. db *MemoryDatabase
  171. writes []keyvalue
  172. size int
  173. }
  174. // Put inserts the given value into the batch for later committing.
  175. func (b *memoryBatch) Put(key, value []byte) error {
  176. b.writes = append(b.writes, keyvalue{common.CopyBytes(key), common.CopyBytes(value), false})
  177. b.size += len(value)
  178. return nil
  179. }
  180. // Delete inserts the a key removal into the batch for later committing.
  181. func (b *memoryBatch) Delete(key []byte) error {
  182. b.writes = append(b.writes, keyvalue{common.CopyBytes(key), nil, true})
  183. b.size += 1
  184. return nil
  185. }
  186. // ValueSize retrieves the amount of data queued up for writing.
  187. func (b *memoryBatch) ValueSize() int {
  188. return b.size
  189. }
  190. // Write flushes any accumulated data to the memory database.
  191. func (b *memoryBatch) Write() error {
  192. b.db.lock.Lock()
  193. defer b.db.lock.Unlock()
  194. for _, keyvalue := range b.writes {
  195. if keyvalue.delete {
  196. delete(b.db.db, string(keyvalue.key))
  197. continue
  198. }
  199. b.db.db[string(keyvalue.key)] = keyvalue.value
  200. }
  201. return nil
  202. }
  203. // Reset resets the batch for reuse.
  204. func (b *memoryBatch) Reset() {
  205. b.writes = b.writes[:0]
  206. b.size = 0
  207. }
  208. // memoryIterator can walk over the (potentially partial) keyspace of a memory
  209. // key value store. Internally it is a deep copy of the entire iterated state,
  210. // sorted by keys.
  211. type memoryIterator struct {
  212. inited bool
  213. keys []string
  214. values [][]byte
  215. }
  216. // Next moves the iterator to the next key/value pair. It returns whether the
  217. // iterator is exhausted.
  218. func (it *memoryIterator) Next() bool {
  219. // If the iterator was not yet initialized, do it now
  220. if !it.inited {
  221. it.inited = true
  222. return len(it.keys) > 0
  223. }
  224. // Iterator already initialize, advance it
  225. if len(it.keys) > 0 {
  226. it.keys = it.keys[1:]
  227. it.values = it.values[1:]
  228. }
  229. return len(it.keys) > 0
  230. }
  231. // Error returns any accumulated error. Exhausting all the key/value pairs
  232. // is not considered to be an error. A memory iterator cannot encounter errors.
  233. func (it *memoryIterator) Error() error {
  234. return nil
  235. }
  236. // Key returns the key of the current key/value pair, or nil if done. The caller
  237. // should not modify the contents of the returned slice, and its contents may
  238. // change on the next call to Next.
  239. func (it *memoryIterator) Key() []byte {
  240. if len(it.keys) > 0 {
  241. return []byte(it.keys[0])
  242. }
  243. return nil
  244. }
  245. // Value returns the value of the current key/value pair, or nil if done. The
  246. // caller should not modify the contents of the returned slice, and its contents
  247. // may change on the next call to Next.
  248. func (it *memoryIterator) Value() []byte {
  249. if len(it.values) > 0 {
  250. return it.values[0]
  251. }
  252. return nil
  253. }
  254. // Release releases associated resources. Release should always succeed and can
  255. // be called multiple times without causing error.
  256. func (it *memoryIterator) Release() {
  257. it.keys, it.values = nil, nil
  258. }