-
Notifications
You must be signed in to change notification settings - Fork 18
/
libtiledb.cpp
5642 lines (5245 loc) · 215 KB
/
libtiledb.cpp
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
// MIT License
//
// Copyright (c) 2017-2024 TileDB Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "libtiledb.h"
#include "tiledb_version.h"
#include <fstream>
#include <unistd.h>
using namespace Rcpp;
const char* _tiledb_datatype_to_string(tiledb_datatype_t dtype) {
switch (dtype) {
case TILEDB_INT8:
return "INT8";
case TILEDB_UINT8:
return "UINT8";
case TILEDB_INT16:
return "INT16";
case TILEDB_UINT16:
return "UINT16";
case TILEDB_INT32:
return "INT32";
case TILEDB_UINT32:
return "UINT32";
case TILEDB_INT64:
return "INT64";
case TILEDB_UINT64:
return "UINT64";
case TILEDB_FLOAT32:
return "FLOAT32";
case TILEDB_FLOAT64:
return "FLOAT64";
case TILEDB_CHAR:
return "CHAR";
case TILEDB_STRING_ASCII:
return "ASCII";
case TILEDB_STRING_UTF8:
return "UTF8";
case TILEDB_STRING_UTF16:
return "UTF16";
case TILEDB_STRING_UTF32:
return "UTF32";
case TILEDB_STRING_UCS2:
return "UCS2";
case TILEDB_STRING_UCS4:
return "UCS4";
case TILEDB_ANY:
return "ANY";
case TILEDB_DATETIME_YEAR:
return "DATETIME_YEAR";
case TILEDB_DATETIME_MONTH:
return "DATETIME_MONTH";
case TILEDB_DATETIME_WEEK:
return "DATETIME_WEEK";
case TILEDB_DATETIME_DAY:
return "DATETIME_DAY";
case TILEDB_DATETIME_HR:
return "DATETIME_HR";
case TILEDB_DATETIME_MIN:
return "DATETIME_MIN";
case TILEDB_DATETIME_SEC:
return "DATETIME_SEC";
case TILEDB_DATETIME_MS:
return "DATETIME_MS";
case TILEDB_DATETIME_US:
return "DATETIME_US";
case TILEDB_DATETIME_NS:
return "DATETIME_NS";
case TILEDB_DATETIME_PS:
return "DATETIME_PS";
case TILEDB_DATETIME_FS:
return "DATETIME_FS";
case TILEDB_DATETIME_AS:
return "DATETIME_AS";
case TILEDB_BLOB:
return "BLOB";
#if TILEDB_VERSION >= TileDB_Version(2,10,0)
case TILEDB_BOOL:
return "BOOL";
#endif
#if TILEDB_VERSION >= TileDB_Version(2,21,0)
case TILEDB_GEOM_WKB:
return "GEOM_WKB";
case TILEDB_GEOM_WKT:
return "GEOM_WKT";
#endif
default:
Rcpp::stop("unknown tiledb_datatype_t (%d)", dtype);
}
}
tiledb_datatype_t _string_to_tiledb_datatype(std::string typestr) {
if (typestr == "FLOAT32") {
return TILEDB_FLOAT32;
} else if (typestr == "FLOAT64") {
return TILEDB_FLOAT64;
} else if (typestr == "ASCII") {
return TILEDB_STRING_ASCII;
} else if (typestr == "CHAR") {
return TILEDB_CHAR;
} else if (typestr == "INT8") {
return TILEDB_INT8;
} else if (typestr == "UINT8") {
return TILEDB_UINT8;
} else if (typestr == "INT16") {
return TILEDB_INT16;
} else if (typestr == "UINT16") {
return TILEDB_UINT16;
} else if (typestr == "INT32") {
return TILEDB_INT32;
} else if (typestr == "UINT32") {
return TILEDB_UINT32;
} else if (typestr == "INT64") {
return TILEDB_INT64;
} else if (typestr == "UINT64") {
return TILEDB_UINT64;
} else if (typestr == "DATETIME_YEAR") {
return TILEDB_DATETIME_YEAR;
} else if (typestr == "DATETIME_MONTH") {
return TILEDB_DATETIME_MONTH;
} else if (typestr == "DATETIME_WEEK") {
return TILEDB_DATETIME_WEEK;
} else if (typestr == "DATETIME_DAY") {
return TILEDB_DATETIME_DAY;
} else if (typestr == "DATETIME_HR") {
return TILEDB_DATETIME_HR;
} else if (typestr == "DATETIME_MIN") {
return TILEDB_DATETIME_MIN;
} else if (typestr == "DATETIME_SEC") {
return TILEDB_DATETIME_SEC;
} else if (typestr == "DATETIME_MS") {
return TILEDB_DATETIME_MS;
} else if (typestr == "DATETIME_US") {
return TILEDB_DATETIME_US;
} else if (typestr == "DATETIME_NS") {
return TILEDB_DATETIME_NS;
} else if (typestr == "DATETIME_PS") {
return TILEDB_DATETIME_PS;
} else if (typestr == "DATETIME_FS") {
return TILEDB_DATETIME_FS;
} else if (typestr == "DATETIME_AS") {
return TILEDB_DATETIME_AS;
} else if (typestr == "UTF8") {
return TILEDB_STRING_UTF8;
} else if (typestr == "BLOB") {
return TILEDB_BLOB;
#if TILEDB_VERSION >= TileDB_Version(2,10,0)
} else if (typestr == "BOOL") {
return TILEDB_BOOL;
#endif
#if TILEDB_VERSION >= TileDB_Version(2,21,0)
} else if (typestr == "GEOM_WKB") {
return TILEDB_GEOM_WKB;
} else if (typestr == "GEOM_WKT") {
return TILEDB_GEOM_WKT;
#endif
} else {
Rcpp::stop("Unknown TileDB type '%s'", typestr.c_str());
}
}
// [[Rcpp::export]]
int32_t tiledb_datatype_string_to_sizeof(const std::string str) {
return static_cast<int32_t>(tiledb_datatype_size(_string_to_tiledb_datatype(str)));
}
//' Map from TileDB type to R datatype
//'
//' This function maps from the TileDB types to the (fewer) key datatypes in R. This
//' can be lossy as TileDB integers range from (signed and unsigned) 8 to 64 bit whereas
//' R only has (signed) 32 bit values. Similarly, R only has 64 bit doubles whereas TileDB
//' has 32 and 64 bit floating point types. TileDB also has more character encodings, and the
//' full range of (NumPy) date and time types.
//'
//' @param datatype A string describing one TileDB datatype
//' @return A string describing the closest match for an R datatype
//' @export
// [[Rcpp::export]]
std::string tiledb_datatype_R_type(std::string datatype) {
tiledb_datatype_t dtype = _string_to_tiledb_datatype(datatype);
switch (dtype) {
case TILEDB_INT8:
case TILEDB_UINT8:
case TILEDB_INT16:
case TILEDB_UINT16:
case TILEDB_INT32:
case TILEDB_UINT32:
case TILEDB_INT64:
case TILEDB_UINT64:
return "integer";
case TILEDB_FLOAT32:
case TILEDB_FLOAT64:
return "double";
case TILEDB_CHAR:
return "raw";
case TILEDB_STRING_ASCII:
case TILEDB_STRING_UTF8:
case TILEDB_STRING_UTF16:
case TILEDB_STRING_UTF32:
case TILEDB_STRING_UCS2:
case TILEDB_STRING_UCS4:
return "character";
case TILEDB_ANY:
return "any";
case TILEDB_DATETIME_DAY:
return "DATETIME_DAY";
case TILEDB_DATETIME_SEC:
return "DATETIME_SEC";
case TILEDB_DATETIME_MS:
return "DATETIME_MS";
case TILEDB_DATETIME_US:
return "DATETIME_US";
case TILEDB_DATETIME_NS:
return "DATETIME_NS";
#if TILEDB_VERSION >= TileDB_Version(2,10,0)
case TILEDB_BOOL:
return "BOOL";
#endif
default:
Rcpp::stop("unknown tiledb_datatype_t (%d)", dtype);
}
}
const char* _tiledb_layout_to_string(tiledb_layout_t layout) {
switch(layout) {
case TILEDB_ROW_MAJOR:
return "ROW_MAJOR";
case TILEDB_COL_MAJOR:
return "COL_MAJOR";
case TILEDB_GLOBAL_ORDER:
return "GLOBAL_ORDER";
case TILEDB_UNORDERED:
return "UNORDERED";
case TILEDB_HILBERT:
return "HILBERT";
default:
Rcpp::stop("unknown tiledb_layout_t (%d)", layout);
}
}
tiledb_layout_t _string_to_tiledb_layout(std::string lstr) {
if (lstr == "ROW_MAJOR") {
return TILEDB_ROW_MAJOR;
} else if (lstr == "COL_MAJOR") {
return TILEDB_COL_MAJOR;
} else if (lstr == "GLOBAL_ORDER") {
return TILEDB_GLOBAL_ORDER;
} else if (lstr == "UNORDERED") {
return TILEDB_UNORDERED;
} else if (lstr == "HILBERT") {
return TILEDB_HILBERT;
} else {
Rcpp::stop("Unknown TileDB layout '%s' ", lstr.c_str());
}
}
tiledb_filter_type_t _string_to_tiledb_filter(std::string filter) {
if (filter == "NONE") {
return TILEDB_FILTER_NONE;
} else if (filter == "GZIP") {
return TILEDB_FILTER_GZIP;
} else if (filter == "ZSTD") {
return TILEDB_FILTER_ZSTD;
} else if (filter == "LZ4") {
return TILEDB_FILTER_LZ4;
} else if (filter == "RLE") {
return TILEDB_FILTER_RLE;
} else if (filter == "BZIP2") {
return TILEDB_FILTER_BZIP2;
} else if (filter == "DOUBLE_DELTA") {
return TILEDB_FILTER_DOUBLE_DELTA;
} else if (filter == "BIT_WIDTH_REDUCTION") {
return TILEDB_FILTER_BIT_WIDTH_REDUCTION;
} else if (filter == "BITSHUFFLE") {
return TILEDB_FILTER_BITSHUFFLE;
} else if (filter == "BYTESHUFFLE") {
return TILEDB_FILTER_BYTESHUFFLE;
} else if (filter == "POSITIVE_DELTA") {
return TILEDB_FILTER_POSITIVE_DELTA;
} else if (filter == "CHECKSUM_MD5") {
return TILEDB_FILTER_CHECKSUM_MD5;
} else if (filter == "CHECKSUM_SHA256") {
return TILEDB_FILTER_CHECKSUM_SHA256;
#if TILEDB_VERSION >= TileDB_Version(2,9,0)
} else if (filter == "DICTIONARY_ENCODING") {
return TILEDB_FILTER_DICTIONARY;
#endif
#if TILEDB_VERSION >= TileDB_Version(2,11,0)
} else if (filter == "SCALE_FLOAT") {
return TILEDB_FILTER_SCALE_FLOAT;
#endif
#if TILEDB_VERSION >= TileDB_Version(2,12,0)
} else if (filter == "FILTER_XOR") {
return TILEDB_FILTER_XOR;
#endif
} else {
Rcpp::stop("Unknown TileDB filter '%s'", filter.c_str());
}
}
const char* _tiledb_filter_to_string(tiledb_filter_type_t filter) {
switch(filter) {
case TILEDB_FILTER_NONE:
return "NONE";
case TILEDB_FILTER_GZIP:
return "GZIP";
case TILEDB_FILTER_ZSTD:
return "ZSTD";
case TILEDB_FILTER_LZ4:
return "LZ4";
case TILEDB_FILTER_RLE:
return "RLE";
case TILEDB_FILTER_BZIP2:
return "BZIP2";
case TILEDB_FILTER_DOUBLE_DELTA:
return "DOUBLE_DELTA";
case TILEDB_FILTER_BIT_WIDTH_REDUCTION:
return "BIT_WIDTH_REDUCTION";
case TILEDB_FILTER_BITSHUFFLE:
return "BITSHUFFLE";
case TILEDB_FILTER_BYTESHUFFLE:
return "BYTESHUFFLE";
case TILEDB_FILTER_POSITIVE_DELTA:
return "POSITIVE_DELTA";
case TILEDB_FILTER_CHECKSUM_MD5:
return "CHECKSUM_MD5";
case TILEDB_FILTER_CHECKSUM_SHA256:
return "CHECKSUM_SHA256";
#if TILEDB_VERSION >= TileDB_Version(2,9,0)
case TILEDB_FILTER_DICTIONARY:
return "DICTIONARY_ENCODING";
#endif
#if TILEDB_VERSION >= TileDB_Version(2,11,0)
case TILEDB_FILTER_SCALE_FLOAT:
return "SCALE_FLOAT";
#endif
#if TILEDB_VERSION >= TileDB_Version(2,12,0)
case TILEDB_FILTER_XOR:
return "FILTER_XOR";
#endif
default: {
Rcpp::stop("unknown tiledb_filter_t (%d)", filter);
}
}
}
tiledb_filter_option_t _string_to_tiledb_filter_option(std::string filter_option) {
if (filter_option == "COMPRESSION_LEVEL") {
return TILEDB_COMPRESSION_LEVEL;
} else if (filter_option == "BIT_WIDTH_MAX_WINDOW") {
return TILEDB_BIT_WIDTH_MAX_WINDOW;
} else if (filter_option == "POSITIVE_DELTA_MAX_WINDOW") {
return TILEDB_POSITIVE_DELTA_MAX_WINDOW;
#if TILEDB_VERSION >= TileDB_Version(2,11,0)
} else if (filter_option == "SCALE_FLOAT_BYTEWIDTH") {
return TILEDB_SCALE_FLOAT_BYTEWIDTH;
} else if (filter_option == "SCALE_FLOAT_FACTOR") {
return TILEDB_SCALE_FLOAT_FACTOR;
} else if (filter_option == "SCALE_FLOAT_OFFSET") {
return TILEDB_SCALE_FLOAT_OFFSET;
#endif
} else {
Rcpp::stop("Unknown TileDB filter option '%s'", filter_option.c_str());
}
}
const char* _tiledb_filter_option_to_string(tiledb_filter_option_t filter_option) {
switch(filter_option) {
case TILEDB_COMPRESSION_LEVEL:
return "COMPRESSION_LEVEL";
case TILEDB_BIT_WIDTH_MAX_WINDOW:
return "BIT_WIDTH_MAX_WINDOW";
case TILEDB_POSITIVE_DELTA_MAX_WINDOW:
return "POSITIVE_DELTA_MAX_WINDOW";
#if TILEDB_VERSION >= TileDB_Version(2,11,0)
case TILEDB_SCALE_FLOAT_BYTEWIDTH:
return "SCALE_FLOAT_BYTEWIDTH";
case TILEDB_SCALE_FLOAT_FACTOR:
return "SCALE_FLOAT_FACTOR";
case TILEDB_SCALE_FLOAT_OFFSET:
return "SCALE_FLOAT_OFFSET";
#endif
default:
Rcpp::stop("unknown tiledb_filter_option_t (%d)", filter_option);
}
}
tiledb_query_type_t _string_to_tiledb_query_type(std::string qtstr) {
if (qtstr == "READ") {
return TILEDB_READ;
} else if (qtstr == "WRITE") {
return TILEDB_WRITE;
#if TILEDB_VERSION >= TileDB_Version(2,12,0)
} else if (qtstr == "MODIFY_EXCLUSIVE") {
return TILEDB_MODIFY_EXCLUSIVE;
} else if (qtstr == "DELETE") {
return TILEDB_DELETE;
#endif
} else {
Rcpp::stop("Unknown TileDB query type '%s'", qtstr.c_str());
}
}
std::string _tiledb_query_type_to_string(tiledb_query_type_t qtype) {
switch (qtype) {
case TILEDB_READ:
return "READ";
case TILEDB_WRITE:
return "WRITE";
#if TILEDB_VERSION >= TileDB_Version(2,12,0)
case TILEDB_DELETE:
return "DELETE";
case TILEDB_MODIFY_EXCLUSIVE:
return "MODIFY_EXCLUSIVE";
#endif
default:
Rcpp::stop("unknown tiledb_query_type_t (%d)", qtype);
}
}
const char* _tiledb_array_type_to_string(tiledb_array_type_t atype) {
switch (atype) {
case TILEDB_DENSE:
return "dense";
case TILEDB_SPARSE:
return "sparse";
default:
Rcpp::stop("Unknown tiledb_array_type_t");
}
}
tiledb_array_type_t _string_to_tiledb_array_type(std::string tpstr) {
if (tpstr == "DENSE") {
return TILEDB_DENSE;
} else if (tpstr == "SPARSE") {
return TILEDB_SPARSE;
} else {
Rcpp::stop("Unknown tiledb_array_type_t");
}
}
tiledb_vfs_mode_t _string_to_tiledb_vfs_mode_t(std::string modestr) {
if (modestr == "READ") {
return TILEDB_VFS_READ;
} else if (modestr == "WRITE") {
return TILEDB_VFS_WRITE;
} else if (modestr == "APPEND") {
return TILEDB_VFS_APPEND;
} else {
Rcpp::stop("Unknown TileDB VFS mode type '%s'", modestr.c_str());
}
}
#if TILEDB_VERSION >= TileDB_Version(2,25,0)
std::string _tiledb_current_domain_type_to_string(tiledb_current_domain_type_t type) {
if (type == TILEDB_NDRECTANGLE) {
return std::string{"NDRECTANGLE"};
} else {
Rcpp::stop("Unknown TileDB CurrentDomain type (%d)", (int32_t) type);
}
return std::string();
}
#endif
#if TILEDB_VERSION >= TileDB_Version(2,25,0)
tiledb_current_domain_type_t _string_to_tiledb_current_domain_type(std::string typestr) {
if (typestr == "NDRECTANGLE") {
return TILEDB_NDRECTANGLE;
} else {
Rcpp::stop("Unknown TileDB CurrentDomain type '%s'", typestr.c_str());
}
}
#endif
// NB Limited type coverage here as aimed to sizing R allocations of either int, double or char
// Also note that there is 'inline size_t type_size(tiledb_datatype_t type)' in core_interface.h
const size_t _tiledb_datatype_sizeof(const tiledb_datatype_t dtype) {
switch(dtype) {
case TILEDB_FLOAT64:
return sizeof(double);
case TILEDB_INT32:
return sizeof(int32_t);
case TILEDB_CHAR:
return sizeof(char);
default:
Rcpp::stop("Unsupported tiledb_datatype_t '%s'", _tiledb_datatype_to_string(dtype));
}
}
// [[Rcpp::export]]
NumericVector libtiledb_version() {
auto ver = tiledb::version();
return NumericVector::create(Named("major") = std::get<0>(ver),
Named("minor") = std::get<1>(ver),
Named("patch") = std::get<2>(ver));
}
// [[Rcpp::export]]
size_t tiledb_datatype_max_value(const std::string& datatype) {
tiledb_datatype_t dtype = _string_to_tiledb_datatype(datatype);
switch (dtype) {
case TILEDB_INT8: return std::numeric_limits<int8_t>::max();
case TILEDB_UINT8: return std::numeric_limits<uint8_t>::max();
case TILEDB_INT16: return std::numeric_limits<int16_t>::max();
case TILEDB_UINT16: return std::numeric_limits<uint16_t>::max();
case TILEDB_INT32: return std::numeric_limits<int32_t>::max();
case TILEDB_UINT32: return std::numeric_limits<uint32_t>::max();
case TILEDB_INT64: return std::numeric_limits<int64_t>::max();
case TILEDB_UINT64: return std::numeric_limits<uint64_t>::max();
default: Rcpp::stop("currently unsupported datatype (%s)", datatype);
}
}
/**
* TileDB Context
*/
// [[Rcpp::export]]
XPtr<tiledb::Context> libtiledb_ctx(Nullable<XPtr<tiledb::Config>> config = R_NilValue) {
if (config.isNull()) {
return make_xptr<tiledb::Context>(new tiledb::Context());
} else {
XPtr<tiledb::Config> config_xptr(config);
return make_xptr<tiledb::Context>(new tiledb::Context(*config_xptr.get()));
}
}
// [[Rcpp::export]]
XPtr<tiledb::Config> libtiledb_ctx_config(XPtr<tiledb::Context> ctx) {
check_xptr_tag<tiledb::Context>(ctx);
return make_xptr<tiledb::Config>(new tiledb::Config(ctx.get()->config()));
}
// [[Rcpp::export]]
bool libtiledb_ctx_is_supported_fs(XPtr<tiledb::Context> ctx, std::string scheme) {
check_xptr_tag<tiledb::Context>(ctx);
if (scheme == "file") {
return true;
} else if (scheme == "s3") {
return ctx->is_supported_fs(TILEDB_S3);
} else if (scheme == "hdfs") {
return ctx->is_supported_fs(TILEDB_HDFS);
} else if (scheme == "azure") {
return ctx->is_supported_fs(TILEDB_AZURE);
} else if (scheme == "gcs") {
return ctx->is_supported_fs(TILEDB_GCS);
} else if (scheme == "memory") {
return ctx->is_supported_fs(TILEDB_MEMFS);
} else {
Rcpp::stop("Unknown TileDB fs scheme: '%s'", scheme.c_str());
}
}
// [[Rcpp::export]]
void libtiledb_ctx_set_tag(XPtr<tiledb::Context> ctx, std::string key, std::string value) {
check_xptr_tag<tiledb::Context>(ctx);
ctx->set_tag(key, value);
}
// [[Rcpp::export]]
std::string libtiledb_ctx_stats(XPtr<tiledb::Context> ctx) {
check_xptr_tag<tiledb::Context>(ctx);
return ctx->stats();
}
/**
* TileDB Config
*/
// [[Rcpp::export]]
XPtr<tiledb::Config> libtiledb_config(Nullable<CharacterVector> config = R_NilValue) {
XPtr<tiledb::Config> ptr = make_xptr<tiledb::Config>(new tiledb::Config());
if (config.isNotNull()) {
auto config_vec = config.as();
auto config_names = as<CharacterVector>(config_vec.names());
for (auto &name : config_names) {
auto param = as<std::string>(name);
auto value = as<std::string>(config_vec[param]);
ptr->set(param, value);
}
}
return ptr;
}
// [[Rcpp::export]]
std::string libtiledb_config_save_to_file(XPtr<tiledb::Config> config, std::string filename) {
check_xptr_tag<tiledb::Config>(config);
config->save_to_file(filename);
return filename;
}
// [[Rcpp::export]]
XPtr<tiledb::Config> libtiledb_config_load_from_file(std::string filename) {
XPtr<tiledb::Config> ptr = make_xptr<tiledb::Config>(new tiledb::Config(filename));
return ptr;
}
// [[Rcpp::export]]
CharacterVector libtiledb_config_vector(XPtr<tiledb::Config> config) {
check_xptr_tag<tiledb::Config>(config);
CharacterVector config_vec;
for (auto& p : *config) {
config_vec[p.first] = p.second;
}
return config_vec;
}
// [[Rcpp::export]]
XPtr<tiledb::Config> libtiledb_config_set(XPtr<tiledb::Config> config,
std::string param, std::string value) {
check_xptr_tag<tiledb::Config>(config);
(*config)[param] = value;
return config;
}
// [[Rcpp::export]]
CharacterVector libtiledb_config_get(XPtr<tiledb::Config> config, CharacterVector params) {
check_xptr_tag<tiledb::Config>(config);
CharacterVector result;
for (auto const& p : params) {
auto param = as<std::string>(p);
try {
result.push_back(config->get(param), param);
} catch (std::exception& err) {
result.push_back(NA_STRING, as<std::string>(NA_STRING));
}
}
return result;
}
// [[Rcpp::export]]
XPtr<tiledb::Config> libtiledb_config_unset(XPtr<tiledb::Config> config, std::string param) {
check_xptr_tag<tiledb::Config>(config);
config->unset(param);
return config;
}
// [[Rcpp::export]]
void libtiledb_config_dump(XPtr<tiledb::Config> config) {
check_xptr_tag<tiledb::Config>(config);
Rcout << "Config settings:\n";
for (auto& p : *config) {
Rcout << "\"" << p.first << "\" : \"" << p.second << "\"\n";
}
}
// Placed here as it relates to config. (New as of 2.17, uses experimental header)
// [[Rcpp::export]]
std::string libtiledb_as_built_dump() {
std::string str = "";
#if TILEDB_VERSION >= TileDB_Version(2,17,0)
str = tiledb::AsBuilt::dump();
#endif
return str;
}
/**
* TileDB Dimension
*/
// [[Rcpp::export]]
XPtr<tiledb::Dimension> libtiledb_dim(XPtr<tiledb::Context> ctx,
std::string name,
std::string type,
SEXP domain,
SEXP tile_extent) {
check_xptr_tag<tiledb::Context>(ctx);
// check that the dimension type is supported
const tiledb_datatype_t dtype = _string_to_tiledb_datatype(type);
if (dtype != TILEDB_INT8 &&
dtype != TILEDB_INT16 &&
dtype != TILEDB_INT32 &&
dtype != TILEDB_INT64 &&
dtype != TILEDB_UINT8 &&
dtype != TILEDB_UINT16 &&
dtype != TILEDB_UINT32 &&
dtype != TILEDB_UINT64 &&
dtype != TILEDB_FLOAT32 &&
dtype != TILEDB_FLOAT64 &&
dtype != TILEDB_DATETIME_YEAR &&
dtype != TILEDB_DATETIME_MONTH &&
dtype != TILEDB_DATETIME_WEEK &&
dtype != TILEDB_DATETIME_DAY &&
dtype != TILEDB_DATETIME_HR &&
dtype != TILEDB_DATETIME_MIN &&
dtype != TILEDB_DATETIME_SEC &&
dtype != TILEDB_DATETIME_MS &&
dtype != TILEDB_DATETIME_US &&
dtype != TILEDB_DATETIME_NS &&
dtype != TILEDB_DATETIME_PS &&
dtype != TILEDB_DATETIME_FS &&
dtype != TILEDB_DATETIME_AS &&
dtype != TILEDB_STRING_ASCII) {
Rcpp::stop("only integer ((U)INT{8,16,32,64}), real (FLOAT{32,64}), DATETIME_{YEAR,MONTH,WEEK,DAY,HR,MIN,SEC,MS,US,NS,PS,FS,AS}, STRING_ASCII domains supported");
}
// check that the dimension type aligns with the domain and tiledb_extent type
if (dtype == TILEDB_INT32 && (TYPEOF(domain) != INTSXP || TYPEOF(tile_extent) != INTSXP)) {
Rcpp::stop("domain or tile_extent does not match dimension type");
} else if (dtype == TILEDB_FLOAT64 && (TYPEOF(domain) != REALSXP || TYPEOF(tile_extent) != REALSXP)) {
Rcpp::stop("domain or tile_extent does not match dimension type");
}
if (dtype == TILEDB_INT32) {
Rcpp::IntegerVector domain_vec = Rcpp::IntegerVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<int32_t, 2> _domain = {domain_vec[0], domain_vec[1]};
int32_t _tile_extent = Rcpp::as<int32_t>(tile_extent);
auto dim = new tiledb::Dimension(tiledb::Dimension::create<int32_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_UINT32) {
Rcpp::IntegerVector domain_vec = Rcpp::IntegerVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<uint32_t, 2> _domain = {static_cast<uint32_t>(domain_vec[0]), static_cast<uint32_t>(domain_vec[1])};
uint32_t _tile_extent = Rcpp::as<uint32_t>(tile_extent);
auto dim = new tiledb::Dimension(tiledb::Dimension::create<uint32_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_INT16) {
Rcpp::IntegerVector domain_vec = Rcpp::IntegerVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<int16_t, 2> _domain = {static_cast<int16_t>(domain_vec[0]), static_cast<int16_t>(domain_vec[1])};
int16_t _tile_extent = Rcpp::as<int16_t>(tile_extent);
auto dim = new tiledb::Dimension(tiledb::Dimension::create<int16_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_UINT16) {
Rcpp::IntegerVector domain_vec = Rcpp::IntegerVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<uint16_t, 2> _domain = {static_cast<uint16_t>(domain_vec[0]), static_cast<uint16_t>(domain_vec[1])};
int16_t _tile_extent = Rcpp::as<int16_t>(tile_extent);
auto dim = new tiledb::Dimension(tiledb::Dimension::create<uint16_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_INT8) {
Rcpp::IntegerVector domain_vec = Rcpp::IntegerVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<int8_t, 2> _domain = {static_cast<int8_t>(domain_vec[0]), static_cast<int8_t>(domain_vec[1])};
int8_t _tile_extent = static_cast<int8_t>(Rcpp::as<int16_t>(tile_extent));
auto dim = new tiledb::Dimension(tiledb::Dimension::create<int8_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_UINT8) {
Rcpp::IntegerVector domain_vec = Rcpp::IntegerVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<uint8_t, 2> _domain = {static_cast<uint8_t>(domain_vec[0]), static_cast<uint8_t>(domain_vec[1])};
uint8_t _tile_extent = Rcpp::as<uint8_t>(tile_extent);
auto dim = new tiledb::Dimension(tiledb::Dimension::create<uint8_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_INT64) {
// for int64 domains and extents we require integer64 types which are internally memmap'ed from double
Rcpp::NumericVector dv(domain);
if (!isInteger64(dv)) {
Rcpp::stop("dimension domain for INT64 must be an integer64 type in R");
}
if (dv.size() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::vector<int64_t> domain_vec = fromInteger64(Rcpp::NumericVector(dv));
std::array<int64_t, 2> _domain = {domain_vec[0], domain_vec[1]};
Rcpp::NumericVector ext(tile_extent);
if (!isInteger64(ext)) {
Rcpp::stop("tile exent for INT64 domain must be an integer64 type in R");
}
int64_t _tile_extent = fromInteger64(ext[0]);
auto dim = new tiledb::Dimension(tiledb::Dimension::create<int64_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_UINT64) {
// for uint64 domains and extents we require integer64 types which are internally memmap'ed from double
Rcpp::NumericVector dv(domain);
if (!isInteger64(dv)) {
Rcpp::stop("dimension domain for UINT64 must be an integer64 type in R");
}
if (dv.size() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::vector<int64_t> domain_vec = fromInteger64(Rcpp::NumericVector(dv));
std::array<uint64_t, 2> _domain = { static_cast<uint64_t>(domain_vec[0]), static_cast<uint64_t>(domain_vec[1])};
Rcpp::NumericVector ext(tile_extent);
if (!isInteger64(ext)) {
Rcpp::stop("tile exent for UINT64 domain must be an integer64 type in R");
}
uint64_t _tile_extent = static_cast<uint64_t>(fromInteger64(ext[0]));
auto dim = new tiledb::Dimension(tiledb::Dimension::create<uint64_t>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_FLOAT32) {
Rcpp::NumericVector domain_vec = Rcpp::NumericVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<float, 2> _domain = {static_cast<float>(domain_vec[0]), static_cast<float>(domain_vec[1])};
float_t _tile_extent = Rcpp::as<float>(tile_extent);
auto d = new tiledb::Dimension(tiledb::Dimension::create<float>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(d);
return ptr;
} else if (dtype == TILEDB_FLOAT64) {
Rcpp::NumericVector domain_vec = Rcpp::NumericVector(domain);
if (domain_vec.length() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
std::array<double, 2> _domain = {static_cast<double>(domain_vec[0]), static_cast<double>(domain_vec[1])};
double_t _tile_extent = Rcpp::as<double>(tile_extent);
auto d = new tiledb::Dimension(tiledb::Dimension::create<double>(*ctx.get(), name, _domain, _tile_extent));
auto ptr = make_xptr<tiledb::Dimension>(d);
return ptr;
} else if (dtype == TILEDB_DATETIME_YEAR ||
dtype == TILEDB_DATETIME_MONTH ||
dtype == TILEDB_DATETIME_WEEK ||
dtype == TILEDB_DATETIME_DAY ||
dtype == TILEDB_DATETIME_HR ||
dtype == TILEDB_DATETIME_MIN ||
dtype == TILEDB_DATETIME_SEC ||
dtype == TILEDB_DATETIME_MS ||
dtype == TILEDB_DATETIME_US ||
dtype == TILEDB_DATETIME_NS ||
dtype == TILEDB_DATETIME_PS ||
dtype == TILEDB_DATETIME_FS ||
dtype == TILEDB_DATETIME_AS ) {
auto domain_vec = as<std::vector<int64_t>>(domain);
if (domain_vec.size() != 2) {
Rcpp::stop("dimension domain must be a c(lower bound, upper bound) pair");
}
int64_t domain[] = {domain_vec[0], domain_vec[1]};
int64_t extent = Rcpp::as<int64_t>(tile_extent);
auto dim = new tiledb::Dimension(tiledb::Dimension::create(*ctx.get(), name, dtype, domain, &extent));
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else if (dtype == TILEDB_STRING_ASCII) {
if (Rf_isNull(domain) && Rf_isNull(tile_extent)) {
auto d = tiledb::Dimension::create(*ctx.get(), name, dtype, nullptr, nullptr);
auto dim = new tiledb::Dimension(d);
auto ptr = make_xptr<tiledb::Dimension>(dim);
return ptr;
} else {
Rcpp::stop("Non-null domain or extent to be added.");
}
} else {
Rcpp::stop("Unsupported tiledb type (%d) this should not happen!", dtype);
}
}
// [[Rcpp::export]]
std::string libtiledb_dim_get_name(XPtr<tiledb::Dimension> dim) {
check_xptr_tag<tiledb::Dimension>(dim);
return dim->name();
}
// [[Rcpp::export]]
SEXP libtiledb_dim_get_domain(XPtr<tiledb::Dimension> dim) {
check_xptr_tag<tiledb::Dimension>(dim);
auto dim_type = dim->type();
switch (dim_type) {
case TILEDB_FLOAT32: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_FLOAT32>::type;
return NumericVector({dim->domain<DataType>().first,
dim->domain<DataType>().second});
}
case TILEDB_FLOAT64: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_FLOAT64>::type;
auto d1 = dim->domain<DataType>().first;
auto d2 = dim->domain<DataType>().second;
if (d1 == R_NaReal || d2 == R_NaReal) {
Rcpp::stop("tiledb_dim domain FLOAT64 value not representable as an R double");
}
return NumericVector({d1, d2});
}
case TILEDB_INT8: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_INT8>::type;
return IntegerVector({dim->domain<DataType>().first,
dim->domain<DataType>().second});
}
case TILEDB_UINT8: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_UINT8>::type;
return IntegerVector({dim->domain<DataType>().first,
dim->domain<DataType>().second});
}
case TILEDB_INT16: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_INT16>::type;
return IntegerVector({dim->domain<DataType>().first,
dim->domain<DataType>().second});
}
case TILEDB_UINT16: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_INT16>::type;
return IntegerVector({dim->domain<DataType>().first,
dim->domain<DataType>().second});
}
case TILEDB_INT32: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_INT32>::type;
auto d1 = dim->domain<DataType>().first;
auto d2 = dim->domain<DataType>().second;
if (d1 == R_NaInt || d2 == R_NaInt) {
Rcpp::stop("tiledb_dim domain INT32 value not representable as an R integer");
}
return IntegerVector({d1, d2});
}
case TILEDB_UINT32: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_UINT32>::type;
auto d1 = dim->domain<DataType>().first;
auto d2 = dim->domain<DataType>().second;
if (d1 > std::numeric_limits<uint32_t>::max() ||
d2 > std::numeric_limits<uint32_t>::max()) {
Rcpp::stop("tiledb_dim domain UINT32 value not representable as an R integer64 type");
}
std::vector<int64_t> v = { static_cast<int64_t>(d1), static_cast<int64_t>(d2) };
return toInteger64(v);
}
case TILEDB_INT64: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_INT64>::type;
auto d1 = dim->domain<DataType>().first;
auto d2 = dim->domain<DataType>().second;
if (d1 > std::numeric_limits<int64_t>::max() || d2 > std::numeric_limits<int64_t>::max()) {
return NumericVector({static_cast<double>(d1), static_cast<double>(d2)});
}
std::vector<int64_t> v = { d1, d2 };
return toInteger64(v);
}
case TILEDB_UINT64: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_UINT64>::type;
auto d1 = dim->domain<DataType>().first;
auto d2 = dim->domain<DataType>().second;
if (d1 > std::numeric_limits<int64_t>::max() || d2 > std::numeric_limits<int64_t>::max()) {
return NumericVector({static_cast<double>(d1), static_cast<double>(d2)});
}
std::vector<int64_t> v = { static_cast<int64_t>(d1), static_cast<int64_t>(d2) };
return toInteger64(v);
}
case TILEDB_DATETIME_YEAR:
case TILEDB_DATETIME_MONTH:
case TILEDB_DATETIME_WEEK:
case TILEDB_DATETIME_DAY:
case TILEDB_DATETIME_HR:
case TILEDB_DATETIME_MIN:
case TILEDB_DATETIME_SEC:
case TILEDB_DATETIME_MS:
case TILEDB_DATETIME_US:
case TILEDB_DATETIME_NS:
case TILEDB_DATETIME_PS:
case TILEDB_DATETIME_FS:
case TILEDB_DATETIME_AS: {
using DataType = tiledb::impl::tiledb_to_type<TILEDB_INT64>::type;
auto d1 = dim->domain<DataType>().first;
auto d2 = dim->domain<DataType>().second;
if (d1 <= R_NaInt || d1 > std::numeric_limits<int32_t>::max() ||
d2 <= R_NaInt || d2 > std::numeric_limits<int32_t>::max()) {
std::vector<int64_t> v{d1, d2}; // return as int64
return toInteger64(v); // which 'travels' as a double
}
return IntegerVector({static_cast<int32_t>(d1), static_cast<int32_t>(d2)});
}
default:
Rcpp::stop("invalid tiledb_dim domain type (%s)", _tiledb_datatype_to_string(dim_type));
}
}
// [[Rcpp::export]]