storage.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2018 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 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package storage
  18. import (
  19. "fmt"
  20. )
  21. type Storage interface {
  22. // Put stores a value by key. 0-length keys results in no-op
  23. Put(key, value string)
  24. // Get returns the previously stored value, or the empty string if it does not exist or key is of 0-length
  25. Get(key string) string
  26. }
  27. // EphemeralStorage is an in-memory storage that does
  28. // not persist values to disk. Mainly used for testing
  29. type EphemeralStorage struct {
  30. data map[string]string
  31. namespace string
  32. }
  33. func (s *EphemeralStorage) Put(key, value string) {
  34. if len(key) == 0 {
  35. return
  36. }
  37. fmt.Printf("storage: put %v -> %v\n", key, value)
  38. s.data[key] = value
  39. }
  40. func (s *EphemeralStorage) Get(key string) string {
  41. if len(key) == 0 {
  42. return ""
  43. }
  44. fmt.Printf("storage: get %v\n", key)
  45. if v, exist := s.data[key]; exist {
  46. return v
  47. }
  48. return ""
  49. }
  50. func NewEphemeralStorage() Storage {
  51. s := &EphemeralStorage{
  52. data: make(map[string]string),
  53. }
  54. return s
  55. }