ClickHouse/src/Disks/ObjectStorages/MetadataStorageFactory.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
4.4 KiB
C++
Raw Normal View History

2023-12-29 18:50:14 +00:00
#include <Disks/ObjectStorages/MetadataStorageFactory.h>
#include <Disks/ObjectStorages/MetadataStorageFromDisk.h>
#include <Disks/ObjectStorages/MetadataStorageFromPlainObjectStorage.h>
2024-01-13 11:56:27 +00:00
#ifndef CLICKHOUSE_KEEPER_STANDALONE_BUILD
2023-12-29 18:50:14 +00:00
#include <Disks/ObjectStorages/Web/MetadataStorageFromStaticFilesWebServer.h>
2024-01-13 11:56:27 +00:00
#endif
2023-12-29 18:50:14 +00:00
#include <Disks/DiskLocal.h>
#include <Interpreters/Context.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NO_ELEMENTS_IN_CONFIG;
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
2024-01-10 15:44:21 +00:00
extern const int LOGICAL_ERROR;
2023-12-29 18:50:14 +00:00
}
MetadataStorageFactory & MetadataStorageFactory::instance()
{
static MetadataStorageFactory factory;
return factory;
}
void MetadataStorageFactory::registerMetadataStorageType(const std::string & metadata_type, Creator creator)
{
if (!registry.emplace(metadata_type, creator).second)
{
throw Exception(ErrorCodes::LOGICAL_ERROR,
"MetadataStorageFactory: the metadata type '{}' is not unique",
metadata_type);
}
}
MetadataStoragePtr MetadataStorageFactory::create(
const std::string & name,
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
ObjectStoragePtr object_storage,
const std::string & compatibility_type_hint) const
{
if (compatibility_type_hint.empty() && !config.has(config_prefix + ".metadata_type"))
{
throw Exception(ErrorCodes::NO_ELEMENTS_IN_CONFIG, "Expected `metadata_type` in config");
}
2024-01-10 12:14:10 +00:00
const auto type = config.getString(config_prefix + ".metadata_type", compatibility_type_hint);
2023-12-29 18:50:14 +00:00
const auto it = registry.find(type);
if (it == registry.end())
{
throw Exception(ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG,
"MetadataStorageFactory: unknown metadata storage type: {}", type);
}
return it->second(name, config, config_prefix, object_storage);
}
2024-01-12 16:55:57 +00:00
static std::string getObjectKeyCompatiblePrefix(
2024-01-10 12:14:10 +00:00
const IObjectStorage & object_storage,
const Poco::Util::AbstractConfiguration & config,
const String & config_prefix)
{
2024-01-12 16:27:45 +00:00
return config.getString(config_prefix + ".key_compatibility_prefix", object_storage.getCommonKeyPrefix());
2024-01-10 12:14:10 +00:00
}
2023-12-29 18:50:14 +00:00
void registerMetadataStorageFromDisk(MetadataStorageFactory & factory)
{
2024-01-10 12:14:10 +00:00
factory.registerMetadataStorageType("local", [](
2023-12-29 18:50:14 +00:00
const std::string & name,
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
ObjectStoragePtr object_storage) -> MetadataStoragePtr
{
auto metadata_path = config.getString(config_prefix + ".metadata_path",
fs::path(Context::getGlobalContextInstance()->getPath()) / "disks" / name / "");
fs::create_directories(metadata_path);
auto metadata_disk = std::make_shared<DiskLocal>(name + "-metadata", metadata_path, 0, config, config_prefix);
2024-01-12 16:55:57 +00:00
auto key_compatibility_prefix = getObjectKeyCompatiblePrefix(*object_storage, config, config_prefix);
2024-01-10 12:14:10 +00:00
return std::make_shared<MetadataStorageFromDisk>(metadata_disk, key_compatibility_prefix);
});
2023-12-29 18:50:14 +00:00
}
2024-01-10 12:14:10 +00:00
void registerPlainMetadataStorage(MetadataStorageFactory & factory)
2023-12-29 18:50:14 +00:00
{
2024-01-10 12:14:10 +00:00
factory.registerMetadataStorageType("plain", [](
2023-12-29 18:50:14 +00:00
const std::string & /* name */,
2024-01-10 12:14:10 +00:00
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
2023-12-29 18:50:14 +00:00
ObjectStoragePtr object_storage) -> MetadataStoragePtr
{
2024-01-12 16:55:57 +00:00
auto key_compatibility_prefix = getObjectKeyCompatiblePrefix(*object_storage, config, config_prefix);
2024-01-10 12:14:10 +00:00
return std::make_shared<MetadataStorageFromPlainObjectStorage>(object_storage, key_compatibility_prefix);
});
2023-12-29 18:50:14 +00:00
}
2024-01-13 11:56:27 +00:00
#ifndef CLICKHOUSE_KEEPER_STANDALONE_BUILD
2023-12-29 18:50:14 +00:00
void registerMetadataStorageFromStaticFilesWebServer(MetadataStorageFactory & factory)
{
2024-01-10 12:14:10 +00:00
factory.registerMetadataStorageType("web", [](
2023-12-29 18:50:14 +00:00
const std::string & /* name */,
const Poco::Util::AbstractConfiguration & /* config */,
const std::string & /* config_prefix */,
ObjectStoragePtr object_storage) -> MetadataStoragePtr
{
return std::make_shared<MetadataStorageFromStaticFilesWebServer>(assert_cast<const WebObjectStorage &>(*object_storage));
2024-01-10 12:14:10 +00:00
});
}
2024-01-13 11:56:27 +00:00
#endif
2024-01-10 12:14:10 +00:00
void registerMetadataStorages()
{
auto & factory = MetadataStorageFactory::instance();
registerMetadataStorageFromDisk(factory);
registerPlainMetadataStorage(factory);
2024-01-13 11:56:27 +00:00
#ifndef CLICKHOUSE_KEEPER_STANDALONE_BUILD
2024-01-10 12:14:10 +00:00
registerMetadataStorageFromStaticFilesWebServer(factory);
2024-01-13 11:56:27 +00:00
#endif
2023-12-29 18:50:14 +00:00
}
}