storage.go 2.5 KB

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