-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasereader.go
140 lines (113 loc) · 2.72 KB
/
basereader.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
package pkg
import (
"bufio"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"github.com/splunk/stef/go/pkg/schema"
)
type BaseReader struct {
Source *bufio.Reader
FixedHeader FixedHeader
VarHeader VarHeader
Schema *schema.Schema
ReadBufs ReadBufs
FrameDecoder FrameDecoder
FrameRecordCount uint64
RecordCount uint64
}
func (r *BaseReader) Init(source *bufio.Reader) error {
r.Source = source
if err := r.ReadFixedHeader(); err != nil {
return err
}
if err := r.FrameDecoder.Init(r.Source, r.FixedHeader.Compression); err != nil {
return err
}
return nil
}
func (r *BaseReader) ReadFixedHeader() error {
hdrSignature := make([]byte, len(HdrSignature))
_, err := r.Source.Read(hdrSignature)
if err != nil {
return err
}
if string(hdrSignature) != HdrSignature {
return ErrInvalidHeaderSignature
}
contentSize, err := binary.ReadUvarint(r.Source)
if err != nil {
return err
}
if contentSize < 2 || contentSize > HdrContentSizeLimit {
return ErrInvalidHeader
}
hdrContent := make([]byte, contentSize)
_, err = r.Source.Read(hdrContent)
if err != nil {
return err
}
versionAndType := hdrContent[0]
version := versionAndType & HdrFormatVersionMask
if version != HdrFormatVersion {
return ErrInvalidFormatVersion
}
flags := hdrContent[1]
r.FixedHeader.Compression = Compression(flags & HdrFlagsCompressionMethod)
switch r.FixedHeader.Compression {
case CompressionNone, CompressionZstd:
default:
return ErrInvalidCompression
}
var n int
r.FixedHeader.TimestampMultiplier, n = binary.Uvarint(hdrContent[2:])
if n <= 0 {
return errors.New("invalid TimestampMultiplier in FixedHeader")
}
return nil
}
func (r *BaseReader) ReadVarHeader(ownSchema *schema.Schema) error {
if _, err := r.FrameDecoder.Next(); err != nil {
return err
}
hdrBytes := make([]byte, r.FrameDecoder.RemainingSize())
n, err := r.FrameDecoder.Read(hdrBytes)
if err != nil {
return err
}
hdrBytes = hdrBytes[:n]
err = json.Unmarshal(hdrBytes, &r.VarHeader)
if err != nil {
return err
}
if r.VarHeader.Schema != nil {
r.Schema = &schema.Schema{}
err = json.Unmarshal(*r.VarHeader.Schema, r.Schema)
if err != nil {
return err
}
if _, err := ownSchema.Compatible(r.Schema); err != nil {
return fmt.Errorf("schema is not compatible with BaseReader: %w", err)
}
}
return nil
}
func (r *BaseReader) NextFrame() error {
frameFlag, err := r.FrameDecoder.Next()
_ = frameFlag
if err != nil {
return err
}
//if frameFlag&pkg.RestartDictionaries != 0 {
// r.restartDictionaries()
//}
r.FrameRecordCount, err = binary.ReadUvarint(&r.FrameDecoder)
if err != nil {
return err
}
if err := r.ReadBufs.ReadFrom(&r.FrameDecoder); err != nil {
return err
}
return nil
}