memory_database.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "fmt"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. /*
  22. * This is a test memory database. Do not use for any production it does not get persisted
  23. */
  24. type MemDatabase struct {
  25. db map[string][]byte
  26. }
  27. func NewMemDatabase() (*MemDatabase, error) {
  28. db := &MemDatabase{db: make(map[string][]byte)}
  29. return db, nil
  30. }
  31. func (db *MemDatabase) Put(key []byte, value []byte) error {
  32. db.db[string(key)] = common.CopyBytes(value)
  33. return nil
  34. }
  35. func (db *MemDatabase) Set(key []byte, value []byte) {
  36. db.Put(key, value)
  37. }
  38. func (db *MemDatabase) Get(key []byte) ([]byte, error) {
  39. return db.db[string(key)], nil
  40. }
  41. func (db *MemDatabase) Keys() [][]byte {
  42. keys := [][]byte{}
  43. for key, _ := range db.db {
  44. keys = append(keys, []byte(key))
  45. }
  46. return keys
  47. }
  48. /*
  49. func (db *MemDatabase) GetKeys() []*common.Key {
  50. data, _ := db.Get([]byte("KeyRing"))
  51. return []*common.Key{common.NewKeyFromBytes(data)}
  52. }
  53. */
  54. func (db *MemDatabase) Delete(key []byte) error {
  55. delete(db.db, string(key))
  56. return nil
  57. }
  58. func (db *MemDatabase) Print() {
  59. for key, val := range db.db {
  60. fmt.Printf("%x(%d): ", key, len(key))
  61. node := common.NewValueFromBytes(val)
  62. fmt.Printf("%q\n", node.Val)
  63. }
  64. }
  65. func (db *MemDatabase) Close() {
  66. }
  67. func (db *MemDatabase) LastKnownTD() []byte {
  68. data, _ := db.Get([]byte("LastKnownTotalDifficulty"))
  69. if len(data) == 0 || data == nil {
  70. data = []byte{0x0}
  71. }
  72. return data
  73. }
  74. func (db *MemDatabase) NewBatch() Batch {
  75. return &memBatch{db: db}
  76. }
  77. type kv struct{ k, v []byte }
  78. type memBatch struct {
  79. db *MemDatabase
  80. writes []kv
  81. }
  82. func (w *memBatch) Put(key, value []byte) error {
  83. w.writes = append(w.writes, kv{key, common.CopyBytes(value)})
  84. return nil
  85. }
  86. func (w *memBatch) Write() error {
  87. for _, kv := range w.writes {
  88. w.db.db[string(kv.k)] = kv.v
  89. }
  90. return nil
  91. }