This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
reader.go
403 lines (345 loc) · 8.49 KB
/
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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package proio
import (
"bytes"
"compress/gzip"
"encoding/binary"
"errors"
"io"
"os"
"sync"
"github.com/decibelcooper/proio/go-proio/proto"
"github.com/pierrec/lz4"
)
// Reader serves to read Events from a stream in the proio format.
type Reader struct {
streamReader io.Reader
bucket *bytes.Reader
bucketReader io.Reader
bucketHeader *proto.BucketHeader
bucketEventsRead int
decompressor io.Reader
Err chan error
EvtScanBufSize int
deferredUntilStopScan []func()
deferredUntilClose []func() error
sync.Mutex
}
// Open opens the given existing file (in read-only mode), returning an error
// where appropriate. Upon success, a new Reader is created to wrap the file,
// and returned. Either Open or NewReader should be called to construct a new
// Reader.
func Open(filename string) (*Reader, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return NewReader(file), nil
}
// NewReader wraps an existing io.Reader for reading proio Events. Either Open
// or NewReader should be called to construct a new Reader.
func NewReader(streamReader io.Reader) *Reader {
rdr := &Reader{
streamReader: streamReader,
bucket: &bytes.Reader{},
bucketReader: &bytes.Buffer{},
Err: make(chan error, evtScanBufferSize),
EvtScanBufSize: evtScanBufferSize,
}
return rdr
}
// Close closes any file that was opened by the library, and stops any
// unfinished scans. Close does not close io.Readers passed directly to
// NewReader.
func (rdr *Reader) Close() {
rdr.Lock()
defer rdr.Unlock()
rdr.StopScan()
closer, ok := rdr.streamReader.(io.Closer)
if ok {
closer.Close()
}
}
// Next retrieves the next event from the stream.
func (rdr *Reader) Next() (*Event, error) {
rdr.Lock()
defer rdr.Unlock()
return rdr.readFromBucket(true)
}
// NextHeader returns the next bucket header from the stream, and discards the
// bucket payload.
func (rdr *Reader) NextHeader() (*proto.BucketHeader, error) {
rdr.Lock()
defer rdr.Unlock()
if _, err := rdr.readBucket(1 << 62); err != nil {
return nil, err
}
return rdr.bucketHeader, nil
}
// Skip skips nEvents events. If the return error is nil, nEvents have been
// skipped.
func (rdr *Reader) Skip(nEvents int) (nSkipped int, err error) {
rdr.Lock()
defer rdr.Unlock()
bucketEventsLeft := 0
if rdr.bucketHeader != nil {
bucketEventsLeft = int(rdr.bucketHeader.NEvents) - rdr.bucketEventsRead
}
if nEvents > bucketEventsLeft {
nSkipped += bucketEventsLeft
var n int
for n != 0 && err == nil {
n, err = rdr.readBucket(nEvents - nSkipped)
if err != nil {
return
}
nSkipped += n
}
}
for nSkipped < nEvents {
_, err = rdr.readFromBucket(false)
if err != nil {
return
}
nSkipped++
}
return
}
// SeekToStart seeks seekable streams to the beginning, and prepares the stream
// to read from there.
func (rdr *Reader) SeekToStart() error {
rdr.Lock()
defer rdr.Unlock()
seeker, ok := rdr.streamReader.(io.Seeker)
if !ok {
return errors.New("stream not seekable")
}
for {
n, err := seeker.Seek(0, 0 /*io.SeekStart*/)
if err != nil {
return err
}
if n == 0 {
break
}
}
rdr.readBucket(0)
return nil
}
//ScanEvents returns a buffered channel of type Event where all of the events
//in the stream will be pushed. The channel buffer size is defined by
//Reader.EvtScanBufSize which defaults to 100. The goroutine responsible
//for fetching events will not break until there are no more events,
//Reader.StopScan() is called, or Reader.Close() is called. In this scenario,
//errors are pushed to the Reader.Err channel.
func (rdr *Reader) ScanEvents() <-chan *Event {
rdr.Lock()
defer rdr.Unlock()
events := make(chan *Event, rdr.EvtScanBufSize)
quit := make(chan int)
go func() {
defer close(events)
for {
event, err := rdr.Next()
if err != nil {
select {
case rdr.Err <- err:
default:
}
}
if event == nil {
return
}
select {
case events <- event:
case <-quit:
return
}
}
}()
rdr.deferUntilStopScan(
func() {
close(quit)
},
)
return events
}
// StopScan stops all scans initiated by Reader.ScanEvents()
func (rdr *Reader) StopScan() {
//rdr.Lock()
//defer rdr.Unlock()
for _, thisFunc := range rdr.deferredUntilStopScan {
thisFunc()
}
rdr.deferredUntilStopScan = make([]func(), 0)
}
var evtScanBufferSize int = 100
func (rdr *Reader) deferUntilStopScan(thisFunc func()) {
rdr.deferredUntilStopScan = append(rdr.deferredUntilStopScan, thisFunc)
}
func (rdr *Reader) readFromBucket(doUnmarshal bool) (*Event, error) {
protoSizeBuf := make([]byte, 4)
if err := readBytes(rdr.bucketReader, protoSizeBuf); err != nil {
if err == io.EOF {
_, err = rdr.readBucket(0)
if err != nil {
return nil, err
}
} else {
return nil, err
}
if err := readBytes(rdr.bucketReader, protoSizeBuf); err != nil {
return nil, err
}
}
protoSize := binary.LittleEndian.Uint32(protoSizeBuf)
protoBuf := make([]byte, protoSize)
if err := readBytes(rdr.bucketReader, protoBuf); err != nil {
return nil, err
}
rdr.bucketEventsRead++
var event *Event
if doUnmarshal {
eventProto := &proto.Event{}
if err := eventProto.Unmarshal(protoBuf); err != nil {
return nil, err
}
event = newEventFromProto(eventProto)
}
return event, nil
}
func (rdr *Reader) readBucket(maxSkipEvents int) (eventsSkipped int, err error) {
rdr.bucketEventsRead = 0
_, err = rdr.syncToMagic()
if err != nil {
return
}
headerSizeBuf := make([]byte, 4)
if err = readBytes(rdr.streamReader, headerSizeBuf); err != nil {
return
}
headerSize := binary.LittleEndian.Uint32(headerSizeBuf)
headerBuf := make([]byte, headerSize)
if err = readBytes(rdr.streamReader, headerBuf); err != nil {
return
}
rdr.bucketHeader = &proto.BucketHeader{}
if err = rdr.bucketHeader.Unmarshal(headerBuf); err != nil {
return
}
if int(rdr.bucketHeader.NEvents) > maxSkipEvents {
bucketBytes := make([]byte, rdr.bucketHeader.BucketSize)
if err = readBytes(rdr.streamReader, bucketBytes); err != nil {
return
}
rdr.bucket.Reset(bucketBytes)
} else {
rdr.bucketReader = &bytes.Buffer{}
eventsSkipped = int(rdr.bucketHeader.NEvents)
seeker, ok := rdr.streamReader.(io.Seeker)
if ok {
seekBytes(seeker, int64(rdr.bucketHeader.BucketSize))
} else {
bucketBytes := make([]byte, rdr.bucketHeader.BucketSize)
err = readBytes(rdr.streamReader, bucketBytes)
}
return
}
switch rdr.bucketHeader.Compression {
case proto.BucketHeader_GZIP:
gzipRdr, ok := rdr.decompressor.(*gzip.Reader)
if ok {
gzipRdr.Reset(rdr.bucket)
} else {
gzipRdr, err = gzip.NewReader(rdr.bucket)
if err != nil {
return
}
rdr.decompressor = gzipRdr
}
bucketRdr, ok := rdr.bucketReader.(*bytes.Buffer)
if ok {
bucketRdr.Reset()
} else {
bucketRdr = &bytes.Buffer{}
}
bucketRdr.ReadFrom(gzipRdr)
rdr.bucketReader = bucketRdr
case proto.BucketHeader_LZ4:
lz4Rdr, ok := rdr.decompressor.(*lz4.Reader)
if ok {
lz4Rdr.Reset(rdr.bucket)
} else {
lz4Rdr = lz4.NewReader(rdr.bucket)
rdr.decompressor = lz4Rdr
}
bucketRdr, ok := rdr.bucketReader.(*bytes.Buffer)
if ok {
bucketRdr.Reset()
} else {
bucketRdr = &bytes.Buffer{}
}
bucketRdr.ReadFrom(lz4Rdr)
rdr.bucketReader = bucketRdr
default:
rdr.bucketReader = rdr.bucket
}
return
}
func (rdr *Reader) syncToMagic() (int, error) {
magicByteBuf := make([]byte, 1)
nRead := 0
for {
err := readBytes(rdr.streamReader, magicByteBuf)
if err != nil {
return nRead, err
}
nRead++
if magicByteBuf[0] == magicBytes[0] {
var goodSeq = true
for i := 1; i < len(magicBytes); i++ {
err := readBytes(rdr.streamReader, magicByteBuf)
if err != nil {
return nRead, err
}
nRead++
if magicByteBuf[0] != magicBytes[i] {
goodSeq = false
break
}
}
if goodSeq {
break
}
}
}
return nRead, nil
}
func readBytes(rdr io.Reader, buf []byte) error {
tot := 0
for tot < len(buf) {
n, err := rdr.Read(buf[tot:])
tot += n
if err != nil && tot != len(buf) {
return err
}
}
return nil
}
func seekBytes(seeker io.Seeker, nBytes int64) error {
start, err := seeker.Seek(0, 1 /*io.SeekCurrent*/)
if err != nil {
return err
}
tot := int64(0)
for tot < nBytes {
n, err := seeker.Seek(int64(nBytes-tot), 1 /*io.SeekCurrent*/)
tot += n - start
if err != nil && tot != nBytes {
return err
}
}
return nil
}
func (rdr *Reader) deferUntilClose(thisFunc func() error) {
rdr.deferredUntilClose = append(rdr.deferredUntilClose, thisFunc)
}