-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
reader.go
42 lines (39 loc) · 923 Bytes
/
reader.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
package wal
import (
"errors"
"io"
"github.com/prometheus/prometheus/tsdb/wlog"
)
// If startSegment is <0, it means all the segments.
func NewWalReader(dir string, startSegment int) (*wlog.Reader, io.Closer, error) {
var (
segmentReader io.ReadCloser
err error
)
if startSegment < 0 {
segmentReader, err = wlog.NewSegmentsReader(dir)
if err != nil {
return nil, nil, err
}
} else {
first, last, err := wlog.Segments(dir)
if err != nil {
return nil, nil, err
}
if startSegment > last {
return nil, nil, errors.New("start segment is beyond the last WAL segment")
}
if first > startSegment {
startSegment = first
}
segmentReader, err = wlog.NewSegmentsRangeReader(wlog.SegmentRange{
Dir: dir,
First: startSegment,
Last: -1, // Till the end.
})
if err != nil {
return nil, nil, err
}
}
return wlog.NewReader(segmentReader), segmentReader, nil
}