-
Notifications
You must be signed in to change notification settings - Fork 184
/
writer_base.cc
1203 lines (1051 loc) · 40.4 KB
/
writer_base.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 writer_base.cc
*
* @section LICENSE
*
* The MIT License
*
* @copyright 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.
*
* @section DESCRIPTION
*
* This file implements class WriterBase.
*/
#include "tiledb/sm/query/writers/writer_base.h"
#include "tiledb/common/heap_memory.h"
#include "tiledb/common/logger.h"
#include "tiledb/sm/array/array.h"
#include "tiledb/sm/array_schema/array_schema.h"
#include "tiledb/sm/array_schema/dimension.h"
#include "tiledb/sm/enums/compressor.h"
#include "tiledb/sm/filesystem/vfs.h"
#include "tiledb/sm/filter/compression_filter.h"
#include "tiledb/sm/filter/webp_filter.h"
#include "tiledb/sm/fragment/fragment_identifier.h"
#include "tiledb/sm/fragment/fragment_metadata.h"
#include "tiledb/sm/misc/comparators.h"
#include "tiledb/sm/misc/hilbert.h"
#include "tiledb/sm/misc/parallel_functions.h"
#include "tiledb/sm/misc/tdb_math.h"
#include "tiledb/sm/misc/tdb_time.h"
#include "tiledb/sm/query/hilbert_order.h"
#include "tiledb/sm/query/query_macros.h"
#include "tiledb/sm/stats/global_stats.h"
#include "tiledb/sm/tile/generic_tile_io.h"
#include "tiledb/sm/tile/tile_metadata_generator.h"
#include "tiledb/sm/tile/writer_tile_tuple.h"
#include "tiledb/storage_format/uri/generate_uri.h"
using namespace tiledb;
using namespace tiledb::common;
using namespace tiledb::sm::stats;
namespace tiledb::sm {
class WriterBaseException : public StatusException {
public:
explicit WriterBaseException(const std::string& message)
: StatusException("WriterBase", message) {
}
};
/* ****************************** */
/* CONSTRUCTORS & DESTRUCTORS */
/* ****************************** */
WriterBase::WriterBase(
stats::Stats* stats,
shared_ptr<Logger> logger,
StrategyParams& params,
std::vector<WrittenFragmentInfo>& written_fragment_info,
bool disable_checks_consolidation,
Query::CoordsInfo& coords_info,
bool remote_query,
optional<std::string> fragment_name)
: StrategyBase(stats, logger->clone("Writer", ++logger_id_), params)
, disable_checks_consolidation_(disable_checks_consolidation)
, coords_info_(coords_info)
, check_coord_dups_(false)
, check_coord_oob_(false)
, check_global_order_(false)
, dedup_coords_(false)
, written_fragment_info_(written_fragment_info)
, remote_query_(remote_query) {
// Sanity checks
if (!params.skip_checks_serialization() && buffers_.empty()) {
throw WriterBaseException("Cannot initialize writer; Buffers not set");
}
if (array_schema_.dense() &&
(layout_ == Layout::ROW_MAJOR || layout_ == Layout::COL_MAJOR)) {
for (const auto& b : buffers_) {
if (array_schema_.is_dim(b.first)) {
throw WriterBaseException(
"Cannot initialize writer; Sparse coordinates for dense arrays "
"cannot be provided if the query layout is ROW_MAJOR or COL_MAJOR");
}
}
}
// Get configuration parameters
const char *check_coord_dups, *check_coord_oob, *check_global_order;
const char* dedup_coords;
if (!config_.get("sm.check_coord_dups", &check_coord_dups).ok()) {
throw WriterBaseException("Cannot get setting");
}
if (!config_.get("sm.check_coord_oob", &check_coord_oob).ok()) {
throw WriterBaseException("Cannot get setting");
}
if (!config_.get("sm.check_global_order", &check_global_order).ok()) {
throw WriterBaseException("Cannot get setting");
}
if (!config_.get("sm.dedup_coords", &dedup_coords).ok()) {
throw WriterBaseException("Cannot get setting");
}
assert(check_coord_dups != nullptr && dedup_coords != nullptr);
check_coord_dups_ =
disable_checks_consolidation_ ? false : !strcmp(check_coord_dups, "true");
check_coord_oob_ = !strcmp(check_coord_oob, "true");
check_global_order_ = disable_checks_consolidation_ ?
false :
!strcmp(check_global_order, "true");
dedup_coords_ = !strcmp(dedup_coords, "true");
bool found = false;
offsets_format_mode_ = config_.get("sm.var_offsets.mode", &found);
assert(found);
if (offsets_format_mode_ != "bytes" && offsets_format_mode_ != "elements") {
throw WriterBaseException(
"Cannot initialize writer; Unsupported offsets format in "
"configuration");
}
if (!config_
.get<bool>(
"sm.var_offsets.extra_element", &offsets_extra_element_, &found)
.ok()) {
throw WriterBaseException("Cannot get setting");
}
assert(found);
if (!config_
.get<uint32_t>("sm.var_offsets.bitsize", &offsets_bitsize_, &found)
.ok()) {
throw WriterBaseException("Cannot get setting");
}
assert(found);
if (offsets_bitsize_ != 32 && offsets_bitsize_ != 64) {
throw WriterBaseException(
"Cannot initialize writer; Unsupported offsets bitsize in "
"configuration");
}
// Check subarray is valid for strategy is set or set it to default if unset.
if (subarray_.is_set()) {
if (!array_schema_.dense()) {
throw WriterBaseException(
"Cannot initialize write; Non-default subarray are not supported in "
"sparse writes");
}
if (subarray_.range_num() > 1) {
throw WriterBaseException(
"Cannot initialize writer; Multi-range dense writes are not "
"supported");
}
} else {
subarray_ = Subarray(array_, layout_, stats_, logger_);
}
if (offsets_extra_element_) {
check_extra_element();
}
if (!params.skip_checks_serialization()) {
// Consolidation might set a subarray that is not tile aligned.
if (!disable_checks_consolidation) {
check_subarray();
}
check_buffer_sizes();
}
optimize_layout_for_1D();
check_var_attr_offsets();
// Get the timestamp the array was opened and the array write version.
uint64_t timestamp = array_->timestamp_end_opened_at();
auto write_version = array_->array_schema_latest().write_version();
// Set the fragment URI using either the provided fragment name or a generated
// fragment name.
auto new_fragment_str =
fragment_name.has_value() ?
fragment_name.value() :
storage_format::generate_timestamped_name(timestamp, write_version);
auto frag_dir_uri =
array_->array_directory().get_fragments_dir(write_version);
fragment_uri_ = frag_dir_uri.join_path(new_fragment_str);
FragmentID fragment_id{fragment_uri_};
fragment_timestamp_range_ = fragment_id.timestamp_range();
}
WriterBase::~WriterBase() {
clear_coord_buffers();
}
/* ****************************** */
/* API */
/* ****************************** */
bool WriterBase::get_check_coord_dups() const {
return check_coord_dups_;
}
bool WriterBase::get_check_coord_oob() const {
return check_coord_oob_;
}
bool WriterBase::get_dedup_coords() const {
return dedup_coords_;
}
void WriterBase::set_check_coord_dups(bool b) {
check_coord_dups_ = b;
}
void WriterBase::set_check_coord_oob(bool b) {
check_coord_oob_ = b;
}
void WriterBase::set_dedup_coords(bool b) {
dedup_coords_ = b;
}
void WriterBase::check_var_attr_offsets() const {
for (const auto& it : buffers_) {
const auto& attr = it.first;
if (!array_schema_.var_size(attr)) {
continue;
}
const void* buffer_off = it.second.buffer_;
const uint64_t buffer_off_size =
get_offset_buffer_size(*it.second.buffer_size_);
const uint64_t* buffer_val_size = it.second.buffer_var_size_;
auto num_offsets = buffer_off_size / constants::cell_var_offset_size;
if (num_offsets == 0) {
return;
}
uint64_t prev_offset = get_offset_buffer_element(buffer_off, 0);
// Allow the initial offset to be equal to the size, this indicates
// the first and only value in the buffer is to be empty
if (prev_offset > *buffer_val_size) {
throw WriterBaseException(
"Invalid offsets for attribute " + attr + "; offset " +
std::to_string(prev_offset) + " specified for buffer of size " +
std::to_string(*buffer_val_size));
}
for (uint64_t i = 1; i < num_offsets; i++) {
uint64_t cur_offset = get_offset_buffer_element(buffer_off, i);
if (cur_offset < prev_offset)
throw WriterBaseException(
"Invalid offsets for attribute " + attr +
"; offsets must be given in "
"strictly ascending order.");
// Allow the last offset(s) to be equal to the size, this indicates the
// last value(s) are to be empty
if (cur_offset > *buffer_val_size ||
(cur_offset == *buffer_val_size &&
get_offset_buffer_element(
buffer_off, (i < num_offsets - 1 ? i + 1 : i)) !=
*buffer_val_size))
throw WriterBaseException(
"Invalid offsets for attribute " + attr + "; offset " +
std::to_string(cur_offset) + " specified at index " +
std::to_string(i) + " for buffer of size " +
std::to_string(*buffer_val_size));
prev_offset = cur_offset;
}
}
}
void WriterBase::refresh_config() {
}
/* ****************************** */
/* PRIVATE METHODS */
/* ****************************** */
shared_ptr<FragmentMetadata> WriterBase::create_fragment_metadata() {
return make_shared<FragmentMetadata>(
HERE(),
&resources_,
query_memory_tracker_,
array_->array_schema_latest().write_version());
}
Status WriterBase::add_written_fragment_info(const URI& uri) {
written_fragment_info_.emplace_back(uri, fragment_timestamp_range_);
return Status::Ok();
}
Status WriterBase::calculate_hilbert_values(
const DomainBuffersView& domain_buffers,
std::vector<uint64_t>& hilbert_values) const {
auto dim_num = array_schema_.dim_num();
Hilbert h(dim_num);
auto bits = h.bits();
auto max_bucket_val = ((uint64_t)1 << bits) - 1;
// Calculate Hilbert values in parallel
assert(hilbert_values.size() >= coords_info_.coords_num_);
auto status = parallel_for(
&resources_.compute_tp(), 0, coords_info_.coords_num_, [&](uint64_t c) {
std::vector<uint64_t> coords(dim_num);
for (uint32_t d = 0; d < dim_num; ++d) {
auto dim{array_schema_.dimension_ptr(d)};
coords[d] = hilbert_order::map_to_uint64(
*dim, domain_buffers[d], c, bits, max_bucket_val);
}
hilbert_values[c] = h.coords_to_hilbert(&coords[0]);
return Status::Ok();
});
RETURN_NOT_OK_ELSE(status, logger_->error(status.message()));
return Status::Ok();
}
void WriterBase::check_buffer_sizes() const {
// This is applicable only to dense arrays and ordered layout
if (!array_schema_.dense() ||
(layout_ != Layout::ROW_MAJOR && layout_ != Layout::COL_MAJOR)) {
return;
}
auto cell_num = array_schema_.domain().cell_num(subarray_.ndrange(0));
uint64_t expected_cell_num = 0;
for (const auto& it : buffers_) {
const auto& attr = it.first;
const bool is_var = array_schema_.var_size(attr);
const uint64_t buffer_size =
is_var ? get_offset_buffer_size(*it.second.buffer_size_) :
*it.second.buffer_size_;
if (is_var) {
expected_cell_num = buffer_size / constants::cell_var_offset_size;
} else {
expected_cell_num = buffer_size / array_schema_.cell_size(attr);
}
if (array_schema_.is_nullable(attr)) {
const uint64_t buffer_validity_size =
*it.second.validity_vector_.buffer_size();
const uint64_t expected_validity_num =
buffer_validity_size / constants::cell_validity_size;
if (expected_validity_num != cell_num) {
std::stringstream ss;
ss << "Buffer sizes check failed; Invalid number of validity cells "
"given for ";
ss << "attribute '" << attr << "'";
ss << " (" << expected_validity_num << " != " << cell_num << ")";
throw WriterBaseException(ss.str());
}
} else {
if (expected_cell_num != cell_num) {
std::stringstream ss;
ss << "Buffer sizes check failed; Invalid number of cells given for ";
ss << "attribute '" << attr << "'";
ss << " (" << expected_cell_num << " != " << cell_num << ")";
throw WriterBaseException(ss.str());
}
}
}
}
Status WriterBase::check_coord_oob() const {
auto timer_se = stats_->start_timer("check_coord_oob");
// Applicable only to sparse writes - exit if coordinates do not exist
if (!coords_info_.has_coords_)
return Status::Ok();
// Exit if there are no coordinates to write
if (coords_info_.coords_num_ == 0)
return Status::Ok();
// Exit if all dimensions are strings
if (array_schema_.domain().all_dims_string())
return Status::Ok();
// Prepare auxiliary vectors for better performance
auto dim_num = array_schema_.dim_num();
std::vector<unsigned char*> buffs(dim_num);
std::vector<uint64_t> coord_sizes(dim_num);
for (unsigned d = 0; d < dim_num; ++d) {
const auto& dim_name{array_schema_.dimension_ptr(d)->name()};
buffs[d] = (unsigned char*)buffers_.find(dim_name)->second.buffer_;
coord_sizes[d] = array_schema_.cell_size(dim_name);
}
// Check if all coordinates fall in the domain in parallel
auto status = parallel_for_2d(
&resources_.compute_tp(),
0,
coords_info_.coords_num_,
0,
dim_num,
[&](uint64_t c, unsigned d) {
auto dim{array_schema_.dimension_ptr(d)};
if (datatype_is_string(dim->type()))
return Status::Ok();
return dim->oob(buffs[d] + c * coord_sizes[d]);
});
RETURN_NOT_OK(status);
// Success
return Status::Ok();
}
void WriterBase::check_subarray() const {
if (array_schema_.dense()) {
if (subarray_.range_num() != 1) {
throw WriterBaseException("Multi-range dense writes are not supported");
}
if (layout_ == Layout::GLOBAL_ORDER && !subarray_.coincides_with_tiles()) {
throw WriterBaseException(
"Cannot initialize query; In global writes for dense arrays, the "
"subarray must coincide with the tile bounds");
}
}
}
bool is_sorted_buffer(
const QueryBuffer& buffer, Datatype type, bool increasing) {
switch (type) {
case Datatype::INT8:
return increasing ?
buffer.is_sorted<int8_t, std::less_equal<int8_t>>() :
buffer.is_sorted<int8_t, std::greater_equal<int8_t>>();
case Datatype::UINT8:
return increasing ?
buffer.is_sorted<uint8_t, std::less_equal<uint8_t>>() :
buffer.is_sorted<uint8_t, std::greater_equal<uint8_t>>();
case Datatype::INT16:
return increasing ?
buffer.is_sorted<int16_t, std::less_equal<int16_t>>() :
buffer.is_sorted<int16_t, std::greater_equal<int16_t>>();
case Datatype::UINT16:
return increasing ?
buffer.is_sorted<uint16_t, std::less_equal<uint16_t>>() :
buffer.is_sorted<uint16_t, std::greater_equal<uint16_t>>();
case Datatype::INT32:
return increasing ?
buffer.is_sorted<int32_t, std::less_equal<int32_t>>() :
buffer.is_sorted<int32_t, std::greater_equal<int32_t>>();
case Datatype::UINT32:
return increasing ?
buffer.is_sorted<uint32_t, std::less_equal<uint32_t>>() :
buffer.is_sorted<uint32_t, std::greater_equal<uint32_t>>();
case Datatype::INT64:
return increasing ?
buffer.is_sorted<int64_t, std::less_equal<int64_t>>() :
buffer.is_sorted<int64_t, std::greater_equal<int64_t>>();
case Datatype::UINT64:
return increasing ?
buffer.is_sorted<uint64_t, std::less_equal<uint64_t>>() :
buffer.is_sorted<uint64_t, std::greater_equal<uint64_t>>();
case Datatype::FLOAT32:
return increasing ? buffer.is_sorted<float, std::less_equal<float>>() :
buffer.is_sorted<float, std::greater_equal<float>>();
case Datatype::FLOAT64:
return increasing ?
buffer.is_sorted<double, std::less_equal<double>>() :
buffer.is_sorted<double, std::greater_equal<double>>();
case Datatype::DATETIME_YEAR:
case Datatype::DATETIME_MONTH:
case Datatype::DATETIME_WEEK:
case Datatype::DATETIME_DAY:
case Datatype::DATETIME_HR:
case Datatype::DATETIME_MIN:
case Datatype::DATETIME_SEC:
case Datatype::DATETIME_MS:
case Datatype::DATETIME_US:
case Datatype::DATETIME_NS:
case Datatype::DATETIME_PS:
case Datatype::DATETIME_FS:
case Datatype::DATETIME_AS:
case Datatype::TIME_HR:
case Datatype::TIME_MIN:
case Datatype::TIME_SEC:
case Datatype::TIME_MS:
case Datatype::TIME_US:
case Datatype::TIME_NS:
case Datatype::TIME_PS:
case Datatype::TIME_FS:
case Datatype::TIME_AS:
return increasing ?
buffer.is_sorted<int64_t, std::less_equal<int64_t>>() :
buffer.is_sorted<int64_t, std::greater_equal<int64_t>>();
case Datatype::STRING_ASCII:
return increasing ?
buffer.is_sorted_str<std::less_equal<std::string_view>>() :
buffer.is_sorted_str<std::greater_equal<std::string_view>>();
default:
throw WriterBaseException(
"Unexpected datatype '" + datatype_str(type) +
"' for an ordered attribute.");
}
return true;
}
void WriterBase::check_attr_order() const {
auto timer_se = stats_->start_timer("check_attr_order");
for (const auto& [name, buffer] : buffers_) {
// Skip non-attribute buffers.
if (!array_schema_.is_attr(name)) {
continue;
}
// Get the attribute data order. If the data type is unordered, no futher
// checks are needed.
const auto* attr{array_schema_.attribute(name)};
if (attr->order() == DataOrder::UNORDERED_DATA) {
continue;
}
bool increasing{attr->order() == DataOrder::INCREASING_DATA};
// Check the attribute sort. This assumes all ordered attributes are fixed
// except STRING_ASCII which is assumed to always be variable.
if (!is_sorted_buffer(buffer, attr->type(), increasing)) {
throw WriterBaseException(
"The data for attribute '" + name +
"' is not in the expected order.");
}
}
}
void WriterBase::clear_coord_buffers() {
// Applicable only if the coordinate buffers have been allocated by
// TileDB, which happens only when the zipped coordinates buffer is set
for (auto b : to_clean_)
tdb_free(b);
to_clean_.clear();
coord_buffer_sizes_.clear();
}
std::vector<std::string> WriterBase::buffer_names() const {
std::vector<std::string> ret;
// Add to the buffer names the attributes, as well as the dimensions only if
// coords_buffer_ has not been set
for (const auto& it : buffers_) {
if (!array_schema_.is_dim(it.first) || (!coords_info_.coords_buffer_))
ret.push_back(it.first);
}
// Special zipped coordinates name
if (coords_info_.coords_buffer_)
ret.push_back(constants::coords);
return ret;
}
Status WriterBase::close_files(shared_ptr<FragmentMetadata> meta) const {
// Close attribute and dimension files
const auto buffer_name = buffer_names();
std::vector<URI> file_uris;
file_uris.reserve(buffer_name.size() * 3);
for (const auto& name : buffer_name) {
file_uris.emplace_back(meta->uri(name));
if (array_schema_.var_size(name)) {
file_uris.emplace_back(meta->var_uri(name));
}
if (array_schema_.is_nullable(name)) {
file_uris.emplace_back(meta->validity_uri(name));
}
}
auto status =
parallel_for(&resources_.io_tp(), 0, file_uris.size(), [&](uint64_t i) {
const auto& file_uri = file_uris[i];
if (layout_ == Layout::GLOBAL_ORDER && remote_query()) {
resources_.vfs().finalize_and_close_file(file_uri);
} else {
throw_if_not_ok(resources_.vfs().close_file(file_uri));
}
return Status::Ok();
});
throw_if_not_ok(status);
return Status::Ok();
}
std::vector<NDRange> WriterBase::compute_mbrs(
const tdb::pmr::unordered_map<std::string, WriterTileTupleVector>& tiles)
const {
auto timer_se = stats_->start_timer("compute_coord_meta");
// Applicable only if there are coordinates
if (!coords_info_.has_coords_) {
return std::vector<NDRange>();
}
// Check if tiles are empty
if (tiles.empty() || tiles.begin()->second.empty()) {
return std::vector<NDRange>();
}
// Compute number of tiles. Assumes all attributes and
// and dimensions have the same number of tiles
auto tile_num = tiles.begin()->second.size();
auto dim_num = array_schema_.dim_num();
// Compute MBRs
std::vector<NDRange> mbrs(tile_num);
auto status =
parallel_for(&resources_.compute_tp(), 0, tile_num, [&](uint64_t i) {
mbrs[i].resize(dim_num);
std::vector<const void*> data(dim_num);
for (unsigned d = 0; d < dim_num; ++d) {
auto dim{array_schema_.dimension_ptr(d)};
const auto& dim_name = dim->name();
auto tiles_it = tiles.find(dim_name);
assert(tiles_it != tiles.end());
mbrs[i][d] = dim->var_size() ?
dim->compute_mbr_var(
tiles_it->second[i].offset_tile(),
tiles_it->second[i].var_tile()) :
dim->compute_mbr(tiles_it->second[i].fixed_tile());
}
return Status::Ok();
});
throw_if_not_ok(status);
return mbrs;
}
void WriterBase::set_coords_metadata(
const uint64_t start_tile_idx,
const uint64_t end_tile_idx,
const tdb::pmr::unordered_map<std::string, WriterTileTupleVector>& tiles,
const std::vector<NDRange>& mbrs,
shared_ptr<FragmentMetadata> meta) const {
// Applicable only if there are coordinates
if (!coords_info_.has_coords_) {
return;
}
// Check if tiles are empty
if (tiles.empty() || tiles.begin()->second.empty()) {
return;
}
auto status = parallel_for(
&resources_.compute_tp(), start_tile_idx, end_tile_idx, [&](uint64_t i) {
meta->set_mbr(i - start_tile_idx, mbrs[i]);
return Status::Ok();
});
throw_if_not_ok(status);
// Set last tile cell number
auto dim_0{array_schema_.dimension_ptr(0)};
const auto& dim_tiles = tiles.find(dim_0->name())->second;
auto cell_num = dim_tiles.at(end_tile_idx - 1).cell_num();
meta->set_last_tile_cell_num(cell_num);
}
Status WriterBase::compute_tiles_metadata(
uint64_t tile_num,
tdb::pmr::unordered_map<std::string, WriterTileTupleVector>& tiles) const {
auto* compute_tp = &resources_.compute_tp();
// Parallelize over attributes?
if (tiles.size() > tile_num) {
auto st = parallel_for(compute_tp, 0, tiles.size(), [&](uint64_t i) {
auto tiles_it = tiles.begin();
std::advance(tiles_it, i);
const auto& attr = tiles_it->first;
auto& attr_tiles = tiles.at(attr);
const auto type = array_schema_.type(attr);
const auto is_dim = array_schema_.is_dim(attr);
const auto var_size = array_schema_.var_size(attr);
const auto cell_size = array_schema_.cell_size(attr);
const auto cell_val_num = array_schema_.cell_val_num(attr);
for (auto& tile : attr_tiles) {
TileMetadataGenerator md_generator(
type, is_dim, var_size, cell_size, cell_val_num);
md_generator.process_full_tile(tile);
md_generator.set_tile_metadata(tile);
}
return Status::Ok();
});
RETURN_NOT_OK(st);
} else { // Parallelize over tiles
for (auto& tile_vec : tiles) {
const auto& attr = tile_vec.first;
auto& attr_tiles = tile_vec.second;
const auto type = array_schema_.type(attr);
const auto is_dim = array_schema_.is_dim(attr);
const auto var_size = array_schema_.var_size(attr);
const auto cell_size = array_schema_.cell_size(attr);
const auto cell_val_num = array_schema_.cell_val_num(attr);
auto st = parallel_for(compute_tp, 0, tile_num, [&](uint64_t t) {
TileMetadataGenerator md_generator(
type, is_dim, var_size, cell_size, cell_val_num);
md_generator.process_full_tile(attr_tiles[t]);
md_generator.set_tile_metadata(attr_tiles[t]);
return Status::Ok();
});
RETURN_NOT_OK(st);
}
}
return Status::Ok();
}
std::string WriterBase::coords_to_str(uint64_t i) const {
std::stringstream ss;
auto dim_num = array_schema_.dim_num();
ss << "(";
for (unsigned d = 0; d < dim_num; ++d) {
auto dim{array_schema_.dimension_ptr(d)};
const auto& dim_name = dim->name();
ss << buffers_.find(dim_name)->second.dimension_datum_at(*dim, i);
if (d < dim_num - 1)
ss << ", ";
}
ss << ")";
return ss.str();
}
Status WriterBase::create_fragment(
bool dense, shared_ptr<FragmentMetadata>& frag_meta) {
// Get write version, timestamp array was opened, and a reference to the
// array directory.
auto write_version = array_->array_schema_latest().write_version();
uint64_t timestamp = array_->timestamp_end_opened_at();
auto& array_dir = array_->array_directory();
// Create the directories.
// Create the fragment directory, the directory for the new fragment
// URI, and the commit directory.
throw_if_not_ok(
resources_.vfs().create_dir(array_dir.get_fragments_dir(write_version)));
throw_if_not_ok(resources_.vfs().create_dir(fragment_uri_));
throw_if_not_ok(
resources_.vfs().create_dir(array_dir.get_commits_dir(write_version)));
// Create fragment metadata.
auto timestamp_range = std::pair<uint64_t, uint64_t>(timestamp, timestamp);
const bool has_timestamps = buffers_.count(constants::timestamps) != 0;
const bool has_delete_metadata =
buffers_.count(constants::delete_timestamps) != 0;
frag_meta = make_shared<FragmentMetadata>(
HERE(),
&resources_,
array_->array_schema_latest_ptr(),
fragment_uri_,
timestamp_range,
query_memory_tracker_,
dense,
has_timestamps,
has_delete_metadata);
frag_meta->init(subarray_.ndrange(0));
return Status::Ok();
}
Status WriterBase::filter_tiles(
tdb::pmr::unordered_map<std::string, WriterTileTupleVector>* tiles) {
auto timer_se = stats_->start_timer("filter_tiles");
auto status =
parallel_for(&resources_.compute_tp(), 0, tiles->size(), [&](uint64_t i) {
auto tiles_it = tiles->begin();
std::advance(tiles_it, i);
throw_if_not_ok(filter_tiles(tiles_it->first, &tiles_it->second));
throw_if_cancelled();
return Status::Ok();
});
RETURN_NOT_OK(status);
return Status::Ok();
}
Status WriterBase::filter_tiles(
const std::string& name, WriterTileTupleVector* tiles) {
const bool var_size = array_schema_.var_size(name);
const bool nullable = array_schema_.is_nullable(name);
// Filter all tiles
auto tile_num = tiles->size();
// Process all tiles, minus offsets, they get processed separately.
std::vector<std::tuple<WriterTile*, WriterTile*, bool, bool>> args;
args.reserve(tile_num * (1 + nullable));
for (auto& tile : *tiles) {
if (var_size) {
args.emplace_back(&tile.var_tile(), &tile.offset_tile(), false, false);
} else {
args.emplace_back(&tile.fixed_tile(), nullptr, false, false);
}
if (nullable) {
args.emplace_back(&tile.validity_tile(), nullptr, false, true);
}
}
// For fixed size, process everything, for var size, everything minus offsets.
auto status =
parallel_for(&resources_.compute_tp(), 0, args.size(), [&](uint64_t i) {
const auto& [tile, offset_tile, contains_offsets, is_nullable] =
args[i];
throw_if_not_ok(filter_tile(
name, tile, offset_tile, contains_offsets, is_nullable));
return Status::Ok();
});
RETURN_NOT_OK(status);
// Process offsets for var size.
if (var_size) {
auto status = parallel_for(
&resources_.compute_tp(), 0, tiles->size(), [&](uint64_t i) {
auto& tile = (*tiles)[i];
throw_if_not_ok(
filter_tile(name, &tile.offset_tile(), nullptr, true, false));
return Status::Ok();
});
RETURN_NOT_OK(status);
}
return Status::Ok();
}
Status WriterBase::filter_tile(
const std::string& name,
WriterTile* const tile,
WriterTile* const offsets_tile,
const bool offsets,
const bool nullable) {
auto timer_se = stats_->start_timer("filter_tile");
// Get a copy of the appropriate filter pipeline.
FilterPipeline filters;
if (offsets) {
assert(!nullable);
filters = array_schema_.cell_var_offsets_filters();
} else if (nullable) {
filters = array_schema_.cell_validity_filters();
} else {
filters = array_schema_.filters(name);
}
// If those offsets belong to a var-sized string dimension/attribute then
// don't filter the offsets as the information will be included in, and can be
// reconstructed from, the filtered data tile.
if (offsets && array_schema_.filters(name).skip_offsets_filtering(
array_schema_.type(name))) {
tile->filtered_buffer().expand(sizeof(uint64_t));
uint64_t nchunks = 0;
memcpy(tile->filtered_buffer().data(), &nchunks, sizeof(uint64_t));
tile->clear_data();
return Status::Ok();
}
// Append an encryption filter when necessary.
throw_if_not_ok(FilterPipeline::append_encryption_filter(
&filters, array_->get_encryption_key()));
// Check if chunk or tile level filtering/unfiltering is appropriate
bool use_chunking = filters.use_tile_chunking(
array_schema_.var_size(name), array_schema_.version(), tile->type());
assert(!tile->filtered());
filters.run_forward(
stats_, tile, offsets_tile, &resources_.compute_tp(), use_chunking);
assert(tile->filtered());
return Status::Ok();
}
bool WriterBase::has_min_max_metadata(
const std::string& name, const bool var_size) {
const auto type = array_schema_.type(name);
const auto is_dim = array_schema_.is_dim(name);
const auto cell_val_num = array_schema_.cell_val_num(name);
return TileMetadataGenerator::has_min_max_metadata(
type, is_dim, var_size, cell_val_num);
}
bool WriterBase::has_sum_metadata(
const std::string& name, const bool var_size) {
const auto type = array_schema_.type(name);
const auto cell_val_num = array_schema_.cell_val_num(name);
return TileMetadataGenerator::has_sum_metadata(type, var_size, cell_val_num);
}
Status WriterBase::init_tiles(
const std::string& name,
uint64_t tile_num,
WriterTileTupleVector* tiles) const {
// Initialize tiles
const bool var_size = array_schema_.var_size(name);
const bool nullable = array_schema_.is_nullable(name);
const uint64_t cell_size = array_schema_.cell_size(name);
const auto type = array_schema_.type(name);
const auto& domain{array_schema_.domain()};
const auto capacity = array_schema_.capacity();
const auto cell_num_per_tile =
coords_info_.has_coords_ ? capacity : domain.cell_num_per_tile();
tiles->reserve(tile_num);
for (uint64_t i = 0; i < tile_num; i++) {
tiles->emplace_back(
array_schema_,
cell_num_per_tile,
var_size,
nullable,
cell_size,
type,
query_memory_tracker_);
}
return Status::Ok();
}
void WriterBase::optimize_layout_for_1D() {
if (array_schema_.dim_num() == 1 && layout_ != Layout::GLOBAL_ORDER &&
layout_ != Layout::UNORDERED)
layout_ = array_schema_.cell_order();
}
void WriterBase::check_extra_element() {
for (const auto& it : buffers_) {
const auto& attr = it.first;
if (!array_schema_.var_size(attr) || array_schema_.is_dim(attr))
continue;
const void* buffer_off = it.second.buffer_;
uint64_t* buffer_off_size = it.second.buffer_size_;
const auto num_offsets = *buffer_off_size / constants::cell_var_offset_size;
const uint64_t* buffer_val_size = it.second.buffer_var_size_;
const uint64_t attr_datatype_size = datatype_size(array_schema_.type(attr));
const uint64_t max_offset = offsets_format_mode_ == "bytes" ?
*buffer_val_size :
*buffer_val_size / attr_datatype_size;
const uint64_t last_offset =
get_offset_buffer_element(buffer_off, num_offsets - 1);
if (last_offset != max_offset) {
throw WriterBaseException(
"Invalid offsets for attribute " + attr +
"; the last offset: " + std::to_string(last_offset) +
" is not equal to the size of the data buffer: " +
std::to_string(max_offset));
}
}
}
Status WriterBase::split_coords_buffer() {
auto timer_se = stats_->start_timer("split_coords_buff");
// Do nothing if the coordinates buffer is not set
if (coords_info_.coords_buffer_ == nullptr)
return Status::Ok();
// For easy reference
auto dim_num = array_schema_.dim_num();
auto coords_size{
dim_num * array_schema_.domain().dimension_ptr(0)->coord_size()};
coords_info_.coords_num_ = *coords_info_.coords_buffer_size_ / coords_size;
clear_coord_buffers();