github.com/koko1123/flow-go-1@v0.29.6/engine/consensus/approvals/caches.go (about)

     1  package approvals
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  )
     8  
     9  // ApprovalsCache encapsulates a map for storing result approvals indexed by approval partial ID
    10  // to provide concurrent access.
    11  type ApprovalsCache struct {
    12  	cache map[flow.Identifier]*flow.ResultApproval
    13  	lock  sync.RWMutex
    14  }
    15  
    16  func NewApprovalsCache(sizeHint uint) *ApprovalsCache {
    17  	return &ApprovalsCache{
    18  		cache: make(map[flow.Identifier]*flow.ResultApproval, sizeHint),
    19  		lock:  sync.RWMutex{},
    20  	}
    21  }
    22  
    23  // Put saves approval into cache; returns true iff approval was newly added
    24  // the approvalID should be calculated as `approval.Body.PartialID()`
    25  // it is taken as an input so that the caller can optimize by reusing the calculated approvalID.
    26  func (c *ApprovalsCache) Put(approvalID flow.Identifier, approval *flow.ResultApproval) bool {
    27  	c.lock.Lock()
    28  	defer c.lock.Unlock()
    29  	if _, found := c.cache[approvalID]; !found {
    30  		c.cache[approvalID] = approval
    31  		return true
    32  	}
    33  	return false
    34  }
    35  
    36  // Get returns ResultApproval for the given ID (or nil if none is stored)
    37  func (c *ApprovalsCache) Get(approvalID flow.Identifier) *flow.ResultApproval {
    38  	c.lock.RLock()
    39  	defer c.lock.RUnlock()
    40  	return c.cache[approvalID]
    41  }
    42  
    43  // All returns all stored approvals
    44  func (c *ApprovalsCache) All() []*flow.ResultApproval {
    45  	c.lock.RLock()
    46  	defer c.lock.RUnlock()
    47  	all := make([]*flow.ResultApproval, 0, len(c.cache))
    48  	for _, approval := range c.cache {
    49  		all = append(all, approval)
    50  	}
    51  	return all
    52  }
    53  
    54  // IncorporatedResultsCache encapsulates a map for storing IncorporatedResults
    55  // to provide concurrent access.
    56  type IncorporatedResultsCache struct {
    57  	cache map[flow.Identifier]*flow.IncorporatedResult
    58  	lock  sync.RWMutex
    59  }
    60  
    61  func NewIncorporatedResultsCache(sizeHint uint) *IncorporatedResultsCache {
    62  	return &IncorporatedResultsCache{
    63  		cache: make(map[flow.Identifier]*flow.IncorporatedResult, sizeHint),
    64  		lock:  sync.RWMutex{},
    65  	}
    66  }
    67  
    68  // Put saves IncorporatedResult into cache; returns true iff IncorporatedResult was newly added
    69  func (c *IncorporatedResultsCache) Put(key flow.Identifier, incRes *flow.IncorporatedResult) bool {
    70  	c.lock.Lock()
    71  	defer c.lock.Unlock()
    72  	if _, found := c.cache[key]; !found {
    73  		c.cache[key] = incRes
    74  		return true
    75  	}
    76  	return false
    77  }
    78  
    79  // Get returns IncorporatedResult for the given ID (or nil if none is stored)
    80  func (c *IncorporatedResultsCache) Get(key flow.Identifier) *flow.IncorporatedResult {
    81  	c.lock.RLock()
    82  	defer c.lock.RUnlock()
    83  	return c.cache[key]
    84  }
    85  
    86  // All returns all stored approvals
    87  func (c *IncorporatedResultsCache) All() []*flow.IncorporatedResult {
    88  	c.lock.RLock()
    89  	defer c.lock.RUnlock()
    90  	all := make([]*flow.IncorporatedResult, 0, len(c.cache))
    91  	for _, approval := range c.cache {
    92  		all = append(all, approval)
    93  	}
    94  	return all
    95  }