/
timestamp_iterator.go
325 lines (280 loc) · 8.3 KB
/
timestamp_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
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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package m3tsz
import (
"encoding/binary"
"fmt"
"time"
"github.com/m3db/m3/src/dbnode/encoding"
"github.com/m3db/m3/src/dbnode/ts"
xtime "github.com/m3db/m3/src/x/time"
)
// TimestampIterator encapsulates all the state required for iterating over
// delta-of-delta compresed timestamps.
type TimestampIterator struct {
PrevTime xtime.UnixNano
PrevTimeDelta time.Duration
PrevAnt ts.Annotation
TimeUnit xtime.Unit
Opts encoding.Options
TimeUnitChanged bool
Done bool
// Controls whether the iterator will "look ahead" for marker encoding
// schemes. Setting SkipMarkers to true disables the look ahead behavior
// for situations where looking ahead is not safe.
SkipMarkers bool
numValueBits uint
numBits uint
markerEncodingScheme encoding.MarkerEncodingScheme
}
// NewTimestampIterator creates a new TimestampIterator.
func NewTimestampIterator(opts encoding.Options, skipMarkers bool) TimestampIterator {
mes := opts.MarkerEncodingScheme()
return TimestampIterator{
Opts: opts,
SkipMarkers: skipMarkers,
numValueBits: uint(mes.NumValueBits()),
numBits: uint(mes.NumOpcodeBits() + mes.NumValueBits()),
markerEncodingScheme: mes,
}
}
// ReadTimestamp reads the first or next timestamp.
func (it *TimestampIterator) ReadTimestamp(stream encoding.IStream) (bool, bool, error) {
it.PrevAnt = nil
var (
first = false
err error
)
if it.PrevTime == 0 {
first = true
err = it.readFirstTimestamp(stream)
} else {
err = it.readNextTimestamp(stream)
}
if err != nil {
return false, false, err
}
// NB(xichen): reset time delta to 0 when there is a time unit change to be
// consistent with the encoder.
if it.TimeUnitChanged {
it.PrevTimeDelta = 0
it.TimeUnitChanged = false
}
return first, it.Done, nil
}
// ReadTimeUnit reads an encoded time unit and updates the iterator's state
// accordingly. It is exposed as a public method so that callers can control
// the encoding / decoding of the time unit on their own if they choose.
func (it *TimestampIterator) ReadTimeUnit(stream encoding.IStream) error {
tuBits, err := stream.ReadByte()
if err != nil {
return err
}
tu := xtime.Unit(tuBits)
if tu.IsValid() && tu != it.TimeUnit {
it.TimeUnitChanged = true
}
it.TimeUnit = tu
return nil
}
func (it *TimestampIterator) readFirstTimestamp(stream encoding.IStream) error {
ntBits, err := stream.ReadBits(64)
if err != nil {
return err
}
// NB(xichen): first time stamp is always normalized to nanoseconds.
nt := xtime.UnixNano(ntBits)
if it.TimeUnit == xtime.None {
it.TimeUnit = initialTimeUnit(nt, it.Opts.DefaultTimeUnit())
}
err = it.readNextTimestamp(stream)
if err != nil {
return err
}
it.PrevTime = nt + xtime.UnixNano(it.PrevTimeDelta)
return nil
}
func (it *TimestampIterator) readNextTimestamp(stream encoding.IStream) error {
dod, err := it.readMarkerOrDeltaOfDelta(stream)
if err != nil {
return err
}
it.PrevTimeDelta += dod
it.PrevTime = it.PrevTime + xtime.UnixNano(it.PrevTimeDelta)
return nil
}
func (it *TimestampIterator) tryReadMarker(stream encoding.IStream) (time.Duration, bool, error) {
opcodeAndValue, success := it.tryPeekBits(stream, it.numBits)
if !success {
return 0, false, nil
}
opcode := opcodeAndValue >> it.numValueBits
if opcode != it.markerEncodingScheme.Opcode() {
return 0, false, nil
}
var (
valueMask = (1 << it.numValueBits) - 1
markerValue = int64(opcodeAndValue & uint64(valueMask))
)
switch encoding.Marker(markerValue) {
case it.markerEncodingScheme.EndOfStream():
_, err := stream.ReadBits(it.numBits)
if err != nil {
return 0, false, err
}
it.Done = true
return 0, true, nil
case it.markerEncodingScheme.Annotation():
_, err := stream.ReadBits(it.numBits)
if err != nil {
return 0, false, err
}
err = it.readAnnotation(stream)
if err != nil {
return 0, false, err
}
markerOrDOD, err := it.readMarkerOrDeltaOfDelta(stream)
if err != nil {
return 0, false, err
}
return markerOrDOD, true, nil
case it.markerEncodingScheme.TimeUnit():
_, err := stream.ReadBits(it.numBits)
if err != nil {
return 0, false, err
}
err = it.ReadTimeUnit(stream)
if err != nil {
return 0, false, err
}
markerOrDOD, err := it.readMarkerOrDeltaOfDelta(stream)
if err != nil {
return 0, false, err
}
return markerOrDOD, true, nil
default:
return 0, false, nil
}
}
func (it *TimestampIterator) readMarkerOrDeltaOfDelta(stream encoding.IStream) (time.Duration, error) {
if !it.SkipMarkers {
dod, success, err := it.tryReadMarker(stream)
if err != nil {
return 0, err
}
if it.Done {
return 0, nil
}
if success {
return dod, nil
}
}
tes, exists := it.Opts.TimeEncodingSchemes().SchemeForUnit(it.TimeUnit)
if !exists {
return 0, fmt.Errorf("time encoding scheme for time unit %v doesn't exist", it.TimeUnit)
}
return it.readDeltaOfDelta(stream, tes)
}
func (it *TimestampIterator) readDeltaOfDelta(
stream encoding.IStream, tes encoding.TimeEncodingScheme) (time.Duration, error) {
if it.TimeUnitChanged {
// NB(xichen): if the time unit has changed, always read 64 bits as normalized
// dod in nanoseconds.
dodBits, err := stream.ReadBits(64)
if err != nil {
return 0, err
}
dod := encoding.SignExtend(dodBits, 64)
return time.Duration(dod), nil
}
cb, err := stream.ReadBits(1)
if err != nil {
return 0, err
}
if cb == tes.ZeroBucket().Opcode() {
return 0, nil
}
buckets := tes.Buckets()
for i := 0; i < len(buckets); i++ {
nextCB, err := stream.ReadBits(1)
if err != nil {
return 0, nil
}
cb = (cb << 1) | nextCB
if cb == buckets[i].Opcode() {
dodBits, err := stream.ReadBits(uint(buckets[i].NumValueBits()))
if err != nil {
return 0, err
}
dod := encoding.SignExtend(dodBits, uint(buckets[i].NumValueBits()))
timeUnit, err := it.TimeUnit.Value()
if err != nil {
return 0, nil
}
return xtime.FromNormalizedDuration(dod, timeUnit), nil
}
}
numValueBits := uint(tes.DefaultBucket().NumValueBits())
dodBits, err := stream.ReadBits(numValueBits)
if err != nil {
return 0, err
}
dod := encoding.SignExtend(dodBits, numValueBits)
timeUnit, err := it.TimeUnit.Value()
if err != nil {
return 0, nil
}
return xtime.FromNormalizedDuration(dod, timeUnit), nil
}
func (it *TimestampIterator) readAnnotation(stream encoding.IStream) error {
antLen, err := it.readVarint(stream)
if err != nil {
return err
}
// NB: we add 1 here to offset the 1 we subtracted during encoding.
antLen = antLen + 1
if antLen <= 0 {
return fmt.Errorf("unexpected annotation length %d", antLen)
}
// TODO(xichen): use pool to allocate the buffer once the pool diff lands.
buf := make([]byte, antLen)
n, err := stream.Read(buf)
if err != nil {
return err
}
if n != antLen {
return fmt.Errorf(
"expected to read %d annotation bytes but read: %d",
antLen, n)
}
it.PrevAnt = buf
return nil
}
func (it *TimestampIterator) readVarint(stream encoding.IStream) (int, error) {
res, err := binary.ReadVarint(stream)
return int(res), err
}
func (it *TimestampIterator) tryPeekBits(stream encoding.IStream, numBits uint) (uint64, bool) {
res, err := stream.PeekBits(numBits)
if err != nil {
return 0, false
}
return res, true
}