forked from kelindar/binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters.go
65 lines (51 loc) · 1.33 KB
/
counters.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
package sorted
import (
"reflect"
"sort"
"github.com/kelindar/binary"
)
// ------------------------------------------------------------------------------
type tczCodec struct{}
// EncodeTo encodes a value into the encoder.
func (tczCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
data := rv.Interface().(TimeCounters)
if !sort.IsSorted(&data) {
sort.Sort(&data)
}
buffer := make([]byte, 0, 4*len(data.Time))
buffer = appendDelta(buffer, data.Time)
buffer = appendDelta(buffer, data.Data)
// 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 (tczCodec) 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 := TimeCounters{
Time: make([]uint64, count),
Data: make([]uint64, count),
}
// Current offset
offset := readDelta(result.Time, buffer[0:])
offset = readDelta(result.Data, buffer[offset:])
rv.Set(reflect.ValueOf(result))
return nil
}