-
Notifications
You must be signed in to change notification settings - Fork 197
/
pruningBuffer.go
59 lines (45 loc) · 1.28 KB
/
pruningBuffer.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
package pruningBuffer
import (
"sync"
logger "github.com/ElrondNetwork/elrond-go-logger"
)
var log = logger.GetOrCreate("state/pruningBuffer")
type pruningBuffer struct {
mutOp sync.RWMutex
buffer [][]byte
size uint32
}
// NewPruningBuffer creates a new instance of pruning buffer
func NewPruningBuffer(pruningBufferLen uint32) *pruningBuffer {
return &pruningBuffer{
buffer: make([][]byte, 0),
size: pruningBufferLen,
}
}
// Add appends a new byteArray to the buffer if there is any space left
func (pb *pruningBuffer) Add(rootHash []byte) {
pb.mutOp.Lock()
defer pb.mutOp.Unlock()
if uint32(len(pb.buffer)) == pb.size {
log.Warn("pruning buffer is full", "rootHash", rootHash)
return
}
pb.buffer = append(pb.buffer, rootHash)
log.Trace("pruning buffer add", "rootHash", rootHash)
}
// RemoveAll empties the buffer and returns all the contained byteArrays
func (pb *pruningBuffer) RemoveAll() [][]byte {
pb.mutOp.Lock()
defer pb.mutOp.Unlock()
log.Trace("pruning buffer", "len", len(pb.buffer))
buffer := make([][]byte, len(pb.buffer))
copy(buffer, pb.buffer)
pb.buffer = pb.buffer[:0]
return buffer
}
// Len returns the number of elements from the buffer
func (pb *pruningBuffer) Len() int {
pb.mutOp.RLock()
defer pb.mutOp.RUnlock()
return len(pb.buffer)
}