memory_database.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ethdb
  17. import (
  18. "errors"
  19. "sync"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. /*
  23. * This is a test memory database. Do not use for any production it does not get persisted
  24. */
  25. type MemDatabase struct {
  26. db map[string][]byte
  27. lock sync.RWMutex
  28. }
  29. func NewMemDatabase() *MemDatabase {
  30. return &MemDatabase{
  31. db: make(map[string][]byte),
  32. }
  33. }
  34. func NewMemDatabaseWithCap(size int) *MemDatabase {
  35. return &MemDatabase{
  36. db: make(map[string][]byte, size),
  37. }
  38. }
  39. func (db *MemDatabase) Put(key []byte, value []byte) error {
  40. db.lock.Lock()
  41. defer db.lock.Unlock()
  42. db.db[string(key)] = common.CopyBytes(value)
  43. return nil
  44. }
  45. func (db *MemDatabase) Has(key []byte) (bool, error) {
  46. db.lock.RLock()
  47. defer db.lock.RUnlock()
  48. _, ok := db.db[string(key)]
  49. return ok, nil
  50. }
  51. func (db *MemDatabase) Get(key []byte) ([]byte, error) {
  52. db.lock.RLock()
  53. defer db.lock.RUnlock()
  54. if entry, ok := db.db[string(key)]; ok {
  55. return common.CopyBytes(entry), nil
  56. }
  57. return nil, errors.New("not found")
  58. }
  59. func (db *MemDatabase) Keys() [][]byte {
  60. db.lock.RLock()
  61. defer db.lock.RUnlock()
  62. keys := [][]byte{}
  63. for key := range db.db {
  64. keys = append(keys, []byte(key))
  65. }
  66. return keys
  67. }
  68. func (db *MemDatabase) Delete(key []byte) error {
  69. db.lock.Lock()
  70. defer db.lock.Unlock()
  71. delete(db.db, string(key))
  72. return nil
  73. }
  74. func (db *MemDatabase) Close() {}
  75. func (db *MemDatabase) NewBatch() Batch {
  76. return &memBatch{db: db}
  77. }
  78. func (db *MemDatabase) Len() int { return len(db.db) }
  79. type kv struct{ k, v []byte }
  80. type memBatch struct {
  81. db *MemDatabase
  82. writes []kv
  83. size int
  84. }
  85. func (b *memBatch) Put(key, value []byte) error {
  86. b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)})
  87. b.size += len(value)
  88. return nil
  89. }
  90. func (b *memBatch) Write() error {
  91. b.db.lock.Lock()
  92. defer b.db.lock.Unlock()
  93. for _, kv := range b.writes {
  94. b.db.db[string(kv.k)] = kv.v
  95. }
  96. return nil
  97. }
  98. func (b *memBatch) ValueSize() int {
  99. return b.size
  100. }
  101. func (b *memBatch) Reset() {
  102. b.writes = b.writes[:0]
  103. b.size = 0
  104. }