github.com/s7techlab/cckit@v0.10.5/state/testdata/cc_state_cached.go (about)

     1  package testdata
     2  
     3  import (
     4  	"github.com/s7techlab/cckit/router"
     5  	"github.com/s7techlab/cckit/state"
     6  )
     7  
     8  const (
     9  	TxStateCachedReadAfterWrite  = `ReadAfterWrite`
    10  	TxStateCachedReadAfterDelete = `ReadAfterDelete`
    11  	TxStateCachedListAfterWrite  = `ListAfterWrite`
    12  	TxStateCachedListAfterDelete = `ListAfterDelete`
    13  
    14  	BasePrefix = `prefix`
    15  )
    16  
    17  var (
    18  	Keys = []string{`a`, `b`, `c`}
    19  )
    20  
    21  type Value struct {
    22  	Value string
    23  }
    24  
    25  func Key(key string) []string {
    26  	return []string{BasePrefix, key}
    27  }
    28  
    29  func KeyValue(key string) Value {
    30  	return Value{Value: key + `_value`}
    31  }
    32  func NewStateCachedCC() *router.Chaincode {
    33  	r := router.New(`state_cached`)
    34  
    35  	r.Query(TxStateCachedReadAfterWrite, ReadAfterWrite).
    36  		Query(TxStateCachedReadAfterDelete, ReadAfterDelete).
    37  		Query(TxStateCachedListAfterWrite, ListAfterWrite).
    38  		Query(TxStateCachedListAfterDelete, ListAfterDelete)
    39  
    40  	return router.NewChaincode(r)
    41  }
    42  
    43  func ReadAfterWrite(ctx router.Context) (interface{}, error) {
    44  	stateWithCache := state.WithCache(ctx.State())
    45  	for _, k := range Keys {
    46  		if err := stateWithCache.Put(Key(k), KeyValue(k)); err != nil {
    47  			return nil, err
    48  		}
    49  	}
    50  
    51  	// return non empty, cause state changes cached
    52  	return stateWithCache.Get(Key(Keys[0]), &Value{})
    53  }
    54  
    55  func ReadAfterDelete(ctx router.Context) (interface{}, error) {
    56  	ctxWithStateCache := router.ContextWithStateCache(ctx)
    57  	// delete all keys
    58  	for _, k := range Keys {
    59  		if err := ctxWithStateCache.State().Delete(Key(k)); err != nil {
    60  			return nil, err
    61  		}
    62  	}
    63  
    64  	// return empty, cause state changes cached
    65  	val, _ := ctxWithStateCache.State().Get(Key(Keys[0]))
    66  	// if we return error - state changes will not applied
    67  	return val, nil
    68  }
    69  
    70  func ListAfterWrite(ctx router.Context) (interface{}, error) {
    71  	stateWithCache := state.WithCache(ctx.State())
    72  	for _, k := range Keys {
    73  		if err := stateWithCache.Put(Key(k), KeyValue(k)); err != nil {
    74  			return nil, err
    75  		}
    76  	}
    77  
    78  	// return list, cause state changes cached
    79  	return stateWithCache.List(BasePrefix, &Value{})
    80  }
    81  
    82  func ListAfterDelete(ctx router.Context) (interface{}, error) {
    83  	ctxWithStateCache := router.ContextWithStateCache(ctx)
    84  	// delete only one key, two keys remained
    85  	if err := ctxWithStateCache.State().Delete(Key(Keys[0])); err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	// return list with 2 items, cause first item is deleted and state is cached
    90  	return ctxWithStateCache.State().List(BasePrefix, &Value{})
    91  }