-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreader.go
More file actions
47 lines (38 loc) · 836 Bytes
/
reader.go
File metadata and controls
47 lines (38 loc) · 836 Bytes
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
package protobuf
import (
"bufio"
"context"
"encoding/binary"
"fmt"
"io"
"github.com/christianalexander/kvdb/stores"
"github.com/gogo/protobuf/proto"
)
type protoReader struct {
reader io.Reader
}
func NewReader(reader io.Reader) stores.Reader {
return protoReader{reader}
}
func (r protoReader) Read(ctx context.Context, records chan<- stores.Record) error {
br := bufio.NewReader(r.reader)
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
l, err := binary.ReadUvarint(br)
if err != nil {
return fmt.Errorf("failed to read from record file: %v", err)
}
buf := make([]byte, l)
br.Read(buf)
var record Record
err = proto.Unmarshal(buf, &record)
if err != nil {
return fmt.Errorf("failed to unmarshal record: %v", err)
}
records <- *record.ToRecord()
}
}
}