ClickHouse/dbms/src/Dictionaries/ExecutableDictionarySource.cpp
2017-05-25 22:26:17 +03:00

98 lines
3.1 KiB
C++

#include <Dictionaries/ExecutableDictionarySource.h>
#include <Common/ShellCommand.h>
#include <Interpreters/Context.h>
#include <DataStreams/OwningBlockInputStream.h>
#include <Dictionaries/DictionarySourceHelpers.h>
#include <DataStreams/IBlockOutputStream.h>
#include <DataTypes/DataTypesNumber.h>
#include <common/logger_useful.h>
namespace DB
{
static const size_t max_block_size = 8192;
ExecutableDictionarySource::ExecutableDictionarySource(const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
Block & sample_block, const Context & context)
: log(&Logger::get("ExecutableDictionarySource")),
dict_struct{dict_struct_},
command{config.getString(config_prefix + ".command")},
format{config.getString(config_prefix + ".format")},
sample_block{sample_block},
context(context)
{
}
ExecutableDictionarySource::ExecutableDictionarySource(const ExecutableDictionarySource & other)
: log(&Logger::get("ExecutableDictionarySource")),
dict_struct{other.dict_struct},
command{other.command},
format{other.format},
sample_block{other.sample_block},
context(other.context)
{
}
BlockInputStreamPtr ExecutableDictionarySource::loadAll()
{
LOG_TRACE(log, "loadAll " + toString());
auto process = ShellCommand::execute(command);
auto input_stream = context.getInputFormat(format, process->out, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ShellCommand>>(input_stream, std::move(process));
}
BlockInputStreamPtr ExecutableDictionarySource::loadIds(const std::vector<UInt64> & ids)
{
LOG_TRACE(log, "loadIds " << toString() << " size = " << ids.size());
auto process = ShellCommand::execute(command);
auto output_stream = context.getOutputFormat(format, process->in, sample_block);
formatIDs(output_stream, ids);
process->in.close();
auto input_stream = context.getInputFormat(format, process->out, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ShellCommand>>(input_stream, std::move(process));
}
BlockInputStreamPtr ExecutableDictionarySource::loadKeys(
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows)
{
LOG_TRACE(log, "loadKeys " << toString() << " size = " << requested_rows.size());
auto process = ShellCommand::execute(command);
auto output_stream = context.getOutputFormat(format, process->in, sample_block);
formatKeys(dict_struct, output_stream, key_columns);
process->in.close();
auto input_stream = context.getInputFormat(format, process->out, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ShellCommand>>(input_stream, std::move(process));
}
bool ExecutableDictionarySource::isModified() const
{
return true;
}
bool ExecutableDictionarySource::supportsSelectiveLoad() const
{
return true;
}
DictionarySourcePtr ExecutableDictionarySource::clone() const
{
return std::make_unique<ExecutableDictionarySource>(*this);
}
std::string ExecutableDictionarySource::toString() const
{
return "Executable: " + command;
}
}