-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
timeseries.go
115 lines (93 loc) · 2.4 KB
/
timeseries.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
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package sorted
import (
bin "encoding/binary"
"math"
"math/bits"
"reflect"
"sort"
"github.com/kelindar/binary"
)
// ------------------------------------------------------------------------------
type tszCodec struct{}
// EncodeTo encodes a value into the encoder.
func (tszCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
data := rv.Interface().(TimeSeries)
if !sort.IsSorted(&data) {
sort.Sort(&data)
}
buffer := make([]byte, 0, 4*len(data.Time))
// Write the timestamps into the buffer
prev := uint64(0)
for _, curr := range data.Time {
diff := curr - prev
prev = curr
buffer = appendDelta(buffer, diff)
}
// Write the values into the buffer
prev = uint64(0)
for _, v := range data.Data {
curr := uint64(bits.Reverse32(math.Float32bits(float32(v))))
diff := curr ^ prev
prev = curr
buffer = appendDelta(buffer, diff)
}
// Writhe the size and the buffer
e.WriteUvarint(uint64(len(data.Time)))
e.WriteUvarint(uint64(len(buffer)))
e.Write(buffer)
return
}
// DecodeTo decodes into a reflect value from the decoder.
func (tszCodec) DecodeTo(d *binary.Decoder, rv reflect.Value) error {
// Read the number of timestamps
count, err := d.ReadUvarint()
if err != nil {
return err
}
// Read the size in bytes
size, err := d.ReadUvarint()
if err != nil {
return err
}
// Read the timestamp buffer
buffer, err := d.Slice(int(size))
if err != nil {
return err
}
// Read the timestamps
result := TimeSeries{
Time: make([]uint64, count),
Data: make([]float64, count),
}
// Current offset
offset := 0
// Read encoded timestamps
prev := uint64(0)
for i := 0; i < int(count); i++ {
diff, n := bin.Uvarint(buffer[offset:])
prev = prev + diff
result.Time[i] = prev
offset += n
}
d.ReadUvarint()
// Read encoded values
prev = uint64(0)
for i := 0; i < int(count); i++ {
diff, n := bin.Uvarint(buffer[offset:])
prev = prev ^ diff
result.Data[i] = float64(math.Float32frombits(bits.Reverse32(uint32(prev))))
offset += n
}
rv.Set(reflect.ValueOf(result))
return nil
}
// appendDelta appends a delta into the buffer
func appendDelta(buffer []byte, delta uint64) []byte {
for delta >= 0x80 {
buffer = append(buffer, byte(delta)|0x80)
delta >>= 7
}
return append(buffer, byte(delta))
}