github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/stdmap/collections.go (about)

     1  package stdmap
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  )
     6  
     7  // Collections implements a mempool storing collections.
     8  type Collections struct {
     9  	*Backend
    10  }
    11  
    12  // NewCollections creates a new memory pool for collection.
    13  func NewCollections(limit uint) (*Collections, error) {
    14  	c := &Collections{
    15  		Backend: NewBackend(WithLimit(limit)),
    16  	}
    17  	return c, nil
    18  }
    19  
    20  // Add adds a collection to the mempool.
    21  func (c *Collections) Add(coll *flow.Collection) bool {
    22  	added := c.Backend.Add(coll)
    23  	return added
    24  }
    25  
    26  // Remove removes a collection by ID from memory
    27  func (c *Collections) Remove(collID flow.Identifier) bool {
    28  	ok := c.Backend.Remove(collID)
    29  	return ok
    30  }
    31  
    32  // ByID returns the collection with the given ID from the mempool.
    33  func (c *Collections) ByID(collID flow.Identifier) (*flow.Collection, bool) {
    34  	entity, exists := c.Backend.ByID(collID)
    35  	if !exists {
    36  		return nil, false
    37  	}
    38  	coll := entity.(*flow.Collection)
    39  	return coll, true
    40  }
    41  
    42  // All returns all collections from the mempool.
    43  func (c *Collections) All() []*flow.Collection {
    44  	entities := c.Backend.All()
    45  	colls := make([]*flow.Collection, 0, len(entities))
    46  	for _, entity := range entities {
    47  		colls = append(colls, entity.(*flow.Collection))
    48  	}
    49  	return colls
    50  }