-
Notifications
You must be signed in to change notification settings - Fork 1
/
sink_mem.go
96 lines (80 loc) · 1.79 KB
/
sink_mem.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
87
88
89
90
91
92
93
94
95
96
package wal
import (
"io"
"sync"
)
// MemorySink is a Sink implementation that only stores data in memory.
type MemorySink struct {
mu sync.RWMutex
segments []*Segment
}
// NewMemorySink returns a Sink implementation that stores segments in memory.
func NewMemorySink() (*MemorySink, error) {
return &MemorySink{
segments: make([]*Segment, 0),
}, nil
}
func (s *MemorySink) Analyze() error {
return nil
}
func (s *MemorySink) LoadSegment(offset Offset) (*Segment, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if offset.Equal(ZeroOffset) {
return s.segments[0], nil
}
for i, seg := range s.segments {
a, b := seg.Limits()
if offset.Within(a, b) || offset.Before(a) {
return s.segments[i], nil
}
}
return nil, io.EOF
}
func (s *MemorySink) WriteSegment(seg *Segment) error {
first, last := seg.Limits()
if first.Equal(ZeroOffset) && last.Equal(ZeroOffset) {
return nil
}
s.mu.Lock()
s.segments = append(s.segments, seg)
s.mu.Unlock()
return nil
}
func (s *MemorySink) Offsets() (first, last Offset) {
s.mu.RLock()
defer s.mu.RUnlock()
first, _ = s.segments[0].Limits()
_, last = s.segments[len(s.segments)-1].Limits()
return first, last
}
func (s *MemorySink) NumSegments() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.segments)
}
func (s *MemorySink) Truncate(offset Offset) error {
s.mu.Lock()
defer s.mu.Unlock()
// See if there are any whole segments we can remove.
removed := 0
for _, seg := range s.segments {
_, end := seg.Limits()
if end.Before(offset) {
removed++
} else {
break
}
}
if removed > 0 {
s.segments = s.segments[removed:]
}
// See if we need to truncate the first segment.
if offset.Within(s.segments[0].Limits()) {
s.segments[0].Truncate(offset)
}
return nil
}
func (s *MemorySink) Close() error {
return nil
}