-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
232 lines (196 loc) · 4.4 KB
/
journal.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
package splunker
import (
"encoding/binary"
"fmt"
"io"
"os"
"path/filepath"
"github.com/klauspost/compress/zstd"
)
func NewJournalDecoder(name string) (*JournalDecoder, error) {
r, err := openJournal(name)
if err != nil {
return nil, err
}
jd := &JournalDecoder{
n: name,
cr: newCountedReader(r),
}
jd.s.fields = make(map[byte][]string)
return jd, nil
}
// to buffer up to uint64 reads
const decBufSize = 8
type JournalDecoder struct {
cr *CountedReader
err error
opcode byte
e Event
decBuf [decBufSize]byte
n string
s struct {
fields map[byte][]string
baseTime int32
activeHost uint64
activeSource uint64
activeSourceType uint64
}
}
func (jd *JournalDecoder) Host() string {
return jd.s.fields[byte(OpcodeNewHost)][jd.s.activeHost-1]
}
func (jd *JournalDecoder) Source() string {
return jd.s.fields[byte(OpcodeNewSource)][jd.s.activeSource-1]
}
func (jd *JournalDecoder) SourceType() string {
return jd.s.fields[byte(OpcodeNewSourceType)][jd.s.activeSourceType-1]
}
func (jd *JournalDecoder) Scan() bool {
next:
jd.opcode, jd.err = jd.cr.ReadByte()
if jd.err != nil {
return false
}
if jd.isEventOpcode() {
jd.e.reset()
}
jd.err = jd.decodeNext()
if jd.err != nil {
return false
}
if !jd.isEventOpcode() {
goto next
}
return true
}
func (jd *JournalDecoder) Err() error {
if jd.err == io.EOF {
return nil
}
return jd.err
}
func (jd *JournalDecoder) decodeNewState() (err error) {
// active host
if jd.opcode&0x8 != 0 {
jd.s.activeHost, err = binary.ReadUvarint(jd.cr)
if err != nil {
return err
}
}
// active source
if jd.opcode&0x4 != 0 {
jd.s.activeSource, err = binary.ReadUvarint(jd.cr)
if err != nil {
return err
}
}
// active source type
if jd.opcode&0x2 != 0 {
jd.s.activeSourceType, err = binary.ReadUvarint(jd.cr)
if err != nil {
return err
}
}
// base time
if jd.opcode&0x1 != 0 {
err = binary.Read(jd.cr, binary.LittleEndian, &jd.s.baseTime)
if err != nil {
return err
}
}
return nil
}
func (jd *JournalDecoder) decodeNext() error {
if d := fetchDecoder(Opcode(jd.opcode)); d != nil {
return d.Decode(jd, jd.cr, jd.opcode)
}
if jd.opcode >= 17 && jd.opcode <= 31 {
return jd.decodeNewState()
}
if jd.isEventOpcode() {
return jd.eventDecoder(jd.cr, jd.opcode)
}
return fmt.Errorf("unknown opcode: 0x%02x", jd.opcode)
}
func (jd *JournalDecoder) isEventOpcode() bool {
return jd.opcode == byte(OpcodeOldstyleEvent) || jd.opcode == byte(OpcodeOldstyleEventWithHash) || (jd.opcode >= 32 && jd.opcode <= 43)
}
const hashSize = 20
type Event struct {
messageLength uint64
hasExtendedStorage bool
extendedStorageLen uint64
hasHash bool
hash [hashSize]byte
streamID uint64
streamOffset uint64
streamSubOffset uint64
indexTime int64
subSeconds uint64
metadataCount uint64
message []byte
includePunctuation bool
}
func (e Event) String() string {
return fmt.Sprintf(
"messageLength: %v - "+
"extendedStorageLen: %v - "+
"hash: %02x - "+
"streamID: %v - "+
"streamOffset: %v - "+
"streamSubOffset: %v - "+
"indexTime: %v - "+
"subSeconds: %v - "+
"metadataCount: %v - "+
"message: %v - "+
"includePunctuation: %v",
e.messageLength,
e.extendedStorageLen,
e.hash,
e.streamID,
e.streamOffset,
e.streamSubOffset,
e.indexTime,
e.subSeconds,
e.metadataCount,
e.MessageString(),
e.includePunctuation,
)
}
func (e Event) Message() []byte {
return e.message
}
func (e Event) MessageString() string {
return bytesToStr(e.message)
}
func (e Event) reset() {
e.messageLength = 0
e.hasExtendedStorage = false
e.extendedStorageLen = 0
e.hasHash = false
e.streamID = 0
e.streamOffset = 0
e.streamSubOffset = 0
e.indexTime = 0
e.subSeconds = 0
e.metadataCount = 0
e.message = e.message[:0]
e.includePunctuation = false
}
// Event returns a struct filled with the current event data.
// calling Scan again will fill it with the next event data
func (jd *JournalDecoder) Event() Event {
return jd.e
}
func openJournal(name string) (io.Reader, error) {
p := filepath.Join(name, "rawdata", "journal.zst")
file, err := os.Open(p)
if err != nil {
return nil, err
}
zstdReader, err := zstd.NewReader(file, zstd.WithDecoderConcurrency(0))
if err != nil {
return nil, fmt.Errorf("zstd.NewReader: %v", err)
}
return zstdReader, nil
}