github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/chaincode/go/map/map.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 main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/hyperledger/fabric/core/chaincode/shim"
    24  	pb "github.com/hyperledger/fabric/protos/peer"
    25  )
    26  
    27  // This chaincode implements a simple map that is stored in the state.
    28  // The following operations are available.
    29  
    30  // Invoke operations
    31  // put - requires two arguments, a key and value
    32  // remove - requires a key
    33  // get - requires one argument, a key, and returns a value
    34  // keys - requires no arguments, returns all keys
    35  
    36  // SimpleChaincode example simple Chaincode implementation
    37  type SimpleChaincode struct {
    38  }
    39  
    40  // Init is a no-op
    41  func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    42  	return shim.Success(nil)
    43  }
    44  
    45  // Invoke has two functions
    46  // put - takes two arguements, a key and value, and stores them in the state
    47  // remove - takes one argument, a key, and removes if from the state
    48  func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    49  	function, args := stub.GetFunctionAndParameters()
    50  	switch function {
    51  	case "put":
    52  		if len(args) < 2 {
    53  			return shim.Error("put operation must include two arguments, a key and value")
    54  		}
    55  		key := args[0]
    56  		value := args[1]
    57  
    58  		err := stub.PutState(key, []byte(value))
    59  		if err != nil {
    60  			fmt.Printf("Error putting state %s", err)
    61  			return shim.Error(fmt.Sprintf("put operation failed. Error updating state: %s", err))
    62  		}
    63  		return shim.Success(nil)
    64  
    65  	case "remove":
    66  		if len(args) < 1 {
    67  			return shim.Error("remove operation must include one argument, a key")
    68  		}
    69  		key := args[0]
    70  
    71  		err := stub.DelState(key)
    72  		if err != nil {
    73  			return shim.Error(fmt.Sprintf("remove operation failed. Error updating state: %s", err))
    74  		}
    75  		return shim.Success(nil)
    76  
    77  	case "get":
    78  		if len(args) < 1 {
    79  			return shim.Error("get operation must include one argument, a key")
    80  		}
    81  		key := args[0]
    82  		value, err := stub.GetState(key)
    83  		if err != nil {
    84  			return shim.Error(fmt.Sprintf("get operation failed. Error accessing state: %s", err))
    85  		}
    86  		return shim.Success(value)
    87  
    88  	case "keys":
    89  		if len(args) < 2 {
    90  			return shim.Error("put operation must include two arguments, a key and value")
    91  		}
    92  		startKey := args[0]
    93  		endKey := args[1]
    94  
    95  		keysIter, err := stub.GetStateByRange(startKey, endKey)
    96  		if err != nil {
    97  			return shim.Error(fmt.Sprintf("keys operation failed. Error accessing state: %s", err))
    98  		}
    99  		defer keysIter.Close()
   100  
   101  		var keys []string
   102  		for keysIter.HasNext() {
   103  			key, _, iterErr := keysIter.Next()
   104  			if iterErr != nil {
   105  				return shim.Error(fmt.Sprintf("keys operation failed. Error accessing state: %s", err))
   106  			}
   107  			keys = append(keys, key)
   108  		}
   109  
   110  		jsonKeys, err := json.Marshal(keys)
   111  		if err != nil {
   112  			return shim.Error(fmt.Sprintf("keys operation failed. Error marshaling JSON: %s", err))
   113  		}
   114  
   115  		return shim.Success(jsonKeys)
   116  	case "query":
   117  		query := args[0]
   118  		keysIter, err := stub.GetQueryResult(query)
   119  		if err != nil {
   120  			return shim.Error(fmt.Sprintf("query operation failed. Error accessing state: %s", err))
   121  		}
   122  		defer keysIter.Close()
   123  
   124  		var keys []string
   125  		for keysIter.HasNext() {
   126  			key, _, iterErr := keysIter.Next()
   127  			if iterErr != nil {
   128  				return shim.Error(fmt.Sprintf("query operation failed. Error accessing state: %s", err))
   129  			}
   130  			keys = append(keys, key)
   131  		}
   132  
   133  		jsonKeys, err := json.Marshal(keys)
   134  		if err != nil {
   135  			return shim.Error(fmt.Sprintf("query operation failed. Error marshaling JSON: %s", err))
   136  		}
   137  
   138  		return shim.Success(jsonKeys)
   139  
   140  	default:
   141  		return shim.Success([]byte("Unsupported operation"))
   142  	}
   143  }
   144  
   145  func main() {
   146  	err := shim.Start(new(SimpleChaincode))
   147  	if err != nil {
   148  		fmt.Printf("Error starting chaincode: %s", err)
   149  	}
   150  }