github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/signer/storage/storage.go (about)

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