Skip to content

Commit

Permalink
Migrate APIs out of StorageManager: array_evolve_schema. (#5038)
Browse files Browse the repository at this point in the history
Migrate APIs out of `StorageManager`: `array_evolve_schema`.

[sc-44986]

---
TYPE: NO_HISTORY
DESC: Migrate APIs out of `StorageManager`: `array_evolve_schema`.
  • Loading branch information
bekadavis9 authored Jun 5, 2024
1 parent c303e3c commit 18018e1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 86 deletions.
8 changes: 5 additions & 3 deletions test/src/unit-enumerations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2064,9 +2064,11 @@ TEST_CASE_METHOD(

auto ase = make_shared<ArraySchemaEvolution>(HERE(), memory_tracker_);
ase->extend_enumeration(new_enmr);
auto st = ctx_.storage_manager()->array_evolve_schema(
array->array_uri(), ase.get(), array->get_encryption_key());
throw_if_not_ok(st);
throw_if_not_ok(tiledb::sm::Array::evolve_array_schema(
ctx_.resources(),
array->array_uri(),
ase.get(),
array->get_encryption_key()));

// Check that we can not rewrite the query condition.
array = get_array(QueryType::READ);
Expand Down
65 changes: 65 additions & 0 deletions tiledb/sm/array/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,71 @@ OpenedArray::load_delete_and_update_conditions() {
return {Status::Ok(), conditions, update_values};
}

Status Array::evolve_array_schema(
ContextResources& resources,
const URI& array_uri,
ArraySchemaEvolution* schema_evolution,
const EncryptionKey& encryption_key) {
// Check array schema
if (schema_evolution == nullptr) {
throw ArrayException("Cannot evolve array; Empty schema evolution");
}

if (array_uri.is_tiledb()) {
return resources.rest_client()->post_array_schema_evolution_to_rest(
array_uri, schema_evolution);
}

// Load URIs from the array directory
tiledb::sm::ArrayDirectory array_dir{
resources,
array_uri,
0,
UINT64_MAX,
tiledb::sm::ArrayDirectoryMode::SCHEMA_ONLY};

// Check if array exists
if (!is_array(resources, array_uri)) {
throw ArrayException(
"Cannot evolve array; '" + array_uri.to_string() + "' does not exist");
}

auto&& array_schema = array_dir.load_array_schema_latest(
encryption_key, resources.ephemeral_memory_tracker());

// Load required enumerations before evolution.
auto enmr_names = schema_evolution->enumeration_names_to_extend();
if (enmr_names.size() > 0) {
std::unordered_set<std::string> enmr_path_set;
for (auto name : enmr_names) {
enmr_path_set.insert(array_schema->get_enumeration_path_name(name));
}
std::vector<std::string> enmr_paths;
for (auto path : enmr_path_set) {
enmr_paths.emplace_back(path);
}

auto loaded_enmrs = array_dir.load_enumerations_from_paths(
enmr_paths, encryption_key, resources.create_memory_tracker());

for (auto enmr : loaded_enmrs) {
array_schema->store_enumeration(enmr);
}
}

// Evolve schema
auto array_schema_evolved = schema_evolution->evolve_schema(array_schema);

Status st =
store_array_schema(resources, array_schema_evolved, encryption_key);
if (!st.ok()) {
throw ArrayException(
"Cannot evolve schema; Not able to store evolved array schema.");
}

return Status::Ok();
}

const URI& Array::array_uri() const {
return array_uri_;
}
Expand Down
16 changes: 16 additions & 0 deletions tiledb/sm/array/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ using namespace tiledb::common;
namespace tiledb::sm {

class ArraySchema;
class ArraySchemaEvolution;
class SchemaEvolution;
class FragmentMetadata;
class MemoryTracker;
Expand Down Expand Up @@ -363,6 +364,21 @@ class Array {
opened_array_->set_array_schemas_all(std::move(all_schemas));
}

/**
* Evolve a TileDB array schema and store its new schema.
*
* @param resources The context resources.
* @param array_uri The uri of the array whose schema is to be evolved.
* @param schema_evolution The schema evolution.
* @param encryption_key The encryption key to use.
* @return Status
*/
static Status evolve_array_schema(
ContextResources& resources,
const URI& array_uri,
ArraySchemaEvolution* array_schema,
const EncryptionKey& encryption_key);

/** Returns the array URI. */
const URI& array_uri() const;

Expand Down
7 changes: 5 additions & 2 deletions tiledb/sm/c_api/tiledb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3016,8 +3016,11 @@ int32_t tiledb_array_evolve(
throw_if_not_ok(
key.set_key(tiledb::sm::EncryptionType::NO_ENCRYPTION, nullptr, 0));
// Evolve schema
throw_if_not_ok(ctx->storage_manager()->array_evolve_schema(
uri, array_schema_evolution->array_schema_evolution_, key));
throw_if_not_ok(tiledb::sm::Array::evolve_array_schema(
ctx->resources(),
uri,
array_schema_evolution->array_schema_evolution_,
key));

// Success
return TILEDB_OK;
Expand Down
66 changes: 0 additions & 66 deletions tiledb/sm/storage_manager/storage_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,72 +131,6 @@ StorageManagerCanonical::~StorageManagerCanonical() {
/* API */
/* ****************************** */

Status StorageManager::array_evolve_schema(
const URI& array_uri,
ArraySchemaEvolution* schema_evolution,
const EncryptionKey& encryption_key) {
// Check array schema
if (schema_evolution == nullptr) {
throw StorageManagerException(
"Cannot evolve array; Empty schema evolution");
}

if (array_uri.is_tiledb()) {
return resources_.rest_client()->post_array_schema_evolution_to_rest(
array_uri, schema_evolution);
}

// Load URIs from the array directory
tiledb::sm::ArrayDirectory array_dir{
resources(),
array_uri,
0,
UINT64_MAX,
tiledb::sm::ArrayDirectoryMode::SCHEMA_ONLY};

// Check if array exists
if (!is_array(resources_, array_uri)) {
throw StorageManagerException(
"Cannot evolve array; Array '" + array_uri.to_string() +
"' not exists");
}

auto&& array_schema = array_dir.load_array_schema_latest(
encryption_key, resources_.ephemeral_memory_tracker());

// Load required enumerations before evolution.
auto enmr_names = schema_evolution->enumeration_names_to_extend();
if (enmr_names.size() > 0) {
std::unordered_set<std::string> enmr_path_set;
for (auto name : enmr_names) {
enmr_path_set.insert(array_schema->get_enumeration_path_name(name));
}
std::vector<std::string> enmr_paths;
for (auto path : enmr_path_set) {
enmr_paths.emplace_back(path);
}

auto loaded_enmrs = array_dir.load_enumerations_from_paths(
enmr_paths, encryption_key, resources_.create_memory_tracker());

for (auto enmr : loaded_enmrs) {
array_schema->store_enumeration(enmr);
}
}

// Evolve schema
auto array_schema_evolved = schema_evolution->evolve_schema(array_schema);

Status st =
store_array_schema(resources_, array_schema_evolved, encryption_key);
if (!st.ok()) {
throw StorageManagerException(
"Cannot evolve schema; Not able to store evolved array schema.");
}

return Status::Ok();
}

Status StorageManagerCanonical::array_upgrade_version(
const URI& array_uri, const Config& override_config) {
// Check if array exists
Expand Down
15 changes: 0 additions & 15 deletions tiledb/sm/storage_manager/storage_manager_canonical.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ namespace tiledb::sm {
class Array;
class ArrayDirectory;
class ArraySchema;
class ArraySchemaEvolution;
class Consolidator;
class EncryptionKey;
class Query;
Expand Down Expand Up @@ -135,20 +134,6 @@ class StorageManagerCanonical {
/* API */
/* ********************************* */

/**
* Evolve a TileDB array schema and store its new schema.
*
* @param array_dir The ArrayDirectory object used to retrieve the
* various URIs in the array directory.
* @param schema_evolution The schema evolution.
* @param encryption_key The encryption key to use.
* @return Status
*/
Status array_evolve_schema(
const URI& uri,
ArraySchemaEvolution* array_schema,
const EncryptionKey& encryption_key);

/**
* Upgrade a TileDB array to latest format version.
*
Expand Down

0 comments on commit 18018e1

Please sign in to comment.