diff --git a/apis/python/setup.py b/apis/python/setup.py index e21a4f9a69..81a9e1587b 100644 --- a/apis/python/setup.py +++ b/apis/python/setup.py @@ -202,7 +202,7 @@ def run(self): CXX_FLAGS.append(f'-Wl,-rpath,{str(tiledb_dir / "lib")}') if sys.platform == "darwin": - CXX_FLAGS.append("-mmacosx-version-min=10.14") + CXX_FLAGS.append("-mmacosx-version-min=11.0") if os.name == "posix" and sys.platform != "darwin": LIB_DIRS.append(str(libtiledbsoma_dir / "lib" / "x86_64-linux-gnu")) diff --git a/apis/r/configure b/apis/r/configure index 1ebc4c3acb..1fa5d6e01d 100755 --- a/apis/r/configure +++ b/apis/r/configure @@ -49,7 +49,7 @@ tools/build_libtiledbsoma.sh pkgincl="-I../inst/tiledb/include -I../inst/tiledbsoma/include" pkglibs="-ltiledb -L../inst/tiledb/lib -ltiledbsoma -L../inst/tiledbsoma/lib" rpath="-Wl,-rpath,'\$\$ORIGIN/../tiledb/lib' -Wl,-rpath,'\$\$ORIGIN/../tiledbsoma/lib'" -macosver=`${R_HOME}/bin/Rscript -e 'if (Sys.info()["machine"] == "x86_64" && Sys.info()["sysname"] == "Darwin") cat("-mmacosx-version-min=10.14") else cat("")'` +macosver=`${R_HOME}/bin/Rscript -e 'if (Sys.info()["sysname"] == "Darwin") cat("-mmacosx-version-min=11.0") else cat("")'` sed -e "s|@tiledb_include@|$pkgincl |" \ -e "s|@tiledb_libs@|$pkglibs|" \ diff --git a/libtiledbsoma/src/soma/soma_array.cc b/libtiledbsoma/src/soma/soma_array.cc index 4686b49f55..6edebd9e9d 100644 --- a/libtiledbsoma/src/soma/soma_array.cc +++ b/libtiledbsoma/src/soma/soma_array.cc @@ -164,7 +164,7 @@ void SOMAArray::fill_metadata_cache() { } } -const std::string& SOMAArray::uri() const { +const std::string SOMAArray::uri() const { return uri_; }; @@ -189,6 +189,7 @@ void SOMAArray::open( arr_->close(); arr_->open(tdb_mode); } + reset(column_names(), batch_size_, result_order_); } void SOMAArray::close() { diff --git a/libtiledbsoma/src/soma/soma_array.h b/libtiledbsoma/src/soma/soma_array.h index a4d5d2e460..b1ce098273 100644 --- a/libtiledbsoma/src/soma/soma_array.h +++ b/libtiledbsoma/src/soma/soma_array.h @@ -43,11 +43,12 @@ #include "enums.h" #include "logger_public.h" #include "managed_query.h" +#include "soma_object.h" namespace tiledbsoma { using namespace tiledb; -class SOMAArray { +class SOMAArray : public SOMAObject { public: //=================================================================== //= public static @@ -167,9 +168,28 @@ class SOMAArray { ResultOrder result_order, std::optional> timestamp = std::nullopt); + SOMAArray(const SOMAArray& other) + : ctx_(other.ctx_) + , config_(other.config_) + , uri_(other.uri_) + , name_(other.name_) + , batch_size_(other.batch_size_) + , result_order_(other.result_order_) + , metadata_(other.metadata_) + , timestamp_(other.timestamp_) + , mq_(std::make_unique( + other.arr_, other.ctx_, other.name_)) + , arr_(other.arr_) + , first_read_next_(other.first_read_next_) + , submitted_(other.submitted_) { + } + + SOMAArray(const SOMAObject& other) + : SOMAObject(other) { + } + SOMAArray() = delete; - SOMAArray(const SOMAArray&) = delete; - SOMAArray(SOMAArray&&) = default; + SOMAArray(SOMAArray&&) = delete; ~SOMAArray() = default; /** @@ -177,7 +197,7 @@ class SOMAArray { * * @return std::string URI */ - const std::string& uri() const; + const std::string uri() const; /** * @brief Get Ctx of the SOMAArray. @@ -193,19 +213,6 @@ class SOMAArray { */ std::map config(); - std::optional soma_object_type() { - auto soma_object_type = this->get_metadata("soma_object_type"); - - if (!soma_object_type.has_value()) - return std::nullopt; - - const char* dtype = (const char*)std::get( - *soma_object_type); - uint32_t sz = std::get(*soma_object_type); - - return std::string(dtype, sz); - } - /** * Open the SOMAArray object. * @@ -709,6 +716,9 @@ class SOMAArray { // SOMAArray URI std::string uri_; + // SOMAArray name for debugging + std::string_view name_; + // Batch size std::string batch_size_; diff --git a/libtiledbsoma/src/soma/soma_collection.cc b/libtiledbsoma/src/soma/soma_collection.cc index 0a2d4444d4..d25998f55d 100644 --- a/libtiledbsoma/src/soma/soma_collection.cc +++ b/libtiledbsoma/src/soma/soma_collection.cc @@ -114,19 +114,22 @@ void SOMACollection::set( std::shared_ptr SOMACollection::get(const std::string& key) { auto member = group_->get_member(key); - std::string soma_object_type = this->type(); + std::optional soma_object_type = this->type(); - if (soma_object_type.compare("SOMACollection") == 0) + if (!soma_object_type) + throw TileDBSOMAError("Saw invalid SOMA object."); + + if (soma_object_type->compare("SOMACollection") == 0) return SOMACollection::open(member.uri(), OpenMode::read); - else if (soma_object_type.compare("SOMAExperiment") == 0) + else if (soma_object_type->compare("SOMAExperiment") == 0) return SOMAExperiment::open(member.uri(), OpenMode::read); - else if (soma_object_type.compare("SOMAMeasurement") == 0) + else if (soma_object_type->compare("SOMAMeasurement") == 0) return SOMAMeasurement::open(member.uri(), OpenMode::read); - else if (soma_object_type.compare("SOMADataFrame") == 0) + else if (soma_object_type->compare("SOMADataFrame") == 0) return SOMADataFrame::open(member.uri(), OpenMode::read); - else if (soma_object_type.compare("SOMASparseNDArray") == 0) + else if (soma_object_type->compare("SOMASparseNDArray") == 0) return SOMASparseNDArray::open(member.uri(), OpenMode::read); - else if (soma_object_type.compare("SOMADenseNDArray") == 0) + else if (soma_object_type->compare("SOMADenseNDArray") == 0) return SOMADenseNDArray::open(member.uri(), OpenMode::read); throw TileDBSOMAError("Saw invalid SOMA object."); diff --git a/libtiledbsoma/src/soma/soma_collection.h b/libtiledbsoma/src/soma/soma_collection.h index 98eb9a413e..c5bd0e8d4b 100644 --- a/libtiledbsoma/src/soma/soma_collection.h +++ b/libtiledbsoma/src/soma/soma_collection.h @@ -153,15 +153,6 @@ class SOMACollection : public SOMAObject { return group_->is_open(); } - /** - * Return the constant "SOMACollection". - * - * @return std::string - */ - const std::string type() const { - return "SOMACollection"; - } - /** * Get the SOMACollection URI. */ diff --git a/libtiledbsoma/src/soma/soma_dataframe.cc b/libtiledbsoma/src/soma/soma_dataframe.cc index 1b0d1cf9ea..a2bfde53ff 100644 --- a/libtiledbsoma/src/soma/soma_dataframe.cc +++ b/libtiledbsoma/src/soma/soma_dataframe.cc @@ -30,10 +30,6 @@ * This file defines the SOMADataFrame class. */ -#include - -#include -#include "array_buffers.h" #include "soma_dataframe.h" namespace tiledbsoma { @@ -84,96 +80,28 @@ std::unique_ptr SOMADataFrame::open( mode, uri, ctx, column_names, result_order, timestamp); } -//=================================================================== -//= public non-static -//=================================================================== - -SOMADataFrame::SOMADataFrame( - OpenMode mode, - std::string_view uri, - std::shared_ptr ctx, - std::vector column_names, - ResultOrder result_order, - std::optional> timestamp) { - std::string array_name = std::filesystem::path(uri).filename().string(); - array_ = std::make_shared( - mode, - uri, - array_name, // label used when debugging - ctx, - column_names, - "auto", // batch_size, - result_order, - timestamp); -} - -void SOMADataFrame::open( - OpenMode mode, std::optional> timestamp) { - array_->open(mode, timestamp); -} - -void SOMADataFrame::close() { - array_->close(); -} - bool SOMADataFrame::exists(std::string_view uri) { try { - auto soma_dataframe = SOMADataFrame::open(uri, OpenMode::read); - auto soma_object_type = soma_dataframe->get_metadata( - "soma_object_type"); - - if (!soma_object_type.has_value()) - return false; - - const char* dtype = (const char*)std::get( - *soma_object_type); - - uint32_t sz = std::get(*soma_object_type); - - return std::string(dtype, sz) == "SOMADataFrame"; - } catch (std::exception& e) { + return "SOMADataFrame" == SOMAObject::open(uri, OpenMode::read)->type(); + } catch (TileDBSOMAError& e) { return false; } } -bool SOMADataFrame::is_open() const { - return array_->is_open(); -} - -const std::string SOMADataFrame::uri() const { - return array_->uri(); -} - -std::shared_ptr SOMADataFrame::ctx() { - return array_->ctx(); -} - -std::map SOMADataFrame::config() { - return array_->config(); -} +//=================================================================== +//= public non-static +//=================================================================== std::unique_ptr SOMADataFrame::schema() const { - return array_->arrow_schema(); -} - -std::shared_ptr SOMADataFrame::tiledb_schema() const { - return array_->tiledb_schema(); + return this->arrow_schema(); } const std::vector SOMADataFrame::index_column_names() const { - return array_->dimension_names(); -} - -int64_t SOMADataFrame::count() const { - return array_->nnz(); -} - -std::optional> SOMADataFrame::read_next() { - return array_->read_next(); + return this->dimension_names(); } -void SOMADataFrame::write(std::shared_ptr buffers) { - array_->write(buffers); +uint64_t SOMADataFrame::count() { + return this->nnz(); } } // namespace tiledbsoma diff --git a/libtiledbsoma/src/soma/soma_dataframe.h b/libtiledbsoma/src/soma/soma_dataframe.h index 3b2527efb1..9f018fd0a3 100644 --- a/libtiledbsoma/src/soma/soma_dataframe.h +++ b/libtiledbsoma/src/soma/soma_dataframe.h @@ -33,18 +33,17 @@ #ifndef SOMA_DATAFRAME #define SOMA_DATAFRAME -#include "enums.h" +#include + #include "soma_array.h" -#include "soma_object.h" namespace tiledbsoma { -class SOMAArray; class ArrayBuffers; using namespace tiledb; -class SOMADataFrame : public SOMAObject { +class SOMADataFrame : public SOMAArray { public: //=================================================================== //= public static @@ -122,6 +121,13 @@ class SOMADataFrame : public SOMAObject { ResultOrder result_order = ResultOrder::automatic, std::optional> timestamp = std::nullopt); + /** + * @brief Check if the SOMADataFrame exists at the URI. + * + * @param uri URI to create the SOMADataFrame + */ + static bool exists(std::string_view uri); + //=================================================================== //= public non-static //=================================================================== @@ -143,90 +149,28 @@ class SOMADataFrame : public SOMAObject { std::shared_ptr ctx, std::vector column_names, ResultOrder result_order, - std::optional> timestamp = std::nullopt); + std::optional> timestamp = std::nullopt) + : SOMAArray( + mode, + uri, + std::string(std::filesystem::path(uri).filename()), // array name + ctx, + column_names, + "auto", // batch_size + result_order, + timestamp) { + } - SOMADataFrame(std::shared_ptr array) - : array_(array){}; + SOMADataFrame(const SOMAArray& other) + : SOMAArray(other) { + } SOMADataFrame() = delete; SOMADataFrame(const SOMADataFrame&) = default; - SOMADataFrame(SOMADataFrame&&) = default; + SOMADataFrame(SOMADataFrame&&) = delete; ~SOMADataFrame() = default; - /** - * Open the SOMADataFrame object. - * - * @param mode read or write - * @param timestamp Timestamp - */ - void open( - OpenMode mode, - std::optional> timestamp = std::nullopt); - - /** - * Close the SOMADataFrame object. - */ - void close(); - - void reset( - std::vector column_names = {}, - std::string_view batch_size = "auto", - ResultOrder result_order = ResultOrder::automatic) { - array_->reset(column_names, batch_size, result_order); - } - - /** - * @brief Check if the SOMADataFrame exists at the URI. - */ - static bool exists(std::string_view uri); - - /** - * Check if the SOMADataFrame is open. - * - * @return bool true if open - */ - bool is_open() const; - - OpenMode mode() const { - return array_->mode(); - } - - /** - * Return the constant "SOMADataFrame". - * - * @return std::string - */ - const std::string type() const { - return "SOMADataFrame"; - } - - /** - * @brief Get the URI of the SOMADataFrame. - * - * @return std::string URI - */ - const std::string uri() const; - - /** - * Get the Context associated with the SOMADataFrame. - * - * @return std::shared_ptr - */ - std::shared_ptr ctx(); - - /** - * Get the Config associated with the SOMADataFrame. - * - * @return std::map - */ - std::map config(); - - /** - * Return optional timestamp pair SOMADataFrame was opened with. - */ - std::optional> timestamp() { - return array_->timestamp(); - } + using SOMAArray::open; /** * Return the data schema, in the form of a ArrowSchema. @@ -235,13 +179,6 @@ class SOMADataFrame : public SOMAObject { */ std::unique_ptr schema() const; - /** - * Return the data schema, in the form of a TileDB ArraySchema. - * - * @return std::shared_ptr - */ - std::shared_ptr tiledb_schema() const; - /** * Return the index (dimension) column names. * @@ -254,231 +191,7 @@ class SOMADataFrame : public SOMAObject { * * @return int64_t */ - int64_t count() const; - - /** - * Retrieves the non-empty domain of the column index. - * - * @return int64_t - */ - template - std::pair non_empty_domain(const std::string& column_index_name) { - return array_->non_empty_domain(column_index_name); - } - - /** - * Retrieves the non-empty domain of the column index. - * Applicable only to var-sized dimensions. - */ - std::pair non_empty_domain_var( - const std::string& column_index_name) { - return array_->non_empty_domain_var(column_index_name); - } - - /** - * Returns the domain of the given column index. - * - * @tparam T Domain datatype - * @return Pair of [lower, upper] inclusive bounds. - */ - template - std::pair domain(const std::string& column_index_name) const { - return array_->domain(column_index_name); - } - - /** - * @brief Read the next chunk of results from the query. If all results have - * already been read, std::nullopt is returned. - */ - std::optional> read_next(); - - /** - * @brief Set the dimension slice using one point - * - * @note Partitioning is not supported - * - * @tparam T - * @param dim - * @param point - */ - template - void set_dim_point(const std::string& dim, const T& point) { - array_->set_dim_point(dim, point); - } - - /** - * @brief Set the dimension slice using multiple points, with support - * for partitioning. - * - * @tparam T - * @param dim - * @param points - */ - template - void set_dim_points( - const std::string& dim, - const tcb::span points, - int partition_index, - int partition_count) { - array_->set_dim_points(dim, points, partition_index, partition_count); - } - - /** - * @brief Set the dimension slice using multiple points - * - * @note Partitioning is not supported - * - * @tparam T - * @param dim - * @param points - */ - template - void set_dim_points(const std::string& dim, const std::vector& points) { - array_->set_dim_points(dim, points); - } - - /** - * @brief Set the dimension slice using multiple ranges - * - * @note Partitioning is not supported - * - * @tparam T - * @param dim - * @param ranges - */ - template - void set_dim_ranges( - const std::string& dim, const std::vector>& ranges) { - array_->set_dim_ranges(dim, ranges); - } - - /** - * @brief Set a query condition. - * - * @param qc Query condition - */ - void set_condition(QueryCondition& qc) { - array_->set_condition(qc); - } - - /** - * @brief Returns the column names set by the query. - * - * @return std::vector - */ - std::vector column_names() { - return array_->column_names(); - } - - /** - * @brief Write data to the dataframe. - * @param buffers The ArrayBuffers to write - */ - void write(std::shared_ptr buffers); - - /** - * Set metadata key-value items to a SOMADataFrame. The SOMADataFrame must - * opened in WRITE mode, otherwise the function will error out. - * - * @param key The key of the metadata item to be added. UTF-8 encodings - * are acceptable. - * @param value_type The datatype of the value. - * @param value_num The value may consist of more than one items of the - * same datatype. This argument indicates the number of items in the - * value component of the metadata. - * @param value The metadata value in binary form. - * - * @note The writes will take effect only upon closing the array. - */ - void set_metadata( - const std::string& key, - tiledb_datatype_t value_type, - uint32_t value_num, - const void* value) { - array_->set_metadata(key, value_type, value_num, value); - } - - /** - * Delete a metadata key-value item from an open SOMADataFrame. The - * SOMADataFrame must be opened in WRITE mode, otherwise the function will - * error out. - * - * @param key The key of the metadata item to be deleted. - * - * @note The writes will take effect only upon closing the group. - * - * @note If the key does not exist, this will take no effect - * (i.e., the function will not error out). - */ - void delete_metadata(const std::string& key) { - array_->delete_metadata(key); - } - - /** - * @brief Given a key, get the associated value datatype, number of - * values, and value in binary form. - * - * The value may consist of more than one items of the same datatype. Keys - * that do not exist in the metadata will be return NULL for the value. - * - * **Example:** - * @code{.cpp} - * // Open the group for reading - * tiledbsoma::SOMAGroup soma_group = SOMAGroup::open(TILEDB_READ, - "s3://bucket-name/group-name"); - * tiledbsoma::MetadataValue meta_val = soma_group->get_metadata("key"); - * std::string key = std::get(meta_val); - * tiledb_datatype_t dtype = std::get(meta_val); - * uint32_t num = std::get(meta_val); - * const void* value = *((const - int32_t*)std::get(meta_val)); - * @endcode - * - * @param key The key of the metadata item to be retrieved. UTF-8 encodings - * are acceptable. - * @return MetadataValue (std::tuple) - */ - std::optional get_metadata(const std::string& key) { - return array_->get_metadata(key); - } - - /** - * Get a mapping of all metadata keys with its associated value datatype, - * number of values, and value in binary form. - * - * @return std::map - */ - std::map get_metadata() { - return array_->get_metadata(); - } - - /** - * Check if the key exists in metadata from an open SOMADataFrame. - * - * @param key The key of the metadata item to be checked. UTF-8 encodings - * are acceptable. - * @return true if the key exists, else false. - */ - bool has_metadata(const std::string& key) { - return array_->has_metadata(key); - } - - /** - * Return then number of metadata items in an open SOMADataFrame. The group - * must be opened in READ mode, otherwise the function will error out. - */ - uint64_t metadata_num() const { - return array_->metadata_num(); - } - - private: - //=================================================================== - //= private non-static - //=================================================================== - - // SOMAArray - std::shared_ptr array_; + uint64_t count(); }; } // namespace tiledbsoma diff --git a/libtiledbsoma/src/soma/soma_dense_ndarray.cc b/libtiledbsoma/src/soma/soma_dense_ndarray.cc index 93d33f0095..e73b7cc7d3 100644 --- a/libtiledbsoma/src/soma/soma_dense_ndarray.cc +++ b/libtiledbsoma/src/soma/soma_dense_ndarray.cc @@ -29,9 +29,6 @@ * * This file defines the SOMADenseNDArray class. */ - -#include - #include "soma_dense_ndarray.h" namespace tiledbsoma { @@ -89,70 +86,8 @@ std::unique_ptr SOMADenseNDArray::open( //= public non-static //=================================================================== -SOMADenseNDArray::SOMADenseNDArray( - OpenMode mode, - std::string_view uri, - std::shared_ptr ctx, - std::vector column_names, - ResultOrder result_order, - std::optional> timestamp) { - std::string array_name = std::filesystem::path(uri).filename().string(); - array_ = std::make_shared( - mode, - uri, - array_name, // label used when debugging - ctx, - column_names, - "auto", // batch_size, - result_order, - timestamp); - array_->reset(); -} - -void SOMADenseNDArray::open( - OpenMode mode, std::optional> timestamp) { - array_->open(mode, timestamp); - array_->reset(); -} - -void SOMADenseNDArray::close() { - array_->close(); -} - -bool SOMADenseNDArray::is_open() const { - return array_->is_open(); -} - -const std::string SOMADenseNDArray::uri() const { - return array_->uri(); -} - -std::shared_ptr SOMADenseNDArray::ctx() { - return array_->ctx(); -} - std::unique_ptr SOMADenseNDArray::schema() const { - return array_->arrow_schema(); -} - -std::shared_ptr SOMADenseNDArray::tiledb_schema() const { - return array_->tiledb_schema(); -} - -std::vector SOMADenseNDArray::shape() const { - return array_->shape(); -} - -int64_t SOMADenseNDArray::ndim() const { - return array_->ndim(); -} - -std::optional> SOMADenseNDArray::read_next() { - return array_->read_next(); -} - -void SOMADenseNDArray::write(std::shared_ptr buffers) { - array_->write(buffers); + return this->arrow_schema(); } } // namespace tiledbsoma diff --git a/libtiledbsoma/src/soma/soma_dense_ndarray.h b/libtiledbsoma/src/soma/soma_dense_ndarray.h index 5567c34d66..2a0a3667d1 100644 --- a/libtiledbsoma/src/soma/soma_dense_ndarray.h +++ b/libtiledbsoma/src/soma/soma_dense_ndarray.h @@ -33,18 +33,17 @@ #ifndef SOMA_DENSE_NDARRAY #define SOMA_DENSE_NDARRAY -#include "enums.h" +#include + #include "soma_array.h" -#include "soma_object.h" namespace tiledbsoma { -class SOMAArray; class ArrayBuffers; using namespace tiledb; -class SOMADenseNDArray : public SOMAObject { +class SOMADenseNDArray : public SOMAArray { public: //=================================================================== //= public static @@ -142,45 +141,19 @@ class SOMADenseNDArray : public SOMAObject { std::shared_ptr ctx, std::vector column_names, ResultOrder result_order, - std::optional> timestamp); - - /** - * Open the SOMADenseNDArray object. - * - * @param mode read or write - * @param timestamp Timestamp - */ - void open( - OpenMode mode, - std::optional> timestamp = std::nullopt); - - /** - * Closes the SOMADenseNDArray object. - */ - void close(); - - /** - * Check if the SOMADenseNDArray is open. - * - * @return bool true if open - */ - bool is_open() const; - - /** - * Returns the constant "SOMADenseNDArray". - * - * @return std::string - */ - const std::string type() const { - return "SOMADenseNDArray"; + std::optional> timestamp) + : SOMAArray( + mode, + uri, + std::string(std::filesystem::path(uri).filename()), // array name + ctx, + column_names, + "auto", // batch_size + result_order, + timestamp) { } - /** - * Get the Context associated with the SOMADenseNDArray. - * - * @return std::shared_ptr - */ - std::shared_ptr ctx(); + using SOMAArray::open; /** * Return whether the SOMADenseNDArray is sparse. @@ -191,157 +164,12 @@ class SOMADenseNDArray : public SOMAObject { return false; } - /** - * @brief Get URI of the SOMADenseNDArray. - * - * @return std::string URI - */ - const std::string uri() const; - /** * Return the data schema, in the form of an ArrowSchema. * * @return std::unique_ptr */ std::unique_ptr schema() const; - - /** - * Return the data schema, in the form of a TileDB ArraySchema. - * - * @return std::shared_ptr - */ - std::shared_ptr tiledb_schema() const; - - /** - * @brief Get the capacity of each dimension. - * - * @return A vector with length equal to the number of dimensions; each - * value in the vector is the capcity of each dimension. - */ - std::vector shape() const; - - /** - * Return the number of dimensions. - * - * @return int64_t - */ - int64_t ndim() const; - - /** - * @brief Read the next chunk of results from the query. If all results have - * already been read, std::nullopt is returned. - */ - std::optional> read_next(); - - /** - * @brief Write ArrayBuffers data to the dataframe. - * @param buffers The ArrayBuffers to write - */ - void write(std::shared_ptr buffers); - - /** - * Set metadata key-value items to a SOMADenseNDArray. The SOMADenseNDArray - * must opened in WRITE mode, otherwise the function will error out. - * - * @param key The key of the metadata item to be added. UTF-8 encodings - * are acceptable. - * @param value_type The datatype of the value. - * @param value_num The value may consist of more than one items of the - * same datatype. This argument indicates the number of items in the - * value component of the metadata. - * @param value The metadata value in binary form. - * - * @note The writes will take effect only upon closing the array. - */ - void set_metadata( - const std::string& key, - tiledb_datatype_t value_type, - uint32_t value_num, - const void* value) { - array_->set_metadata(key, value_type, value_num, value); - } - - /** - * Delete a metadata key-value item from an open SOMADenseNDArray. The - * SOMADenseNDArray must be opened in WRITE mode, otherwise the function - * will error out. - * - * @param key The key of the metadata item to be deleted. - * - * @note The writes will take effect only upon closing the group. - * - * @note If the key does not exist, this will take no effect - * (i.e., the function will not error out). - */ - void delete_metadata(const std::string& key) { - array_->delete_metadata(key); - } - - /** - * @brief Given a key, get the associated value datatype, number of - * values, and value in binary form. - * - * The value may consist of more than one items of the same datatype. Keys - * that do not exist in the metadata will be return NULL for the value. - * - * **Example:** - * @code{.cpp} - * // Open the group for reading - * tiledbsoma::SOMAGroup soma_group = SOMAGroup::open(TILEDB_READ, - "s3://bucket-name/group-name"); - * tiledbsoma::MetadataValue meta_val = soma_group->get_metadata("key"); - * std::string key = std::get(meta_val); - * tiledb_datatype_t dtype = std::get(meta_val); - * uint32_t num = std::get(meta_val); - * const void* value = *((const - int32_t*)std::get(meta_val)); - * @endcode - * - * @param key The key of the metadata item to be retrieved. UTF-8 encodings - * are acceptable. - * @return MetadataValue (std::tuple) - */ - std::optional get_metadata(const std::string& key) { - return array_->get_metadata(key); - } - - /** - * Get a mapping of all metadata keys with its associated value datatype, - * number of values, and value in binary form. - * - * @return std::map - */ - std::map get_metadata() { - return array_->get_metadata(); - } - - /** - * Check if the key exists in metadata from an open SOMADenseNDArray. - * - * @param key The key of the metadata item to be checked. UTF-8 encodings - * are acceptable. - * @return true if the key exists, else false. - */ - bool has_metadata(const std::string& key) { - return array_->has_metadata(key); - } - - /** - * Return then number of metadata items in an open SOMADenseNDArray. The - * group must be opened in READ mode, otherwise the function will error out. - */ - uint64_t metadata_num() const { - return array_->metadata_num(); - } - - private: - //=================================================================== - //= private non-static - //=================================================================== - - // SOMAArray - std::shared_ptr array_; }; } // namespace tiledbsoma diff --git a/libtiledbsoma/src/soma/soma_group.h b/libtiledbsoma/src/soma/soma_group.h index f0f8fd383f..d53ebe403f 100644 --- a/libtiledbsoma/src/soma/soma_group.h +++ b/libtiledbsoma/src/soma/soma_group.h @@ -40,11 +40,12 @@ #include "../utils/common.h" #include "enums.h" +#include "soma_object.h" namespace tiledbsoma { using namespace tiledb; -class SOMAGroup { +class SOMAGroup : public SOMAObject { public: //=================================================================== //= public static diff --git a/libtiledbsoma/src/soma/soma_object.cc b/libtiledbsoma/src/soma/soma_object.cc index 655adbf30e..974262c164 100644 --- a/libtiledbsoma/src/soma/soma_object.cc +++ b/libtiledbsoma/src/soma/soma_object.cc @@ -10,7 +10,7 @@ namespace tiledbsoma { using namespace tiledb; std::unique_ptr SOMAObject::open( - std::string uri, + std::string_view uri, OpenMode mode, std::map platform_config, std::optional> timestamp) { @@ -19,18 +19,22 @@ std::unique_ptr SOMAObject::open( } std::unique_ptr SOMAObject::open( - std::string uri, + std::string_view uri, OpenMode mode, std::shared_ptr ctx, std::optional> timestamp) { - auto obj = tiledb::Object::object(*ctx, uri); + auto obj = tiledb::Object::object(*ctx, std::string(uri)); if (obj.type() == tiledb::Object::Type::Array) { auto array_ = SOMAArray::open( mode, ctx, uri, "", {}, "auto", ResultOrder::automatic, timestamp); - if (array_->soma_object_type() == "SOMADataFrame") - return std::make_unique(std::move(array_)); + if (!array_->type().has_value()) + throw TileDBSOMAError( + "Invalid SOMAObject passed to SOMAObject::open"); + + if (*(array_->type()) == "SOMADataFrame") + return std::make_unique(*array_); else throw TileDBSOMAError( "Invalid SOMAObject passed to SOMAObject::open"); @@ -39,4 +43,17 @@ std::unique_ptr SOMAObject::open( throw TileDBSOMAError("Invalid TileDB object passed to SOMAObject::open"); } +const std::optional SOMAObject::type() { + auto soma_object_type = this->get_metadata("soma_object_type"); + + if (!soma_object_type.has_value()) + return std::nullopt; + + const char* dtype = (const char*)std::get( + *soma_object_type); + uint32_t sz = std::get(*soma_object_type); + + return std::string(dtype, sz); +} + } // namespace tiledbsoma diff --git a/libtiledbsoma/src/soma/soma_object.h b/libtiledbsoma/src/soma/soma_object.h index 43a209885e..456241ff87 100644 --- a/libtiledbsoma/src/soma/soma_object.h +++ b/libtiledbsoma/src/soma/soma_object.h @@ -41,8 +41,6 @@ namespace tiledbsoma { -class SOMADataFrame; - using namespace tiledb; class SOMAObject { public: @@ -52,13 +50,13 @@ class SOMAObject { virtual ~SOMAObject() = default; static std::unique_ptr open( - std::string uri, + std::string_view uri, OpenMode mode, std::map platform_config = {}, std::optional> timestamp = std::nullopt); static std::unique_ptr open( - std::string uri, + std::string_view uri, OpenMode mode, std::shared_ptr ctx, std::optional> timestamp = std::nullopt); @@ -66,7 +64,7 @@ class SOMAObject { /** * @brief Return a constant string describing the type of the object. */ - virtual const std::string type() const = 0; + const std::optional type(); /** * @brief Get URI of the SOMAObject. diff --git a/libtiledbsoma/src/soma/soma_sparse_ndarray.cc b/libtiledbsoma/src/soma/soma_sparse_ndarray.cc index 308fb0ddde..8147a2d54a 100644 --- a/libtiledbsoma/src/soma/soma_sparse_ndarray.cc +++ b/libtiledbsoma/src/soma/soma_sparse_ndarray.cc @@ -30,8 +30,6 @@ * This file defines the SOMASparseNDArray class. */ -#include - #include "soma_sparse_ndarray.h" namespace tiledbsoma { @@ -89,74 +87,7 @@ std::unique_ptr SOMASparseNDArray::open( //= public non-static //=================================================================== -SOMASparseNDArray::SOMASparseNDArray( - OpenMode mode, - std::string_view uri, - std::shared_ptr ctx, - std::vector column_names, - ResultOrder result_order, - std::optional> timestamp) { - std::string array_name = std::filesystem::path(uri).filename().string(); - array_ = std::make_shared( - mode, - uri, - array_name, // label used when debugging - ctx, - column_names, - "auto", // batch_size, - result_order, - timestamp); - array_->reset(); -} - -void SOMASparseNDArray::open( - OpenMode mode, std::optional> timestamp) { - array_->open(mode, timestamp); - array_->reset(); -} - -void SOMASparseNDArray::close() { - array_->close(); -} - -bool SOMASparseNDArray::is_open() const { - return array_->is_open(); -} - -const std::string SOMASparseNDArray::uri() const { - return array_->uri(); -} - -std::shared_ptr SOMASparseNDArray::ctx() { - return array_->ctx(); -} - std::unique_ptr SOMASparseNDArray::schema() const { - return array_->arrow_schema(); -} - -std::shared_ptr SOMASparseNDArray::tiledb_schema() const { - return array_->tiledb_schema(); -} - -std::vector SOMASparseNDArray::shape() const { - return array_->shape(); + return this->arrow_schema(); } - -int64_t SOMASparseNDArray::ndim() const { - return array_->ndim(); -} - -uint64_t SOMASparseNDArray::nnz() const { - return array_->nnz(); -} - -std::optional> SOMASparseNDArray::read_next() { - return array_->read_next(); -} - -void SOMASparseNDArray::write(std::shared_ptr buffers) { - array_->write(buffers); -} - } // namespace tiledbsoma diff --git a/libtiledbsoma/src/soma/soma_sparse_ndarray.h b/libtiledbsoma/src/soma/soma_sparse_ndarray.h index 911c1b8ba0..0d1979e57d 100644 --- a/libtiledbsoma/src/soma/soma_sparse_ndarray.h +++ b/libtiledbsoma/src/soma/soma_sparse_ndarray.h @@ -33,18 +33,17 @@ #ifndef SOMA_SPARSE_NDARRAY #define SOMA_SPARSE_NDARRAY -#include "enums.h" +#include + #include "soma_array.h" -#include "soma_object.h" namespace tiledbsoma { -class SOMAArray; class ArrayBuffers; using namespace tiledb; -class SOMASparseNDArray : public SOMAObject { +class SOMASparseNDArray : public SOMAArray { public: //=================================================================== //= public static @@ -142,45 +141,19 @@ class SOMASparseNDArray : public SOMAObject { std::shared_ptr ctx, std::vector column_names, ResultOrder result_order, - std::optional> timestamp); - - /** - * Open the SOMASparseNDArray object. - * - * @param mode read or write - * @param timestamp Timestamp - */ - void open( - OpenMode mode, - std::optional> timestamp = std::nullopt); - - /** - * Closes the SOMASparseNDArray object. - */ - void close(); - - /** - * Check if the SOMASparseNDArray is open. - * - * @return bool true if open - */ - bool is_open() const; - - /** - * Returns the constant "SOMASparseNDArray". - * - * @return std::string - */ - const std::string type() const { - return "SOMASparseNDArray"; + std::optional> timestamp) + : SOMAArray( + mode, + uri, + std::string(std::filesystem::path(uri).filename()), // array name + ctx, + column_names, + "auto", // batch_size + result_order, + timestamp) { } - /** - * Get the Context associated with the SOMASparseNDArray. - * - * @return std::shared_ptr - */ - std::shared_ptr ctx(); + using SOMAArray::open; /** * Return whether the NDArray is sparse. @@ -191,165 +164,12 @@ class SOMASparseNDArray : public SOMAObject { return true; } - /** - * @brief Get URI of the SOMASparseNDArray. - * - * @return std::string URI - */ - const std::string uri() const; - /** * Return the data schema, in the form of an ArrowSchema. * * @return std::unique_ptr */ std::unique_ptr schema() const; - - /** - * Return the data schema, in the form of a TileDB ArraySchema. - * - * @return std::shared_ptr - */ - std::shared_ptr tiledb_schema() const; - - /** - * @brief Get the capacity of each dimension. - * - * @return A vector with length equal to the number of dimensions; each - * value in the vector is the capcity of each dimension. - */ - std::vector shape() const; - - /** - * Return the number of dimensions. - * - * @return int64_t - */ - int64_t ndim() const; - - /** - * @brief Get the total number of shared cells in the array. - * - * @return uint64_t Total number of shared cells - */ - uint64_t nnz() const; - - /** - * @brief Read the next chunk of results from the query. If all results have - * already been read, std::nullopt is returned. - */ - std::optional> read_next(); - - /** - * @brief Write ArrayBuffers data to the dataframe. - * @param buffers The ArrayBuffers to write - */ - void write(std::shared_ptr buffers); - - /** - * Set metadata key-value items to a SOMASparseNDArray. The - * SOMASparseNDArray must opened in WRITE mode, otherwise the function will - * error out. - * - * @param key The key of the metadata item to be added. UTF-8 encodings - * are acceptable. - * @param value_type The datatype of the value. - * @param value_num The value may consist of more than one items of the - * same datatype. This argument indicates the number of items in the - * value component of the metadata. - * @param value The metadata value in binary form. - * - * @note The writes will take effect only upon closing the array. - */ - void set_metadata( - const std::string& key, - tiledb_datatype_t value_type, - uint32_t value_num, - const void* value) { - array_->set_metadata(key, value_type, value_num, value); - } - - /** - * Delete a metadata key-value item from an open SOMASparseNDArray. The - * SOMASparseNDArray must be opened in WRITE mode, otherwise the function - * will error out. - * - * @param key The key of the metadata item to be deleted. - * - * @note The writes will take effect only upon closing the group. - * - * @note If the key does not exist, this will take no effect - * (i.e., the function will not error out). - */ - void delete_metadata(const std::string& key) { - array_->delete_metadata(key); - } - - /** - * @brief Given a key, get the associated value datatype, number of - * values, and value in binary form. - * - * The value may consist of more than one items of the same datatype. Keys - * that do not exist in the metadata will be return NULL for the value. - * - * **Example:** - * @code{.cpp} - * // Open the group for reading - * tiledbsoma::SOMAGroup soma_group = SOMAGroup::open(TILEDB_READ, - "s3://bucket-name/group-name"); - * tiledbsoma::MetadataValue meta_val = soma_group->get_metadata("key"); - * std::string key = std::get(meta_val); - * tiledb_datatype_t dtype = std::get(meta_val); - * uint32_t num = std::get(meta_val); - * const void* value = *((const - int32_t*)std::get(meta_val)); - * @endcode - * - * @param key The key of the metadata item to be retrieved. UTF-8 encodings - * are acceptable. - * @return MetadataValue (std::tuple) - */ - std::optional get_metadata(const std::string& key) { - return array_->get_metadata(key); - } - - /** - * Get a mapping of all metadata keys with its associated value datatype, - * number of values, and value in binary form. - * - * @return std::map - */ - std::map get_metadata() { - return array_->get_metadata(); - } - - /** - * Check if the key exists in metadata from an open SOMASparseNDArray. - * - * @param key The key of the metadata item to be checked. UTF-8 encodings - * are acceptable. - * @return true if the key exists, else false. - */ - bool has_metadata(const std::string& key) { - return array_->has_metadata(key); - } - - /** - * Return then number of metadata items in an open SOMASparseNDArray. The - * group must be opened in READ mode, otherwise the function will error out. - */ - uint64_t metadata_num() const { - return array_->metadata_num(); - } - - private: - //=================================================================== - //= private non-static - //=================================================================== - - // SOMAArray - std::shared_ptr array_; }; } // namespace tiledbsoma