-
Notifications
You must be signed in to change notification settings - Fork 175
/
collections.go
52 lines (44 loc) · 1.24 KB
/
collections.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
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"github.com/onflow/flow-go/model/flow"
)
// Collections implements a mempool storing collections.
type Collections struct {
*Backend
}
// NewCollections creates a new memory pool for collection.
func NewCollections(limit uint) (*Collections, error) {
c := &Collections{
Backend: NewBackend(WithLimit(limit)),
}
return c, nil
}
// Add adds a collection to the mempool.
func (c *Collections) Add(coll *flow.Collection) bool {
added := c.Backend.Add(coll)
return added
}
// Remove removes a collection by ID from memory
func (c *Collections) Remove(collID flow.Identifier) bool {
ok := c.Backend.Remove(collID)
return ok
}
// ByID returns the collection with the given ID from the mempool.
func (c *Collections) ByID(collID flow.Identifier) (*flow.Collection, bool) {
entity, exists := c.Backend.ByID(collID)
if !exists {
return nil, false
}
coll := entity.(*flow.Collection)
return coll, true
}
// All returns all collections from the mempool.
func (c *Collections) All() []*flow.Collection {
entities := c.Backend.All()
colls := make([]*flow.Collection, 0, len(entities))
for _, entity := range entities {
colls = append(colls, entity.(*flow.Collection))
}
return colls
}