-
Notifications
You must be signed in to change notification settings - Fork 7
/
linear_iterator.go
134 lines (120 loc) · 3.05 KB
/
linear_iterator.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
package rosbag
import (
"bufio"
"bytes"
"compress/bzip2"
"encoding/binary"
"errors"
"io"
"github.com/pierrec/lz4/v4"
)
type buffer struct {
msg *Message
conn *Connection
err error
}
type linearIterator struct {
currentChunkLength int64
currentChunkRead int64
inChunk bool
reader *bufio.Reader
baseReader io.Reader
connections map[uint32]*Connection
buffer *buffer
lz4reader *lz4.Reader
}
func newLinearIterator(r io.Reader) *linearIterator {
br := bufio.NewReader(r)
return &linearIterator{
reader: br,
baseReader: r,
connections: make(map[uint32]*Connection),
inChunk: false,
lz4reader: lz4.NewReader(nil),
}
}
func (it *linearIterator) More() bool {
return it.buffer == nil || !errors.Is(it.buffer.err, io.EOF)
}
func (it *linearIterator) advanceBuffer(conn *Connection, msg *Message, err error) (*Connection, *Message, error) {
m := it.buffer.msg
c := it.buffer.conn
e := it.buffer.err
it.buffer.msg = msg
it.buffer.conn = conn
it.buffer.err = err
return c, m, e
}
func (it *linearIterator) Next() (*Connection, *Message, error) {
for {
op, record, err := ReadRecord(it.reader)
if err != nil {
if it.buffer != nil {
return it.advanceBuffer(nil, nil, err)
}
return nil, nil, err
}
if it.inChunk {
it.currentChunkRead += int64(len(record))
}
switch op {
case OpChunk:
headerLen := binary.LittleEndian.Uint32(record)
header := record[4 : 4+headerLen]
compression, err := GetHeaderValue(header, "compression")
if err != nil {
return nil, nil, err
}
size, err := GetHeaderValue(header, "size")
if err != nil {
return nil, nil, err
}
it.currentChunkLength = int64(u32(size))
chunkData := record[4+headerLen+4:]
switch string(compression) {
case CompressionNone:
it.reader.Reset(bytes.NewReader(chunkData))
it.inChunk = true
continue
case CompressionLZ4:
it.lz4reader.Reset(bytes.NewReader(chunkData))
it.reader.Reset(it.lz4reader)
it.inChunk = true
continue
case CompressionBZ2:
it.reader.Reset(bzip2.NewReader(bytes.NewReader(chunkData)))
default:
return nil, nil, ErrUnsupportedCompression{string(compression)}
}
case OpMessageData:
msg, err := ParseMessage(record)
if err != nil {
return nil, nil, err
}
conn := it.connections[msg.Conn]
// if this was the last message in the chunk, reset state for the next chunk
if it.inChunk && it.currentChunkRead >= it.currentChunkLength {
it.inChunk = false
it.reader.Reset(it.baseReader)
it.currentChunkRead = 0
it.currentChunkLength = 0
}
// if there's data in the buffer, we're going to return that and
// cache this.
if it.buffer != nil {
return it.advanceBuffer(conn, msg, err)
}
// otherwise, this is the first message. Cache it and head back to the top.
it.buffer = &buffer{msg, conn, nil}
continue
case OpConnection:
conn, err := ParseConnection(record)
if err != nil {
return nil, nil, err
}
it.connections[conn.Conn] = conn
default:
continue
}
}
}