github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/state/inmemorystore.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 state
    18  
    19  import (
    20  	"encoding"
    21  	"encoding/json"
    22  	"sync"
    23  )
    24  
    25  // InmemoryStore is the reference implementation of Store interface that is supposed
    26  // to be used in tests.
    27  type InmemoryStore struct {
    28  	db map[string][]byte
    29  	mu sync.RWMutex
    30  }
    31  
    32  // NewInmemoryStore returns a new instance of InmemoryStore.
    33  func NewInmemoryStore() *InmemoryStore {
    34  	return &InmemoryStore{
    35  		db: make(map[string][]byte),
    36  	}
    37  }
    38  
    39  // Get retrieves a value stored for a specific key. If there is no value found,
    40  // ErrNotFound is returned.
    41  func (s *InmemoryStore) Get(key string, i interface{}) (err error) {
    42  	s.mu.RLock()
    43  	defer s.mu.RUnlock()
    44  
    45  	bytes, ok := s.db[key]
    46  	if !ok {
    47  		return ErrNotFound
    48  	}
    49  
    50  	unmarshaler, ok := i.(encoding.BinaryUnmarshaler)
    51  	if !ok {
    52  		return json.Unmarshal(bytes, i)
    53  	}
    54  
    55  	return unmarshaler.UnmarshalBinary(bytes)
    56  }
    57  
    58  // Put stores a value for a specific key.
    59  func (s *InmemoryStore) Put(key string, i interface{}) (err error) {
    60  	s.mu.Lock()
    61  	defer s.mu.Unlock()
    62  	bytes := []byte{}
    63  
    64  	marshaler, ok := i.(encoding.BinaryMarshaler)
    65  	if !ok {
    66  		if bytes, err = json.Marshal(i); err != nil {
    67  			return err
    68  		}
    69  	} else {
    70  		if bytes, err = marshaler.MarshalBinary(); err != nil {
    71  			return err
    72  		}
    73  	}
    74  
    75  	s.db[key] = bytes
    76  	return nil
    77  }
    78  
    79  // Delete removes value stored under a specific key.
    80  func (s *InmemoryStore) Delete(key string) (err error) {
    81  	s.mu.Lock()
    82  	defer s.mu.Unlock()
    83  
    84  	if _, ok := s.db[key]; !ok {
    85  		return ErrNotFound
    86  	}
    87  	delete(s.db, key)
    88  	return nil
    89  }
    90  
    91  // Close does not do anything.
    92  func (s *InmemoryStore) Close() error {
    93  	return nil
    94  }