-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtablet.cc
More file actions
374 lines (343 loc) · 12.9 KB
/
tablet.cc
File metadata and controls
374 lines (343 loc) · 12.9 KB
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
372
373
374
/*
* 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.
*/
#include "tablet.h"
#include <cstdlib>
using namespace common;
namespace storage {
int Tablet::init() {
ASSERT(timestamps_ == nullptr);
timestamps_ = (int64_t *)malloc(sizeof(int64_t) * max_row_num_);
cur_row_size_ = 0;
size_t schema_count = schema_vec_->size();
std::pair<std::map<std::string, int>::iterator, bool> ins_res;
for (size_t c = 0; c < schema_count; c++) {
ins_res = schema_map_.insert(
std::make_pair(schema_vec_->at(c).measurement_name_, c));
if (!ins_res.second) {
ASSERT(false);
// maybe dup measurement_name
return E_INVALID_ARG;
}
}
ASSERT(schema_map_.size() == schema_count);
value_matrix_ =
(ValueMatrixEntry *)malloc(sizeof(ValueMatrixEntry) * schema_count);
for (size_t c = 0; c < schema_count; ++c) {
const MeasurementSchema &schema = schema_vec_->at(c);
switch (schema.data_type_) {
case BOOLEAN:
value_matrix_[c].bool_data = (bool *)malloc(
get_data_type_size(schema.data_type_) * max_row_num_);
break;
case INT32:
value_matrix_[c].int32_data = (int32_t *)malloc(
get_data_type_size(schema.data_type_) * max_row_num_);
break;
case INT64:
value_matrix_[c].int64_data = (int64_t *)malloc(
get_data_type_size(schema.data_type_) * max_row_num_);
break;
case FLOAT:
value_matrix_[c].float_data = (float *)malloc(
get_data_type_size(schema.data_type_) * max_row_num_);
break;
case DOUBLE:
value_matrix_[c].double_data = (double *)malloc(
get_data_type_size(schema.data_type_) * max_row_num_);
break;
case STRING: {
value_matrix_[c].string_data =
(common::String *)malloc(sizeof(String) * max_row_num_);
break;
}
default:
ASSERT(false);
return E_INVALID_ARG;
}
}
bitmaps_ = new BitMap[schema_count];
for (size_t c = 0; c < schema_count; c++) {
bitmaps_[c].init(max_row_num_, false);
}
return E_OK;
}
void Tablet::destroy() {
if (timestamps_ != nullptr) {
free(timestamps_);
timestamps_ = nullptr;
}
if (value_matrix_ != nullptr) {
for (size_t c = 0; c < schema_vec_->size(); c++) {
const MeasurementSchema &schema = schema_vec_->at(c);
switch (schema.data_type_) {
case INT32:
free(value_matrix_[c].int32_data);
break;
case INT64:
free(value_matrix_[c].int64_data);
break;
case FLOAT:
free(value_matrix_[c].float_data);
break;
case DOUBLE:
free(value_matrix_[c].double_data);
break;
case BOOLEAN:
free(value_matrix_[c].bool_data);
break;
case STRING:
free(value_matrix_[c].string_data);
break;
default:
break;
}
}
free(value_matrix_);
value_matrix_ = nullptr;
}
if (bitmaps_ != nullptr) {
delete[] bitmaps_;
bitmaps_ = nullptr;
}
}
int Tablet::add_timestamp(uint32_t row_index, int64_t timestamp) {
ASSERT(timestamps_ != NULL);
if (UNLIKELY(row_index >= static_cast<uint32_t>(max_row_num_))) {
ASSERT(false);
return E_OUT_OF_RANGE;
}
timestamps_[row_index] = timestamp;
cur_row_size_ = std::max(row_index + 1, cur_row_size_);
return E_OK;
}
void *Tablet::get_value(int row_index, uint32_t schema_index,
common::TSDataType &data_type) const {
if (UNLIKELY(schema_index >= schema_vec_->size())) {
return nullptr;
}
const MeasurementSchema &schema = schema_vec_->at(schema_index);
ValueMatrixEntry column_values = value_matrix_[schema_index];
data_type = schema.data_type_;
if (bitmaps_[schema_index].test(row_index)) {
return nullptr;
}
switch (schema.data_type_) {
case BOOLEAN: {
bool *bool_values = column_values.bool_data;
return &bool_values[row_index];
}
case INT32: {
int32_t *int32_values = column_values.int32_data;
return &int32_values[row_index];
}
case INT64: {
int64_t *int64_values = column_values.int64_data;
return &int64_values[row_index];
}
case FLOAT: {
float *float_values = column_values.float_data;
return &float_values[row_index];
}
case DOUBLE: {
double *double_values = column_values.double_data;
return &double_values[row_index];
}
case STRING: {
auto string_values = column_values.string_data;
return &string_values[row_index];
}
default:
return nullptr;
}
}
template <>
void Tablet::process_val(uint32_t row_index, uint32_t schema_index,
common::String val) {
value_matrix_[schema_index].string_data[row_index].dup_from(val,
page_arena_);
bitmaps_[schema_index].clear(row_index); /* mark as non-null */
}
template <typename T>
void Tablet::process_val(uint32_t row_index, uint32_t schema_index, T val) {
switch (schema_vec_->at(schema_index).data_type_) {
case common::BOOLEAN:
(value_matrix_[schema_index].bool_data)[row_index] =
static_cast<bool>(val);
break;
case common::INT32:
value_matrix_[schema_index].int32_data[row_index] =
static_cast<int32_t>(val);
break;
case common::INT64:
value_matrix_[schema_index].int64_data[row_index] =
static_cast<int64_t>(val);
break;
case common::FLOAT:
value_matrix_[schema_index].float_data[row_index] =
static_cast<float>(val);
break;
case common::DOUBLE:
value_matrix_[schema_index].double_data[row_index] =
static_cast<double>(val);
break;
default:
ASSERT(false);
}
bitmaps_[schema_index].clear(row_index); /* mark as non-null */
}
template <typename T>
int Tablet::add_value(uint32_t row_index, uint32_t schema_index, T val) {
int ret = common::E_OK;
if (UNLIKELY(schema_index >= schema_vec_->size())) {
ASSERT(false);
ret = common::E_OUT_OF_RANGE;
} else {
const MeasurementSchema &schema = schema_vec_->at(schema_index);
if (UNLIKELY(GetDataTypeFromTemplateType<T>() != schema.data_type_)) {
if (GetDataTypeFromTemplateType<T>() == common::INT32 &&
schema.data_type_ == common::INT64) {
process_val(row_index, schema_index, static_cast<int64_t>(val));
} else if (GetDataTypeFromTemplateType<T>() == common::FLOAT &&
schema.data_type_ == common::DOUBLE) {
process_val(row_index, schema_index, static_cast<double>(val));
} else {
ASSERT(false);
return E_TYPE_NOT_MATCH;
}
} else {
process_val(row_index, schema_index, val);
}
}
return ret;
}
template <>
int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
common::String val) {
int ret = common::E_OK;
if (UNLIKELY(schema_index >= schema_vec_->size())) {
ASSERT(false);
ret = common::E_OUT_OF_RANGE;
}
process_val(row_index, schema_index, val);
return ret;
}
template <>
int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
const char *val) {
return add_value(row_index, schema_index, String(val));
}
template <typename T>
int Tablet::add_value(uint32_t row_index, const std::string &measurement_name,
T val) {
int ret = common::E_OK;
SchemaMapIterator find_iter = schema_map_.find(measurement_name);
if (LIKELY(find_iter == schema_map_.end())) {
ASSERT(false);
ret = E_INVALID_ARG;
} else {
ret = add_value(row_index, find_iter->second, val);
}
return ret;
}
template <>
int Tablet::add_value(uint32_t row_index, const std::string &measurement_name,
const char *val) {
return add_value(row_index, measurement_name, String(val));
}
int Tablet::set_batch_data_char(uint32_t col_index, char **data) {
if (col_index > schema_vec_->size()) {
return common::E_INVALID_SCHEMA;
}
for (int i = 0; i < max_row_num_; i++) {
if (data[i] != nullptr) {
value_matrix_[col_index].string_data[i].dup_from(data[i],
page_arena_);
bitmaps_[col_index].clear(i);
}
}
return common::E_OK;
}
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
bool val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
int32_t val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
int64_t val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
float val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
double val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name, bool val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name,
int32_t val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name,
int64_t val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name, float val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name, double val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name, String val);
void Tablet::set_column_categories(
const std::vector<ColumnCategory> &column_categories) {
column_categories_ = column_categories;
id_column_indexes_.clear();
for (size_t i = 0; i < column_categories_.size(); i++) {
ColumnCategory columnCategory = column_categories_[i];
if (columnCategory == ColumnCategory::TAG) {
id_column_indexes_.push_back(i);
}
}
}
int Tablet::set_null_value(uint32_t col_index, uint32_t row_index) {
if (col_index < 0 || col_index >= schema_vec_->size()) {
return common::E_INVALID_ARG;
}
if (row_index < 0 || row_index >= max_row_num_) {
return common::E_INVALID_ARG;
}
bitmaps_[col_index].set(row_index);
return common::E_OK;
}
std::shared_ptr<IDeviceID> Tablet::get_device_id(int i) const {
std::vector<std::string> id_array;
id_array.reserve(id_column_indexes_.size() + 1);
id_array.push_back(insert_target_name_);
for (auto id_column_idx : id_column_indexes_) {
common::TSDataType data_type = INVALID_DATATYPE;
void *value_ptr = get_value(i, id_column_idx, data_type);
common::String str;
switch (data_type) {
case STRING:
str = *static_cast<common::String *>(value_ptr);
id_array.push_back(str.to_std_string());
break;
default:
break;
}
}
if (id_array.size() == id_column_indexes_.size() + 1) {
return std::make_shared<StringArrayDeviceID>(id_array, true);
}
return std::make_shared<StringArrayDeviceID>(id_array);
}
} // end namespace storage