github.com/koko1123/flow-go-1@v0.29.6/module/mempool/stdmap/collections.go (about)

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