Skip to content

Commit 827f609

Browse files
Move tiledb_ctx_alloc_with_error to the stable API header and use it in the C++ API. (#5667)
Move `tiledb_ctx_alloc_with_error` to the stable API header and use it in the C++ API. --- TYPE: C_API DESC: The `tiledb_ctx_alloc_with_error` function was moved to the stable API header. --- TYPE: CPP_API DESC: Improved error reporting when creating a context fails.
1 parent bec2ff5 commit 827f609

File tree

6 files changed

+54
-104
lines changed

6 files changed

+54
-104
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ list(APPEND TILEDB_C_API_RELATIVE_HEADERS
314314
"${CMAKE_SOURCE_DIR}/tiledb/api/c_api/buffer/buffer_api_external.h"
315315
"${CMAKE_SOURCE_DIR}/tiledb/api/c_api/buffer_list/buffer_list_api_external.h"
316316
"${CMAKE_SOURCE_DIR}/tiledb/api/c_api/config/config_api_external.h"
317-
"${CMAKE_SOURCE_DIR}/tiledb/api/c_api/context/context_api_experimental.h"
318317
"${CMAKE_SOURCE_DIR}/tiledb/api/c_api/context/context_api_external.h"
319318
"${CMAKE_SOURCE_DIR}/tiledb/api/c_api/current_domain/current_domain_api_enum.h"
320319
"${CMAKE_SOURCE_DIR}/tiledb/api/c_api/current_domain/current_domain_api_external_experimental.h"

tiledb/api/c_api/context/context_api.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
*/
3232

3333
#include "../config/config_api_internal.h"
34-
#include "context_api_experimental.h"
3534
#include "context_api_external.h"
3635
#include "context_api_internal.h"
3736
#include "tiledb/api/c_api_support/c_api_support.h"

tiledb/api/c_api/context/context_api_experimental.h

Lines changed: 0 additions & 87 deletions
This file was deleted.

tiledb/api/c_api/context/context_api_external.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,39 @@ typedef struct tiledb_ctx_handle_t tiledb_ctx_t;
7373
TILEDB_EXPORT capi_return_t
7474
tiledb_ctx_alloc(tiledb_config_t* config, tiledb_ctx_t** ctx) TILEDB_NOEXCEPT;
7575

76+
/**
77+
* Creates a TileDB context, which contains the TileDB storage manager
78+
* that manages everything in the TileDB library.
79+
*
80+
* **Examples:**
81+
*
82+
* Without config (i.e., use default configuration):
83+
*
84+
* @code{.c}
85+
* tiledb_ctx_t* ctx;
86+
* tiledb_error_t* error;
87+
* tiledb_ctx_alloc_with_error(NULL, &ctx, &error);
88+
* @endcode
89+
*
90+
* With some config:
91+
*
92+
* @code{.c}
93+
* tiledb_ctx_t* ctx;
94+
* tiledb_error_t* error;
95+
* tiledb_ctx_alloc_with_error(config, &ctx, &error);
96+
* @endcode
97+
*
98+
* @param[in] config The configuration parameters (`NULL` means default).
99+
* @param[out] ctx The TileDB context to be created.
100+
* @param[out] error Error object returned upon error (`NULL` if there is
101+
* no error).
102+
* @return `TILEDB_OK` for success and `TILEDB_OOM` or `TILEDB_ERR` for error.
103+
*/
104+
TILEDB_EXPORT capi_return_t tiledb_ctx_alloc_with_error(
105+
tiledb_config_t* config,
106+
tiledb_ctx_t** ctx,
107+
tiledb_error_t** error) TILEDB_NOEXCEPT;
108+
76109
/**
77110
* Destroys the TileDB context, freeing all associated memory and resources.
78111
*

tiledb/sm/c_api/tiledb_experimental.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "tiledb/api/c_api/array_schema/array_schema_api_experimental.h"
4545
#include "tiledb/api/c_api/array_schema_evolution/array_schema_evolution_api_experimental.h"
4646
#include "tiledb/api/c_api/attribute/attribute_api_external_experimental.h"
47-
#include "tiledb/api/c_api/context/context_api_experimental.h"
4847
#include "tiledb/api/c_api/current_domain/current_domain_api_external_experimental.h"
4948
#include "tiledb/api/c_api/enumeration/enumeration_api_experimental.h"
5049
#include "tiledb/api/c_api/fragment_info/fragment_info_api_experimental.h"

tiledb/sm/cpp_api/context.h

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,19 @@ class Context {
7979
* Constructor. Creates a TileDB Context with default configuration.
8080
* @throws TileDBError if construction fails
8181
*/
82-
Context() {
83-
tiledb_ctx_t* ctx;
84-
if (tiledb_ctx_alloc(nullptr, &ctx) != TILEDB_OK)
85-
throw TileDBError("[TileDB::C++API] Error: Failed to create context");
86-
ctx_ = std::shared_ptr<tiledb_ctx_t>(ctx, Context::free);
87-
error_handler_ = default_error_handler;
88-
82+
Context()
83+
: ctx_(make_ctx(nullptr))
84+
, error_handler_(default_error_handler) {
8985
set_tag("x-tiledb-api-language", "c++");
9086
}
9187

9288
/**
9389
* Constructor. Creates a TileDB context with the given configuration.
9490
* @throws TileDBError if construction fails
9591
*/
96-
explicit Context(const Config& config) {
97-
tiledb_ctx_t* ctx;
98-
if (tiledb_ctx_alloc(config.ptr().get(), &ctx) != TILEDB_OK)
99-
throw TileDBError("[TileDB::C++API] Error: Failed to create context");
100-
ctx_ = std::shared_ptr<tiledb_ctx_t>(ctx, Context::free);
101-
error_handler_ = default_error_handler;
102-
92+
explicit Context(const Config& config)
93+
: ctx_(make_ctx(config.ptr().get()))
94+
, error_handler_(default_error_handler) {
10395
set_tag("x-tiledb-api-language", "c++");
10496
}
10597

@@ -264,6 +256,21 @@ class Context {
264256
/** An error handler callback. */
265257
std::function<void(const std::string&)> error_handler_;
266258

259+
static std::shared_ptr<tiledb_ctx_t> make_ctx(tiledb_config_t* config) {
260+
tiledb_ctx_t* ctx;
261+
tiledb_error_t* err;
262+
tiledb_ctx_alloc_with_error(config, &ctx, &err);
263+
if (err != nullptr) {
264+
const char* msg_cstr;
265+
tiledb_error_message(err, &msg_cstr);
266+
std::string msg = "[TileDB::C++API] Error: Failed to create context: " +
267+
std::string(msg_cstr);
268+
tiledb_error_free(&err);
269+
throw TileDBError(msg);
270+
}
271+
return std::shared_ptr<tiledb_ctx_t>(ctx, Context::free);
272+
}
273+
267274
/** Wrapper function for freeing a context C object. */
268275
static void free(tiledb_ctx_t* ctx) {
269276
tiledb_ctx_free(&ctx);

0 commit comments

Comments
 (0)