Skip to content

Commit afa9175

Browse files
committed
Move logic out of tiledb.cc.
1 parent f322b78 commit afa9175

File tree

4 files changed

+72
-102
lines changed

4 files changed

+72
-102
lines changed

tiledb/sm/array_schema/array_schema_operations.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@
3131
*/
3232

3333
#include "tiledb/sm/array_schema/array_schema_operations.h"
34+
#include "tiledb/sm/array/array_directory.h"
3435
#include "tiledb/sm/array_schema/array_schema.h"
3536
#include "tiledb/sm/array_schema/enumeration.h"
37+
#include "tiledb/sm/config/config.h"
38+
#include "tiledb/sm/crypto/encryption_key.h"
3639
#include "tiledb/sm/filesystem/uri.h"
40+
#include "tiledb/sm/rest/rest_client.h"
41+
#include "tiledb/sm/storage_manager/context.h"
3742
#include "tiledb/sm/storage_manager/context_resources.h"
3843
#include "tiledb/sm/tile/generic_tile_io.h"
3944
#include "tiledb/sm/tile/tile.h"
@@ -124,4 +129,59 @@ void store_array_schema(
124129
}
125130
}
126131

132+
shared_ptr<ArraySchema> handle_load_uri(
133+
const Context& ctx, const URI& uri, const Config* config) {
134+
// Check array name
135+
if (uri.is_invalid()) {
136+
throw std::runtime_error("Failed to load array schema; Invalid array URI");
137+
}
138+
139+
if (uri.is_tiledb()) {
140+
auto& rest_client = ctx.rest_client();
141+
auto array_schema_response = rest_client.post_array_schema_from_rest(
142+
config ? *config : ctx.resources().config(), uri, 0, UINT64_MAX);
143+
return std::move(std::get<0>(array_schema_response));
144+
} else {
145+
// Create key
146+
tiledb::sm::EncryptionKey key;
147+
throw_if_not_ok(
148+
key.set_key(tiledb::sm::EncryptionType::NO_ENCRYPTION, nullptr, 0));
149+
150+
// Load URIs from the array directory
151+
optional<tiledb::sm::ArrayDirectory> array_dir;
152+
array_dir.emplace(
153+
ctx.resources(),
154+
uri,
155+
0,
156+
UINT64_MAX,
157+
tiledb::sm::ArrayDirectoryMode::SCHEMA_ONLY);
158+
159+
auto tracker = ctx.resources().ephemeral_memory_tracker();
160+
// Load latest array schema
161+
auto&& array_schema_latest =
162+
array_dir->load_array_schema_latest(key, tracker);
163+
164+
bool incl_enums = config->get<bool>(
165+
"rest.load_enumerations_on_array_open", Config::must_find);
166+
if (config && incl_enums) {
167+
std::vector<std::string> enmr_paths_to_load;
168+
auto enmr_names = array_schema_latest->get_enumeration_names();
169+
for (auto& name : enmr_names) {
170+
if (!array_schema_latest->is_enumeration_loaded(name)) {
171+
auto& path = array_schema_latest->get_enumeration_path_name(name);
172+
enmr_paths_to_load.emplace_back(path);
173+
}
174+
}
175+
176+
auto enmrs_loaded = array_dir->load_enumerations_from_paths(
177+
enmr_paths_to_load, key, tracker);
178+
for (auto& enmr : enmrs_loaded) {
179+
array_schema_latest->store_enumeration(enmr);
180+
}
181+
}
182+
183+
return std::move(array_schema_latest);
184+
}
185+
}
186+
127187
} // namespace tiledb::sm

tiledb/sm/array_schema/array_schema_operations.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ using namespace tiledb::common;
4040
namespace tiledb::sm {
4141

4242
class ArraySchema;
43+
class Config;
44+
class Context;
4345
class ContextResources;
4446
class EncryptionKey;
47+
class URI;
4548

4649
/* ********************************* */
4750
/* API */
@@ -66,6 +69,9 @@ void store_array_schema(
6669
const shared_ptr<ArraySchema>& array_schema,
6770
const EncryptionKey& encryption_key);
6871

72+
shared_ptr<ArraySchema> handle_load_uri(
73+
const Context& ctx, const URI& uri, const Config* config = nullptr);
74+
6975
} // namespace tiledb::sm
7076

7177
#endif // TILEDB_ARRAY_SCHEMA_OPERATIONS_H

tiledb/sm/c_api/tiledb.cc

Lines changed: 5 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "tiledb/common/memory_tracker.h"
5757
#include "tiledb/sm/array/array.h"
5858
#include "tiledb/sm/array_schema/array_schema.h"
59+
#include "tiledb/sm/array_schema/array_schema_operations.h"
5960
#include "tiledb/sm/array_schema/dimension_label.h"
6061
#include "tiledb/sm/c_api/api_argument_validator.h"
6162
#include "tiledb/sm/config/config.h"
@@ -489,49 +490,9 @@ int32_t tiledb_array_schema_load(
489490
throw CAPIStatusException("Failed to allocate TileDB array schema object");
490491
}
491492

492-
// Check array name
493-
tiledb::sm::URI uri(array_uri);
494-
if (uri.is_invalid()) {
495-
throw CAPIStatusException("Failed to load array schema; Invalid array URI");
496-
}
497-
498-
if (uri.is_tiledb()) {
499-
auto& rest_client = ctx->context().rest_client();
500-
try {
501-
auto array_schema_response = rest_client.post_array_schema_from_rest(
502-
ctx->resources().config(), uri, 0, UINT64_MAX);
503-
(*array_schema)->array_schema_ = std::get<0>(array_schema_response);
504-
} catch (...) {
505-
delete *array_schema;
506-
throw;
507-
}
508-
} else {
509-
// Create key
510-
tiledb::sm::EncryptionKey key;
511-
throw_if_not_ok(
512-
key.set_key(tiledb::sm::EncryptionType::NO_ENCRYPTION, nullptr, 0));
513-
514-
// Load URIs from the array directory
515-
optional<tiledb::sm::ArrayDirectory> array_dir;
516-
try {
517-
array_dir.emplace(
518-
ctx->resources(),
519-
uri,
520-
0,
521-
UINT64_MAX,
522-
tiledb::sm::ArrayDirectoryMode::SCHEMA_ONLY);
523-
} catch (const std::logic_error& le) {
524-
delete *array_schema;
525-
throw CAPIStatusException(le.what());
526-
}
527-
528-
auto tracker = ctx->resources().ephemeral_memory_tracker();
529-
// Load latest array schema
530-
auto array_schema_latest =
531-
array_dir->load_array_schema_latest(key, tracker);
493+
(*array_schema)->array_schema_ =
494+
handle_load_uri(ctx->context(), sm::URI(array_uri));
532495

533-
(*array_schema)->array_schema_ = array_schema_latest;
534-
}
535496
return TILEDB_OK;
536497
}
537498

@@ -543,8 +504,6 @@ int32_t tiledb_array_schema_load_with_options(
543504
ensure_context_is_valid(ctx);
544505
ensure_config_is_valid(config);
545506
ensure_output_pointer_is_valid(array_schema);
546-
bool incl_enmrs = config->config().get<bool>(
547-
"rest.load_enumerations_on_array_open", sm::Config::must_find);
548507

549508
// Create array schema
550509
*array_schema = new (std::nothrow) tiledb_array_schema_t;
@@ -554,65 +513,9 @@ int32_t tiledb_array_schema_load_with_options(
554513

555514
// Check array name
556515
tiledb::sm::URI uri(array_uri);
557-
if (uri.is_invalid()) {
558-
throw CAPIStatusException("Failed to load array schema; Invalid array URI");
559-
}
516+
(*array_schema)->array_schema_ = handle_load_uri(
517+
ctx->context(), uri, config ? &config->config() : nullptr);
560518

561-
if (uri.is_tiledb()) {
562-
auto& rest_client = ctx->context().rest_client();
563-
try {
564-
auto array_schema_response = rest_client.post_array_schema_from_rest(
565-
ctx->resources().config(), uri, 0, UINT64_MAX);
566-
(*array_schema)->array_schema_ = std::get<0>(array_schema_response);
567-
} catch (...) {
568-
delete *array_schema;
569-
throw;
570-
}
571-
} else {
572-
// Create key
573-
tiledb::sm::EncryptionKey key;
574-
throw_if_not_ok(
575-
key.set_key(tiledb::sm::EncryptionType::NO_ENCRYPTION, nullptr, 0));
576-
577-
// Load URIs from the array directory
578-
optional<tiledb::sm::ArrayDirectory> array_dir;
579-
try {
580-
array_dir.emplace(
581-
ctx->resources(),
582-
uri,
583-
0,
584-
UINT64_MAX,
585-
tiledb::sm::ArrayDirectoryMode::SCHEMA_ONLY);
586-
} catch (const std::logic_error& le) {
587-
delete *array_schema;
588-
throw CAPIStatusException(le.what());
589-
}
590-
591-
auto tracker = ctx->resources().ephemeral_memory_tracker();
592-
593-
// Load latest array schema
594-
auto&& array_schema_latest =
595-
array_dir->load_array_schema_latest(key, tracker);
596-
597-
if (incl_enmrs) {
598-
std::vector<std::string> enmr_paths_to_load;
599-
auto enmr_names = array_schema_latest->get_enumeration_names();
600-
for (auto& name : enmr_names) {
601-
if (!array_schema_latest->is_enumeration_loaded(name)) {
602-
auto& path = array_schema_latest->get_enumeration_path_name(name);
603-
enmr_paths_to_load.emplace_back(path);
604-
}
605-
}
606-
607-
auto enmrs_loaded = array_dir->load_enumerations_from_paths(
608-
enmr_paths_to_load, key, tracker);
609-
for (auto& enmr : enmrs_loaded) {
610-
array_schema_latest->store_enumeration(enmr);
611-
}
612-
}
613-
614-
(*array_schema)->array_schema_ = array_schema_latest;
615-
}
616519
return TILEDB_OK;
617520
}
618521

tiledb/sm/serialization/array_schema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Buffer;
5252
class ArraySchema;
5353
class Dimension;
5454
class MemoryTracker;
55+
class URI;
5556
enum class SerializationType : uint8_t;
5657

5758
namespace serialization {

0 commit comments

Comments
 (0)