/
codecs.go
196 lines (159 loc) · 4.62 KB
/
codecs.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
// 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"
"reflect"
"sort"
"github.com/kelindar/binary"
)
// IntsCodecAs returns an int slice codec with the specified precision and type.
func IntsCodecAs(sliceType reflect.Type, sizeOfInt int) binary.Codec {
return &intSliceCodec{
sliceType: sliceType,
sizeOfInt: sizeOfInt,
}
}
type intSliceCodec struct {
sliceType reflect.Type
sizeOfInt int
}
// EncodeTo encodes a value into the encoder.
func (c *intSliceCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
sort.Sort(rv.Interface().(sort.Interface))
prev := int64(0)
temp := make([]byte, 10)
bytes := make([]byte, 0, c.sizeOfInt*rv.Len())
for i := 0; i < rv.Len(); i++ {
curr := rv.Index(i).Int()
diff := curr - prev
bytes = append(bytes, temp[:bin.PutVarint(temp, diff)]...)
prev = curr
}
e.WriteUvarint(uint64(len(bytes)))
e.Write(bytes)
return
}
// DecodeTo decodes into a reflect value from the decoder.
func (c *intSliceCodec) DecodeTo(d *binary.Decoder, rv reflect.Value) (err error) {
var l uint64
var b []byte
if l, err = d.ReadUvarint(); err == nil && l > 0 {
if b, err = d.Slice(int(l)); err == nil {
// Create a new slice and figure out its element type
elemType := c.sliceType.Elem()
slice := reflect.MakeSlice(c.sliceType, 0, 64)
// Iterate through and uncompress
prev := int64(0)
for i := 0; i < len(b); {
diff, n := bin.Varint(b[i:])
prev = prev + diff
slice = reflect.Append(slice, reflect.ValueOf(prev).Convert(elemType))
i += n
}
rv.Set(slice)
}
}
return
}
// ------------------------------------------------------------------------------
// UintsCodecAs returns an uint slice codec with the specified precision and type.
func UintsCodecAs(sliceType reflect.Type, sizeOfInt int) binary.Codec {
return &uintSliceCodec{
sliceType: sliceType,
sizeOfInt: sizeOfInt,
}
}
type uintSliceCodec struct {
sliceType reflect.Type
sizeOfInt int
}
// EncodeTo encodes a value into the encoder.
func (c *uintSliceCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
sort.Sort(rv.Interface().(sort.Interface))
prev := uint64(0)
temp := make([]byte, 10)
bytes := make([]byte, 0, c.sizeOfInt*rv.Len())
for i := 0; i < rv.Len(); i++ {
curr := rv.Index(i).Uint()
diff := curr - prev
bytes = append(bytes, temp[:bin.PutUvarint(temp, diff)]...)
prev = curr
}
e.WriteUvarint(uint64(len(bytes)))
e.Write(bytes)
return
}
// DecodeTo decodes into a reflect value from the decoder.
func (c *uintSliceCodec) DecodeTo(d *binary.Decoder, rv reflect.Value) (err error) {
var l uint64
var b []byte
if l, err = d.ReadUvarint(); err == nil && l > 0 {
if b, err = d.Slice(int(l)); err == nil {
// Create a new slice and figure out its element type
elemType := c.sliceType.Elem()
slice := reflect.MakeSlice(c.sliceType, 0, 64)
// Iterate through and uncompress
prev := uint64(0)
for i := 0; i < len(b); {
diff, n := bin.Uvarint(b[i:])
prev = prev + diff
slice = reflect.Append(slice, reflect.ValueOf(prev).Convert(elemType))
i += n
}
rv.Set(slice)
}
}
return
}
// ------------------------------------------------------------------------------
type timestampCodec struct{}
// EncodeTo encodes a value into the encoder.
func (c timestampCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
data := rv.Interface().(Timestamps)
if !sort.IsSorted(Uint64s(data)) {
sort.Sort(Uint64s(data))
}
temp := make([]byte, 10)
buffer := make([]byte, 0, 2*len(data)) // ~1-2 bytes per timestamp
prev := uint64(0)
for _, curr := range data {
diff := curr - prev
prev = curr
buffer = append(buffer, temp[:bin.PutUvarint(temp, uint64(diff))]...)
}
// Writhe the size and the buffer
e.WriteUvarint(uint64(len(data)))
e.WriteUvarint(uint64(len(buffer)))
e.Write(buffer)
return
}
// DecodeTo decodes into a reflect value from the decoder.
func (timestampCodec) 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
slice := make(Timestamps, 0, count)
prev := uint64(0)
for i := 0; i < int(size); {
diff, n := bin.Uvarint(buffer[i:])
prev = prev + diff
slice = append(slice, uint64(prev))
i += n
}
rv.Set(reflect.ValueOf(slice))
return nil
}