-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathslot_sequencer.go
86 lines (72 loc) · 1.87 KB
/
slot_sequencer.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package sonic
// SlotSequencer does two things:
// 1. Provides ordering to ByteBuffer.Slots
// 2. Offsets ByteBuffer.Slots such that they are discarded correctly. See
// slot_offsetter.go for a description of why this is necessary and how it's
// done.
//
// Workflow:
// - slot := ByteBuffer.Save(...)
// - slot = sequencer.Push(x, slot) where x is some sequence number of this Slot.
// - ...
// - ByteBuffer.Discard(sequencer.Pop(slot))
type SlotSequencer struct {
maxBytes int
bytes int
container *sequencedSlots
offsetter *SlotOffsetter
}
func NewSlotSequencer(maxSlots, maxBytes int) *SlotSequencer {
s := &SlotSequencer{
maxBytes: maxBytes,
}
s.container = newSequencedSlots(maxSlots)
s.offsetter = NewSlotOffsetter(maxBytes)
return s
}
// Push a Slot that's uniquely identified and ordered by `seq`.
func (s *SlotSequencer) Push(seq int, slot Slot) (ok bool, err error) {
if s.bytes+slot.Length > s.maxBytes {
return false, ErrNoSpaceLeftForSlot
}
slot, err = s.offsetter.Add(slot)
if err == nil {
ok, err = s.container.Push(seq, slot)
if ok && err == nil {
s.bytes += slot.Length
}
}
return ok, err
}
// Pop the slot identified by `seq`. The popped Slot must be discarded through
// ByteBuffer.Discard before Pop is called again.
func (s *SlotSequencer) Pop(seq int) (Slot, bool) {
slot, ok := s.container.Pop(seq)
if ok {
slot = s.offsetter.Offset(slot)
if s.container.Size() == 0 {
s.offsetter.Reset()
}
s.bytes -= slot.Length
}
return slot, ok
}
func (s *SlotSequencer) Size() int {
return s.container.Size()
}
func (s *SlotSequencer) Bytes() int {
return s.bytes
}
func (s *SlotSequencer) MaxBytes() int {
return s.maxBytes
}
func (s *SlotSequencer) FillPct() float64 {
a := float64(s.Bytes())
b := float64(s.MaxBytes())
return a / b * 100.0
}
func (s *SlotSequencer) Reset() {
s.offsetter.Reset()
s.container.Reset()
s.bytes = 0
}