-
Notifications
You must be signed in to change notification settings - Fork 179
/
queues.go
64 lines (52 loc) · 1.28 KB
/
queues.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
63
64
package stdmap
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool/queue"
)
type Queues struct {
*Backend
}
// QueuesBackdata is mempool map for ingestion.Queues (head Node ID -> Queues)
type QueuesBackdata struct {
*Backdata
}
func NewQueues() *Queues {
return &Queues{NewBackend(WithEject(EjectPanic))}
}
func (b *QueuesBackdata) ByID(queueID flow.Identifier) (*queue.Queue, bool) {
entity, exists := b.Backdata.ByID(queueID)
if !exists {
return nil, false
}
queue := entity.(*queue.Queue)
return queue, true
}
func (b *QueuesBackdata) All() []*queue.Queue {
entities := b.Backdata.All()
queues := make([]*queue.Queue, len(entities))
for i, entity := range entities {
queue, ok := entity.(*queue.Queue)
if !ok {
panic(fmt.Sprintf("invalid entity in queue mempool (%T)", entity))
}
queues[i] = queue
}
return queues
}
func (b *Queues) Add(queue *queue.Queue) bool {
return b.Backend.Add(queue)
}
func (b *Queues) Get(queueID flow.Identifier) (*queue.Queue, bool) {
backdata := &QueuesBackdata{&b.Backdata}
return backdata.ByID(queueID)
}
func (b *Queues) Run(f func(backdata *QueuesBackdata) error) error {
b.Lock()
defer b.Unlock()
err := f(&QueuesBackdata{&b.Backdata})
if err != nil {
return err
}
return nil
}