-
Notifications
You must be signed in to change notification settings - Fork 179
/
unit-capi-array_schema.cc
1904 lines (1670 loc) · 60.1 KB
/
unit-capi-array_schema.cc
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
/**
* @file unit-capi-array_schema.cc
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 2017-2021 TileDB, Inc.
* @copyright Copyright (c) 2016 MIT and Intel Corporation
*
* 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.
*
* @section DESCRIPTION
*
* Tests for the C API tiledb_array_schema_t spec, along with
* tiledb_attribute_iter_t and tiledb_dimension_iter_t.
*/
#include <cassert>
#include <cstring>
#include <iostream>
#include <sstream>
#include <thread>
#include "catch.hpp"
#include "test/src/helpers.h"
#include "test/src/vfs_helpers.h"
#ifdef _WIN32
#include "tiledb/sm/filesystem/win.h"
#else
#include "tiledb/sm/filesystem/posix.h"
#endif
#include "tiledb/sm/c_api/tiledb.h"
#include "tiledb/sm/c_api/tiledb_serialization.h"
#include "tiledb/sm/enums/serialization_type.h"
#include "tiledb/sm/misc/utils.h"
using namespace tiledb::test;
struct ArraySchemaFx {
// Constant parameters
const std::string ARRAY_NAME = "dense_test_100x100_10x10";
tiledb_array_type_t ARRAY_TYPE = TILEDB_DENSE;
const char* ARRAY_TYPE_STR = "dense";
const uint64_t CAPACITY = 500;
const char* CAPACITY_STR = "500";
const tiledb_layout_t CELL_ORDER = TILEDB_COL_MAJOR;
const char* CELL_ORDER_STR = "col-major";
const tiledb_layout_t TILE_ORDER = TILEDB_ROW_MAJOR;
const char* TILE_ORDER_STR = "row-major";
const char* ATTR_NAME = "a";
const tiledb_datatype_t ATTR_TYPE = TILEDB_INT32;
const char* ATTR_TYPE_STR = "INT32";
const char* ATTR_COMPRESSOR_STR = "NO_COMPRESSION";
const char* ATTR_COMPRESSION_LEVEL_STR = "-1";
const unsigned int CELL_VAL_NUM = 1;
const char* CELL_VAL_NUM_STR = "1";
const int DIM_NUM = 2;
const char* DIM1_NAME = "d1";
const char* DIM2_NAME = "d2";
const tiledb_datatype_t DIM_TYPE = TILEDB_INT64;
const char* DIM_TYPE_STR = "INT64";
const int64_t DIM_DOMAIN[4] = {0, 99, 20, 60};
const char* DIM1_DOMAIN_STR = "[0,99]";
const char* DIM2_DOMAIN_STR = "[20,60]";
const uint64_t DIM_DOMAIN_SIZE = sizeof(DIM_DOMAIN) / DIM_NUM;
const uint32_t FILL_VALUE = 10;
const char* FILL_VALUE_STR = "10";
const uint32_t FILL_VALUE_SIZE = sizeof(uint32_t);
const int64_t TILE_EXTENTS[2] = {10, 5};
const char* DIM1_TILE_EXTENT_STR = "10";
const char* DIM2_TILE_EXTENT_STR = "5";
const uint64_t TILE_EXTENT_SIZE = sizeof(TILE_EXTENTS) / DIM_NUM;
/**
* If true, array schema is serialized before submission, to test the
* serialization paths.
*/
bool serialize_array_schema_ = false;
// TileDB context and vfs
tiledb_ctx_t* ctx_;
tiledb_vfs_t* vfs_;
// Vector of supported filsystems
const std::vector<std::unique_ptr<SupportedFs>> fs_vec_;
// struct for information of another directory
struct schema_file_struct {
tiledb_ctx_t* ctx;
tiledb_vfs_t* vfs;
std::string path;
};
static int get_schema_file_struct(const char* path, void* data);
// Functions
ArraySchemaFx();
~ArraySchemaFx();
void remove_temp_dir(const std::string& path);
void create_array(const std::string& path);
void create_temp_dir(const std::string& path);
void delete_array(const std::string& path);
bool is_array(const std::string& path);
void load_and_check_array_schema(const std::string& path);
static std::string random_name(const std::string& prefix);
int array_create_wrapper(
const std::string& path, tiledb_array_schema_t* array_schema);
int array_schema_load_wrapper(
const std::string& path, tiledb_array_schema_t** array_schema);
int array_get_schema_wrapper(
tiledb_array_t* array, tiledb_array_schema_t** array_schema);
int array_schema_get_domain_wrapper(
tiledb_array_schema_t* array_schema, tiledb_domain_t** domain);
int tiledb_array_get_non_empty_domain_wrapper(
tiledb_ctx_t* ctx,
tiledb_array_t* array,
void* domain,
int32_t* is_empty);
int tiledb_array_get_non_empty_domain_from_index_wrapper(
tiledb_ctx_t* ctx,
tiledb_array_t* array,
uint32_t index,
void* domain,
int32_t* is_empty);
int tiledb_array_get_non_empty_domain_from_name_wrapper(
tiledb_ctx_t* ctx,
tiledb_array_t* array,
const char* name,
void* domain,
int32_t* is_empty);
};
ArraySchemaFx::ArraySchemaFx()
: fs_vec_(vfs_test_get_fs_vec()) {
// Initialize vfs test
REQUIRE(vfs_test_init(fs_vec_, &ctx_, &vfs_).ok());
}
ArraySchemaFx::~ArraySchemaFx() {
// Close vfs test
REQUIRE(vfs_test_close(fs_vec_, ctx_, vfs_).ok());
tiledb_vfs_free(&vfs_);
tiledb_ctx_free(&ctx_);
}
void ArraySchemaFx::create_temp_dir(const std::string& path) {
remove_temp_dir(path);
REQUIRE(tiledb_vfs_create_dir(ctx_, vfs_, path.c_str()) == TILEDB_OK);
}
void ArraySchemaFx::remove_temp_dir(const std::string& path) {
int is_dir = 0;
REQUIRE(tiledb_vfs_is_dir(ctx_, vfs_, path.c_str(), &is_dir) == TILEDB_OK);
if (is_dir)
REQUIRE(tiledb_vfs_remove_dir(ctx_, vfs_, path.c_str()) == TILEDB_OK);
}
bool ArraySchemaFx::is_array(const std::string& path) {
tiledb_object_t type = TILEDB_INVALID;
REQUIRE(tiledb_object_type(ctx_, path.c_str(), &type) == TILEDB_OK);
return type == TILEDB_ARRAY;
}
void ArraySchemaFx::delete_array(const std::string& path) {
if (!is_array(path))
return;
CHECK(tiledb_object_remove(ctx_, path.c_str()) == TILEDB_OK);
}
int ArraySchemaFx::tiledb_array_get_non_empty_domain_wrapper(
tiledb_ctx_t* ctx, tiledb_array_t* array, void* domain, int32_t* is_empty) {
int ret = tiledb_array_get_non_empty_domain(ctx, array, domain, is_empty);
#ifndef TILEDB_SERIALIZATION
return ret;
#endif
if (ret != TILEDB_OK || !serialize_array_schema_)
return ret;
// Serialize the non_empty_domain
tiledb_buffer_t* buff;
REQUIRE(
tiledb_serialize_array_nonempty_domain(
ctx,
array,
domain,
*is_empty,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&buff) == TILEDB_OK);
// Deserialize to validate we can round-trip
REQUIRE(
tiledb_deserialize_array_nonempty_domain(
ctx,
array,
buff,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
&domain,
is_empty) == TILEDB_OK);
tiledb_buffer_free(&buff);
return TILEDB_OK;
}
int ArraySchemaFx::tiledb_array_get_non_empty_domain_from_index_wrapper(
tiledb_ctx_t* ctx,
tiledb_array_t* array,
uint32_t index,
void* domain,
int32_t* is_empty) {
int ret = tiledb_array_get_non_empty_domain_from_index(
ctx, array, index, domain, is_empty);
#ifndef TILEDB_SERIALIZATION
return ret;
#endif
if (ret != TILEDB_OK || !serialize_array_schema_)
return ret;
// Serialize the non_empty_domain
tiledb_buffer_t* buff;
REQUIRE(
tiledb_serialize_array_non_empty_domain_all_dimensions(
ctx,
array,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&buff) == TILEDB_OK);
// Deserialize to validate we can round-trip
REQUIRE(
tiledb_deserialize_array_non_empty_domain_all_dimensions(
ctx,
array,
buff,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1) == TILEDB_OK);
tiledb_buffer_free(&buff);
return tiledb_array_get_non_empty_domain_from_index(
ctx, array, index, domain, is_empty);
}
int ArraySchemaFx::tiledb_array_get_non_empty_domain_from_name_wrapper(
tiledb_ctx_t* ctx,
tiledb_array_t* array,
const char* name,
void* domain,
int32_t* is_empty) {
int ret = tiledb_array_get_non_empty_domain_from_name(
ctx, array, name, domain, is_empty);
#ifndef TILEDB_SERIALIZATION
return ret;
#endif
if (ret != TILEDB_OK || !serialize_array_schema_)
return ret;
// Serialize the non_empty_domain
tiledb_buffer_t* buff;
REQUIRE(
tiledb_serialize_array_non_empty_domain_all_dimensions(
ctx,
array,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&buff) == TILEDB_OK);
// Deserialize to validate we can round-trip
REQUIRE(
tiledb_deserialize_array_non_empty_domain_all_dimensions(
ctx,
array,
buff,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1) == TILEDB_OK);
tiledb_buffer_free(&buff);
return tiledb_array_get_non_empty_domain_from_name(
ctx, array, name, domain, is_empty);
}
int ArraySchemaFx::array_create_wrapper(
const std::string& path, tiledb_array_schema_t* array_schema) {
#ifndef TILEDB_SERIALIZATION
return tiledb_array_create(ctx_, path.c_str(), array_schema);
#endif
if (!serialize_array_schema_) {
return tiledb_array_create(ctx_, path.c_str(), array_schema);
}
// Serialize the array
tiledb_buffer_t* buff;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
&buff) == TILEDB_OK);
// Load array schema from the rest server
tiledb_array_schema_t* new_array_schema = nullptr;
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&new_array_schema) == TILEDB_OK);
// Create array from new schema
int rc = tiledb_array_create(ctx_, path.c_str(), new_array_schema);
// Serialize the new array schema and deserialize into the original array
// schema.
tiledb_buffer_t* buff2;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
new_array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&buff2) == TILEDB_OK);
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff2,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
&array_schema) == TILEDB_OK);
// Clean up.
tiledb_array_schema_free(&array_schema);
tiledb_array_schema_free(&new_array_schema);
tiledb_buffer_free(&buff);
tiledb_buffer_free(&buff2);
return rc;
}
int ArraySchemaFx::array_schema_load_wrapper(
const std::string& path, tiledb_array_schema_t** array_schema) {
#ifndef TILEDB_SERIALIZATION
return tiledb_array_schema_load(ctx_, path.c_str(), array_schema);
#endif
if (!serialize_array_schema_) {
return tiledb_array_schema_load(ctx_, path.c_str(), array_schema);
}
// Load array.
int rc = tiledb_array_schema_load(ctx_, path.c_str(), array_schema);
REQUIRE(rc == TILEDB_OK);
// Serialize the array
tiledb_buffer_t* buff;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
*array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
&buff) == TILEDB_OK);
// Load array schema from the rest server
tiledb_array_schema_t* new_array_schema = nullptr;
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&new_array_schema) == TILEDB_OK);
// Serialize the new array schema and deserialize into the original array
// schema.
tiledb_array_schema_free(array_schema);
tiledb_buffer_t* buff2;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
new_array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&buff2) == TILEDB_OK);
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff2,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
array_schema) == TILEDB_OK);
// Clean up.
tiledb_array_schema_free(&new_array_schema);
tiledb_buffer_free(&buff);
tiledb_buffer_free(&buff2);
return rc;
}
int ArraySchemaFx::array_get_schema_wrapper(
tiledb_array_t* array, tiledb_array_schema_t** array_schema) {
#ifndef TILEDB_SERIALIZATION
return tiledb_array_get_schema(ctx_, array, array_schema);
#endif
if (!serialize_array_schema_) {
return tiledb_array_get_schema(ctx_, array, array_schema);
}
int rc = tiledb_array_get_schema(ctx_, array, array_schema);
REQUIRE(rc == TILEDB_OK);
// Serialize the array
tiledb_buffer_t* buff;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
*array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
&buff) == TILEDB_OK);
// Load array schema from the rest server
tiledb_array_schema_t* new_array_schema = nullptr;
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&new_array_schema) == TILEDB_OK);
// Serialize the new array schema and deserialize into the original array
// schema.
tiledb_array_schema_free(array_schema);
tiledb_buffer_t* buff2;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
new_array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&buff2) == TILEDB_OK);
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff2,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
array_schema) == TILEDB_OK);
// Clean up.
tiledb_array_schema_free(&new_array_schema);
tiledb_buffer_free(&buff);
tiledb_buffer_free(&buff2);
return rc;
}
int ArraySchemaFx::array_schema_get_domain_wrapper(
tiledb_array_schema_t* array_schema, tiledb_domain_t** domain) {
#ifndef TILEDB_SERIALIZATION
return tiledb_array_schema_get_domain(ctx_, array_schema, domain);
#endif
if (!serialize_array_schema_) {
return tiledb_array_schema_get_domain(ctx_, array_schema, domain);
}
// Serialize the array
tiledb_buffer_t* buff;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
&buff) == TILEDB_OK);
// Load array schema from the rest server
tiledb_array_schema_t* new_array_schema = nullptr;
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&new_array_schema) == TILEDB_OK);
// Get domain from new array.
int rc = tiledb_array_schema_get_domain(ctx_, new_array_schema, domain);
// Serialize the new array schema and deserialize into the original array
// schema.
tiledb_buffer_t* buff2;
REQUIRE(
tiledb_serialize_array_schema(
ctx_,
new_array_schema,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
0,
&buff2) == TILEDB_OK);
REQUIRE(
tiledb_deserialize_array_schema(
ctx_,
buff2,
(tiledb_serialization_type_t)tiledb::sm::SerializationType::CAPNP,
1,
&array_schema) == TILEDB_OK);
// Clean up.
tiledb_array_schema_free(&new_array_schema);
tiledb_buffer_free(&buff);
tiledb_buffer_free(&buff2);
return rc;
}
void ArraySchemaFx::create_array(const std::string& path) {
// Create array schema
tiledb_array_schema_t* array_schema;
int rc = tiledb_array_schema_alloc(ctx_, ARRAY_TYPE, &array_schema);
REQUIRE(rc == TILEDB_OK);
// Set schema members
rc = tiledb_array_schema_set_capacity(ctx_, array_schema, CAPACITY);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_array_schema_set_cell_order(ctx_, array_schema, CELL_ORDER);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_array_schema_set_tile_order(ctx_, array_schema, TILE_ORDER);
REQUIRE(rc == TILEDB_OK);
// Check for invalid array schema
rc = tiledb_array_schema_check(ctx_, array_schema);
REQUIRE(rc == TILEDB_ERR);
rc = tiledb_array_create(ctx_, path.c_str(), array_schema);
REQUIRE(rc == TILEDB_ERR);
// Create dimensions
tiledb_dimension_t* d1;
rc = tiledb_dimension_alloc(
ctx_, DIM1_NAME, TILEDB_INT64, &DIM_DOMAIN[0], &TILE_EXTENTS[0], &d1);
REQUIRE(rc == TILEDB_OK);
tiledb_dimension_t* d2;
rc = tiledb_dimension_alloc(
ctx_, DIM2_NAME, TILEDB_INT64, &DIM_DOMAIN[2], &TILE_EXTENTS[1], &d2);
REQUIRE(rc == TILEDB_OK);
int dim_domain_int[] = {0, 10};
tiledb_dimension_t* d3; // This will be an invalid dimension
int tile_extent = 10000;
rc = tiledb_dimension_alloc( // This will not even be created
ctx_,
DIM2_NAME,
TILEDB_INT32,
dim_domain_int,
&tile_extent,
&d3);
REQUIRE(rc == TILEDB_ERR);
REQUIRE(d3 == nullptr);
// Set up filters
tiledb_filter_t* filter;
rc = tiledb_filter_alloc(ctx_, TILEDB_FILTER_BZIP2, &filter);
REQUIRE(rc == TILEDB_OK);
int level = 5;
rc = tiledb_filter_set_option(ctx_, filter, TILEDB_COMPRESSION_LEVEL, &level);
REQUIRE(rc == TILEDB_OK);
tiledb_filter_list_t* filter_list;
rc = tiledb_filter_list_alloc(ctx_, &filter_list);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_filter_list_add_filter(ctx_, filter_list, filter);
REQUIRE(rc == TILEDB_OK);
tiledb_filter_free(&filter);
rc = tiledb_filter_alloc(ctx_, TILEDB_FILTER_BIT_WIDTH_REDUCTION, &filter);
REQUIRE(rc == TILEDB_OK);
int window = 1000;
rc = tiledb_filter_set_option(
ctx_, filter, TILEDB_BIT_WIDTH_MAX_WINDOW, &window);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_filter_list_add_filter(ctx_, filter_list, filter);
REQUIRE(rc == TILEDB_OK);
// Add filters to dimension
rc = tiledb_dimension_set_filter_list(ctx_, d2, filter_list);
REQUIRE(rc == TILEDB_OK);
// Set domain
tiledb_domain_t* domain;
rc = tiledb_domain_alloc(ctx_, &domain);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_domain_add_dimension(ctx_, domain, d1);
REQUIRE(rc == TILEDB_OK);
tiledb_datatype_t domain_type;
rc = tiledb_domain_get_type(ctx_, domain, &domain_type);
REQUIRE(rc == TILEDB_OK);
REQUIRE(domain_type == TILEDB_INT64);
rc = tiledb_domain_add_dimension(ctx_, domain, d2);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_array_schema_set_domain(ctx_, array_schema, domain);
REQUIRE(rc == TILEDB_OK);
// Check for invalid array schema
rc = tiledb_array_schema_check(ctx_, array_schema);
REQUIRE(rc == TILEDB_ERR);
rc = tiledb_array_create(ctx_, path.c_str(), array_schema);
REQUIRE(rc == TILEDB_ERR);
// Set invalid attribute
tiledb_attribute_t* inv_attr;
rc = tiledb_attribute_alloc(ctx_, "__foo", ATTR_TYPE, &inv_attr);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_array_schema_add_attribute(ctx_, array_schema, inv_attr);
REQUIRE(rc == TILEDB_ERR);
tiledb_attribute_free(&inv_attr);
// Set attribute
tiledb_attribute_t* attr;
rc = tiledb_attribute_alloc(ctx_, ATTR_NAME, ATTR_TYPE, &attr);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_attribute_set_filter_list(ctx_, attr, filter_list);
REQUIRE(rc == TILEDB_OK);
rc =
tiledb_attribute_set_fill_value(ctx_, attr, &FILL_VALUE, FILL_VALUE_SIZE);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_array_schema_add_attribute(ctx_, array_schema, attr);
REQUIRE(rc == TILEDB_OK);
// Create array with invalid URI
rc = array_create_wrapper("file://array", array_schema);
REQUIRE(rc == TILEDB_ERR);
// Create correct array
rc = array_create_wrapper(path.c_str(), array_schema);
REQUIRE(rc == TILEDB_OK);
// Create the array again - should fail
rc = array_create_wrapper(path.c_str(), array_schema);
REQUIRE(rc == TILEDB_ERR);
// Clean up
tiledb_filter_free(&filter);
tiledb_filter_list_free(&filter_list);
tiledb_array_schema_free(&array_schema);
tiledb_attribute_free(&attr);
tiledb_dimension_free(&d1);
tiledb_dimension_free(&d2);
tiledb_domain_free(&domain);
}
void ArraySchemaFx::load_and_check_array_schema(const std::string& path) {
// Load array schema from the disk
tiledb_array_schema_t* array_schema = nullptr;
int rc = array_schema_load_wrapper(path.c_str(), &array_schema);
REQUIRE(rc == TILEDB_OK);
// Check capacity
uint64_t capacity;
rc = tiledb_array_schema_get_capacity(ctx_, array_schema, &capacity);
REQUIRE(rc == TILEDB_OK);
CHECK(capacity == CAPACITY);
// Check cell order
tiledb_layout_t cell_order;
rc = tiledb_array_schema_get_cell_order(ctx_, array_schema, &cell_order);
REQUIRE(rc == TILEDB_OK);
CHECK(cell_order == CELL_ORDER);
// Check tile order
tiledb_layout_t tile_order;
rc = tiledb_array_schema_get_tile_order(ctx_, array_schema, &tile_order);
REQUIRE(rc == TILEDB_OK);
CHECK(tile_order == TILE_ORDER);
// Check array_schema type
tiledb_array_type_t type;
rc = tiledb_array_schema_get_array_type(ctx_, array_schema, &type);
REQUIRE(rc == TILEDB_OK);
CHECK(type == TILEDB_DENSE);
// Check coordinates compression
tiledb_filter_list_t* coords_filters;
rc = tiledb_array_schema_get_coords_filter_list(
ctx_, array_schema, &coords_filters);
REQUIRE(rc == TILEDB_OK);
uint32_t nfilters;
rc = tiledb_filter_list_get_nfilters(ctx_, coords_filters, &nfilters);
CHECK(nfilters == 1);
tiledb_filter_t* coords_filter;
rc = tiledb_filter_list_get_filter_from_index(
ctx_, coords_filters, 0, &coords_filter);
REQUIRE(rc == TILEDB_OK);
tiledb_filter_type_t coords_compression;
int32_t coords_compression_level;
rc = tiledb_filter_get_type(ctx_, coords_filter, &coords_compression);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_filter_get_option(
ctx_, coords_filter, TILEDB_COMPRESSION_LEVEL, &coords_compression_level);
REQUIRE(rc == TILEDB_OK);
CHECK(coords_compression == TILEDB_FILTER_ZSTD);
CHECK(coords_compression_level == -1);
tiledb_filter_free(&coords_filter);
tiledb_filter_list_free(&coords_filters);
// Check attribute
tiledb_attribute_t* attr;
// check that getting an attribute fails when index is out of bounds
rc = tiledb_array_schema_get_attribute_from_index(
ctx_, array_schema, 1, &attr);
REQUIRE(rc == TILEDB_ERR);
// get first attribute by index
rc = tiledb_array_schema_get_attribute_from_index(
ctx_, array_schema, 0, &attr);
REQUIRE(rc == TILEDB_OK);
const char* attr_name;
rc = tiledb_attribute_get_name(ctx_, attr, &attr_name);
REQUIRE(rc == TILEDB_OK);
CHECK_THAT(attr_name, Catch::Equals(ATTR_NAME));
tiledb_attribute_free(&attr);
// get first attribute by name
rc = tiledb_array_schema_get_attribute_from_name(
ctx_, array_schema, ATTR_NAME, &attr);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_attribute_get_name(ctx_, attr, &attr_name);
REQUIRE(rc == TILEDB_OK);
CHECK_THAT(attr_name, Catch::Equals(ATTR_NAME));
tiledb_datatype_t attr_type;
rc = tiledb_attribute_get_type(ctx_, attr, &attr_type);
REQUIRE(rc == TILEDB_OK);
CHECK(attr_type == ATTR_TYPE);
tiledb_filter_list_t* attr_filters;
rc = tiledb_attribute_get_filter_list(ctx_, attr, &attr_filters);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_filter_list_get_nfilters(ctx_, attr_filters, &nfilters);
CHECK(nfilters == 2);
tiledb_filter_list_free(&attr_filters);
unsigned int cell_val_num;
rc = tiledb_attribute_get_cell_val_num(ctx_, attr, &cell_val_num);
REQUIRE(rc == TILEDB_OK);
CHECK(cell_val_num == CELL_VAL_NUM);
const void* fill_value;
uint64_t fill_value_size;
rc = tiledb_attribute_get_fill_value(
ctx_, attr, &fill_value, &fill_value_size);
REQUIRE(rc == TILEDB_OK);
CHECK(fill_value_size == FILL_VALUE_SIZE);
CHECK(!memcmp(fill_value, &FILL_VALUE, FILL_VALUE_SIZE));
unsigned int num_attributes = 0;
rc = tiledb_array_schema_get_attribute_num(
ctx_, array_schema, &num_attributes);
REQUIRE(rc == TILEDB_OK);
CHECK(num_attributes == 1);
// Get domain
tiledb_domain_t* domain;
rc = tiledb_array_schema_get_domain(ctx_, array_schema, &domain);
REQUIRE(rc == TILEDB_OK);
// Check first dimension
// get first dimension by name
tiledb_dimension_t* dim;
rc = tiledb_domain_get_dimension_from_name(ctx_, domain, DIM1_NAME, &dim);
REQUIRE(rc == TILEDB_OK);
const char* dim_name;
rc = tiledb_dimension_get_name(ctx_, dim, &dim_name);
REQUIRE(rc == TILEDB_OK);
CHECK_THAT(dim_name, Catch::Equals(DIM1_NAME));
tiledb_dimension_free(&dim);
// get first dimension by index
rc = tiledb_domain_get_dimension_from_index(ctx_, domain, 0, &dim);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_dimension_get_name(ctx_, dim, &dim_name);
REQUIRE(rc == TILEDB_OK);
CHECK_THAT(dim_name, Catch::Equals(DIM1_NAME));
const void* dim_domain;
rc = tiledb_dimension_get_domain(ctx_, dim, &dim_domain);
REQUIRE(rc == TILEDB_OK);
CHECK(!memcmp(dim_domain, &DIM_DOMAIN[0], DIM_DOMAIN_SIZE));
const void* tile_extent;
rc = tiledb_dimension_get_tile_extent(ctx_, dim, &tile_extent);
REQUIRE(rc == TILEDB_OK);
CHECK(!memcmp(tile_extent, &TILE_EXTENTS[0], TILE_EXTENT_SIZE));
tiledb_dimension_free(&dim);
// Check second dimension
// get second dimension by name
rc = tiledb_domain_get_dimension_from_name(ctx_, domain, DIM2_NAME, &dim);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_dimension_get_name(ctx_, dim, &dim_name);
REQUIRE(rc == TILEDB_OK);
CHECK_THAT(dim_name, Catch::Equals(DIM2_NAME));
tiledb_dimension_free(&dim);
// get from index
rc = tiledb_domain_get_dimension_from_index(ctx_, domain, 1, &dim);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_dimension_get_name(ctx_, dim, &dim_name);
REQUIRE(rc == TILEDB_OK);
CHECK_THAT(dim_name, Catch::Equals(DIM2_NAME));
rc = tiledb_dimension_get_domain(ctx_, dim, &dim_domain);
REQUIRE(rc == TILEDB_OK);
CHECK(!memcmp(dim_domain, &DIM_DOMAIN[2], DIM_DOMAIN_SIZE));
rc = tiledb_dimension_get_tile_extent(ctx_, dim, &tile_extent);
REQUIRE(rc == TILEDB_OK);
CHECK(!memcmp(tile_extent, &TILE_EXTENTS[1], TILE_EXTENT_SIZE));
// check that indexing > 1 returns an error for this domain
rc = tiledb_domain_get_dimension_from_index(ctx_, domain, 2, &dim);
CHECK(rc != TILEDB_OK);
// check that the ndim of the domain is 2
unsigned int ndim = 0;
rc = tiledb_domain_get_ndim(ctx_, domain, &ndim);
REQUIRE(rc == TILEDB_OK);
CHECK(ndim == 2);
// Check dump
std::string dump_str =
std::string("- Array type: ") + ARRAY_TYPE_STR + "\n" +
"- Cell order: " + CELL_ORDER_STR + "\n" +
"- Tile order: " + TILE_ORDER_STR + "\n" + "- Capacity: " + CAPACITY_STR +
"\n"
"- Allows duplicates: " +
"false\n"
"- Coordinates filters: 1\n" +
" > ZSTD: COMPRESSION_LEVEL=-1\n" + "- Offsets filters: 1\n" +
" > ZSTD: COMPRESSION_LEVEL=-1\n" + "- Validity filters: 1\n" +
" > RLE: COMPRESSION_LEVEL=-1\n\n" + "### Dimension ###\n" +
"- Name: " + DIM1_NAME + "\n" + "- Type: INT64\n" +
"- Cell val num: 1\n" + "- Domain: " + DIM1_DOMAIN_STR + "\n" +
"- Tile extent: " + DIM1_TILE_EXTENT_STR + "\n" + "- Filters: 0\n\n" +
"### Dimension ###\n" + "- Name: " + DIM2_NAME + "\n" +
"- Type: INT64\n" + "- Cell val num: 1\n" +
"- Domain: " + DIM2_DOMAIN_STR + "\n" +
"- Tile extent: " + DIM2_TILE_EXTENT_STR + "\n" + "- Filters: 2\n" +
" > BZIP2: COMPRESSION_LEVEL=5\n" +
" > BitWidthReduction: BIT_WIDTH_MAX_WINDOW=1000\n\n" +
"### Attribute ###\n" + "- Name: " + ATTR_NAME + "\n" +
"- Type: " + ATTR_TYPE_STR + "\n" + "- Nullable: false\n" +
"- Cell val num: " + CELL_VAL_NUM_STR + "\n" + "- Filters: 2\n" +
" > BZIP2: COMPRESSION_LEVEL=5\n" +
" > BitWidthReduction: BIT_WIDTH_MAX_WINDOW=1000\n" +
"- Fill value: " + FILL_VALUE_STR + "\n";
FILE* gold_fout = fopen("gold_fout.txt", "w");
const char* dump = dump_str.c_str();
fwrite(dump, sizeof(char), strlen(dump), gold_fout);
fclose(gold_fout);
FILE* fout = fopen("fout.txt", "w");
tiledb_array_schema_dump(ctx_, array_schema, fout);
fclose(fout);
#ifdef _WIN32
CHECK(!system("FC gold_fout.txt fout.txt > nul"));
#else
CHECK(!system("diff gold_fout.txt fout.txt"));
#endif
CHECK(tiledb_vfs_remove_file(ctx_, vfs_, "gold_fout.txt") == TILEDB_OK);
CHECK(tiledb_vfs_remove_file(ctx_, vfs_, "fout.txt") == TILEDB_OK);
// Clean up
tiledb_attribute_free(&attr);
tiledb_dimension_free(&dim);
tiledb_domain_free(&domain);
tiledb_array_schema_free(&array_schema);
}
std::string ArraySchemaFx::random_name(const std::string& prefix) {
std::stringstream ss;
ss << prefix << "-" << std::this_thread::get_id() << "-"
<< TILEDB_TIMESTAMP_NOW_MS;
return ss.str();
}
int ArraySchemaFx::get_schema_file_struct(const char* path, void* data) {
auto data_struct = (ArraySchemaFx::schema_file_struct*)data;
auto ctx = data_struct->ctx;
auto vfs = data_struct->vfs;
int is_dir;
int rc = tiledb_vfs_is_dir(ctx, vfs, path, &is_dir);
CHECK(rc == TILEDB_OK);
data_struct->path = path;
return 1;
}
TEST_CASE_METHOD(
ArraySchemaFx,
"C API: Test array schema creation and retrieval",
"[capi][array-schema]") {
SECTION("- No serialization") {
serialize_array_schema_ = false;
}
SECTION("- Serialization") {
serialize_array_schema_ = true;
}
// TODO: refactor for each supported FS.
std::string array_name = fs_vec_[0]->temp_dir() + ARRAY_NAME;
std::string temp_dir = fs_vec_[0]->temp_dir();
create_temp_dir(temp_dir);
create_array(array_name);
load_and_check_array_schema(array_name);
delete_array(array_name);
remove_temp_dir(temp_dir);
}
TEST_CASE_METHOD(
ArraySchemaFx,
"C API: Test array schema one anonymous dimension",
"[capi], [array-schema]") {
// Create dimensions
tiledb_dimension_t* d1;
int rc = tiledb_dimension_alloc(
ctx_, "", TILEDB_INT64, &DIM_DOMAIN[0], &TILE_EXTENTS[0], &d1);
REQUIRE(rc == TILEDB_OK);
tiledb_dimension_t* d2;
rc = tiledb_dimension_alloc(
ctx_, "d2", TILEDB_INT64, &DIM_DOMAIN[2], &TILE_EXTENTS[1], &d2);
REQUIRE(rc == TILEDB_OK);
// Set domain
tiledb_domain_t* domain;
rc = tiledb_domain_alloc(ctx_, &domain);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_domain_add_dimension(ctx_, domain, d1);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_domain_add_dimension(ctx_, domain, d2);
REQUIRE(rc == TILEDB_OK);
tiledb_dimension_t* get_dim = nullptr;
rc = tiledb_domain_get_dimension_from_name(ctx_, domain, "", &get_dim);
CHECK(rc == TILEDB_OK);
tiledb_dimension_free(&get_dim);
int32_t has_dim = 0;
rc = tiledb_domain_has_dimension(ctx_, domain, "d2", &has_dim);
REQUIRE(rc == TILEDB_OK);
REQUIRE(has_dim == 1);
has_dim = false;
rc = tiledb_domain_has_dimension(ctx_, domain, "d3", &has_dim);
REQUIRE(rc == TILEDB_OK);
REQUIRE(has_dim == 0);
rc = tiledb_domain_get_dimension_from_name(ctx_, domain, "d2", &get_dim);
const char* get_name = nullptr;
CHECK(rc == TILEDB_OK);
rc = tiledb_dimension_get_name(ctx_, get_dim, &get_name);
CHECK(rc == TILEDB_OK);
CHECK_THAT(get_name, Catch::Equals("d2"));
tiledb_dimension_free(&get_dim);
// Clean up
tiledb_dimension_free(&d1);
tiledb_dimension_free(&d2);
tiledb_domain_free(&domain);
}
TEST_CASE_METHOD(