Skip to content

Commit b239ff5

Browse files
Avoid temporary tiledb_array_t* handle allocations in C API implementations. (#5248)
[SC-53176](https://app.shortcut.com/tiledb-inc/story/53176/remove-unnecessarily-temporary-array-handle-allocations) This PR replaces internal allocations of temporary `tiledb_array_t*` handles in the C API implementations, with `shared_ptr<tiledb::sm::Array>`. This simplifies error handling and memory management. --- TYPE: NO_HISTORY
1 parent 70e07bb commit b239ff5

File tree

1 file changed

+24
-69
lines changed

1 file changed

+24
-69
lines changed

tiledb/sm/c_api/tiledb.cc

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,35 +1437,16 @@ int32_t tiledb_array_get_open_timestamp_end(
14371437

14381438
int32_t tiledb_array_delete(tiledb_ctx_t* ctx, const char* uri) {
14391439
// Allocate an array object
1440-
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
1441-
try {
1442-
array->array_ = make_shared<tiledb::sm::Array>(
1443-
HERE(), ctx->resources(), tiledb::sm::URI(uri));
1444-
} catch (std::bad_alloc&) {
1445-
auto st = Status_Error(
1446-
"Failed to create TileDB array object; Memory allocation error");
1447-
delete array;
1448-
array = nullptr;
1449-
LOG_STATUS_NO_RETURN_VALUE(st);
1450-
save_error(ctx, st);
1451-
return TILEDB_OOM;
1452-
}
1453-
1440+
auto array = make_shared<tiledb::sm::Array>(
1441+
HERE(), ctx->resources(), tiledb::sm::URI(uri));
14541442
// Open the array for exclusive modification
1455-
throw_if_not_ok(array->array_->open(
1443+
throw_if_not_ok(array->open(
14561444
tiledb::sm::QueryType::MODIFY_EXCLUSIVE,
14571445
tiledb::sm::EncryptionType::NO_ENCRYPTION,
14581446
nullptr,
14591447
0));
14601448

1461-
try {
1462-
array->array_->delete_array(tiledb::sm::URI(uri));
1463-
} catch (std::exception& e) {
1464-
auto st = Status_ArrayError(e.what());
1465-
LOG_STATUS_NO_RETURN_VALUE(st);
1466-
save_error(ctx, st);
1467-
return TILEDB_ERR;
1468-
}
1449+
array->delete_array(tiledb::sm::URI(uri));
14691450

14701451
return TILEDB_OK;
14711452
}
@@ -1477,46 +1458,33 @@ capi_return_t tiledb_array_delete_fragments_v2(
14771458
uint64_t timestamp_end) {
14781459
auto uri = tiledb::sm::URI(uri_str);
14791460
if (uri.is_invalid()) {
1480-
throw api::CAPIStatusException(
1481-
"Failed to delete fragments; Invalid input uri");
1461+
throw CAPIException("Failed to delete fragments; Invalid input uri");
14821462
}
14831463

14841464
// Allocate an array object
1485-
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
1486-
try {
1487-
array->array_ =
1488-
make_shared<tiledb::sm::Array>(HERE(), ctx->resources(), uri);
1489-
} catch (...) {
1490-
delete array;
1491-
array = nullptr;
1492-
throw api::CAPIStatusException("Failed to create array");
1493-
}
1465+
auto array = make_shared<tiledb::sm::Array>(HERE(), ctx->resources(), uri);
14941466

14951467
// Set array open timestamps
1496-
array->array_->set_timestamp_start(timestamp_start);
1497-
array->array_->set_timestamp_end(timestamp_end);
1468+
array->set_timestamp_start(timestamp_start);
1469+
array->set_timestamp_end(timestamp_end);
14981470

14991471
// Open the array for exclusive modification
1500-
throw_if_not_ok(array->array_->open(
1472+
throw_if_not_ok(array->open(
15011473
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
15021474
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
15031475
nullptr,
15041476
0));
15051477

15061478
// Delete fragments
15071479
try {
1508-
array->array_->delete_fragments(uri, timestamp_start, timestamp_end);
1480+
array->delete_fragments(uri, timestamp_start, timestamp_end);
15091481
} catch (...) {
1510-
throw_if_not_ok(array->array_->close());
1511-
delete array;
1512-
array = nullptr;
1513-
throw api::CAPIStatusException("Failed to delete fragments");
1482+
throw_if_not_ok(array->close());
1483+
throw CAPIException("Failed to delete fragments");
15141484
}
15151485

15161486
// Close and delete the array
1517-
throw_if_not_ok(array->array_->close());
1518-
delete array;
1519-
array = nullptr;
1487+
throw_if_not_ok(array->close());
15201488

15211489
return TILEDB_OK;
15221490
}
@@ -1528,18 +1496,17 @@ capi_return_t tiledb_array_delete_fragments_list(
15281496
const size_t num_fragments) {
15291497
auto uri = tiledb::sm::URI(uri_str);
15301498
if (uri.is_invalid()) {
1531-
throw api::CAPIStatusException(
1532-
"Failed to delete_fragments_list; Invalid input uri");
1499+
throw CAPIException("Failed to delete_fragments_list; Invalid input uri");
15331500
}
15341501

15351502
if (num_fragments < 1) {
1536-
throw api::CAPIStatusException(
1503+
throw CAPIException(
15371504
"Failed to delete_fragments_list; Invalid input number of fragments");
15381505
}
15391506

15401507
for (size_t i = 0; i < num_fragments; i++) {
15411508
if (tiledb::sm::URI(fragment_uris[i]).is_invalid()) {
1542-
throw api::CAPIStatusException(
1509+
throw CAPIException(
15431510
"Failed to delete_fragments_list; Invalid input fragment uri");
15441511
}
15451512
}
@@ -1552,37 +1519,25 @@ capi_return_t tiledb_array_delete_fragments_list(
15521519
}
15531520

15541521
// Allocate an array object
1555-
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
1556-
try {
1557-
array->array_ =
1558-
make_shared<tiledb::sm::Array>(HERE(), ctx->resources(), uri);
1559-
} catch (...) {
1560-
delete array;
1561-
array = nullptr;
1562-
throw api::CAPIStatusException("Failed to create array");
1563-
}
1522+
auto array = make_shared<tiledb::sm::Array>(HERE(), ctx->resources(), uri);
15641523

15651524
// Open the array for exclusive modification
1566-
throw_if_not_ok(array->array_->open(
1567-
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
1568-
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
1525+
throw_if_not_ok(array->open(
1526+
tiledb::sm::QueryType::MODIFY_EXCLUSIVE,
1527+
tiledb::sm::EncryptionType::NO_ENCRYPTION,
15691528
nullptr,
15701529
0));
15711530

15721531
// Delete fragments list
15731532
try {
1574-
array->array_->delete_fragments_list(uris);
1533+
array->delete_fragments_list(uris);
15751534
} catch (...) {
1576-
throw_if_not_ok(array->array_->close());
1577-
delete array;
1578-
array = nullptr;
1579-
throw api::CAPIStatusException("Failed to delete fragments_list");
1535+
throw_if_not_ok(array->close());
1536+
throw CAPIException("Failed to delete fragments_list");
15801537
}
15811538

15821539
// Close the array
1583-
throw_if_not_ok(array->array_->close());
1584-
delete array;
1585-
array = nullptr;
1540+
throw_if_not_ok(array->close());
15861541

15871542
return TILEDB_OK;
15881543
}

0 commit comments

Comments
 (0)