github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/examples/chaincode/go/utxo/util/store.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  // Store interface describes the storage used by this chaincode. The interface
    20  // was created so either the state database store can be used or a in memory
    21  // store can be used for unit testing.
    22  type Store interface {
    23  	GetState(Key) (*TX_TXOUT, bool, error)
    24  	PutState(Key, *TX_TXOUT) error
    25  	DelState(Key) error
    26  	GetTran(string) ([]byte, bool, error)
    27  	PutTran(string, []byte) error
    28  }
    29  
    30  // InMemoryStore used for unit testing
    31  type InMemoryStore struct {
    32  	Map     map[Key]*TX_TXOUT
    33  	TranMap map[string][]byte
    34  }
    35  
    36  // MakeInMemoryStore creates a new in memory store
    37  func MakeInMemoryStore() Store {
    38  	ims := &InMemoryStore{}
    39  	ims.Map = make(map[Key]*TX_TXOUT)
    40  	ims.TranMap = make(map[string][]byte)
    41  	return ims
    42  }
    43  
    44  // GetState returns the transaction for the given key
    45  func (ims *InMemoryStore) GetState(key Key) (*TX_TXOUT, bool, error) {
    46  	value, ok := ims.Map[key]
    47  	return value, ok, nil
    48  }
    49  
    50  // DelState deletes the given key and corresponding transactions
    51  func (ims *InMemoryStore) DelState(key Key) error {
    52  	delete(ims.Map, key)
    53  	return nil
    54  }
    55  
    56  // PutState saves the key and transaction in memory
    57  func (ims *InMemoryStore) PutState(key Key, value *TX_TXOUT) error {
    58  	ims.Map[key] = value
    59  	return nil
    60  }
    61  
    62  // GetTran returns the transaction for the given hash
    63  func (ims *InMemoryStore) GetTran(key string) ([]byte, bool, error) {
    64  	value, ok := ims.TranMap[key]
    65  	return value, ok, nil
    66  }
    67  
    68  // PutTran saves the hash and transaction in memory
    69  func (ims *InMemoryStore) PutTran(key string, value []byte) error {
    70  	ims.TranMap[key] = value
    71  	return nil
    72  }