memory_database.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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)] = 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. /*
  42. func (db *MemDatabase) GetKeys() []*common.Key {
  43. data, _ := db.Get([]byte("KeyRing"))
  44. return []*common.Key{common.NewKeyFromBytes(data)}
  45. }
  46. */
  47. func (db *MemDatabase) Delete(key []byte) error {
  48. delete(db.db, string(key))
  49. return nil
  50. }
  51. func (db *MemDatabase) Print() {
  52. for key, val := range db.db {
  53. fmt.Printf("%x(%d): ", key, len(key))
  54. node := common.NewValueFromBytes(val)
  55. fmt.Printf("%q\n", node.Val)
  56. }
  57. }
  58. func (db *MemDatabase) Close() {
  59. }
  60. func (db *MemDatabase) LastKnownTD() []byte {
  61. data, _ := db.Get([]byte("LastKnownTotalDifficulty"))
  62. if len(data) == 0 || data == nil {
  63. data = []byte{0x0}
  64. }
  65. return data
  66. }
  67. func (db *MemDatabase) Flush() error {
  68. return nil
  69. }