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