Skip to content

Commit

Permalink
[c++] Use schema from tiledb::Array instead of ManagedQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenv committed Nov 1, 2024
1 parent b4ab8d2 commit a543b75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
28 changes: 15 additions & 13 deletions libtiledbsoma/src/soma/soma_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ SOMAArray::SOMAArray(
, result_order_(ResultOrder::automatic)
, timestamp_(timestamp)
, mq_(std::make_unique<ManagedQuery>(arr, ctx_->tiledb_ctx(), name_))
, arr_(arr) {
, arr_(arr)
, schema_(std::make_shared<ArraySchema>(arr->schema())) {
reset({}, batch_size_, result_order_);
fill_metadata_cache();
}
Expand Down Expand Up @@ -356,7 +357,7 @@ std::vector<std::string> SOMAArray::attribute_names() const {
}

void SOMAArray::write(bool sort_coords) {
if (mq_->query_type() != TILEDB_WRITE) {
if (arr_->query_type() != TILEDB_WRITE) {
throw TileDBSOMAError("[SOMAArray] array must be opened in write mode");
}
mq_->submit_write(sort_coords);
Expand Down Expand Up @@ -447,6 +448,7 @@ void SOMAArray::validate(
LOG_TRACE(fmt::format("[SOMAArray] loading enumerations"));
ArrayExperimental::load_all_enumerations(
*ctx_->tiledb_ctx(), *(arr_.get()));
schema_ = std::make_shared<ArraySchema>(arr_->schema());
mq_ = std::make_unique<ManagedQuery>(arr_, ctx_->tiledb_ctx(), name);
} catch (const std::exception& e) {
throw TileDBSOMAError(
Expand Down Expand Up @@ -580,7 +582,7 @@ ArrowTable SOMAArray::_get_core_domainish(enum Domainish which_kind) {

uint64_t SOMAArray::nnz() {
// Verify array is sparse
if (mq_->schema()->array_type() != TILEDB_SPARSE) {
if (schema_->array_type() != TILEDB_SPARSE) {
throw TileDBSOMAError(
"[SOMAArray] nnz is only supported for sparse arrays");
}
Expand Down Expand Up @@ -621,7 +623,7 @@ uint64_t SOMAArray::nnz() {
// If the application is allowing duplicates (in which case it's the
// application's job to otherwise ensure uniqueness), then
// sum-over-fragments is the right thing to do.
if (!mq_->schema()->allows_dups() && frag_ts.first != frag_ts.second) {
if (!schema_->allows_dups() && frag_ts.first != frag_ts.second) {
return _nnz_slow();
}
}
Expand Down Expand Up @@ -712,7 +714,7 @@ uint64_t SOMAArray::_nnz_slow() {
uri_,
ctx_,
"count_cells",
{mq_->schema()->domain().dimension(0).name()},
{schema_->domain().dimension(0).name()},
batch_size_,
result_order_,
timestamp_);
Expand Down Expand Up @@ -749,7 +751,7 @@ StatusAndReason SOMAArray::_can_set_shape_helper(
// E.g. it's an error to try to upgrade_domain or resize specifying
// a 3-D shape on a 2-D array.
auto arg_ndim = newshape.size();
auto array_ndim = arr_->schema().domain().ndim();
auto array_ndim = schema_->domain().ndim();
if (array_ndim != arg_ndim) {
return std::pair(
false,
Expand Down Expand Up @@ -823,7 +825,7 @@ StatusAndReason SOMAArray::_can_set_shape_domainish_subhelper(
const std::vector<int64_t>& newshape,
bool check_current_domain,
std::string function_name_for_messages) {
Domain domain = arr_->schema().domain();
Domain domain = schema_->domain();

for (unsigned i = 0; i < domain.ndim(); i++) {
const auto& dim = domain.dimension(i);
Expand Down Expand Up @@ -947,7 +949,7 @@ void SOMAArray::_set_shape_helper(
const std::vector<int64_t>& newshape,
bool must_already_have,
std::string function_name_for_messages) {
if (mq_->query_type() != TILEDB_WRITE) {
if (arr_->query_type() != TILEDB_WRITE) {
throw TileDBSOMAError(fmt::format(
"{} array must be opened in write mode",
function_name_for_messages));
Expand Down Expand Up @@ -1003,7 +1005,7 @@ void SOMAArray::_set_soma_joinid_shape_helper(
int64_t newshape,
bool must_already_have,
std::string function_name_for_messages) {
if (mq_->query_type() != TILEDB_WRITE) {
if (arr_->query_type() != TILEDB_WRITE) {
throw TileDBSOMAError(fmt::format(
"{}: array must be opened in write mode",
function_name_for_messages));
Expand Down Expand Up @@ -1382,7 +1384,7 @@ void SOMAArray::_set_domain_helper(
const ArrowTable& newdomain,
bool must_already_have,
std::string function_name_for_messages) {
if (mq_->query_type() != TILEDB_WRITE) {
if (arr_->query_type() != TILEDB_WRITE) {
throw TileDBSOMAError(fmt::format(
"{}: array must be opened in write mode",
function_name_for_messages));
Expand Down Expand Up @@ -1572,7 +1574,7 @@ std::vector<int64_t> SOMAArray::_tiledb_domain() {
_check_dims_are_int64();

std::vector<int64_t> result;
auto dimensions = mq_->schema()->domain().dimensions();
auto dimensions = schema_->domain().dimensions();

for (const auto& dim : dimensions) {
result.push_back(
Expand All @@ -1595,7 +1597,7 @@ std::optional<int64_t> SOMAArray::_maybe_soma_joinid_maxshape() {
std::optional<int64_t> SOMAArray::_maybe_soma_joinid_tiledb_current_domain() {
const std::string dim_name = "soma_joinid";

auto dom = arr_->schema().domain();
auto dom = schema_->domain();
if (!dom.has_dimension(dim_name)) {
return std::nullopt;
}
Expand Down Expand Up @@ -1629,7 +1631,7 @@ std::optional<int64_t> SOMAArray::_maybe_soma_joinid_tiledb_current_domain() {
std::optional<int64_t> SOMAArray::_maybe_soma_joinid_tiledb_domain() {
const std::string dim_name = "soma_joinid";

auto dom = arr_->schema().domain();
auto dom = schema_->domain();
if (!dom.has_dimension(dim_name)) {
return std::nullopt;
}
Expand Down
37 changes: 10 additions & 27 deletions libtiledbsoma/src/soma/soma_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class SOMAArray : public SOMAObject {
, mq_(std::make_unique<ManagedQuery>(
other.arr_, other.ctx_->tiledb_ctx(), other.name_))
, arr_(other.arr_)
, schema_(other.schema_)
, meta_cache_arr_(other.meta_cache_arr_)
, first_read_next_(other.first_read_next_)
, submitted_(other.submitted_) {
Expand Down Expand Up @@ -299,8 +300,8 @@ class SOMAArray : public SOMAObject {
* @return OpenMode
*/
OpenMode mode() const {
return mq_->query_type() == TILEDB_READ ? OpenMode::read :
OpenMode::write;
return arr_->query_type() == TILEDB_READ ? OpenMode::read :
OpenMode::write;
}

/**
Expand Down Expand Up @@ -635,7 +636,7 @@ class SOMAArray : public SOMAObject {
* @return std::shared_ptr<ArraySchema> Schema
*/
std::shared_ptr<ArraySchema> tiledb_schema() const {
return mq_->schema();
return schema_;
}

/**
Expand All @@ -656,30 +657,9 @@ class SOMAArray : public SOMAObject {
* @return PlatformConfig
*/
PlatformConfig config_options_from_schema() const {
return ArrowAdapter::platform_config_from_tiledb_schema(*mq_->schema());
return ArrowAdapter::platform_config_from_tiledb_schema(*schema_);
}

/**
* @brief Get the mapping of attributes to Enumerations.
*
* @return std::map<std::string, Enumeration>
*/
std::map<std::string, Enumeration> get_attr_to_enum_mapping();

/**
* @brief Get the Enumeration name associated with the given Attr.
*
* @return std::optional<std::string> The enumeration name if one exists.
*/
std::optional<std::string> get_enum_label_on_attr(std::string attr_name);

/**
* @brief Check if the given attribute has an associated enumeration.
*
* @return bool
*/
bool attr_has_enum(std::string attr_name);

/**
* Set metadata key-value items to an open array. The array must
* opened in WRITE mode, otherwise the function will error out.
Expand Down Expand Up @@ -959,7 +939,7 @@ class SOMAArray : public SOMAObject {
"SOMAArray::_core_domain_slot: template-specialization "
"failure.");
}
return arr_->schema().domain().dimension(name).domain<T>();
return schema_->domain().dimension(name).domain<T>();
}

std::pair<std::string, std::string> _core_domain_slot_string(
Expand Down Expand Up @@ -1343,7 +1323,7 @@ class SOMAArray : public SOMAObject {
*/
CurrentDomain _get_current_domain() const {
return tiledb::ArraySchemaExperimental::current_domain(
*ctx_->tiledb_ctx(), arr_->schema());
*ctx_->tiledb_ctx(), *schema_);
}

/**
Expand Down Expand Up @@ -1587,6 +1567,9 @@ class SOMAArray : public SOMAObject {
// Array associated with mq_
std::shared_ptr<Array> arr_;

// Array schema
std::shared_ptr<ArraySchema> schema_;

// Array associated with metadata_. Metadata values need to be
// accessible in write mode as well. We need to keep this read-mode
// array alive in order for the metadata value pointers in the cache to
Expand Down

0 comments on commit a543b75

Please sign in to comment.