storage.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2018 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 storage
  17. import "errors"
  18. var (
  19. // ErrZeroKey is returned if an attempt was made to inset a 0-length key.
  20. ErrZeroKey = errors.New("0-length key")
  21. // ErrNotFound is returned if an unknown key is attempted to be retrieved.
  22. ErrNotFound = errors.New("not found")
  23. )
  24. type Storage interface {
  25. // Put stores a value by key. 0-length keys results in noop.
  26. Put(key, value string)
  27. // Get returns the previously stored value, or an error if the key is 0-length
  28. // or unknown.
  29. Get(key string) (string, error)
  30. // Del removes a key-value pair. If the key doesn't exist, the method is a noop.
  31. Del(key string)
  32. }
  33. // EphemeralStorage is an in-memory storage that does
  34. // not persist values to disk. Mainly used for testing
  35. type EphemeralStorage struct {
  36. data map[string]string
  37. namespace string
  38. }
  39. // Put stores a value by key. 0-length keys results in noop.
  40. func (s *EphemeralStorage) Put(key, value string) {
  41. if len(key) == 0 {
  42. return
  43. }
  44. s.data[key] = value
  45. }
  46. // Get returns the previously stored value, or an error if the key is 0-length
  47. // or unknown.
  48. func (s *EphemeralStorage) Get(key string) (string, error) {
  49. if len(key) == 0 {
  50. return "", ErrZeroKey
  51. }
  52. if v, ok := s.data[key]; ok {
  53. return v, nil
  54. }
  55. return "", ErrNotFound
  56. }
  57. // Del removes a key-value pair. If the key doesn't exist, the method is a noop.
  58. func (s *EphemeralStorage) Del(key string) {
  59. delete(s.data, key)
  60. }
  61. func NewEphemeralStorage() Storage {
  62. s := &EphemeralStorage{
  63. data: make(map[string]string),
  64. }
  65. return s
  66. }
  67. // NoStorage is a dummy construct which doesn't remember anything you tell it
  68. type NoStorage struct{}
  69. func (s *NoStorage) Put(key, value string) {}
  70. func (s *NoStorage) Del(key string) {}
  71. func (s *NoStorage) Get(key string) (string, error) {
  72. return "", errors.New("I forgot")
  73. }