memory_database.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "fmt"
  20. "sync"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. /*
  24. * This is a test memory database. Do not use for any production it does not get persisted
  25. */
  26. type MemDatabase struct {
  27. db map[string][]byte
  28. lock sync.RWMutex
  29. }
  30. func NewMemDatabase() (*MemDatabase, error) {
  31. return &MemDatabase{
  32. db: make(map[string][]byte),
  33. }, nil
  34. }
  35. func (db *MemDatabase) Put(key []byte, value []byte) error {
  36. db.lock.Lock()
  37. defer db.lock.Unlock()
  38. db.db[string(key)] = common.CopyBytes(value)
  39. return nil
  40. }
  41. func (db *MemDatabase) Set(key []byte, value []byte) {
  42. db.lock.Lock()
  43. defer db.lock.Unlock()
  44. db.Put(key, value)
  45. }
  46. func (db *MemDatabase) Get(key []byte) ([]byte, error) {
  47. db.lock.RLock()
  48. defer db.lock.RUnlock()
  49. if entry, ok := db.db[string(key)]; ok {
  50. return entry, nil
  51. }
  52. return nil, errors.New("not found")
  53. }
  54. func (db *MemDatabase) Keys() [][]byte {
  55. db.lock.RLock()
  56. defer db.lock.RUnlock()
  57. keys := [][]byte{}
  58. for key, _ := range db.db {
  59. keys = append(keys, []byte(key))
  60. }
  61. return keys
  62. }
  63. /*
  64. func (db *MemDatabase) GetKeys() []*common.Key {
  65. data, _ := db.Get([]byte("KeyRing"))
  66. return []*common.Key{common.NewKeyFromBytes(data)}
  67. }
  68. */
  69. func (db *MemDatabase) Delete(key []byte) error {
  70. db.lock.Lock()
  71. defer db.lock.Unlock()
  72. delete(db.db, string(key))
  73. return nil
  74. }
  75. func (db *MemDatabase) Print() {
  76. db.lock.RLock()
  77. defer db.lock.RUnlock()
  78. for key, val := range db.db {
  79. fmt.Printf("%x(%d): ", key, len(key))
  80. node := common.NewValueFromBytes(val)
  81. fmt.Printf("%q\n", node.Val)
  82. }
  83. }
  84. func (db *MemDatabase) Close() {
  85. }
  86. func (db *MemDatabase) LastKnownTD() []byte {
  87. data, _ := db.Get([]byte("LastKnownTotalDifficulty"))
  88. if len(data) == 0 || data == nil {
  89. data = []byte{0x0}
  90. }
  91. return data
  92. }
  93. func (db *MemDatabase) NewBatch() Batch {
  94. return &memBatch{db: db}
  95. }
  96. type kv struct{ k, v []byte }
  97. type memBatch struct {
  98. db *MemDatabase
  99. writes []kv
  100. lock sync.RWMutex
  101. }
  102. func (b *memBatch) Put(key, value []byte) error {
  103. b.lock.Lock()
  104. defer b.lock.Unlock()
  105. b.writes = append(b.writes, kv{key, common.CopyBytes(value)})
  106. return nil
  107. }
  108. func (b *memBatch) Write() error {
  109. b.lock.RLock()
  110. defer b.lock.RUnlock()
  111. b.db.lock.Lock()
  112. defer b.db.lock.Unlock()
  113. for _, kv := range b.writes {
  114. b.db.db[string(kv.k)] = kv.v
  115. }
  116. return nil
  117. }