-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtsfile_cwrapper.cc
More file actions
1709 lines (1579 loc) · 63.5 KB
/
Copy pathtsfile_cwrapper.cc
File metadata and controls
1709 lines (1579 loc) · 63.5 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "cwrapper/tsfile_cwrapper.h"
#include <file/write_file.h>
#include <reader/qds_without_timegenerator.h>
#include <writer/tsfile_table_writer.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <cstring>
#include <set>
#include <vector>
#include "common/device_id.h"
#include "common/statistic.h"
#include "common/tablet.h"
#include "common/tsfile_common.h"
#include "reader/filter/tag_filter.h"
#include "reader/result_set.h"
#include "reader/table_result_set.h"
#include "reader/tsfile_reader.h"
#include "writer/tsfile_writer.h"
// Forward declarations for arrow namespace functions (defined in arrow_c.cc)
namespace arrow {
int TsBlockToArrowStruct(common::TsBlock& tsblock, ArrowArray* out_array,
ArrowSchema* out_schema);
int ArrowStructToTablet(const char* table_name, const ArrowArray* in_array,
const ArrowSchema* in_schema,
const storage::TableSchema* reg_schema,
storage::Tablet** out_tablet, int time_col_index);
} // namespace arrow
#ifdef __cplusplus
extern "C" {
#endif
void init_tsfile_config() { storage::libtsfile_init(); }
uint8_t get_global_time_encoding() {
return common::get_global_time_encoding();
}
uint8_t get_global_time_compression() {
return common::get_global_time_compression();
}
uint8_t get_datatype_encoding(uint8_t data_type) {
return common::get_datatype_encoding(data_type);
}
uint8_t get_global_compression() { return common::get_global_compression(); }
int set_global_time_encoding(uint8_t encoding) {
return common::set_global_time_encoding(encoding);
}
int set_global_time_compression(uint8_t compression) {
return common::set_global_time_compression(compression);
}
int set_datatype_encoding(uint8_t data_type, uint8_t encoding) {
return common::set_datatype_encoding(data_type, encoding);
}
int set_global_compression(uint8_t compression) {
return common::set_global_compression(compression);
}
WriteFile write_file_new(const char* pathname, ERRNO* err_code) {
int ret;
init_tsfile_config();
if (access(pathname, F_OK) == 0) {
*err_code = common::E_ALREADY_EXIST;
return nullptr;
}
int flags = O_RDWR | O_CREAT | O_TRUNC;
#ifdef _WIN32
flags |= O_BINARY;
#endif
mode_t mode = 0666;
storage::WriteFile* file = new storage::WriteFile;
ret = file->create(pathname, flags, mode);
*err_code = ret;
return file;
}
TsFileWriter tsfile_writer_new(WriteFile file, TableSchema* schema,
ERRNO* err_code) {
if (schema->column_num == 0) {
*err_code = common::E_INVALID_SCHEMA;
return nullptr;
}
init_tsfile_config();
std::vector<common::ColumnSchema> column_schemas;
std::set<std::string> column_names;
for (int i = 0; i < schema->column_num; i++) {
ColumnSchema cur_schema = schema->column_schemas[i];
if (column_names.find(cur_schema.column_name) != column_names.end()) {
*err_code = common::E_INVALID_SCHEMA;
return nullptr;
}
column_names.insert(cur_schema.column_name);
if (cur_schema.column_category == TAG &&
cur_schema.data_type != TS_DATATYPE_STRING) {
*err_code = common::E_INVALID_SCHEMA;
return nullptr;
}
column_schemas.emplace_back(
cur_schema.column_name,
static_cast<common::TSDataType>(cur_schema.data_type),
static_cast<common::ColumnCategory>(cur_schema.column_category));
}
storage::TableSchema* table_schema =
new storage::TableSchema(schema->table_name, column_schemas);
auto table_writer = new storage::TsFileTableWriter(
static_cast<storage::WriteFile*>(file), table_schema);
delete table_schema;
*err_code = common::E_OK;
return table_writer;
}
TsFileWriter tsfile_writer_new_with_memory_threshold(WriteFile file,
TableSchema* schema,
uint64_t memory_threshold,
ERRNO* err_code) {
if (schema->column_num == 0) {
*err_code = common::E_INVALID_SCHEMA;
return nullptr;
}
init_tsfile_config();
std::vector<common::ColumnSchema> column_schemas;
std::set<std::string> column_names;
for (int i = 0; i < schema->column_num; i++) {
ColumnSchema cur_schema = schema->column_schemas[i];
if (column_names.find(cur_schema.column_name) == column_names.end()) {
*err_code = common::E_INVALID_SCHEMA;
return nullptr;
}
column_names.insert(cur_schema.column_name);
column_schemas.emplace_back(
cur_schema.column_name,
static_cast<common::TSDataType>(cur_schema.data_type),
static_cast<common::ColumnCategory>(cur_schema.column_category));
}
storage::TableSchema* table_schema =
new storage::TableSchema(schema->table_name, column_schemas);
auto table_writer = new storage::TsFileTableWriter(
static_cast<storage::WriteFile*>(file), table_schema, memory_threshold);
*err_code = common::E_OK;
delete table_schema;
return table_writer;
}
TsFileReader tsfile_reader_new(const char* pathname, ERRNO* err_code) {
init_tsfile_config();
auto reader = new storage::TsFileReader();
int ret = reader->open(pathname);
if (ret != common::E_OK) {
*err_code = ret;
delete reader;
return nullptr;
}
return reader;
}
ERRNO tsfile_writer_close(TsFileWriter writer) {
if (writer == nullptr) {
return common::E_OK;
}
auto* w = static_cast<storage::TsFileTableWriter*>(writer);
int ret = w->flush();
if (ret != common::E_OK) {
return ret;
}
ret = w->close();
if (ret != common::E_OK) {
return ret;
}
delete w;
return ret;
}
ERRNO tsfile_reader_close(TsFileReader reader) {
auto* ts_reader = static_cast<storage::TsFileReader*>(reader);
delete ts_reader;
return common::E_OK;
}
Tablet tablet_new(char** column_name_list, TSDataType* data_types,
uint32_t column_num, uint32_t max_rows) {
std::vector<std::string> measurement_list;
std::vector<common::TSDataType> data_type_list;
for (uint32_t i = 0; i < column_num; i++) {
measurement_list.emplace_back(storage::to_lower(column_name_list[i]));
data_type_list.push_back(
static_cast<common::TSDataType>(*(data_types + i)));
}
return new storage::Tablet(measurement_list, data_type_list, max_rows);
}
uint32_t tablet_get_cur_row_size(Tablet tablet) {
return static_cast<storage::Tablet*>(tablet)->get_cur_row_size();
}
ERRNO tablet_add_timestamp(Tablet tablet, uint32_t row_index,
Timestamp timestamp) {
return static_cast<storage::Tablet*>(tablet)->add_timestamp(row_index,
timestamp);
}
#define TABLET_ADD_VALUE_BY_NAME_DEF(type) \
ERRNO tablet_add_value_by_name_##type(Tablet tablet, uint32_t row_index, \
const char* column_name, \
const type value) { \
return static_cast<storage::Tablet*>(tablet)->add_value( \
row_index, storage::to_lower(column_name), value); \
}
TABLET_ADD_VALUE_BY_NAME_DEF(int32_t);
TABLET_ADD_VALUE_BY_NAME_DEF(int64_t);
TABLET_ADD_VALUE_BY_NAME_DEF(float);
TABLET_ADD_VALUE_BY_NAME_DEF(double);
TABLET_ADD_VALUE_BY_NAME_DEF(bool);
ERRNO tablet_add_value_by_name_string_with_len(Tablet tablet,
uint32_t row_index,
const char* column_name,
const char* value,
int value_len) {
return static_cast<storage::Tablet*>(tablet)->add_value(
row_index, storage::to_lower(column_name),
common::String(value, value_len));
}
#define TABLE_ADD_VALUE_BY_INDEX_DEF(type) \
ERRNO tablet_add_value_by_index_##type(Tablet tablet, uint32_t row_index, \
uint32_t column_index, \
const type value) { \
return static_cast<storage::Tablet*>(tablet)->add_value( \
row_index, column_index, value); \
}
ERRNO tablet_add_value_by_index_string_with_len(Tablet tablet,
uint32_t row_index,
uint32_t column_index,
const char* value,
int value_len) {
return static_cast<storage::Tablet*>(tablet)->add_value(
row_index, column_index, common::String(value, value_len));
}
TABLE_ADD_VALUE_BY_INDEX_DEF(int32_t);
TABLE_ADD_VALUE_BY_INDEX_DEF(int64_t);
TABLE_ADD_VALUE_BY_INDEX_DEF(float);
TABLE_ADD_VALUE_BY_INDEX_DEF(double);
TABLE_ADD_VALUE_BY_INDEX_DEF(bool);
// TsRecord API
TsRecord _ts_record_new(const char* device_id, Timestamp timestamp,
int timeseries_num) {
auto* record = new storage::TsRecord(timestamp, device_id, timeseries_num);
return record;
}
#define INSERT_DATA_INTO_TS_RECORD_BY_NAME_DEF(type) \
ERRNO _insert_data_into_ts_record_by_name_##type( \
TsRecord data, const char* measurement_name, type value) { \
auto* record = (storage::TsRecord*)data; \
storage::DataPoint point(measurement_name, value); \
if (record->points_.size() + 1 > record->points_.capacity()) \
return common::E_BUF_NOT_ENOUGH; \
record->points_.push_back(point); \
return common::E_OK; \
}
ERRNO _insert_data_into_ts_record_by_name_string_with_len(
TsRecord data, const char* measurement_name, const char* value,
const uint32_t value_len) {
auto* record = (storage::TsRecord*)data;
if (record->points_.size() + 1 > record->points_.capacity())
return common::E_BUF_NOT_ENOUGH;
common::String str_value;
str_value.dup_from(value, value_len, record->pa);
record->add_point(measurement_name, str_value);
return common::E_OK;
}
INSERT_DATA_INTO_TS_RECORD_BY_NAME_DEF(int32_t);
INSERT_DATA_INTO_TS_RECORD_BY_NAME_DEF(int64_t);
INSERT_DATA_INTO_TS_RECORD_BY_NAME_DEF(bool);
INSERT_DATA_INTO_TS_RECORD_BY_NAME_DEF(float);
INSERT_DATA_INTO_TS_RECORD_BY_NAME_DEF(double);
/*
TsFileWriter tsfile_writer_new_with_conf(const char *pathname,
const mode_t flag, ERRNO *err_code,
TsFileConf *conf) {
init_tsfile_config();
auto *writer = new storage::TsFileWriter();
const int ret = writer->open(pathname, O_CREAT | O_RDWR, flag);
if (ret != common::E_OK) {
delete writer;
*err_code = ret;
return nullptr;
}
return writer;
}
*/
ERRNO tsfile_writer_write(TsFileWriter writer, Tablet tablet) {
auto* w = static_cast<storage::TsFileTableWriter*>(writer);
auto* tbl = static_cast<storage::Tablet*>(tablet);
return w->write_table(*tbl);
}
// ERRNO tsfile_writer_flush_data(TsFileWriter writer) {
// auto *w = static_cast<storage::TsFileWriter *>(writer);
// return w->flush();
// }
// Query
ResultSet tsfile_query_table(TsFileReader reader, const char* table_name,
char** columns, uint32_t column_num,
Timestamp start_time, Timestamp end_time,
ERRNO* err_code) {
auto* r = static_cast<storage::TsFileReader*>(reader);
storage::ResultSet* table_result_set = nullptr;
std::vector<std::string> column_names;
for (uint32_t i = 0; i < column_num; i++) {
column_names.emplace_back(columns[i]);
}
*err_code = r->query(table_name, column_names, start_time, end_time,
table_result_set);
return table_result_set;
}
ResultSet tsfile_query_table_on_tree(TsFileReader reader, char** columns,
uint32_t column_num, Timestamp start_time,
Timestamp end_time, ERRNO* err_code) {
auto* r = static_cast<storage::TsFileReader*>(reader);
storage::ResultSet* table_result_set = nullptr;
std::vector<std::string> column_names;
for (uint32_t i = 0; i < column_num; i++) {
column_names.emplace_back(columns[i]);
}
*err_code = r->query_table_on_tree(column_names, start_time, end_time,
table_result_set);
return table_result_set;
}
ResultSet tsfile_reader_query_tree_by_row(TsFileReader reader,
char** device_ids, int device_ids_len,
char** measurement_names,
int measurement_names_len, int offset,
int limit, ERRNO* err_code) {
auto* r = static_cast<storage::TsFileReader*>(reader);
storage::ResultSet* result_set = nullptr;
std::vector<std::string> path_list;
if (device_ids_len > 0 && measurement_names_len > 0) {
path_list.reserve(static_cast<size_t>(device_ids_len) *
static_cast<size_t>(measurement_names_len));
}
for (int i = 0; i < device_ids_len; i++) {
const char* device_id = device_ids[i];
if (device_id == nullptr) {
continue;
}
for (int j = 0; j < measurement_names_len; j++) {
const char* measurement_name = measurement_names[j];
if (measurement_name == nullptr) {
continue;
}
path_list.emplace_back(std::string(device_id) + "." +
std::string(measurement_name));
}
}
*err_code = r->queryByRow(path_list, offset, limit, result_set);
return result_set;
}
ResultSet tsfile_reader_query_table_by_row(
TsFileReader reader, const char* table_name, char** column_names,
int column_names_len, int offset, int limit, TagFilterHandle tag_filter,
int batch_size, ERRNO* err_code) {
auto* r = static_cast<storage::TsFileReader*>(reader);
storage::ResultSet* result_set = nullptr;
std::vector<std::string> columns;
if (column_names_len > 0) {
columns.reserve(static_cast<size_t>(column_names_len));
}
for (int i = 0; i < column_names_len; i++) {
const char* name = column_names[i];
columns.emplace_back(name == nullptr ? "" : std::string(name));
}
*err_code = r->queryByRow(
table_name == nullptr ? "" : table_name, columns, offset, limit,
result_set, static_cast<storage::Filter*>(tag_filter), batch_size);
return result_set;
}
ResultSet tsfile_query_table_batch(TsFileReader reader, const char* table_name,
char** columns, uint32_t column_num,
Timestamp start_time, Timestamp end_time,
TagFilterHandle tag_filter, int batch_size,
ERRNO* err_code) {
auto* r = static_cast<storage::TsFileReader*>(reader);
storage::ResultSet* table_result_set = nullptr;
std::vector<std::string> column_names;
for (uint32_t i = 0; i < column_num; i++) {
column_names.emplace_back(columns[i]);
}
*err_code = r->query(table_name, column_names, start_time, end_time,
table_result_set,
static_cast<storage::Filter*>(tag_filter), batch_size);
return table_result_set;
}
bool tsfile_result_set_next(ResultSet result_set, ERRNO* err_code) {
auto* r = static_cast<storage::ResultSet*>(result_set);
bool has_next = true;
int ret = common::E_OK;
ret = r->next(has_next);
*err_code = ret;
if (ret != common::E_OK) {
return false;
}
return has_next;
}
ERRNO tsfile_result_set_get_next_tsblock_as_arrow(ResultSet result_set,
ArrowArray* out_array,
ArrowSchema* out_schema) {
if (result_set == nullptr || out_array == nullptr ||
out_schema == nullptr) {
return common::E_INVALID_ARG;
}
auto* r = static_cast<storage::ResultSet*>(result_set);
auto* table_result_set = dynamic_cast<storage::TableResultSet*>(r);
if (table_result_set == nullptr) {
return common::E_INVALID_ARG;
}
common::TsBlock* tsblock = nullptr;
int ret = table_result_set->get_next_tsblock(tsblock);
if (ret != common::E_OK) {
return ret;
}
if (tsblock == nullptr) {
return common::E_NO_MORE_DATA;
}
ret = arrow::TsBlockToArrowStruct(*tsblock, out_array, out_schema);
return ret;
}
#define TSFILE_RESULT_SET_GET_VALUE_BY_NAME_DEF(type) \
type tsfile_result_set_get_value_by_name_##type(ResultSet result_set, \
const char* column_name) { \
auto* r = static_cast<storage::ResultSet*>(result_set); \
std::string column_name_(column_name); \
return r->get_value<type>(column_name_); \
}
TSFILE_RESULT_SET_GET_VALUE_BY_NAME_DEF(bool);
TSFILE_RESULT_SET_GET_VALUE_BY_NAME_DEF(int32_t);
TSFILE_RESULT_SET_GET_VALUE_BY_NAME_DEF(int64_t);
TSFILE_RESULT_SET_GET_VALUE_BY_NAME_DEF(float);
TSFILE_RESULT_SET_GET_VALUE_BY_NAME_DEF(double);
char* tsfile_result_set_get_value_by_name_string(ResultSet result_set,
const char* column_name) {
auto* r = static_cast<storage::ResultSet*>(result_set);
std::string column_name_(column_name);
common::String* ret = r->get_value<common::String*>(column_name_);
// Caller should free return's char* 's space.
char* dup = (char*)malloc(ret->len_ + 1);
if (dup) {
memcpy(dup, ret->buf_, ret->len_);
dup[ret->len_] = '\0';
}
return dup;
}
#define TSFILE_RESULT_SET_GET_VALUE_BY_INDEX_DEF(type) \
type tsfile_result_set_get_value_by_index_##type(ResultSet result_set, \
uint32_t column_index) { \
auto* r = static_cast<storage::ResultSet*>(result_set); \
return r->get_value<type>(column_index); \
}
TSFILE_RESULT_SET_GET_VALUE_BY_INDEX_DEF(int32_t);
TSFILE_RESULT_SET_GET_VALUE_BY_INDEX_DEF(int64_t);
TSFILE_RESULT_SET_GET_VALUE_BY_INDEX_DEF(float);
TSFILE_RESULT_SET_GET_VALUE_BY_INDEX_DEF(double);
TSFILE_RESULT_SET_GET_VALUE_BY_INDEX_DEF(bool);
char* tsfile_result_set_get_value_by_index_string(ResultSet result_set,
uint32_t column_index) {
auto* r = static_cast<storage::ResultSet*>(result_set);
common::String* ret = r->get_value<common::String*>(column_index);
// Caller should free return's char* 's space.
char* dup = (char*)malloc(ret->len_ + 1);
if (dup) {
memcpy(dup, ret->buf_, ret->len_);
dup[ret->len_] = '\0';
}
return dup;
}
bool tsfile_result_set_is_null_by_name(ResultSet result_set,
const char* column_name) {
auto* r = static_cast<storage::ResultSet*>(result_set);
return r->is_null(column_name);
}
bool tsfile_result_set_is_null_by_index(const ResultSet result_set,
const uint32_t column_index) {
auto* r = static_cast<storage::ResultSet*>(result_set);
return r->is_null(column_index);
}
ResultSetMetaData tsfile_result_set_get_metadata(ResultSet result_set) {
auto* r = static_cast<storage::ResultSet*>(result_set);
if (result_set == NULL) {
return ResultSetMetaData();
}
ResultSetMetaData meta_data;
std::shared_ptr<storage::ResultSetMetadata> result_set_metadata =
r->get_metadata();
meta_data.column_num = result_set_metadata->get_column_count();
meta_data.column_names =
static_cast<char**>(malloc(meta_data.column_num * sizeof(char*)));
meta_data.data_types = static_cast<TSDataType*>(
malloc(meta_data.column_num * sizeof(TSDataType)));
for (int i = 0; i < meta_data.column_num; i++) {
meta_data.column_names[i] =
strdup(result_set_metadata->get_column_name(i + 1).c_str());
meta_data.data_types[i] = static_cast<TSDataType>(
result_set_metadata->get_column_type(i + 1));
}
return meta_data;
}
char* tsfile_result_set_metadata_get_column_name(ResultSetMetaData result_set,
uint32_t column_index) {
if (column_index > (uint32_t)result_set.column_num) {
return nullptr;
}
return result_set.column_names[column_index - 1];
}
TSDataType tsfile_result_set_metadata_get_data_type(
ResultSetMetaData result_set, uint32_t column_index) {
if (column_index > (uint32_t)result_set.column_num) {
return TS_DATATYPE_INVALID;
}
return result_set.data_types[column_index - 1];
}
int tsfile_result_set_metadata_get_column_num(ResultSetMetaData result_set) {
return result_set.column_num;
}
TableSchema tsfile_reader_get_table_schema(TsFileReader reader,
const char* table_name) {
auto* r = static_cast<storage::TsFileReader*>(reader);
auto table_shcema = r->get_table_schema(table_name);
TableSchema ret_schema;
ret_schema.table_name = strdup(table_shcema->get_table_name().c_str());
int column_num = table_shcema->get_columns_num();
ret_schema.column_num = column_num;
ret_schema.column_schemas =
static_cast<ColumnSchema*>(malloc(sizeof(ColumnSchema) * column_num));
for (int i = 0; i < column_num; i++) {
auto column_schema = table_shcema->get_measurement_schemas()[i];
ret_schema.column_schemas[i].column_name =
strdup(column_schema->measurement_name_.c_str());
ret_schema.column_schemas[i].data_type =
static_cast<TSDataType>(column_schema->data_type_);
ret_schema.column_schemas[i].column_category =
static_cast<ColumnCategory>(
table_shcema->get_column_categories()[i]);
}
return ret_schema;
}
TableSchema* tsfile_reader_get_all_table_schemas(TsFileReader reader,
uint32_t* size) {
auto* r = static_cast<storage::TsFileReader*>(reader);
auto table_schemas = r->get_all_table_schemas();
size_t table_num = table_schemas.size();
TableSchema* ret =
static_cast<TableSchema*>(malloc(sizeof(TableSchema) * table_num));
for (size_t i = 0; i < table_schemas.size(); i++) {
ret[i].table_name = strdup(table_schemas[i]->get_table_name().c_str());
int column_num = table_schemas[i]->get_columns_num();
ret[i].column_num = column_num;
ret[i].column_schemas = static_cast<ColumnSchema*>(
malloc(column_num * sizeof(ColumnSchema)));
auto column_schemas = table_schemas[i]->get_measurement_schemas();
for (int j = 0; j < column_num; j++) {
ret[i].column_schemas[j].column_name =
strdup(column_schemas[j]->measurement_name_.c_str());
ret[i].column_schemas[j].data_type =
static_cast<TSDataType>(column_schemas[j]->data_type_);
ret[i].column_schemas[j].column_category =
static_cast<ColumnCategory>(
table_schemas[i]->get_column_categories()[j]);
}
}
*size = table_num;
return ret;
}
DeviceSchema* tsfile_reader_get_all_timeseries_schemas(TsFileReader reader,
uint32_t* size) {
auto* r = static_cast<storage::TsFileReader*>(reader);
auto device_ids = r->get_all_device_ids();
if (size == nullptr) {
return nullptr;
}
*size = static_cast<uint32_t>(device_ids.size());
if (device_ids.empty()) {
return nullptr;
}
DeviceSchema* device_schema = static_cast<DeviceSchema*>(
malloc(sizeof(DeviceSchema) * device_ids.size()));
if (device_schema == nullptr) {
*size = 0;
return nullptr;
}
size_t device_index = 0;
for (const auto& device_id : device_ids) {
DeviceSchema& cur_schema = device_schema[device_index++];
std::string device_name =
device_id == nullptr ? "" : device_id->get_device_name();
cur_schema.device_name = strdup(device_name.c_str());
cur_schema.timeseries_num = 0;
cur_schema.timeseries_schema = nullptr;
std::vector<storage::MeasurementSchema> schemas;
int ret = r->get_timeseries_schema(device_id, schemas);
if (ret != common::E_OK || schemas.empty()) {
continue;
}
cur_schema.timeseries_num = static_cast<int>(schemas.size());
cur_schema.timeseries_schema = static_cast<TimeseriesSchema*>(
malloc(sizeof(TimeseriesSchema) * schemas.size()));
for (size_t i = 0; i < schemas.size(); ++i) {
const auto& measurement_schema = schemas[i];
cur_schema.timeseries_schema[i].timeseries_name =
strdup(measurement_schema.measurement_name_.c_str());
cur_schema.timeseries_schema[i].data_type =
static_cast<TSDataType>(measurement_schema.data_type_);
cur_schema.timeseries_schema[i].encoding =
static_cast<TSEncoding>(measurement_schema.encoding_);
cur_schema.timeseries_schema[i].compression =
static_cast<CompressionType>(
measurement_schema.compression_type_);
}
}
return device_schema;
}
void tsfile_device_id_free_contents(DeviceID* d) {
if (d == nullptr) {
return;
}
free(d->path);
d->path = nullptr;
free(d->table_name);
d->table_name = nullptr;
if (d->segments != nullptr) {
for (uint32_t k = 0; k < d->segment_count; k++) {
free(d->segments[k]);
}
free(d->segments);
d->segments = nullptr;
}
d->segment_count = 0;
}
namespace {
char* dup_common_string_to_cstr(const common::String& s) {
if (s.buf_ == nullptr || s.len_ == 0) {
return strdup("");
}
char* p = static_cast<char*>(malloc(static_cast<size_t>(s.len_) + 1U));
if (p == nullptr) {
return nullptr;
}
memcpy(p, s.buf_, static_cast<size_t>(s.len_));
p[s.len_] = '\0';
return p;
}
static TSDataType cpp_stat_type_to_c(common::TSDataType t) {
return static_cast<TSDataType>(static_cast<uint8_t>(t));
}
void free_timeseries_statistic_heap(TimeseriesStatistic* s) {
if (s == nullptr) {
return;
}
TsFileStatisticBase* b = tsfile_statistic_base(s);
if (!b->has_statistic) {
return;
}
switch (b->type) {
case TS_DATATYPE_STRING:
free(s->u.string_s.str_min);
s->u.string_s.str_min = nullptr;
free(s->u.string_s.str_max);
s->u.string_s.str_max = nullptr;
free(s->u.string_s.str_first);
s->u.string_s.str_first = nullptr;
free(s->u.string_s.str_last);
s->u.string_s.str_last = nullptr;
break;
case TS_DATATYPE_TEXT:
free(s->u.text_s.str_first);
s->u.text_s.str_first = nullptr;
free(s->u.text_s.str_last);
s->u.text_s.str_last = nullptr;
break;
default:
break;
}
}
void clear_timeseries_statistic(TimeseriesStatistic* s) {
memset(s, 0, sizeof(*s));
tsfile_statistic_base(s)->type = TS_DATATYPE_INVALID;
}
/**
* Fills @p out from C++ Statistic. On allocation failure returns E_OOM and
* clears/frees any partial string fields in @p out.
*/
int fill_timeseries_statistic(storage::Statistic* st,
TimeseriesStatistic* out) {
clear_timeseries_statistic(out);
if (st == nullptr) {
return common::E_OK;
}
const common::TSDataType t = st->get_type();
switch (t) {
case common::BOOLEAN: {
auto* bs = static_cast<storage::BooleanStatistic*>(st);
TsFileBoolStatistic* p = &out->u.bool_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::BOOLEAN);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->sum = static_cast<double>(bs->sum_value_);
p->first_bool = bs->first_value_;
p->last_bool = bs->last_value_;
break;
}
case common::INT32: {
auto* is = static_cast<storage::Int32Statistic*>(st);
TsFileIntStatistic* p = &out->u.int_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::INT32);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->sum = static_cast<double>(is->sum_value_);
if (p->base.row_count > 0) {
p->min_int64 = static_cast<int64_t>(is->min_value_);
p->max_int64 = static_cast<int64_t>(is->max_value_);
p->first_int64 = static_cast<int64_t>(is->first_value_);
p->last_int64 = static_cast<int64_t>(is->last_value_);
}
break;
}
case common::DATE: {
auto* is = static_cast<storage::Int32Statistic*>(st);
TsFileIntStatistic* p = &out->u.int_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::DATE);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->sum = static_cast<double>(is->sum_value_);
if (p->base.row_count > 0) {
p->min_int64 = static_cast<int64_t>(is->min_value_);
p->max_int64 = static_cast<int64_t>(is->max_value_);
p->first_int64 = static_cast<int64_t>(is->first_value_);
p->last_int64 = static_cast<int64_t>(is->last_value_);
}
break;
}
case common::INT64: {
auto* ls = static_cast<storage::Int64Statistic*>(st);
TsFileIntStatistic* p = &out->u.int_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::INT64);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->sum = ls->sum_value_;
if (p->base.row_count > 0) {
p->min_int64 = ls->min_value_;
p->max_int64 = ls->max_value_;
p->first_int64 = ls->first_value_;
p->last_int64 = ls->last_value_;
}
break;
}
case common::TIMESTAMP: {
auto* ls = static_cast<storage::Int64Statistic*>(st);
TsFileIntStatistic* p = &out->u.int_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::TIMESTAMP);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->sum = ls->sum_value_;
if (p->base.row_count > 0) {
p->min_int64 = ls->min_value_;
p->max_int64 = ls->max_value_;
p->first_int64 = ls->first_value_;
p->last_int64 = ls->last_value_;
}
break;
}
case common::FLOAT: {
auto* fs = static_cast<storage::FloatStatistic*>(st);
TsFileFloatStatistic* p = &out->u.float_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::FLOAT);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->sum = static_cast<double>(fs->sum_value_);
if (p->base.row_count > 0) {
p->min_float64 = static_cast<double>(fs->min_value_);
p->max_float64 = static_cast<double>(fs->max_value_);
p->first_float64 = static_cast<double>(fs->first_value_);
p->last_float64 = static_cast<double>(fs->last_value_);
}
break;
}
case common::DOUBLE: {
auto* ds = static_cast<storage::DoubleStatistic*>(st);
TsFileFloatStatistic* p = &out->u.float_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::DOUBLE);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->sum = ds->sum_value_;
if (p->base.row_count > 0) {
p->min_float64 = ds->min_value_;
p->max_float64 = ds->max_value_;
p->first_float64 = ds->first_value_;
p->last_float64 = ds->last_value_;
}
break;
}
case common::STRING: {
auto* ss = static_cast<storage::StringStatistic*>(st);
TsFileStringStatistic* p = &out->u.string_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::STRING);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->str_min = dup_common_string_to_cstr(ss->min_value_);
if (p->str_min == nullptr) {
free_timeseries_statistic_heap(out);
clear_timeseries_statistic(out);
return common::E_OOM;
}
p->str_max = dup_common_string_to_cstr(ss->max_value_);
if (p->str_max == nullptr) {
free_timeseries_statistic_heap(out);
clear_timeseries_statistic(out);
return common::E_OOM;
}
p->str_first = dup_common_string_to_cstr(ss->first_value_);
if (p->str_first == nullptr) {
free_timeseries_statistic_heap(out);
clear_timeseries_statistic(out);
return common::E_OOM;
}
p->str_last = dup_common_string_to_cstr(ss->last_value_);
if (p->str_last == nullptr) {
free_timeseries_statistic_heap(out);
clear_timeseries_statistic(out);
return common::E_OOM;
}
break;
}
case common::TEXT: {
auto* ts = static_cast<storage::TextStatistic*>(st);
TsFileTextStatistic* p = &out->u.text_s;
p->base.has_statistic = true;
p->base.type = cpp_stat_type_to_c(common::TEXT);
p->base.row_count = st->get_count();
p->base.start_time = st->start_time_;
p->base.end_time = st->get_end_time();
p->str_first = dup_common_string_to_cstr(ts->first_value_);
if (p->str_first == nullptr) {
free_timeseries_statistic_heap(out);
clear_timeseries_statistic(out);
return common::E_OOM;
}
p->str_last = dup_common_string_to_cstr(ts->last_value_);
if (p->str_last == nullptr) {
free_timeseries_statistic_heap(out);
clear_timeseries_statistic(out);
return common::E_OOM;
}
break;
}
default: {
TsFileStatisticBase* b = tsfile_statistic_base(out);
b->has_statistic = true;
b->type = TS_DATATYPE_INVALID;
b->row_count = st->get_count();
b->start_time = st->start_time_;
b->end_time = st->get_end_time();
break;
}
}
return common::E_OK;
}
int fill_timeline_statistic(storage::ITimeseriesIndex* idx,
TimeseriesStatistic* out) {
clear_timeseries_statistic(out);
if (idx == nullptr) {
return common::E_OK;
}
auto* aligned_idx = dynamic_cast<storage::AlignedTimeseriesIndex*>(idx);
if (aligned_idx != nullptr && aligned_idx->time_ts_idx_ != nullptr &&
aligned_idx->time_ts_idx_->get_statistic() != nullptr) {
auto* st = aligned_idx->time_ts_idx_->get_statistic();
TsFileStatisticBase* b = tsfile_statistic_base(out);
b->has_statistic = true;
b->type = TS_DATATYPE_VECTOR;
b->row_count = st->get_count();
b->start_time = st->start_time_;
b->end_time = st->get_end_time();
return common::E_OK;
}
if (idx->get_statistic() != nullptr &&
idx->get_time_chunk_meta_list() == nullptr) {
auto* st = idx->get_statistic();
TsFileStatisticBase* b = tsfile_statistic_base(out);
b->has_statistic = true;
b->type = TS_DATATYPE_VECTOR;
b->row_count = st->get_count();
b->start_time = st->start_time_;
b->end_time = st->get_end_time();
return common::E_OK;
}