2018-11-28 11:37:12 +00:00
|
|
|
#include "FileDictionarySource.h"
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-05-25 19:26:17 +00:00
|
|
|
#include <DataStreams/OwningBlockInputStream.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
2016-12-08 02:49:04 +00:00
|
|
|
#include <Poco/File.h>
|
2018-11-28 11:37:12 +00:00
|
|
|
#include "DictionarySourceFactory.h"
|
|
|
|
#include "DictionaryStructure.h"
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
static const size_t max_block_size = 8192;
|
|
|
|
|
|
|
|
|
|
|
|
FileDictionarySource::FileDictionarySource(const std::string & filename, const std::string & format, Block & sample_block,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Context & context)
|
|
|
|
: filename{filename}, format{format}, sample_block{sample_block}, context(context)
|
2016-12-08 02:49:04 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
FileDictionarySource::FileDictionarySource(const FileDictionarySource & other)
|
2017-04-01 07:20:54 +00:00
|
|
|
: filename{other.filename}, format{other.format},
|
|
|
|
sample_block{other.sample_block}, context(other.context),
|
|
|
|
last_modification{other.last_modification}
|
2016-12-08 02:49:04 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr FileDictionarySource::loadAll()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto in_ptr = std::make_unique<ReadBufferFromFile>(filename);
|
|
|
|
auto stream = context.getInputFormat(
|
|
|
|
format, *in_ptr, sample_block, max_block_size);
|
|
|
|
last_modification = getLastModification();
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<OwningBlockInputStream<ReadBuffer>>(stream, std::move(in_ptr));
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string FileDictionarySource::toString() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return "File: " + filename + ' ' + format;
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Poco::Timestamp FileDictionarySource::getLastModification() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return Poco::File{filename}.getLastModified();
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 11:37:12 +00:00
|
|
|
void registerDictionarySourceFile(DictionarySourceFactory & factory)
|
|
|
|
{
|
|
|
|
auto createTableSource = [=](const DictionaryStructure & dict_struct,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
|
|
|
Block & sample_block,
|
|
|
|
const Context & context) -> DictionarySourcePtr {
|
|
|
|
if (dict_struct.has_expressions)
|
|
|
|
throw Exception {"Dictionary source of type `file` does not support attribute expressions", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
|
|
|
|
const auto filename = config.getString(config_prefix + ".file.path");
|
|
|
|
const auto format = config.getString(config_prefix + ".file.format");
|
|
|
|
|
|
|
|
return std::make_unique<FileDictionarySource>(filename, format, sample_block, context);
|
|
|
|
};
|
|
|
|
|
|
|
|
factory.registerSource("file", createTableSource);
|
|
|
|
}
|
|
|
|
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|