-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
reads_resultset.go
245 lines (217 loc) · 6.31 KB
/
reads_resultset.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
package mock
import (
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/pkg/data/gen"
"github.com/influxdata/influxdb/v2/storage/reads"
"github.com/influxdata/influxdb/v2/tsdb/cursors"
)
type GeneratorResultSet struct {
sg gen.SeriesGenerator
tags models.Tags
max int
count int
f floatTimeValuesGeneratorCursor
i integerTimeValuesGeneratorCursor
u unsignedTimeValuesGeneratorCursor
s stringTimeValuesGeneratorCursor
b booleanTimeValuesGeneratorCursor
cur cursors.Cursor
}
var _ reads.ResultSet = (*GeneratorResultSet)(nil)
type GeneratorOptionFn func(*GeneratorResultSet)
// WithGeneratorMaxValues limits the number of values
// produced by GeneratorResultSet to n.
func WithGeneratorMaxValues(n int) GeneratorOptionFn {
return func(g *GeneratorResultSet) {
g.max = n
}
}
// NewResultSetFromSeriesGenerator transforms a SeriesGenerator into a ResultSet,
// and therefore may be used anywhere a ResultSet is required.
func NewResultSetFromSeriesGenerator(sg gen.SeriesGenerator, opts ...GeneratorOptionFn) *GeneratorResultSet {
s := &GeneratorResultSet{sg: sg}
for _, opt := range opts {
opt(s)
}
s.f.max = s.max
s.i.max = s.max
s.u.max = s.max
s.s.max = s.max
s.b.max = s.max
s.f.count = &s.count
s.i.count = &s.count
s.u.count = &s.count
s.s.count = &s.count
s.b.count = &s.count
return s
}
func (g *GeneratorResultSet) Next() bool {
g.tags = g.tags[:0]
remain := g.max - g.count
return g.sg.Next() && (g.max == 0 || remain > 0)
}
func (g *GeneratorResultSet) Cursor() cursors.Cursor {
switch g.sg.FieldType() {
case models.Float:
g.f.tv = g.sg.TimeValuesGenerator()
g.cur = &g.f
case models.Integer:
g.i.tv = g.sg.TimeValuesGenerator()
g.cur = &g.i
case models.Unsigned:
g.u.tv = g.sg.TimeValuesGenerator()
g.cur = &g.u
case models.String:
g.s.tv = g.sg.TimeValuesGenerator()
g.cur = &g.s
case models.Boolean:
g.b.tv = g.sg.TimeValuesGenerator()
g.cur = &g.b
default:
panic("unreachable")
}
return g.cur
}
func copyTags(dst, src models.Tags) models.Tags {
if cap(dst) < src.Len() {
dst = make(models.Tags, src.Len())
} else {
dst = dst[:src.Len()]
}
copy(dst, src)
return dst
}
func (g *GeneratorResultSet) Tags() models.Tags {
if len(g.tags) == 0 {
g.tags = copyTags(g.tags, g.sg.Tags())
g.tags.Set(models.MeasurementTagKeyBytes, g.sg.Name())
g.tags.Set(models.FieldKeyTagKeyBytes, g.sg.Field())
}
return g.tags
}
func (g *GeneratorResultSet) Close() {}
func (g *GeneratorResultSet) Err() error { return nil }
func (g *GeneratorResultSet) Stats() cursors.CursorStats {
var stats cursors.CursorStats
stats.Add(g.f.Stats())
stats.Add(g.i.Stats())
stats.Add(g.u.Stats())
stats.Add(g.s.Stats())
stats.Add(g.b.Stats())
return stats
}
// cursors
type timeValuesGeneratorCursor struct {
tv gen.TimeValuesSequence
max int
count *int
stats cursors.CursorStats
}
func (t timeValuesGeneratorCursor) Close() {}
func (t timeValuesGeneratorCursor) Err() error { return nil }
func (t timeValuesGeneratorCursor) Stats() cursors.CursorStats { return t.stats }
func (t *timeValuesGeneratorCursor) add(n int) { *t.count += n }
func (t *timeValuesGeneratorCursor) checkCount() bool { return t.max == 0 || *t.count < t.max }
func (t *timeValuesGeneratorCursor) remain() int { return t.max - *t.count }
type floatTimeValuesGeneratorCursor struct {
timeValuesGeneratorCursor
a cursors.FloatArray
}
func (c *floatTimeValuesGeneratorCursor) Next() *cursors.FloatArray {
if c.checkCount() && c.tv.Next() {
c.tv.Values().(gen.FloatValues).Copy(&c.a)
if remain := c.remain(); c.max > 0 && remain < c.a.Len() {
c.a.Timestamps = c.a.Timestamps[:remain]
c.a.Values = c.a.Values[:remain]
}
c.stats.ScannedBytes += len(c.a.Values) * 8
c.stats.ScannedValues += c.a.Len()
c.add(c.a.Len())
} else {
c.a.Timestamps = c.a.Timestamps[:0]
c.a.Values = c.a.Values[:0]
}
return &c.a
}
type integerTimeValuesGeneratorCursor struct {
timeValuesGeneratorCursor
a cursors.IntegerArray
}
func (c *integerTimeValuesGeneratorCursor) Next() *cursors.IntegerArray {
if c.checkCount() && c.tv.Next() {
c.tv.Values().(gen.IntegerValues).Copy(&c.a)
if remain := c.remain(); c.max > 0 && remain < c.a.Len() {
c.a.Timestamps = c.a.Timestamps[:remain]
c.a.Values = c.a.Values[:remain]
}
c.stats.ScannedBytes += len(c.a.Values) * 8
c.stats.ScannedValues += c.a.Len()
c.add(c.a.Len())
} else {
c.a.Timestamps = c.a.Timestamps[:0]
c.a.Values = c.a.Values[:0]
}
return &c.a
}
type unsignedTimeValuesGeneratorCursor struct {
timeValuesGeneratorCursor
a cursors.UnsignedArray
}
func (c *unsignedTimeValuesGeneratorCursor) Next() *cursors.UnsignedArray {
if c.checkCount() && c.tv.Next() {
c.tv.Values().(gen.UnsignedValues).Copy(&c.a)
if remain := c.remain(); c.max > 0 && remain < c.a.Len() {
c.a.Timestamps = c.a.Timestamps[:remain]
c.a.Values = c.a.Values[:remain]
}
c.stats.ScannedBytes += len(c.a.Values) * 8
c.stats.ScannedValues += c.a.Len()
c.add(c.a.Len())
} else {
c.a.Timestamps = c.a.Timestamps[:0]
c.a.Values = c.a.Values[:0]
}
return &c.a
}
type stringTimeValuesGeneratorCursor struct {
timeValuesGeneratorCursor
a cursors.StringArray
}
func (c *stringTimeValuesGeneratorCursor) Next() *cursors.StringArray {
if c.checkCount() && c.tv.Next() {
c.tv.Values().(gen.StringValues).Copy(&c.a)
if remain := c.remain(); c.max > 0 && remain < c.a.Len() {
c.a.Timestamps = c.a.Timestamps[:remain]
c.a.Values = c.a.Values[:remain]
}
for _, v := range c.a.Values {
c.stats.ScannedBytes += len(v)
}
c.stats.ScannedValues += c.a.Len()
c.add(c.a.Len())
} else {
c.a.Timestamps = c.a.Timestamps[:0]
c.a.Values = c.a.Values[:0]
}
return &c.a
}
type booleanTimeValuesGeneratorCursor struct {
timeValuesGeneratorCursor
a cursors.BooleanArray
}
func (c *booleanTimeValuesGeneratorCursor) Next() *cursors.BooleanArray {
if c.checkCount() && c.tv.Next() {
c.tv.Values().(gen.BooleanValues).Copy(&c.a)
if remain := c.remain(); c.max > 0 && remain < c.a.Len() {
c.a.Timestamps = c.a.Timestamps[:remain]
c.a.Values = c.a.Values[:remain]
}
c.stats.ScannedBytes += len(c.a.Values)
c.stats.ScannedValues += c.a.Len()
c.add(c.a.Len())
} else {
c.a.Timestamps = c.a.Timestamps[:0]
c.a.Values = c.a.Values[:0]
}
return &c.a
}