-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
types.go
143 lines (106 loc) · 4.85 KB
/
types.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
// 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 (
"reflect"
"github.com/kelindar/binary"
)
// ------------------------------------------------------------------------------
// Uint16s represents a slice serialized in an unsafe, non portable manner.
type Uint16s []uint16
func (s Uint16s) Len() int { return len(s) }
func (s Uint16s) Less(i, j int) bool { return s[i] < s[j] }
func (s Uint16s) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// GetBinaryCodec retrieves a custom binary codec.
func (s *Uint16s) GetBinaryCodec() binary.Codec {
return UintsCodecAs(reflect.TypeOf(Uint16s{}), 2)
}
// ------------------------------------------------------------------------------
// Int16s represents a slice serialized in an unsafe, non portable manner.
type Int16s []int16
func (s Int16s) Len() int { return len(s) }
func (s Int16s) Less(i, j int) bool { return s[i] < s[j] }
func (s Int16s) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// GetBinaryCodec retrieves a custom binary codec.
func (s *Int16s) GetBinaryCodec() binary.Codec {
return IntsCodecAs(reflect.TypeOf(Int16s{}), 2)
}
// ------------------------------------------------------------------------------
// Uint32s represents a slice serialized in an unsafe, non portable manner.
type Uint32s []uint32
func (s Uint32s) Len() int { return len(s) }
func (s Uint32s) Less(i, j int) bool { return s[i] < s[j] }
func (s Uint32s) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// GetBinaryCodec retrieves a custom binary codec.
func (s *Uint32s) GetBinaryCodec() binary.Codec {
return UintsCodecAs(reflect.TypeOf(Uint32s{}), 4)
}
// ------------------------------------------------------------------------------
// Int32s represents a slice serialized in an unsafe, non portable manner.
type Int32s []int32
func (s Int32s) Len() int { return len(s) }
func (s Int32s) Less(i, j int) bool { return s[i] < s[j] }
func (s Int32s) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// GetBinaryCodec retrieves a custom binary codec.
func (s *Int32s) GetBinaryCodec() binary.Codec {
return IntsCodecAs(reflect.TypeOf(Int32s{}), 4)
}
// ------------------------------------------------------------------------------
// Uint64s represents a slice serialized in an unsafe, non portable manner.
type Uint64s []uint64
func (s Uint64s) Len() int { return len(s) }
func (s Uint64s) Less(i, j int) bool { return s[i] < s[j] }
func (s Uint64s) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// GetBinaryCodec retrieves a custom binary codec.
func (s *Uint64s) GetBinaryCodec() binary.Codec {
return UintsCodecAs(reflect.TypeOf(Uint64s{}), 8)
}
// ------------------------------------------------------------------------------
// Int64s represents a slice serialized in an unsafe, non portable manner.
type Int64s []int64
func (s Int64s) Len() int { return len(s) }
func (s Int64s) Less(i, j int) bool { return s[i] < s[j] }
func (s Int64s) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// GetBinaryCodec retrieves a custom binary codec.
func (s *Int64s) GetBinaryCodec() binary.Codec {
return IntsCodecAs(reflect.TypeOf(Int64s{}), 8)
}
// ------------------------------------------------------------------------------
// Timestamps represents the slice of sorted timestamps
type Timestamps []uint64
// GetBinaryCodec retrieves a custom binary codec.
func (ts *Timestamps) GetBinaryCodec() binary.Codec {
return timestampCodec{}
}
// ------------------------------------------------------------------------------
// TimeSeries represents a compressed time-series data. The implementation is based
// on Gorilla paper (https://www.vldb.org/pvldb/vol8/p1816-teller.pdf), but instead
// of bit-weaving it is byte-aligned. If you are using this, consider using snappy
// compression on the output, as it will give a significantly better compression than
// simply marshaling the time-series using this binary encoder.
type TimeSeries struct {
Time []uint64 // Sorted timestamps compressed using delta-encoding
Data []float64 // Corresponding float-64 values
}
// Append appends a new value into the time series.
func (ts *TimeSeries) Append(time uint64, value float64) {
ts.Time = append(ts.Time, time)
ts.Data = append(ts.Data, value)
}
// Len returns the length of the time-series
func (ts *TimeSeries) Len() int {
return len(ts.Time)
}
// Less compares two elements of the time series
func (ts *TimeSeries) Less(i, j int) bool {
return ts.Time[i] < ts.Time[j]
}
// Swap swaps two elements of the time series
func (ts *TimeSeries) Swap(i, j int) {
ts.Time[i], ts.Time[j] = ts.Time[j], ts.Time[i]
ts.Data[i], ts.Data[j] = ts.Data[j], ts.Data[i]
}
// GetBinaryCodec retrieves a custom binary codec.
func (ts *TimeSeries) GetBinaryCodec() binary.Codec {
return tszCodec{}
}