ClickHouse/src/Dictionaries/LibraryDictionarySource.cpp

195 lines
5.9 KiB
C++
Raw Normal View History

#include "LibraryDictionarySource.h"
2021-03-05 09:38:00 +00:00
#include <Interpreters/Context.h>
2021-10-02 07:13:14 +00:00
#include <base/logger_useful.h>
#include <Common/filesystemHelpers.h>
2021-03-05 09:38:00 +00:00
#include <IO/WriteBufferFromString.h>
2021-03-10 18:02:43 +00:00
#include <IO/WriteHelpers.h>
2021-04-30 20:35:44 +00:00
#include <filesystem>
#include <Dictionaries/DictionarySourceFactory.h>
#include <Dictionaries/DictionarySourceHelpers.h>
#include <Dictionaries/DictionaryStructure.h>
#include <Dictionaries/registerDictionaries.h>
2021-03-05 09:38:00 +00:00
2021-04-30 20:35:44 +00:00
namespace fs = std::filesystem;
namespace DB
{
2021-03-05 09:38:00 +00:00
namespace ErrorCodes
{
extern const int FILE_DOESNT_EXIST;
extern const int EXTERNAL_LIBRARY_ERROR;
extern const int PATH_ACCESS_DENIED;
}
LibraryDictionarySource::LibraryDictionarySource(
const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config,
2019-08-03 11:02:40 +00:00
const std::string & config_prefix_,
Block & sample_block_,
2021-06-01 12:20:52 +00:00
ContextPtr context_,
bool created_from_ddl)
2020-05-30 21:57:37 +00:00
: log(&Poco::Logger::get("LibraryDictionarySource"))
, dict_struct{dict_struct_}
2019-08-03 11:02:40 +00:00
, config_prefix{config_prefix_}
, path{config.getString(config_prefix + ".path", "")}
2021-03-22 14:39:17 +00:00
, dictionary_id(getDictID())
2019-08-03 11:02:40 +00:00
, sample_block{sample_block_}
, context(Context::createCopy(context_))
{
2021-08-27 10:46:59 +00:00
auto dictionaries_lib_path = context->getDictionariesLibPath();
2021-10-17 08:42:36 +00:00
if (created_from_ddl && !fileOrSymlinkPathStartsWith(path, dictionaries_lib_path))
2021-08-27 10:46:59 +00:00
throw Exception(ErrorCodes::PATH_ACCESS_DENIED, "File path {} is not inside {}", path, dictionaries_lib_path);
2021-04-30 20:35:44 +00:00
if (!fs::exists(path))
throw Exception(ErrorCodes::FILE_DOESNT_EXIST, "LibraryDictionarySource: Can't load library {}: file doesn't exist", path);
2021-03-05 09:38:00 +00:00
2021-03-24 08:41:42 +00:00
description.init(sample_block);
2021-03-05 09:38:00 +00:00
LibraryBridgeHelper::LibraryInitData library_data
{
.library_path = path,
.library_settings = getLibrarySettingsString(config, config_prefix + ".settings"),
.dict_attributes = getDictAttributesString()
};
2021-08-01 09:56:48 +00:00
bridge_helper = std::make_shared<LibraryBridgeHelper>(context, description.sample_block, dictionary_id, library_data);
2021-03-05 09:38:00 +00:00
2021-08-02 07:18:51 +00:00
if (!bridge_helper->initLibrary())
2021-03-07 19:53:10 +00:00
throw Exception(ErrorCodes::EXTERNAL_LIBRARY_ERROR, "Failed to create shared library from path: {}", path);
}
2021-03-05 09:38:00 +00:00
LibraryDictionarySource::~LibraryDictionarySource()
{
try
{
bridge_helper->removeLibrary();
}
catch (...)
{
tryLogCurrentException("LibraryDictionarySource");
}
2021-03-05 09:38:00 +00:00
}
LibraryDictionarySource::LibraryDictionarySource(const LibraryDictionarySource & other)
2020-05-30 21:57:37 +00:00
: log(&Poco::Logger::get("LibraryDictionarySource"))
, dict_struct{other.dict_struct}
, config_prefix{other.config_prefix}
, path{other.path}
2021-03-22 14:39:17 +00:00
, dictionary_id{getDictID()}
, sample_block{other.sample_block}
2021-03-05 09:38:00 +00:00
, context(other.context)
, description{other.description}
{
bridge_helper = std::make_shared<LibraryBridgeHelper>(context, description.sample_block, dictionary_id, other.bridge_helper->getLibraryData());
2021-08-02 07:18:51 +00:00
if (!bridge_helper->cloneLibrary(other.dictionary_id))
2021-08-01 09:56:48 +00:00
throw Exception(ErrorCodes::EXTERNAL_LIBRARY_ERROR, "Failed to clone library");
}
2021-03-05 09:38:00 +00:00
2021-03-07 15:23:20 +00:00
bool LibraryDictionarySource::isModified() const
2021-03-05 09:38:00 +00:00
{
2021-03-07 15:23:20 +00:00
return bridge_helper->isModified();
2021-03-05 09:38:00 +00:00
}
2021-03-07 15:23:20 +00:00
bool LibraryDictionarySource::supportsSelectiveLoad() const
{
2021-03-07 15:23:20 +00:00
return bridge_helper->supportsSelectiveLoad();
}
2021-03-05 09:38:00 +00:00
Pipe LibraryDictionarySource::loadAll()
{
2021-03-07 15:23:20 +00:00
LOG_TRACE(log, "loadAll {}", toString());
2021-03-24 09:23:29 +00:00
return bridge_helper->loadAll();
}
2021-03-05 09:38:00 +00:00
Pipe LibraryDictionarySource::loadIds(const std::vector<UInt64> & ids)
{
2021-03-07 15:23:20 +00:00
LOG_TRACE(log, "loadIds {} size = {}", toString(), ids.size());
2021-08-01 09:56:48 +00:00
return bridge_helper->loadIds(ids);
}
2021-03-05 09:38:00 +00:00
Pipe LibraryDictionarySource::loadKeys(const Columns & key_columns, const std::vector<std::size_t> & requested_rows)
{
2021-03-07 15:23:20 +00:00
LOG_TRACE(log, "loadKeys {} size = {}", toString(), requested_rows.size());
2021-03-10 13:10:05 +00:00
auto block = blockForKeys(dict_struct, key_columns, requested_rows);
2021-03-24 08:41:42 +00:00
return bridge_helper->loadKeys(block);
}
2021-03-05 09:38:00 +00:00
DictionarySourcePtr LibraryDictionarySource::clone() const
{
return std::make_shared<LibraryDictionarySource>(*this);
}
2021-03-05 09:38:00 +00:00
std::string LibraryDictionarySource::toString() const
{
return path;
}
2021-03-05 09:38:00 +00:00
2021-03-05 15:37:43 +00:00
String LibraryDictionarySource::getLibrarySettingsString(const Poco::Util::AbstractConfiguration & config, const std::string & config_root)
{
Poco::Util::AbstractConfiguration::Keys config_keys;
config.keys(config_root, config_keys);
2021-03-24 07:53:15 +00:00
WriteBufferFromOwnString out;
std::vector<std::string> settings;
2021-03-05 15:37:43 +00:00
for (const auto & key : config_keys)
{
std::string key_name = key;
auto bracket_pos = key.find('[');
if (bracket_pos != std::string::npos && bracket_pos > 0)
key_name = key.substr(0, bracket_pos);
2021-03-24 07:53:15 +00:00
settings.push_back(key_name);
settings.push_back(config.getString(config_root + "." + key));
2021-03-05 15:37:43 +00:00
}
2021-03-24 07:53:15 +00:00
writeVectorBinary(settings, out);
return out.str();
2021-03-05 15:37:43 +00:00
}
2021-03-24 19:32:31 +00:00
String LibraryDictionarySource::getDictAttributesString()
{
std::vector<String> attributes_names(dict_struct.attributes.size());
for (size_t i = 0; i < dict_struct.attributes.size(); ++i)
attributes_names[i] = dict_struct.attributes[i].name;
WriteBufferFromOwnString out;
writeVectorBinary(attributes_names, out);
return out.str();
}
void registerDictionarySourceLibrary(DictionarySourceFactory & factory)
{
auto create_table_source = [=](const DictionaryStructure & dict_struct,
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
Block & sample_block,
2021-08-12 15:16:55 +00:00
ContextPtr global_context,
2020-08-15 03:10:57 +00:00
const std::string & /* default_database */,
bool created_from_ddl) -> DictionarySourcePtr
{
2021-08-12 15:16:55 +00:00
return std::make_unique<LibraryDictionarySource>(dict_struct, config, config_prefix + ".library", sample_block, global_context, created_from_ddl);
};
2021-03-05 09:38:00 +00:00
factory.registerSource("library", create_table_source);
}
2021-03-05 09:38:00 +00:00
}