github.com/CryptoKass/go-ethereum@v1.9.8-0.20191108085857-de2259d27c75/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  	namespace string
    46  }
    47  
    48  // Put stores a value by key. 0-length keys results in noop.
    49  func (s *EphemeralStorage) Put(key, value string) {
    50  	if len(key) == 0 {
    51  		return
    52  	}
    53  	s.data[key] = value
    54  }
    55  
    56  // Get returns the previously stored value, or an error if the key is 0-length
    57  // or unknown.
    58  func (s *EphemeralStorage) Get(key string) (string, error) {
    59  	if len(key) == 0 {
    60  		return "", ErrZeroKey
    61  	}
    62  	if v, ok := s.data[key]; ok {
    63  		return v, nil
    64  	}
    65  	return "", ErrNotFound
    66  }
    67  
    68  // Del removes a key-value pair. If the key doesn't exist, the method is a noop.
    69  func (s *EphemeralStorage) Del(key string) {
    70  	delete(s.data, key)
    71  }
    72  
    73  func NewEphemeralStorage() Storage {
    74  	s := &EphemeralStorage{
    75  		data: make(map[string]string),
    76  	}
    77  	return s
    78  }
    79  
    80  // NoStorage is a dummy construct which doesn't remember anything you tell it
    81  type NoStorage struct{}
    82  
    83  func (s *NoStorage) Put(key, value string) {}
    84  func (s *NoStorage) Del(key string)        {}
    85  func (s *NoStorage) Get(key string) (string, error) {
    86  	return "", errors.New("I forgot")
    87  }