storage.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }
  38. // Put stores a value by key. 0-length keys results in noop.
  39. func (s *EphemeralStorage) Put(key, value string) {
  40. if len(key) == 0 {
  41. return
  42. }
  43. s.data[key] = value
  44. }
  45. // Get returns the previously stored value, or an error if the key is 0-length
  46. // or unknown.
  47. func (s *EphemeralStorage) Get(key string) (string, error) {
  48. if len(key) == 0 {
  49. return "", ErrZeroKey
  50. }
  51. if v, ok := s.data[key]; ok {
  52. return v, nil
  53. }
  54. return "", ErrNotFound
  55. }
  56. // Del removes a key-value pair. If the key doesn't exist, the method is a noop.
  57. func (s *EphemeralStorage) Del(key string) {
  58. delete(s.data, key)
  59. }
  60. func NewEphemeralStorage() Storage {
  61. s := &EphemeralStorage{
  62. data: make(map[string]string),
  63. }
  64. return s
  65. }
  66. // NoStorage is a dummy construct which doesn't remember anything you tell it
  67. type NoStorage struct{}
  68. func (s *NoStorage) Put(key, value string) {}
  69. func (s *NoStorage) Del(key string) {}
  70. func (s *NoStorage) Get(key string) (string, error) {
  71. return "", errors.New("missing key, I probably forgot")
  72. }