-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtablet.go
371 lines (342 loc) · 10.2 KB
/
tablet.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 client
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"sort"
"time"
)
type MeasurementSchema struct {
Measurement string
DataType TSDataType
}
type ColumnCategory int8
const (
TAG = iota
FIELD
ATTRIBUTE
)
type Tablet struct {
insertTargetName string
measurementSchemas []*MeasurementSchema
columnCategories []ColumnCategory
timestamps []int64
values []interface{}
bitMaps []*BitMap
maxRowNumber int
RowSize int
}
func (t *Tablet) Len() int {
return t.RowSize
}
func (t *Tablet) Swap(i, j int) {
for index, schema := range t.measurementSchemas {
switch schema.DataType {
case BOOLEAN:
sortedSlice := t.values[index].([]bool)
sortedSlice[i], sortedSlice[j] = sortedSlice[j], sortedSlice[i]
case INT32, DATE:
sortedSlice := t.values[index].([]int32)
sortedSlice[i], sortedSlice[j] = sortedSlice[j], sortedSlice[i]
case INT64, TIMESTAMP:
sortedSlice := t.values[index].([]int64)
sortedSlice[i], sortedSlice[j] = sortedSlice[j], sortedSlice[i]
case FLOAT:
sortedSlice := t.values[index].([]float32)
sortedSlice[i], sortedSlice[j] = sortedSlice[j], sortedSlice[i]
case DOUBLE:
sortedSlice := t.values[index].([]float64)
sortedSlice[i], sortedSlice[j] = sortedSlice[j], sortedSlice[i]
case TEXT, BLOB, STRING:
sortedSlice := t.values[index].([][]byte)
sortedSlice[i], sortedSlice[j] = sortedSlice[j], sortedSlice[i]
}
}
if t.bitMaps != nil {
for _, bitMap := range t.bitMaps {
if bitMap != nil {
isNilI := bitMap.IsMarked(i)
isNilJ := bitMap.IsMarked(j)
if isNilI {
bitMap.Mark(j)
} else {
bitMap.UnMark(j)
}
if isNilJ {
bitMap.Mark(i)
} else {
bitMap.UnMark(i)
}
}
}
}
t.timestamps[i], t.timestamps[j] = t.timestamps[j], t.timestamps[i]
}
func (t *Tablet) Less(i, j int) bool {
return t.timestamps[i] < t.timestamps[j]
}
func (t *Tablet) SetTimestamp(timestamp int64, rowIndex int) {
t.timestamps[rowIndex] = timestamp
}
func (t *Tablet) SetValueAt(value interface{}, columnIndex, rowIndex int) error {
if columnIndex < 0 || columnIndex > len(t.measurementSchemas) {
return fmt.Errorf("illegal argument columnIndex %d", columnIndex)
}
if rowIndex < 0 || rowIndex > t.maxRowNumber {
return fmt.Errorf("illegal argument rowIndex %d", rowIndex)
}
if value == nil {
// Init the bitMap to mark nil value
if t.bitMaps == nil {
t.bitMaps = make([]*BitMap, len(t.values))
}
if t.bitMaps[columnIndex] == nil {
t.bitMaps[columnIndex] = NewBitMap(t.maxRowNumber)
}
// Mark the nil value position
t.bitMaps[columnIndex].Mark(rowIndex)
return nil
}
switch t.measurementSchemas[columnIndex].DataType {
case BOOLEAN:
values := t.values[columnIndex].([]bool)
switch v := value.(type) {
case bool:
values[rowIndex] = v
case *bool:
values[rowIndex] = *v
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
case INT32:
values := t.values[columnIndex].([]int32)
switch v := value.(type) {
case int32:
values[rowIndex] = v
case *int32:
values[rowIndex] = *v
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
case INT64, TIMESTAMP:
values := t.values[columnIndex].([]int64)
switch v := value.(type) {
case int64:
values[rowIndex] = v
case *int64:
values[rowIndex] = *v
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
case FLOAT:
values := t.values[columnIndex].([]float32)
switch v := value.(type) {
case float32:
values[rowIndex] = v
case *float32:
values[rowIndex] = *v
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
case DOUBLE:
values := t.values[columnIndex].([]float64)
switch v := value.(type) {
case float64:
values[rowIndex] = v
case *float64:
values[rowIndex] = *v
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
case TEXT, STRING:
values := t.values[columnIndex].([][]byte)
switch v := value.(type) {
case string:
values[rowIndex] = []byte(v)
case []byte:
values[rowIndex] = v
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
case BLOB:
values := t.values[columnIndex].([][]byte)
switch v := value.(type) {
case []byte:
values[rowIndex] = v
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
case DATE:
values := t.values[columnIndex].([]int32)
switch v := value.(type) {
case time.Time:
val, err := dateToInt32(v)
if err != nil {
return err
}
values[rowIndex] = val
default:
return fmt.Errorf("illegal argument value %v %v", value, reflect.TypeOf(value))
}
}
return nil
}
func (t *Tablet) GetMaxRowNumber() int {
return t.maxRowNumber
}
func (t *Tablet) GetValueAt(columnIndex, rowIndex int) (interface{}, error) {
if columnIndex < 0 || columnIndex > len(t.measurementSchemas) {
return nil, fmt.Errorf("illegal argument columnIndex %d", columnIndex)
}
if rowIndex < 0 || rowIndex > t.maxRowNumber {
return nil, fmt.Errorf("illegal argument rowIndex %d", rowIndex)
}
schema := t.measurementSchemas[columnIndex]
if t.bitMaps != nil && t.bitMaps[columnIndex] != nil && t.bitMaps[columnIndex].IsMarked(rowIndex) {
return nil, nil
}
switch schema.DataType {
case BOOLEAN:
return t.values[columnIndex].([]bool)[rowIndex], nil
case INT32:
return t.values[columnIndex].([]int32)[rowIndex], nil
case INT64, TIMESTAMP:
return t.values[columnIndex].([]int64)[rowIndex], nil
case FLOAT:
return t.values[columnIndex].([]float32)[rowIndex], nil
case DOUBLE:
return t.values[columnIndex].([]float64)[rowIndex], nil
case TEXT, STRING:
return string(t.values[columnIndex].([][]byte)[rowIndex]), nil
case BLOB:
return t.values[columnIndex].([][]byte)[rowIndex], nil
case DATE:
return int32ToDate(t.values[columnIndex].([]int32)[rowIndex])
default:
return nil, fmt.Errorf("illegal datatype %v", schema.DataType)
}
}
func (t *Tablet) GetTimestampBytes() []byte {
buff := &bytes.Buffer{}
binary.Write(buff, binary.BigEndian, t.timestamps[0:t.RowSize])
return buff.Bytes()
}
func (t *Tablet) GetMeasurements() []string {
measurements := make([]string, len(t.measurementSchemas))
for i, s := range t.measurementSchemas {
measurements[i] = s.Measurement
}
return measurements
}
func (t *Tablet) getDataTypes() []int32 {
types := make([]int32, len(t.measurementSchemas))
for i, s := range t.measurementSchemas {
types[i] = int32(s.DataType)
}
return types
}
func (t *Tablet) getColumnCategories() []int8 {
columnCategories := make([]int8, len(t.columnCategories))
for i := range t.columnCategories {
columnCategories[i] = int8(t.columnCategories[i])
}
return columnCategories
}
func (t *Tablet) getValuesBytes() ([]byte, error) {
buff := &bytes.Buffer{}
for i, schema := range t.measurementSchemas {
switch schema.DataType {
case BOOLEAN:
binary.Write(buff, binary.BigEndian, t.values[i].([]bool)[0:t.RowSize])
case INT32, DATE:
binary.Write(buff, binary.BigEndian, t.values[i].([]int32)[0:t.RowSize])
case INT64, TIMESTAMP:
binary.Write(buff, binary.BigEndian, t.values[i].([]int64)[0:t.RowSize])
case FLOAT:
binary.Write(buff, binary.BigEndian, t.values[i].([]float32)[0:t.RowSize])
case DOUBLE:
binary.Write(buff, binary.BigEndian, t.values[i].([]float64)[0:t.RowSize])
case TEXT, STRING, BLOB:
for _, s := range t.values[i].([][]byte)[0:t.RowSize] {
binary.Write(buff, binary.BigEndian, int32(len(s)))
binary.Write(buff, binary.BigEndian, s)
}
default:
return nil, fmt.Errorf("illegal datatype %v", schema.DataType)
}
}
if t.bitMaps != nil {
for _, bitMap := range t.bitMaps {
columnHasNil := bitMap != nil && !bitMap.IsAllUnmarked()
binary.Write(buff, binary.BigEndian, columnHasNil)
if columnHasNil {
// Need to maintain consistency with the calculation method on the IoTDB side.
binary.Write(buff, binary.BigEndian, bitMap.GetBits()[0:t.RowSize/8+1])
}
}
}
return buff.Bytes(), nil
}
func (t *Tablet) Sort() error {
sort.Sort(t)
return nil
}
func (t *Tablet) Reset() {
t.RowSize = 0
t.bitMaps = nil
}
func NewTablet(insertTargetName string, measurementSchemas []*MeasurementSchema, maxRowNumber int) (*Tablet, error) {
tablet := &Tablet{
insertTargetName: insertTargetName,
measurementSchemas: measurementSchemas,
maxRowNumber: maxRowNumber,
}
tablet.timestamps = make([]int64, maxRowNumber)
tablet.values = make([]interface{}, len(measurementSchemas))
for i, schema := range tablet.measurementSchemas {
switch schema.DataType {
case BOOLEAN:
tablet.values[i] = make([]bool, maxRowNumber)
case INT32, DATE:
tablet.values[i] = make([]int32, maxRowNumber)
case INT64, TIMESTAMP:
tablet.values[i] = make([]int64, maxRowNumber)
case FLOAT:
tablet.values[i] = make([]float32, maxRowNumber)
case DOUBLE:
tablet.values[i] = make([]float64, maxRowNumber)
case TEXT, STRING, BLOB:
tablet.values[i] = make([][]byte, maxRowNumber)
default:
return nil, fmt.Errorf("illegal datatype %v", schema.DataType)
}
}
return tablet, nil
}
func NewRelationalTablet(tableName string, measurementSchemas []*MeasurementSchema, columnCategories []ColumnCategory, maxRowNumber int) (*Tablet, error) {
tablet, err := NewTablet(tableName, measurementSchemas, maxRowNumber)
if err != nil {
return nil, err
}
tablet.columnCategories = columnCategories
return tablet, nil
}