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

     1  package stdmap
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  	"github.com/onflow/flow-go/module/mempool/model"
     6  )
     7  
     8  // Identifiers represents a concurrency-safe memory pool for IDs.
     9  type Identifiers struct {
    10  	*Backend
    11  }
    12  
    13  // NewIdentifiers creates a new memory pool for identifiers.
    14  func NewIdentifiers(limit uint) (*Identifiers, error) {
    15  	i := &Identifiers{
    16  		Backend: NewBackend(WithLimit(limit)),
    17  	}
    18  	return i, nil
    19  }
    20  
    21  // Add will add the given identifier to the memory pool or it will error if
    22  // the identifier is already in the memory pool.
    23  func (i *Identifiers) Add(id flow.Identifier) bool {
    24  	// wraps ID around an ID entity to be stored in the mempool
    25  	idEntity := &model.IdEntity{
    26  		Id: id,
    27  	}
    28  	return i.Backend.Add(idEntity)
    29  }
    30  
    31  // Has checks whether the mempool has the identifier
    32  func (i *Identifiers) Has(id flow.Identifier) bool {
    33  	return i.Backend.Has(id)
    34  }
    35  
    36  // Remove removes the given identifier from the memory pool; it will
    37  // return true if the identifier was known and removed.
    38  func (i *Identifiers) Remove(id flow.Identifier) bool {
    39  	return i.Backend.Remove(id)
    40  }
    41  
    42  // All returns all identifiers stored in the mempool
    43  func (i *Identifiers) All() flow.IdentifierList {
    44  	entities := i.Backend.All()
    45  	idEntities := make([]flow.Identifier, 0, len(entities))
    46  	for _, entity := range entities {
    47  		idEntity := entity.(*model.IdEntity)
    48  		idEntities = append(idEntities, idEntity.Id)
    49  	}
    50  	return idEntities
    51  }