-
Notifications
You must be signed in to change notification settings - Fork 175
/
assignments.go
62 lines (52 loc) · 1.83 KB
/
assignments.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
52
53
54
55
56
57
58
59
60
61
62
package stdmap
import (
chunkmodels "github.com/onflow/flow-go/model/chunks"
"github.com/onflow/flow-go/model/flow"
)
// Assignments implements the chunk assignment memory pool.
type Assignments struct {
*Backend
}
// NewAssignments creates a new memory pool for Assignments.
func NewAssignments(limit uint) (*Assignments, error) {
a := &Assignments{
Backend: NewBackend(WithLimit(limit)),
}
return a, nil
}
// Has checks whether the Assignment with the given hash is currently in
// the memory pool.
func (a *Assignments) Has(assignmentID flow.Identifier) bool {
return a.Backend.Has(assignmentID)
}
// ByID retrieves the chunk assignment from mempool based on provided ID
func (a *Assignments) ByID(assignmentID flow.Identifier) (*chunkmodels.Assignment, bool) {
entity, exists := a.Backend.ByID(assignmentID)
if !exists {
return nil, false
}
adp := entity.(*chunkmodels.AssignmentDataPack)
return adp.Assignment(), true
}
// Add adds an Assignment to the mempool.
func (a *Assignments) Add(fingerprint flow.Identifier, assignment *chunkmodels.Assignment) bool {
return a.Backend.Add(chunkmodels.NewAssignmentDataPack(fingerprint, assignment))
}
// Rem will remove the given Assignment from the memory pool; it will
// return true if the Assignment was known and removed.
func (a *Assignments) Rem(assignmentID flow.Identifier) bool {
return a.Backend.Rem(assignmentID)
}
// Size will return the current size of the memory pool.
func (a *Assignments) Size() uint {
return a.Backend.Size()
}
// All returns all chunk data packs from the pool.
func (a *Assignments) All() []*chunkmodels.Assignment {
entities := a.Backend.All()
assignments := make([]*chunkmodels.Assignment, 0, len(entities))
for _, entity := range entities {
assignments = append(assignments, entity.(*chunkmodels.AssignmentDataPack).Assignment())
}
return assignments
}