github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/signer/storage/storage.go (about)

     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  
    18  package storage
    19  
    20  type Storage interface {
    21  	// Put stores a value by key. 0-length keys results in no-op
    22  	Put(key, value string)
    23  	// Get returns the previously stored value, or the empty string if it does not exist or key is of 0-length
    24  	Get(key string) string
    25  }
    26  
    27  // EphemeralStorage is an in-memory storage that does
    28  // not persist values to disk. Mainly used for testing
    29  type EphemeralStorage struct {
    30  	data      map[string]string
    31  	namespace string
    32  }
    33  
    34  func (s *EphemeralStorage) Put(key, value string) {
    35  	if len(key) == 0 {
    36  		return
    37  	}
    38  	//fmt.Printf("storage: put %v -> %v\n", key, value)
    39  	s.data[key] = value
    40  }
    41  
    42  func (s *EphemeralStorage) Get(key string) string {
    43  	if len(key) == 0 {
    44  		return ""
    45  	}
    46  	//fmt.Printf("storage: get %v\n", key)
    47  	if v, exist := s.data[key]; exist {
    48  		return v
    49  	}
    50  	return ""
    51  }
    52  
    53  func NewEphemeralStorage() Storage {
    54  	s := &EphemeralStorage{
    55  		data: make(map[string]string),
    56  	}
    57  	return s
    58  }
    59  
    60  // NoStorage is a dummy construct which doesn't remember anything you tell it
    61  type NoStorage struct{}
    62  
    63  func (s *NoStorage) Put(key, value string) {}
    64  func (s *NoStorage) Get(key string) string {
    65  	return ""
    66  }