Skip to content

Commit

Permalink
Error handling optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
bekadavis9 authored and KiterLuc committed Sep 29, 2023
1 parent b1da1de commit 64823c3
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 182 deletions.
3 changes: 1 addition & 2 deletions test/src/unit-cppapi-deletes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1774,8 +1774,7 @@ TEST_CASE_METHOD(
REQUIRE_THROWS_WITH(
Array::delete_fragments_list(
ctx_, SPARSE_ARRAY_NAME, extraneous_fragments, 1),
Catch::Matchers::ContainsSubstring(
"is not a fragment of the ArrayDirectory"));
Catch::Matchers::ContainsSubstring("Failed to delete fragments_list"));
CHECK(tiledb::test::num_fragments(SPARSE_ARRAY_NAME) == 2);

remove_sparse_array();
Expand Down
29 changes: 24 additions & 5 deletions tiledb/sm/array/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,18 @@ void Array::delete_fragments(
ensure_array_is_valid_for_delete(uri);

// Delete fragments
storage_manager_->delete_fragments(
uri.c_str(), timestamp_start, timestamp_end);
if (remote_) {
auto rest_client = resources_.rest_client();
if (rest_client == nullptr) {
throw ArrayException(
"[delete_fragments] Remote array with no REST client.");
}
rest_client->delete_fragments_from_rest(
uri, timestamp_start, timestamp_end);
} else {
storage_manager_->delete_fragments(
uri.c_str(), timestamp_start, timestamp_end);
}
}

void Array::delete_fragments_list(
Expand All @@ -562,9 +572,18 @@ void Array::delete_fragments_list(
ensure_array_is_valid_for_delete(uri);

// Delete fragments_list
auto array_dir =
ArrayDirectory(resources_, uri, 0, std::numeric_limits<uint64_t>::max());
array_dir.delete_fragments_list(fragment_uris);
if (remote_) {
auto rest_client = resources_.rest_client();
if (rest_client == nullptr) {
throw ArrayException(
"[delete_fragments_list] Remote array with no REST client.");
}
rest_client->delete_fragments_list_from_rest(uri, fragment_uris);
} else {
auto array_dir = ArrayDirectory(
resources_, uri, 0, std::numeric_limits<uint64_t>::max());
array_dir.delete_fragments_list(fragment_uris);
}
}

shared_ptr<const Enumeration> Array::get_enumeration(
Expand Down
287 changes: 112 additions & 175 deletions tiledb/sm/c_api/tiledb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2380,55 +2380,39 @@ capi_return_t tiledb_array_delete_fragments_v2(
"Failed to delete fragments; Invalid input uri");
}

if (uri.is_tiledb()) {
// Check REST client
auto rest_client = ctx->storage_manager()->rest_client();
if (rest_client == nullptr) {
auto st = Status_Error(
"Failed to delete fragments; remote array with no REST client.");
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}

rest_client->delete_fragments_from_rest(
uri, timestamp_start, timestamp_end);
} else {
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (std::bad_alloc&) {
auto st = Status_Error(
"Failed to create TileDB array object; Memory allocation error");
delete array;
array = nullptr;
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_OOM;
}

// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (...) {
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to create array");
}

try {
array->array_->delete_fragments(uri, timestamp_start, timestamp_end);
} catch (std::exception& e) {
auto st = Status_ArrayError(e.what());
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}
// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));

// Close the array
// Delete fragments
try {
array->array_->delete_fragments(uri, timestamp_start, timestamp_end);
} catch (...) {
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to delete fragments");
}

// Close and delete the array
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;

return TILEDB_OK;
}

Expand Down Expand Up @@ -2462,54 +2446,39 @@ capi_return_t tiledb_array_delete_fragments_list(
uris.emplace_back(tiledb::sm::URI(fragment_uris[i]));
}

if (uri.is_tiledb()) {
// Check REST client
auto rest_client = ctx->storage_manager()->rest_client();
if (rest_client == nullptr) {
auto st = Status_Error(
"Failed to delete fragments; remote array with no REST client.");
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}
rest_client->delete_fragments_list_from_rest(uri, uris);
} else {
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (std::bad_alloc&) {
auto st = Status_Error(
"Failed to create TileDB array object; Memory allocation error");
delete array;
array = nullptr;
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_OOM;
}

// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (...) {
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to create array");
}

try {
array->array_->delete_fragments_list(uri, uris);
} catch (std::exception& e) {
throw_if_not_ok(array->array_->close());
auto st = Status_ArrayError(e.what());
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}
// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));

// Close the array
// Delete fragments list
try {
array->array_->delete_fragments_list(uri, uris);
} catch (...) {
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to delete fragments_list");
}

// Close the array
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;

return TILEDB_OK;
}

Expand Down Expand Up @@ -4096,55 +4065,39 @@ capi_return_t tiledb_deserialize_array_delete_fragments_timestamps_request(
"Failed to delete fragments; Invalid input uri");
}

// Delete fragments
if (uri.is_tiledb()) {
// Check REST client
auto rest_client = ctx->storage_manager()->rest_client();
if (rest_client == nullptr) {
auto st = Status_Error(
"Failed to delete fragments; remote array with no REST client.");
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}
rest_client->delete_fragments_from_rest(
uri, timestamp_start, timestamp_end);
} else {
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (std::bad_alloc&) {
auto st = Status_Error(
"Failed to create TileDB array object; Memory allocation error");
delete array;
array = nullptr;
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_OOM;
}

// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (...) {
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to create array");
}

try {
array->array_->delete_fragments(uri, timestamp_start, timestamp_end);
} catch (std::exception& e) {
auto st = Status_ArrayError(e.what());
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}
// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));

// Close the array
// Delete fragments
try {
array->array_->delete_fragments(uri, timestamp_start, timestamp_end);
} catch (...) {
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to delete fragments");
}

// Close and delete the array
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;

return TILEDB_OK;
}

Expand All @@ -4164,55 +4117,39 @@ capi_return_t tiledb_deserialize_array_delete_fragments_list_request(
"Failed to delete_fragments_list; Invalid input uri");
}

// Delete fragments list
if (uri.is_tiledb()) {
// Check REST client
auto rest_client = ctx->storage_manager()->rest_client();
if (rest_client == nullptr) {
auto st = Status_Error(
"Failed to delete fragments; remote array with no REST client.");
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}
rest_client->delete_fragments_list_from_rest(uri, uris);
} else {
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (std::bad_alloc&) {
auto st = Status_Error(
"Failed to create TileDB array object; Memory allocation error");
delete array;
array = nullptr;
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_OOM;
}

// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));
// Allocate an array object
tiledb_array_t* array = new (std::nothrow) tiledb_array_t;
try {
array->array_ =
make_shared<tiledb::sm::Array>(HERE(), uri, ctx->storage_manager());
} catch (...) {
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to create array");
}

try {
array->array_->delete_fragments_list(uri, uris);
} catch (std::exception& e) {
throw_if_not_ok(array->array_->close());
auto st = Status_ArrayError(e.what());
LOG_STATUS_NO_RETURN_VALUE(st);
save_error(ctx, st);
return TILEDB_ERR;
}
// Open the array for exclusive modification
throw_if_not_ok(array->array_->open(
static_cast<tiledb::sm::QueryType>(TILEDB_MODIFY_EXCLUSIVE),
static_cast<tiledb::sm::EncryptionType>(TILEDB_NO_ENCRYPTION),
nullptr,
0));

// Close the array
// Delete fragments list
try {
array->array_->delete_fragments_list(uri, uris);
} catch (...) {
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;
throw api::CAPIStatusException("Failed to delete fragments_list");
}

// Close and delete the array
throw_if_not_ok(array->array_->close());
delete array;
array = nullptr;

return TILEDB_OK;
}

Expand Down

0 comments on commit 64823c3

Please sign in to comment.