github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/order/keeper/memory_cache.go (about)

     1  package keeper
     2  
     3  import (
     4  	"github.com/fibonacci-chain/fbc/x/order/types"
     5  	"github.com/willf/bitset"
     6  )
     7  
     8  // Cache stores some caches that will not be written to disk
     9  type Cache struct {
    10  	// Reset at BeginBlock
    11  	updatedOrderIDs    []string
    12  	blockMatchResult   *types.BlockMatchResult
    13  	handlerTxMsgResult []bitset.BitSet
    14  
    15  	// for statistic
    16  	cancelNum      int64 // canceled orders num in this block
    17  	expireNum      int64 // expired orders num in this block
    18  	partialFillNum int64 // partially filled orders num in this block
    19  	fullFillNum    int64 // fully filled orders num in this block
    20  }
    21  
    22  // nolint
    23  func NewCache() *Cache {
    24  	return &Cache{
    25  		updatedOrderIDs:  []string{},
    26  		blockMatchResult: nil,
    27  	}
    28  }
    29  
    30  // reset resets temporary cache, called at BeginBlock
    31  func (c *Cache) reset() {
    32  	c.updatedOrderIDs = []string{}
    33  	c.blockMatchResult = &types.BlockMatchResult{}
    34  	c.handlerTxMsgResult = []bitset.BitSet{}
    35  
    36  	c.cancelNum = 0
    37  	c.expireNum = 0
    38  	c.fullFillNum = 0
    39  	c.partialFillNum = 0
    40  }
    41  
    42  func (c *Cache) addUpdatedOrderID(orderID string) {
    43  	c.updatedOrderIDs = append(c.updatedOrderIDs, orderID)
    44  }
    45  
    46  func (c *Cache) setBlockMatchResult(result *types.BlockMatchResult) {
    47  	c.blockMatchResult = result
    48  }
    49  
    50  func (c *Cache) addTxHandlerMsgResult(resultSet bitset.BitSet) {
    51  	c.handlerTxMsgResult = append(c.handlerTxMsgResult, resultSet)
    52  }
    53  
    54  // nolint
    55  func (c *Cache) IncreaseExpireNum() int64 {
    56  	c.expireNum++
    57  	return c.expireNum
    58  }
    59  
    60  // --------
    61  
    62  // nolint
    63  func (c *Cache) DecreaseCancelNum() int64 {
    64  	c.cancelNum--
    65  	return c.cancelNum
    66  }
    67  
    68  // nolint
    69  func (c *Cache) IncreaseCancelNum() int64 {
    70  	c.cancelNum++
    71  	return c.cancelNum
    72  }
    73  
    74  // nolint
    75  func (c *Cache) DecreaseFullFillNum() int64 {
    76  	c.fullFillNum--
    77  	return c.fullFillNum
    78  }
    79  
    80  // nolint
    81  func (c *Cache) IncreaseFullFillNum() int64 {
    82  	c.fullFillNum++
    83  	return c.fullFillNum
    84  }
    85  
    86  // nolint
    87  func (c *Cache) DecreasePartialFillNum() int64 {
    88  	c.partialFillNum--
    89  	return c.partialFillNum
    90  }
    91  
    92  // nolint
    93  func (c *Cache) IncreasePartialFillNum() int64 {
    94  	c.partialFillNum++
    95  	return c.partialFillNum
    96  }
    97  
    98  func (c *Cache) getBlockMatchResult() *types.BlockMatchResult {
    99  	return c.blockMatchResult
   100  }
   101  
   102  func (c *Cache) getUpdatedOrderIDs() []string {
   103  	return c.updatedOrderIDs
   104  }
   105  
   106  // toggleCopyTxHandlerMsgResult: copy and reset the handlerTxMsgResult
   107  func (c *Cache) toggleCopyTxHandlerMsgResult() []bitset.BitSet {
   108  	txResultCopy := make([]bitset.BitSet, 0, len(c.handlerTxMsgResult))
   109  	txResultCopy = append(txResultCopy, c.handlerTxMsgResult...)
   110  	c.handlerTxMsgResult = []bitset.BitSet{}
   111  
   112  	return txResultCopy
   113  }
   114  
   115  // nolint
   116  func (c *Cache) GetFullFillNum() int64 {
   117  	return c.fullFillNum
   118  }
   119  
   120  // nolint
   121  func (c *Cache) GetCancelNum() int64 {
   122  	return c.cancelNum
   123  }
   124  
   125  // nolint
   126  func (c *Cache) GetExpireNum() int64 {
   127  	return c.expireNum
   128  }
   129  
   130  // nolint
   131  func (c *Cache) GetPartialFillNum() int64 {
   132  	return c.partialFillNum
   133  }