Skip to content

Commit

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

[sc-44985]

---
TYPE: NO_HISTORY
DESC: Migrate APIs out of `StorageManager`: `array_create`.
  • Loading branch information
bekadavis9 authored Jun 3, 2024
1 parent 61686a3 commit a872f29
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 133 deletions.
2 changes: 1 addition & 1 deletion test/src/unit-enumerations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2879,7 +2879,7 @@ shared_ptr<ArraySchema> EnumerationFx::create_schema() {

void EnumerationFx::create_array() {
auto schema = create_schema();
throw_if_not_ok(ctx_.storage_manager()->array_create(uri_, schema, enc_key_));
throw_if_not_ok(Array::create(ctx_.resources(), uri_, schema, enc_key_));
}

shared_ptr<Array> EnumerationFx::get_array(QueryType type) {
Expand Down
4 changes: 2 additions & 2 deletions test/src/unit-request-handlers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2023 TileDB Inc.
* @copyright Copyright (c) 2023-2024 TileDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -357,7 +357,7 @@ RequestHandlerFx::~RequestHandlerFx() {

void RequestHandlerFx::create_array() {
auto schema = create_schema();
throw_if_not_ok(ctx_.storage_manager()->array_create(uri_, schema, enc_key_));
throw_if_not_ok(Array::create(ctx_.resources(), uri_, schema, enc_key_));
}

void RequestHandlerFx::delete_array() {
Expand Down
99 changes: 99 additions & 0 deletions tiledb/sm/array/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "tiledb/sm/array_schema/array_schema.h"
#include "tiledb/sm/array_schema/array_schema_evolution.h"
#include "tiledb/sm/array_schema/attribute.h"
#include "tiledb/sm/array_schema/auxiliary.h"
#include "tiledb/sm/array_schema/dimension.h"
#include "tiledb/sm/array_schema/domain.h"
#include "tiledb/sm/crypto/crypto.h"
Expand All @@ -51,6 +52,8 @@
#include "tiledb/sm/misc/parallel_functions.h"
#include "tiledb/sm/misc/tdb_time.h"
#include "tiledb/sm/misc/utils.h"
#include "tiledb/sm/object/object.h"
#include "tiledb/sm/object/object_mutex.h"
#include "tiledb/sm/query/deletes_and_updates/serialization.h"
#include "tiledb/sm/query/update_value.h"
#include "tiledb/sm/rest/rest_client.h"
Expand Down Expand Up @@ -174,6 +177,102 @@ const EncryptionKey* Array::encryption_key() const {
return opened_array_->encryption_key();
}

Status Array::create(
ContextResources& resources,
const URI& array_uri,
const shared_ptr<ArraySchema>& array_schema,
const EncryptionKey& encryption_key) {
// Check array schema
if (array_schema == nullptr) {
throw ArrayException("Cannot create array; Empty array schema");
}

// Check if array exists
if (is_array(resources, array_uri)) {
throw ArrayException(
"Cannot create array; Array '" + array_uri.to_string() +
"' already exists");
}

std::lock_guard<std::mutex> lock{object_mtx};
array_schema->set_array_uri(array_uri);
array_schema->generate_uri();
array_schema->check(resources.config());

// Create array directory
throw_if_not_ok(resources.vfs().create_dir(array_uri));

// Create array schema directory
URI array_schema_dir_uri =
array_uri.join_path(constants::array_schema_dir_name);
throw_if_not_ok(resources.vfs().create_dir(array_schema_dir_uri));

// Create the enumerations directory inside the array schema directory
URI array_enumerations_uri =
array_schema_dir_uri.join_path(constants::array_enumerations_dir_name);
throw_if_not_ok(resources.vfs().create_dir(array_enumerations_uri));

// Create commit directory
URI array_commit_uri = array_uri.join_path(constants::array_commits_dir_name);
throw_if_not_ok(resources.vfs().create_dir(array_commit_uri));

// Create fragments directory
URI array_fragments_uri =
array_uri.join_path(constants::array_fragments_dir_name);
throw_if_not_ok(resources.vfs().create_dir(array_fragments_uri));

// Create array metadata directory
URI array_metadata_uri =
array_uri.join_path(constants::array_metadata_dir_name);
throw_if_not_ok(resources.vfs().create_dir(array_metadata_uri));

// Create fragment metadata directory
URI array_fragment_metadata_uri =
array_uri.join_path(constants::array_fragment_meta_dir_name);
throw_if_not_ok(resources.vfs().create_dir(array_fragment_metadata_uri));

// Create dimension label directory
URI array_dimension_labels_uri =
array_uri.join_path(constants::array_dimension_labels_dir_name);
throw_if_not_ok(resources.vfs().create_dir(array_dimension_labels_uri));

// Get encryption key from config
Status st;
if (encryption_key.encryption_type() == EncryptionType::NO_ENCRYPTION) {
bool found = false;
std::string encryption_key_from_cfg =
resources.config().get("sm.encryption_key", &found);
assert(found);
std::string encryption_type_from_cfg =
resources.config().get("sm.encryption_type", &found);
assert(found);
auto&& [st_enc, etc] = encryption_type_enum(encryption_type_from_cfg);
throw_if_not_ok(st_enc);
EncryptionType encryption_type_cfg = etc.value();

EncryptionKey encryption_key_cfg;
if (encryption_key_from_cfg.empty()) {
throw_if_not_ok(
encryption_key_cfg.set_key(encryption_type_cfg, nullptr, 0));
} else {
throw_if_not_ok(encryption_key_cfg.set_key(
encryption_type_cfg,
(const void*)encryption_key_from_cfg.c_str(),
static_cast<uint32_t>(encryption_key_from_cfg.size())));
}
st = store_array_schema(resources, array_schema, encryption_key_cfg);
} else {
st = store_array_schema(resources, array_schema, encryption_key);
}

if (!st.ok()) {
throw_if_not_ok(resources.vfs().remove_dir(array_uri));
return st;
}

return Status::Ok();
}

// Used in Consolidator
Status Array::open_without_fragments(
EncryptionType encryption_type,
Expand Down
15 changes: 15 additions & 0 deletions tiledb/sm/array/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,21 @@ class Array {
* serialization in pre TileDB 2.4 */
const URI& array_uri_serialized() const;

/**
* Creates a TileDB array, storing its schema.
*
* @param resources The context resources.
* @param array_uri The URI of the array to be created.
* @param array_schema The array schema.
* @param encryption_key The encryption key to use.
* @return Status
*/
static Status create(
ContextResources& resources,
const URI& array_uri,
const shared_ptr<ArraySchema>& array_schema,
const EncryptionKey& encryption_key);

/**
* Opens the array for reading at a timestamp retrieved from the config
* or for writing.
Expand Down
8 changes: 2 additions & 6 deletions tiledb/sm/array/test/unit_consistency.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2022 TileDB, Inc.
* @copyright Copyright (c) 2022-2024 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -124,11 +124,7 @@ class WhiteboxConsistencyController : public ConsistencyController {
throw_if_not_ok(key.set_key(EncryptionType::NO_ENCRYPTION, nullptr, 0));

// Create the (empty) array on disk.
Status st = sm->array_create(uri, schema, key);
if (!st.ok()) {
throw std::runtime_error(
"[WhiteboxConsistencyController] Could not create array.");
}
throw_if_not_ok(Array::create(sm->resources(), uri, schema, key));
tdb_unique_ptr<Array> array(new Array{uri, sm, *this});

return array;
Expand Down
22 changes: 14 additions & 8 deletions tiledb/sm/c_api/tiledb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2562,8 +2562,8 @@ int32_t tiledb_array_create(
throw_if_not_ok(
key.set_key(tiledb::sm::EncryptionType::NO_ENCRYPTION, nullptr, 0));
// Create the array
throw_if_not_ok(ctx->storage_manager()->array_create(
uri, array_schema->array_schema_, key));
throw_if_not_ok(tiledb::sm::Array::create(
ctx->resources(), uri, array_schema->array_schema_, key));

// Create any dimension labels in the array.
for (tiledb::sm::ArraySchema::dimension_label_size_type ilabel{0};
Expand All @@ -2581,8 +2581,11 @@ int32_t tiledb_array_create(
}

// Create the dimension label array with the same key.
throw_if_not_ok(ctx->storage_manager()->array_create(
dim_label_ref.uri(uri), dim_label_ref.schema(), key));
throw_if_not_ok(tiledb::sm::Array::create(
ctx->resources(),
dim_label_ref.uri(uri),
dim_label_ref.schema(),
key));
}
}
return TILEDB_OK;
Expand Down Expand Up @@ -2630,8 +2633,8 @@ int32_t tiledb_array_create_with_key(
key_length));

// Create the array
throw_if_not_ok(ctx->storage_manager()->array_create(
uri, array_schema->array_schema_, key));
throw_if_not_ok(tiledb::sm::Array::create(
ctx->resources(), uri, array_schema->array_schema_, key));

// Create any dimension labels in the array.
for (tiledb::sm::ArraySchema::dimension_label_size_type ilabel{0};
Expand All @@ -2649,8 +2652,11 @@ int32_t tiledb_array_create_with_key(
}

// Create the dimension label array with the same key.
throw_if_not_ok(ctx->storage_manager()->array_create(
dim_label_ref.uri(uri), dim_label_ref.schema(), key));
throw_if_not_ok(tiledb::sm::Array::create(
ctx->resources(),
dim_label_ref.uri(uri),
dim_label_ref.schema(),
key));
}
}
return TILEDB_OK;
Expand Down
7 changes: 2 additions & 5 deletions tiledb/sm/query_plan/test/unit_query_plan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2023 TileDB Inc.
* @copyright Copyright (c) 2023-2024 TileDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -95,10 +95,7 @@ tdb_unique_ptr<Array> QueryPlanFx::create_array(const URI uri) {
throw_if_not_ok(key.set_key(EncryptionType::NO_ENCRYPTION, nullptr, 0));

// Create the (empty) array on disk.
Status st = sm_->array_create(uri, schema, key);
if (!st.ok()) {
throw std::runtime_error("Could not create array.");
}
throw_if_not_ok(Array::create(resources_, uri, schema, key));
tdb_unique_ptr<Array> array(new Array{uri, sm_.get()});

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

Status StorageManagerCanonical::array_create(
const URI& array_uri,
const shared_ptr<ArraySchema>& array_schema,
const EncryptionKey& encryption_key) {
// Check array schema
if (array_schema == nullptr) {
throw StorageManagerException("Cannot create array; Empty array schema");
}

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

std::lock_guard<std::mutex> lock{object_mtx};
array_schema->set_array_uri(array_uri);
array_schema->generate_uri();
array_schema->check(config_);

// Create array directory
throw_if_not_ok(resources_.vfs().create_dir(array_uri));

// Create array schema directory
URI array_schema_dir_uri =
array_uri.join_path(constants::array_schema_dir_name);
throw_if_not_ok(resources_.vfs().create_dir(array_schema_dir_uri));

// Create the enumerations directory inside the array schema directory
URI array_enumerations_uri =
array_schema_dir_uri.join_path(constants::array_enumerations_dir_name);
throw_if_not_ok(resources_.vfs().create_dir(array_enumerations_uri));

// Create commit directory
URI array_commit_uri = array_uri.join_path(constants::array_commits_dir_name);
throw_if_not_ok(resources_.vfs().create_dir(array_commit_uri));

// Create fragments directory
URI array_fragments_uri =
array_uri.join_path(constants::array_fragments_dir_name);
throw_if_not_ok(resources_.vfs().create_dir(array_fragments_uri));

// Create array metadata directory
URI array_metadata_uri =
array_uri.join_path(constants::array_metadata_dir_name);
throw_if_not_ok(resources_.vfs().create_dir(array_metadata_uri));

// Create fragment metadata directory
URI array_fragment_metadata_uri =
array_uri.join_path(constants::array_fragment_meta_dir_name);
throw_if_not_ok(resources_.vfs().create_dir(array_fragment_metadata_uri));

// Create dimension label directory
URI array_dimension_labels_uri =
array_uri.join_path(constants::array_dimension_labels_dir_name);
throw_if_not_ok(resources_.vfs().create_dir(array_dimension_labels_uri));

// Get encryption key from config
Status st;
if (encryption_key.encryption_type() == EncryptionType::NO_ENCRYPTION) {
bool found = false;
std::string encryption_key_from_cfg =
config_.get("sm.encryption_key", &found);
assert(found);
std::string encryption_type_from_cfg =
config_.get("sm.encryption_type", &found);
assert(found);
auto&& [st_enc, etc] = encryption_type_enum(encryption_type_from_cfg);
RETURN_NOT_OK(st_enc);
EncryptionType encryption_type_cfg = etc.value();

EncryptionKey encryption_key_cfg;
if (encryption_key_from_cfg.empty()) {
RETURN_NOT_OK(
encryption_key_cfg.set_key(encryption_type_cfg, nullptr, 0));
} else {
RETURN_NOT_OK(encryption_key_cfg.set_key(
encryption_type_cfg,
(const void*)encryption_key_from_cfg.c_str(),
static_cast<uint32_t>(encryption_key_from_cfg.size())));
}
st = store_array_schema(resources_, array_schema, encryption_key_cfg);
} else {
st = store_array_schema(resources_, array_schema, encryption_key);
}

// Store array schema
if (!st.ok()) {
throw_if_not_ok(resources_.vfs().remove_dir(array_uri));
return st;
}

return Status::Ok();
}

Status StorageManager::array_evolve_schema(
const URI& array_uri,
ArraySchemaEvolution* schema_evolution,
Expand Down
13 changes: 0 additions & 13 deletions tiledb/sm/storage_manager/storage_manager_canonical.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,6 @@ class StorageManagerCanonical {
/* API */
/* ********************************* */

/**
* Creates a TileDB array storing its schema.
*
* @param array_uri The URI of the array to be created.
* @param array_schema The array schema.
* @param encryption_key The encryption key to use.
* @return Status
*/
Status array_create(
const URI& array_uri,
const shared_ptr<ArraySchema>& array_schema,
const EncryptionKey& encryption_key);

/**
* Evolve a TileDB array schema and store its new schema.
*
Expand Down
Loading

0 comments on commit a872f29

Please sign in to comment.