-
Notifications
You must be signed in to change notification settings - Fork 179
/
chunk_data_packs.go
67 lines (57 loc) · 1.81 KB
/
chunk_data_packs.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
65
66
67
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"github.com/onflow/flow-go/model/flow"
)
// ChunkDataPacks implements the ChunkDataPack memory pool.
type ChunkDataPacks struct {
*Backend
}
// NewChunkDataPacks creates a new memory pool for ChunkDataPacks.
func NewChunkDataPacks(limit uint) (*ChunkDataPacks, error) {
a := &ChunkDataPacks{
Backend: NewBackend(WithLimit(limit)),
}
return a, nil
}
// Has checks whether the ChunkDataPack with the given chunkID is currently in
// the memory pool.
func (c *ChunkDataPacks) Has(chunkID flow.Identifier) bool {
return c.Backend.Has(chunkID)
}
// Add adds an chunkDataPack to the mempool.
func (c *ChunkDataPacks) Add(cdp *flow.ChunkDataPack) bool {
added := c.Backend.Add(cdp)
return added
}
// Rem will remove chunk data pack by ID
func (c *ChunkDataPacks) Rem(chunkID flow.Identifier) bool {
removed := c.Backend.Rem(chunkID)
return removed
}
// ByChunkID returns the chunk data pack with the given chunkID from the mempool.
func (c *ChunkDataPacks) ByChunkID(chunkID flow.Identifier) (*flow.ChunkDataPack, bool) {
entity, exists := c.Backend.ByID(chunkID)
if !exists {
return nil, false
}
chunkDataPack := entity.(*flow.ChunkDataPack)
return chunkDataPack, true
}
// Size will return the current size of the memory pool.
func (c *ChunkDataPacks) Size() uint {
return c.Backend.Size()
}
// All returns all chunk data packs from the pool.
func (c *ChunkDataPacks) All() []*flow.ChunkDataPack {
entities := c.Backend.All()
chunkDataPack := make([]*flow.ChunkDataPack, 0, len(entities))
for _, entity := range entities {
chunkDataPack = append(chunkDataPack, entity.(*flow.ChunkDataPack))
}
return chunkDataPack
}
// Hash will return a hash of the contents of the memory pool.
func (c *ChunkDataPacks) Hash() flow.Identifier {
return c.Backend.Hash()
}