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

     1  package stdmap
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  )
     6  
     7  // ChunkDataPacks implements the ChunkDataPack memory pool.
     8  type ChunkDataPacks struct {
     9  	*Backend
    10  }
    11  
    12  // NewChunkDataPacks creates a new memory pool for ChunkDataPacks.
    13  func NewChunkDataPacks(limit uint) (*ChunkDataPacks, error) {
    14  	a := &ChunkDataPacks{
    15  		Backend: NewBackend(WithLimit(limit)),
    16  	}
    17  	return a, nil
    18  }
    19  
    20  // Has checks whether the ChunkDataPack with the given chunkID is currently in
    21  // the memory pool.
    22  func (c *ChunkDataPacks) Has(chunkID flow.Identifier) bool {
    23  	return c.Backend.Has(chunkID)
    24  }
    25  
    26  // Add adds an chunkDataPack to the mempool.
    27  func (c *ChunkDataPacks) Add(cdp *flow.ChunkDataPack) bool {
    28  	added := c.Backend.Add(cdp)
    29  	return added
    30  }
    31  
    32  // Remove will remove chunk data pack by ID
    33  func (c *ChunkDataPacks) Remove(chunkID flow.Identifier) bool {
    34  	removed := c.Backend.Remove(chunkID)
    35  	return removed
    36  }
    37  
    38  // ByChunkID returns the chunk data pack with the given chunkID from the mempool.
    39  func (c *ChunkDataPacks) ByChunkID(chunkID flow.Identifier) (*flow.ChunkDataPack, bool) {
    40  	entity, exists := c.Backend.ByID(chunkID)
    41  	if !exists {
    42  		return nil, false
    43  	}
    44  	chunkDataPack := entity.(*flow.ChunkDataPack)
    45  	return chunkDataPack, true
    46  }
    47  
    48  // Size will return the current size of the memory pool.
    49  func (c *ChunkDataPacks) Size() uint {
    50  	return c.Backend.Size()
    51  }
    52  
    53  // All returns all chunk data packs from the pool.
    54  func (c *ChunkDataPacks) All() []*flow.ChunkDataPack {
    55  	entities := c.Backend.All()
    56  	chunkDataPack := make([]*flow.ChunkDataPack, 0, len(entities))
    57  	for _, entity := range entities {
    58  		chunkDataPack = append(chunkDataPack, entity.(*flow.ChunkDataPack))
    59  	}
    60  	return chunkDataPack
    61  }