-
Notifications
You must be signed in to change notification settings - Fork 146
/
first_last_reader.go
261 lines (216 loc) · 6.96 KB
/
first_last_reader.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Copyright 2023 Huawei Cloud Computing Technologies Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package immutable
import (
"fmt"
"sync"
"github.com/openGemini/openGemini/lib/numberenc"
"github.com/openGemini/openGemini/lib/record"
"github.com/openGemini/openGemini/lib/util/lifted/vm/protoparser/influx"
"go.uber.org/zap"
)
var firstLastReaderPool sync.Pool
func readFirstOrLast(cm *ChunkMeta, ref *record.Field, dst *record.Record, ctx *ReadContext, cr ColumnReader, copied, first bool, ioPriority int) error {
reader, ok := firstLastReaderPool.Get().(*FirstLastReader)
if !ok || reader == nil {
reader = &FirstLastReader{}
}
defer reader.Release()
reader.Init(cm, cr, ref, dst, first)
return reader.Read(ctx, copied, ioPriority)
}
type FirstLastReader struct {
first bool
cr ColumnReader
ref *record.Field
cm *ChunkMeta
dst *record.Record
timeCol *record.ColVal
dataCol *record.ColVal
meta *record.ColMeta
timeBuf []byte
dataBuf []byte
segIndex int
}
func (r *FirstLastReader) Init(cm *ChunkMeta, cr ColumnReader, ref *record.Field, dst *record.Record, first bool) *FirstLastReader {
r.first = first
r.ref = ref
r.cr = cr
r.cm = cm
r.dst = dst
idx := dst.Schema.FieldIndex(ref.Name)
if idx < 0 {
panic(fmt.Sprintf("column(%v) not find in %v", ref.String(), dst.Schema.String()))
}
r.dataCol = dst.Column(idx)
r.timeCol = dst.TimeColumn()
r.meta = &dst.ColMeta[idx]
r.segIndex = -1
if !first {
r.segIndex = int(cm.segCount)
}
return r
}
func (r *FirstLastReader) Read(ctx *ReadContext, copied bool, ioPriority int) error {
idx := r.cm.columnIndex(r.ref)
if idx < 0 {
return nil
}
colMeta := &r.cm.colMeta[idx]
tmMeta := r.cm.timeMeta()
var err error
for r.next() {
minMaxSeg := &r.cm.timeRange[r.segIndex]
if !ctx.tr.Overlaps(minMaxSeg.minTime(), minMaxSeg.maxTime()) {
continue
}
// in some scenarios, the first value is exactly the min value or
// the last value is exactly the max value.
// can directly use the min/max value in the pre-aggregated data as the first/last
val, tm, ok := r.readFirstOrLastFromPreAgg(ctx, minMaxSeg, colMeta)
if ok {
rowIndex := 0
if !r.first {
rowIndex = int(numberenc.UnmarshalUint32(r.cm.timeMeta().preAgg)) - 1
}
err = r.after(val, tm, rowIndex, ctx, copied, ioPriority)
break
}
if err := r.readDataColVal(ctx, &colMeta.entries[r.segIndex], copied, ioPriority); err != nil {
return err
}
var rowIndex int
if r.first && r.dataCol.NilCount == 0 && minMaxSeg.minTime() >= ctx.tr.Min {
// query time range: --------------
// segment time range: ---------------
// If there is no null value, the first row of data is the result
tm = r.cm.minTime()
rowIndex = 0
} else if !r.first && r.dataCol.NilCount == 0 && minMaxSeg.maxTime() <= ctx.tr.Max {
// query time range: --------------
// segment time range: ---------------
// If there is no null value, the last row of data is the result
tm = r.cm.maxTime()
rowIndex = r.dataCol.Len - 1
} else {
if err := r.readTimeColVal(ctx, &tmMeta.entries[r.segIndex], copied, ioPriority); err != nil {
return err
}
rowIndex = r.readRowIndex(ctx)
if rowIndex >= r.timeCol.Length() {
continue
}
tm, _ = r.timeCol.IntegerValue(rowIndex)
}
val = getColumnValue(r.ref, r.dataCol, rowIndex)
err = r.after(val, tm, rowIndex, ctx, copied, ioPriority)
break
}
return err
}
func (r *FirstLastReader) Release() {
r.cr = nil
r.ref = nil
r.cm = nil
r.dst = nil
r.timeCol = nil
r.dataCol = nil
r.meta = nil
firstLastReaderPool.Put(r)
}
func (r *FirstLastReader) after(val interface{}, tm int64, rowIndex int, ctx *ReadContext, copied bool, ioPriority int) error {
if r.first {
r.meta.SetFirst(val, tm)
} else {
r.meta.SetLast(val, tm)
}
setColumnDefaultValue(r.ref, r.dataCol)
r.timeCol.Init()
r.timeCol.AppendInteger(tm)
if r.dst.Schema.Len() > 2 && len(ctx.ops) == 1 {
err := readAuxData(r.cm, r.segIndex, rowIndex, r.dst, ctx, r.cr, copied, ioPriority)
if err != nil {
log.Error("read aux data column fail", zap.Error(err))
return err
}
}
return nil
}
func (r *FirstLastReader) readRowIndex(ctx *ReadContext) int {
if r.first {
return readFirstRowIndex(r.timeCol, r.dataCol, ctx.tr)
}
return readLastRowIndex(r.timeCol, r.dataCol, ctx.tr)
}
func (r *FirstLastReader) next() bool {
if r.first {
r.segIndex++
return r.segIndex < int(r.cm.segCount)
}
r.segIndex--
return r.segIndex >= 0
}
func (r *FirstLastReader) readColVal(seg *Segment, buf *[]byte, hook func(buf []byte) error, ioPriority int) error {
offset, size := seg.offsetSize()
data, cachePage, err := r.cr.ReadDataBlock(offset, size, buf, ioPriority)
defer r.cr.UnrefCachePage(cachePage)
if err != nil {
return err
}
return hook(data)
}
func (r *FirstLastReader) readTimeColVal(ctx *ReadContext, seg *Segment, copied bool, ioPriority int) error {
r.timeBuf = r.timeBuf[:0]
return r.readColVal(seg, &r.timeBuf, func(buf []byte) error {
return appendTimeColumnData(buf, r.timeCol, ctx, copied)
}, ioPriority)
}
func (r *FirstLastReader) readDataColVal(ctx *ReadContext, seg *Segment, copied bool, ioPriority int) error {
r.dataBuf = r.dataBuf[:0]
return r.readColVal(seg, &r.dataBuf, func(buf []byte) error {
return decodeColumnData(r.ref, buf, r.dataCol, ctx, copied)
}, ioPriority)
}
func (r *FirstLastReader) readFirstOrLastFromPreAgg(ctx *ReadContext, sr *SegmentRange, colMeta *ColumnMeta) (interface{}, int64, bool) {
if r.first && ctx.tr.Min <= sr.minTime() {
val, tm, ok := r.readMinFromPreAgg(colMeta)
return val, tm, ok && tm == sr.minTime()
}
if !r.first && ctx.tr.Max >= sr.maxTime() {
val, tm, ok := r.readMaxFromPreAgg(colMeta)
return val, tm, ok && tm == sr.maxTime()
}
return 0, 0, false
}
func (r *FirstLastReader) readMinFromPreAgg(colMeta *ColumnMeta) (interface{}, int64, bool) {
return readFromPreAgg(colMeta.ty, colMeta.preAgg, 0)
}
func (r *FirstLastReader) readMaxFromPreAgg(colMeta *ColumnMeta) (interface{}, int64, bool) {
return readFromPreAgg(colMeta.ty, colMeta.preAgg, 8)
}
func readFromPreAgg(ty byte, agg []byte, offset int) (interface{}, int64, bool) {
timeOffset := offset + 16
if PreAggOnlyOneRow(agg) {
offset = 0
timeOffset = 8
}
switch ty {
case influx.Field_Type_Float:
return numberenc.UnmarshalFloat64(agg[offset:]), numberenc.UnmarshalInt64(agg[timeOffset:]), true
case influx.Field_Type_Int:
return numberenc.UnmarshalInt64(agg[offset:]), numberenc.UnmarshalInt64(agg[timeOffset:]), true
default:
break
}
return nil, 0, false
}