ClickHouse/dbms/src/Dictionaries/HTTPDictionarySource.cpp

105 lines
3.6 KiB
C++
Raw Normal View History

#include <Dictionaries/HTTPDictionarySource.h>
2016-11-15 19:51:06 +00:00
2016-11-25 00:16:20 +00:00
#include <Poco/Net/HTTPRequest.h>
#include <Interpreters/Context.h>
#include <Dictionaries/OwningBlockInputStream.h>
#include <IO/ReadWriteBufferFromHTTP.h>
#include <DataStreams/IBlockOutputStream.h>
#include <DataTypes/DataTypesNumber.h>
#include <IO/WriteBufferFromOStream.h>
#include <Dictionaries/ExecutableDictionarySource.h> // idsToBuffer, columnsToBuffer
#include <common/logger_useful.h>
2016-11-15 19:51:06 +00:00
namespace DB
{
2016-12-08 02:49:04 +00:00
static const size_t max_block_size = 8192;
2016-11-22 15:03:54 +00:00
HTTPDictionarySource::HTTPDictionarySource(const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
Block & sample_block, const Context & context)
: log(&Logger::get("HTTPDictionarySource")),
dict_struct{dict_struct_},
url{config.getString(config_prefix + ".url", "")},
format{config.getString(config_prefix + ".format")},
sample_block{sample_block},
context(context)
2016-11-15 19:51:06 +00:00
{
}
HTTPDictionarySource::HTTPDictionarySource(const HTTPDictionarySource & other)
: log(&Logger::get("HTTPDictionarySource")),
dict_struct{other.dict_struct},
url{other.url},
format{other.format},
sample_block{other.sample_block},
context(other.context)
2016-11-15 19:51:06 +00:00
{
}
BlockInputStreamPtr HTTPDictionarySource::loadAll()
{
LOG_TRACE(log, "loadAll " + toString());
Poco::URI uri(url);
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_GET);
auto stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(stream, std::move(in_ptr));
2016-11-15 19:51:06 +00:00
}
BlockInputStreamPtr HTTPDictionarySource::loadIds(const std::vector<UInt64> & ids)
{
LOG_TRACE(log, "loadIds " + toString() + " ids=" + std::to_string(ids.size()));
2016-11-19 00:07:58 +00:00
ReadWriteBufferFromHTTP::OutStreamCallback out_stream_callback = [&](std::ostream & out_stream) {
2016-11-22 15:03:54 +00:00
WriteBufferFromOStream out_buffer(out_stream);
idsToBuffer(context, format, sample_block, out_buffer, ids);
};
2016-11-24 19:57:24 +00:00
Poco::URI uri(url);
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_POST, out_stream_callback);
auto stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(stream, std::move(in_ptr));
2016-11-15 19:51:06 +00:00
}
BlockInputStreamPtr HTTPDictionarySource::loadKeys(
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows)
2016-11-15 19:51:06 +00:00
{
LOG_TRACE(log, "loadKeys " + toString() + " rows=" + std::to_string(requested_rows.size()));
2016-11-22 15:03:54 +00:00
ReadWriteBufferFromHTTP::OutStreamCallback out_stream_callback = [&](std::ostream & out_stream) {
WriteBufferFromOStream out_buffer(out_stream);
columnsToBuffer(context, format, sample_block, out_buffer, dict_struct, key_columns, requested_rows);
};
2016-11-22 15:03:54 +00:00
Poco::URI uri(url);
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_POST, out_stream_callback);
auto stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(stream, std::move(in_ptr));
2016-11-15 19:51:06 +00:00
}
bool HTTPDictionarySource::isModified() const
{
return true;
2016-11-15 19:51:06 +00:00
}
bool HTTPDictionarySource::supportsSelectiveLoad() const
{
return true;
2016-11-15 19:51:06 +00:00
}
DictionarySourcePtr HTTPDictionarySource::clone() const
{
return std::make_unique<HTTPDictionarySource>(*this);
2016-11-15 19:51:06 +00:00
}
std::string HTTPDictionarySource::toString() const
{
Poco::URI uri(url);
return uri.toString();
2016-11-15 19:51:06 +00:00
}
}