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

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