Skip to content

Commit 3707a72

Browse files
authored
Add C/C++ API for getting REST data protocol (#5697)
Adds a new API that can be used to tell which Server/data model the REST client is configured to reach out to. This is needed as there are differences in the data model between legacy and new TileDB Server, especially when it comes to group operations, so a user needs to be able to tell in an easy way to which server she is talking to. --- TYPE: C_API DESC: Add C API for getting REST data protocol TYPE: CPP_API DESC: Add C++ API for getting REST data protocol
1 parent e96ba69 commit 3707a72

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

test/src/unit-ctx.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <test/support/tdb_catch.h>
3535
#include "test/support/src/helpers.h"
36+
#include "test/support/src/vfs_helpers.h"
3637
#include "tiledb/api/c_api/context/context_api_internal.h"
3738
#include "tiledb/sm/c_api/tiledb_struct_def.h"
3839
#include "tiledb/sm/cpp_api/tiledb"
@@ -103,3 +104,58 @@ TEST_CASE("C++ API: Test context tags", "[cppapi][ctx-tags]") {
103104
}
104105
}
105106
}
107+
108+
TEST_CASE(
109+
"C API: Test context data protocol", "[capi][ctx][data-protocol][rest]") {
110+
tiledb::test::VFSTestSetup vfs_test_setup{};
111+
tiledb_ctx_t* ctx = vfs_test_setup.ctx_c;
112+
113+
tiledb_data_protocol_t data_protocol;
114+
115+
SECTION("tiledb:// URI") {
116+
if (vfs_test_setup.is_rest()) {
117+
auto rc = tiledb_ctx_get_data_protocol(
118+
ctx, "tiledb://workspace/teamspace/array", &data_protocol);
119+
REQUIRE(rc == TILEDB_OK);
120+
if (vfs_test_setup.is_legacy_rest()) {
121+
REQUIRE(data_protocol == TILEDB_DATA_PROTOCOL_v2);
122+
} else {
123+
REQUIRE(data_protocol == TILEDB_DATA_PROTOCOL_v3);
124+
}
125+
}
126+
}
127+
128+
SECTION("non-tiledb:// URI") {
129+
auto rc = tiledb_ctx_get_data_protocol(
130+
ctx, "azure://bucket/array", &data_protocol);
131+
REQUIRE(rc == TILEDB_OK);
132+
REQUIRE(data_protocol == TILEDB_DATA_PROTOCOL_v2);
133+
}
134+
}
135+
136+
TEST_CASE(
137+
"C++ API: Test context data protocol",
138+
"[cppapi][ctx][data-protocol][rest]") {
139+
tiledb::test::VFSTestSetup vfs_test_setup{};
140+
tiledb::Context ctx{vfs_test_setup.ctx()};
141+
142+
SECTION("tiledb:// URI") {
143+
if (vfs_test_setup.is_rest()) {
144+
if (vfs_test_setup.is_legacy_rest()) {
145+
REQUIRE(
146+
ctx.data_protocol("tiledb://workspace/teamspace/array") ==
147+
tiledb::Context::DataProtocol::v2);
148+
} else {
149+
REQUIRE(
150+
ctx.data_protocol("tiledb://workspace/teamspace/array") ==
151+
tiledb::Context::DataProtocol::v3);
152+
}
153+
}
154+
}
155+
156+
SECTION("non-tiledb:// URI") {
157+
REQUIRE(
158+
ctx.data_protocol("s3://bucket/array") ==
159+
tiledb::Context::DataProtocol::v2);
160+
}
161+
}

tiledb/api/c_api/context/context_api.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ capi_return_t tiledb_ctx_set_tag(
142142
return TILEDB_OK;
143143
}
144144

145+
capi_return_t tiledb_ctx_get_data_protocol(
146+
tiledb_ctx_t* ctx, const char* uri, tiledb_data_protocol_t* data_protocol) {
147+
ensure_context_is_valid(ctx);
148+
ensure_output_pointer_is_valid(data_protocol);
149+
150+
auto uri_to_check = tiledb::sm::URI(uri);
151+
if (uri_to_check.is_invalid()) {
152+
throw CAPIException("Cannot get data protocol; Invalid URI");
153+
}
154+
155+
if (tiledb::sm::URI::is_tiledb(uri)) {
156+
if (!ctx->has_rest_client()) {
157+
throw CAPIException(
158+
"Cannot get data protocol; REST client not configured");
159+
}
160+
161+
if (ctx->rest_client().rest_legacy()) {
162+
*data_protocol = TILEDB_DATA_PROTOCOL_v2;
163+
} else {
164+
*data_protocol = TILEDB_DATA_PROTOCOL_v3;
165+
}
166+
} else {
167+
*data_protocol = TILEDB_DATA_PROTOCOL_v2;
168+
}
169+
170+
return TILEDB_OK;
171+
}
172+
145173
} // namespace tiledb::api
146174

147175
using tiledb::api::api_entry_with_context;
@@ -206,3 +234,12 @@ CAPI_INTERFACE(
206234
return api_entry_with_context<tiledb::api::tiledb_ctx_set_tag>(
207235
ctx, key, value);
208236
}
237+
238+
CAPI_INTERFACE(
239+
ctx_get_data_protocol,
240+
tiledb_ctx_t* ctx,
241+
const char* uri,
242+
tiledb_data_protocol_t* data_protocol) {
243+
return api_entry_with_context<tiledb::api::tiledb_ctx_get_data_protocol>(
244+
ctx, uri, data_protocol);
245+
}

tiledb/api/c_api/context/context_api_external.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ extern "C" {
4646
*/
4747
typedef struct tiledb_ctx_handle_t tiledb_ctx_t;
4848

49+
/**
50+
* REST data model version.
51+
*/
52+
typedef enum {
53+
/** REST API v2 (legacy) */
54+
TILEDB_DATA_PROTOCOL_v2 = 0,
55+
/** REST API v3 (Tiledb 3.0+) */
56+
TILEDB_DATA_PROTOCOL_v3 = 1
57+
} tiledb_data_protocol_t;
58+
4959
/**
5060
* Creates a TileDB context, which contains the TileDB storage manager
5161
* that manages everything in the TileDB library.
@@ -227,6 +237,27 @@ TILEDB_EXPORT capi_return_t tiledb_ctx_cancel_tasks(tiledb_ctx_t* ctx)
227237
TILEDB_EXPORT capi_return_t tiledb_ctx_set_tag(
228238
tiledb_ctx_t* ctx, const char* key, const char* value) TILEDB_NOEXCEPT;
229239

240+
/**
241+
* Returns the REST data model version for the given context.
242+
*
243+
* **Example:**
244+
*
245+
* @code{.c}
246+
* tiledb_data_protocol_t data_protocol;
247+
* tiledb_ctx_get_data_protocol(ctx, "tiledb://workspace/teamspace/array",
248+
* &data_protocol);
249+
* @endcode
250+
*
251+
* @param ctx The TileDB context.
252+
* @param uri The URI to check.
253+
* @param data_protocol Set to the data protocol version.
254+
* @return `TILEDB_OK` for success and `TILEDB_ERR` for error.
255+
*/
256+
TILEDB_EXPORT capi_return_t tiledb_ctx_get_data_protocol(
257+
tiledb_ctx_t* ctx,
258+
const char* uri,
259+
tiledb_data_protocol_t* data_protocol) TILEDB_NOEXCEPT;
260+
230261
#ifdef __cplusplus
231262
}
232263
#endif

tiledb/sm/cpp_api/context.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,35 @@ class Context {
221221
handle_error(tiledb_ctx_set_tag(ctx_.get(), key.c_str(), value.c_str()));
222222
}
223223

224+
/**
225+
* Data protocol version enum.
226+
*/
227+
enum class DataProtocol : uint8_t {
228+
/** Data protocol v2 (legacy) */
229+
v2 = TILEDB_DATA_PROTOCOL_v2,
230+
/** Data protocol v3 (TileDB 3.0+) */
231+
v3 = TILEDB_DATA_PROTOCOL_v3
232+
};
233+
234+
/**
235+
* Returns the data protocol version for the given URI.
236+
*
237+
* **Example:**
238+
* @code{.cpp}
239+
* tiledb::Context ctx;
240+
* auto data_protocol = ctx.data_protocol("tiledb://namespace/array");
241+
* @endcode
242+
*
243+
* @param uri The URI to check.
244+
* @return The data protocol version.
245+
*/
246+
DataProtocol data_protocol(const std::string& uri) const {
247+
tiledb_data_protocol_t protocol;
248+
handle_error(
249+
tiledb_ctx_get_data_protocol(ctx_.get(), uri.c_str(), &protocol));
250+
return static_cast<DataProtocol>(protocol);
251+
}
252+
224253
/** Returns a JSON-formatted string of the stats. */
225254
std::string stats() {
226255
char* c_str;

0 commit comments

Comments
 (0)