Skip to content

Commit

Permalink
Move store_array_schema into a standalone file, separate from the arr…
Browse files Browse the repository at this point in the history
…ay_schema OL.
  • Loading branch information
bekadavis9 committed May 30, 2024
1 parent da37aca commit 6149fc8
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 88 deletions.
1 change: 1 addition & 0 deletions tiledb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ set(TILEDB_CORE_SOURCES
${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/array_schema/array_schema.cc
${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/array_schema/array_schema_evolution.cc
${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/array_schema/attribute.cc
${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/array_schema/auxiliary.cc
${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/array_schema/dimension.cc
${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/array_schema/dimension_label.cc
${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/array_schema/domain.cc
Expand Down
1 change: 1 addition & 0 deletions tiledb/api/c_api/dimension_label/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ commence(object_library capi_dimension_label_stub)
this_target_sources(dimension_label_api.cc)
this_target_link_libraries(export)
this_target_object_libraries(capi_datatype capi_data_order capi_context_stub)
this_target_object_libraries(array_schema)
conclude(object_library)

add_test_subdirectory()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2023-2024 TileDB, Inc.
* @copyright Copyright (c) 2023 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 @@ -53,7 +53,9 @@ typedef struct tiledb_dimension_label_handle_t tiledb_dimension_label_t;
*
* @code{.c}
* tiledb_dimension_label_t* dim_label;
* tiledb_dimension_label_free(&dim_label);
* tiledb_array_schema_get_dimension_label_from_name(
* ctx, array_schema, "label1", &dimension_label);
* tiledb_dimension_label_free(&dimension_label);
* @endcode
*
* @param[in,out] dim_label The dimension label to be destroyed.
Expand Down
2 changes: 1 addition & 1 deletion tiledb/sm/array_schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ conclude(object_library)
commence(object_library array_schema)
this_target_sources(array_schema.cc dimension_label.cc)
this_target_object_libraries(
attribute domain enumeration fragment generic_tile_io time uri_format vfs)
attribute domain enumeration fragment time uri_format vfs)
conclude(object_library)

add_test_subdirectory()
71 changes: 0 additions & 71 deletions tiledb/sm/array_schema/array_schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -873,77 +873,6 @@ void ArraySchema::serialize(Serializer& serializer) const {
}
}

Status ArraySchema::store(
ContextResources& resources, const EncryptionKey& encryption_key) {
// Serialize
SizeComputationSerializer size_computation_serializer;
serialize(size_computation_serializer);

auto tile{WriterTile::from_generic(
size_computation_serializer.size(),
resources.ephemeral_memory_tracker())};
Serializer serializer(tile->data(), tile->size());
serialize(serializer);
resources.stats().add_counter("write_array_schema_size", tile->size());

// Delete file if it exists already
bool exists;
throw_if_not_ok(resources.vfs().is_file(uri(), &exists));
if (exists) {
throw_if_not_ok(resources.vfs().remove_file(uri()));
}

// Check if the array schema directory exists
// If not create it, this is caused by a pre-v10 array
bool schema_dir_exists = false;
URI array_schema_dir_uri =
array_uri().join_path(constants::array_schema_dir_name);
throw_if_not_ok(
resources.vfs().is_dir(array_schema_dir_uri, &schema_dir_exists));

if (!schema_dir_exists) {
throw_if_not_ok(resources.vfs().create_dir(array_schema_dir_uri));
}

GenericTileIO::store_data(resources, uri(), tile, encryption_key);

// Create the `__enumerations` directory under `__schema` if it doesn't
// exist. This might happen if someone tries to add an enumeration to an
// array created before version 19.
bool enumerations_dir_exists = false;
URI array_enumerations_dir_uri =
array_schema_dir_uri.join_path(constants::array_enumerations_dir_name);
throw_if_not_ok(resources.vfs().is_dir(
array_enumerations_dir_uri, &enumerations_dir_exists));

if (!enumerations_dir_exists) {
throw_if_not_ok(resources.vfs().create_dir(array_enumerations_dir_uri));
}

// Serialize all enumerations into the `__enumerations` directory
for (auto& enmr_name : get_loaded_enumeration_names()) {
auto enmr = get_enumeration(enmr_name);
if (enmr == nullptr) {
throw ArraySchemaException(
"Error serializing enumeration; Loaded enumeration is null");
}

SizeComputationSerializer enumeration_size_serializer;
enmr->serialize(enumeration_size_serializer);

auto tile{WriterTile::from_generic(
enumeration_size_serializer.size(),
resources.ephemeral_memory_tracker())};
Serializer serializer(tile->data(), tile->size());
enmr->serialize(serializer);

auto abs_enmr_uri = array_enumerations_dir_uri.join_path(enmr->path_name());
GenericTileIO::store_data(resources, abs_enmr_uri, tile, encryption_key);
}

return Status::Ok();
}

Layout ArraySchema::tile_order() const {
return tile_order_;
}
Expand Down
10 changes: 0 additions & 10 deletions tiledb/sm/array_schema/array_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,6 @@ class ArraySchema {
*/
void serialize(Serializer& serializer) const;

/**
* Stores the array schema into persistent storage.
*
* @param resources The context resources.
* @param encryption_key The encryption key to use.
* @return Status
*/
Status store(
ContextResources& resources, const EncryptionKey& encryption_key);

/** Returns the tile order. */
Layout tile_order() const;

Expand Down
129 changes: 129 additions & 0 deletions tiledb/sm/array_schema/auxiliary.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* @file auxiliary.cc
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @section DESCRIPTION
*
* This file implements auxiliary functions to support the ArraySchema class.
*
* This file exists solely as an intermediary solution to resolve build issues
* with `capi_context_stub` when migrating `store_array_schema` out of
* `StorageManagerCanonical`. At present, there is an interdependency chain
* with `generic_tile_io` which must be resolved before this function can be
* placed into `class ArraySchema`. As such, this file is _intentionally_ left
* out of the `array_schema` object library and _must_ remain that way. Once
* these build issues are resolved, this file should be removed and replaced by
* a member function, `ArraySchema::store`.
*/

#include "tiledb/sm/array_schema/auxiliary.h"
#include "tiledb/sm/array_schema/enumeration.h"
#include "tiledb/sm/filesystem/uri.h"
#include "tiledb/sm/tile/generic_tile_io.h"
#include "tiledb/sm/tile/tile.h"

namespace tiledb::sm {

/* ********************************* */
/* API */
/* ********************************* */

Status store_array_schema(
ContextResources& resources,
const shared_ptr<ArraySchema>& array_schema,
const EncryptionKey& encryption_key) {
const URI schema_uri = array_schema->uri();

// Serialize
SizeComputationSerializer size_computation_serializer;
array_schema->serialize(size_computation_serializer);

auto tile{WriterTile::from_generic(
size_computation_serializer.size(),
resources.ephemeral_memory_tracker())};
Serializer serializer(tile->data(), tile->size());
array_schema->serialize(serializer);
resources.stats().add_counter("write_array_schema_size", tile->size());

// Delete file if it exists already
bool exists;
throw_if_not_ok(resources.vfs().is_file(schema_uri, &exists));
if (exists) {
throw_if_not_ok(resources.vfs().remove_file(schema_uri));
}

// Check if the array schema directory exists
// If not create it, this is caused by a pre-v10 array
bool schema_dir_exists = false;
URI array_schema_dir_uri =
array_schema->array_uri().join_path(constants::array_schema_dir_name);
throw_if_not_ok(
resources.vfs().is_dir(array_schema_dir_uri, &schema_dir_exists));

if (!schema_dir_exists) {
throw_if_not_ok(resources.vfs().create_dir(array_schema_dir_uri));
}

GenericTileIO::store_data(resources, schema_uri, tile, encryption_key);

// Create the `__enumerations` directory under `__schema` if it doesn't
// exist. This might happen if someone tries to add an enumeration to an
// array created before version 19.
bool enumerations_dir_exists = false;
URI array_enumerations_dir_uri =
array_schema_dir_uri.join_path(constants::array_enumerations_dir_name);
throw_if_not_ok(resources.vfs().is_dir(
array_enumerations_dir_uri, &enumerations_dir_exists));

if (!enumerations_dir_exists) {
throw_if_not_ok(resources.vfs().create_dir(array_enumerations_dir_uri));
}

// Serialize all enumerations into the `__enumerations` directory
for (auto& enmr_name : array_schema->get_loaded_enumeration_names()) {
auto enmr = array_schema->get_enumeration(enmr_name);
if (enmr == nullptr) {
throw std::runtime_error(
"Error serializing enumeration; Loaded enumeration is null");
}

SizeComputationSerializer enumeration_size_serializer;
enmr->serialize(enumeration_size_serializer);

auto tile{WriterTile::from_generic(
enumeration_size_serializer.size(),
resources.ephemeral_memory_tracker())};
Serializer serializer(tile->data(), tile->size());
enmr->serialize(serializer);

auto abs_enmr_uri = array_enumerations_dir_uri.join_path(enmr->path_name());
GenericTileIO::store_data(resources, abs_enmr_uri, tile, encryption_key);
}

return Status::Ok();
}

} // namespace tiledb::sm
72 changes: 72 additions & 0 deletions tiledb/sm/array_schema/auxiliary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @file auxiliary.h
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @section DESCRIPTION
*
* This file defines auxiliary functions to support class ArraySchema.
*
* This file exists solely as an intermediary solution to resolve build issues
* with `capi_context_stub` when migrating `store_array_schema` out of
* `StorageManagerCanonical`. At present, there is an interdependency chain
* with `generic_tile_io` which must be resolved before this function can be
* placed into `class ArraySchema`. As such, this file is _intentionally_ left
* out of the `array_schema` object library and _must_ remain that way. Once
* these build issues are resolved, this file should be removed and replaced by
* a member function, `ArraySchema::store`.
*/

#ifndef TILEDB_AUXILIARY_ARRAY_SCHEMA_H
#define TILEDB_AUXILIARY_ARRAY_SCHEMA_H

#include "tiledb/common/common.h"
#include "tiledb/sm/array_schema/array_schema.h"
#include "tiledb/sm/storage_manager/context_resources.h"

using namespace tiledb::common;

namespace tiledb::sm {

/* ********************************* */
/* API */
/* ********************************* */

/**
* Stores an array schema into persistent storage.
*
* @param resources The context resources.
* @param array_schema The array schema to be stored.
* @param encryption_key The encryption key to use.
* @return Status
*/
Status store_array_schema(
ContextResources& resources,
const shared_ptr<ArraySchema>& array_schema,
const EncryptionKey& encryption_key);

} // namespace tiledb::sm

#endif // TILEDB_AUXILIARY_ARRAY_SCHEMA_H
10 changes: 6 additions & 4 deletions tiledb/sm/storage_manager/storage_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "tiledb/sm/array/array_directory.h"
#include "tiledb/sm/array_schema/array_schema.h"
#include "tiledb/sm/array_schema/array_schema_evolution.h"
#include "tiledb/sm/array_schema/auxiliary.h"
#include "tiledb/sm/array_schema/enumeration.h"
#include "tiledb/sm/consolidator/consolidator.h"
#include "tiledb/sm/enums/array_type.h"
Expand Down Expand Up @@ -205,9 +206,9 @@ Status StorageManagerCanonical::array_create(
(const void*)encryption_key_from_cfg.c_str(),
static_cast<uint32_t>(encryption_key_from_cfg.size())));
}
st = array_schema->store(resources_, encryption_key_cfg);
st = store_array_schema(resources_, array_schema, encryption_key_cfg);
} else {
st = array_schema->store(resources_, encryption_key);
st = store_array_schema(resources_, array_schema, encryption_key);
}

// Store array schema
Expand Down Expand Up @@ -275,7 +276,8 @@ Status StorageManager::array_evolve_schema(
// Evolve schema
auto array_schema_evolved = schema_evolution->evolve_schema(array_schema);

Status st = array_schema_evolved->store(resources_, encryption_key);
Status st =
store_array_schema(resources_, array_schema_evolved, encryption_key);
if (!st.ok()) {
logger_->status_no_return_value(st);
return logger_->status(Status_StorageManagerError(
Expand Down Expand Up @@ -337,7 +339,7 @@ Status StorageManagerCanonical::array_upgrade_version(
RETURN_NOT_OK_ELSE(st, logger_->status_no_return_value(st));

// Store array schema
st = array_schema->store(resources_, encryption_key_cfg);
st = store_array_schema(resources_, array_schema, encryption_key_cfg);
RETURN_NOT_OK_ELSE(st, logger_->status_no_return_value(st));

// Create commit directory if necessary
Expand Down

0 comments on commit 6149fc8

Please sign in to comment.