Skip to content

Commit

Permalink
dbms: Moved some methods to cpp. [#METR-21516]
Browse files Browse the repository at this point in the history
  • Loading branch information
excitoon committed Jun 5, 2016
1 parent 0403019 commit 70feb3f
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 178 deletions.
140 changes: 13 additions & 127 deletions dbms/include/DB/Dictionaries/MySQLDictionarySource.h
Expand Up @@ -20,146 +20,32 @@ class MySQLDictionarySource final : public IDictionarySource
public:
MySQLDictionarySource(const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
const Block & sample_block)
: dict_struct{dict_struct_},
db{config.getString(config_prefix + ".db", "")},
table{config.getString(config_prefix + ".table")},
where{config.getString(config_prefix + ".where", "")},
dont_check_update_time{config.getBool(config_prefix + ".dont_check_update_time", false)},
sample_block{sample_block},
pool{config, config_prefix},
query_builder{dict_struct, db, table, where},
load_all_query{query_builder.composeLoadAllQuery()}
{}
const Block & sample_block);

/// copy-constructor is provided in order to support cloneability
MySQLDictionarySource(const MySQLDictionarySource & other)
: dict_struct{other.dict_struct},
db{other.db},
table{other.table},
where{other.where},
dont_check_update_time{other.dont_check_update_time},
sample_block{other.sample_block},
pool{other.pool},
query_builder{dict_struct, db, table, where},
load_all_query{other.load_all_query}, last_modification{other.last_modification}
{}

BlockInputStreamPtr loadAll() override
{
last_modification = getLastModification();

LOG_TRACE(log, load_all_query);
return std::make_shared<MySQLBlockInputStream>(pool.Get(), load_all_query, sample_block, max_block_size);
}

BlockInputStreamPtr loadIds(const std::vector<std::uint64_t> & ids) override
{
/// Здесь не логгируем и не обновляем время модификации, так как запрос может быть большим, и часто задаваться.

const auto query = query_builder.composeLoadIdsQuery(ids);
return std::make_shared<MySQLBlockInputStream>(pool.Get(), query, sample_block, max_block_size);
}
MySQLDictionarySource(const MySQLDictionarySource & other);

BlockInputStreamPtr loadAll() override;

BlockInputStreamPtr loadKeys(
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows) override
{
/// Здесь не логгируем и не обновляем время модификации, так как запрос может быть большим, и часто задаваться.

const auto query = query_builder.composeLoadKeysQuery(key_columns, requested_rows, ExternalQueryBuilder::AND_OR_CHAIN);
return std::make_shared<MySQLBlockInputStream>(pool.Get(), query, sample_block, max_block_size);
}
BlockInputStreamPtr loadIds(const std::vector<std::uint64_t> & ids) override;

bool isModified() const override
{
if (dont_check_update_time)
return true;
BlockInputStreamPtr loadKeys(
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows) override;

return getLastModification() > last_modification;
}
bool isModified() const override;

bool supportsSelectiveLoad() const override { return true; }
bool supportsSelectiveLoad() const override;

DictionarySourcePtr clone() const override { return std::make_unique<MySQLDictionarySource>(*this); }
DictionarySourcePtr clone() const override;

std::string toString() const override
{
return "MySQL: " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
}
std::string toString() const override;

private:
Logger * log = &Logger::get("MySQLDictionarySource");

static std::string quoteForLike(const std::string s)
{
std::string tmp;
tmp.reserve(s.size());

for (auto c : s)
{
if (c == '%' || c == '_' || c == '\\')
tmp.push_back('\\');
tmp.push_back(c);
}

std::string res;
{
WriteBufferFromString out(res);
writeQuoted(tmp, out);
}
return res;
}


LocalDateTime getLastModification() const
{
LocalDateTime update_time{std::time(nullptr)};

if (dont_check_update_time)
return update_time;

try
{
auto connection = pool.Get();
auto query = connection->query("SHOW TABLE STATUS LIKE " + quoteForLike(table));

LOG_TRACE(log, query.str());

auto result = query.use();

size_t fetched_rows = 0;
if (auto row = result.fetch())
{
++fetched_rows;
const auto UPDATE_TIME_IDX = 12;
const auto & update_time_value = row[UPDATE_TIME_IDX];

if (!update_time_value.isNull())
{
update_time = update_time_value.getDateTime();
LOG_TRACE(log, "Got update time: " << update_time);
}

/// fetch remaining rows to avoid "commands out of sync" error
while (result.fetch())
++fetched_rows;
}

if (0 == fetched_rows)
LOG_ERROR(log, "Cannot find table in SHOW TABLE STATUS result.");

if (fetched_rows > 1)
LOG_ERROR(log, "Found more than one table in SHOW TABLE STATUS result.");
}
catch (...)
{
tryLogCurrentException("MySQLDictionarySource");
}

/// we suppose failure to get modification time is not an error, therefore return current time
return update_time;
}
static std::string quoteForLike(const std::string s);

LocalDateTime getLastModification() const;

const DictionaryStructure dict_struct;
const std::string db;
Expand Down
60 changes: 11 additions & 49 deletions dbms/include/DB/Dictionaries/ODBCDictionarySource.h
Expand Up @@ -21,63 +21,25 @@ class ODBCDictionarySource final : public IDictionarySource
public:
ODBCDictionarySource(const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
const Block & sample_block)
: dict_struct{dict_struct_},
db{config.getString(config_prefix + ".db", "")},
table{config.getString(config_prefix + ".table")},
where{config.getString(config_prefix + ".where", "")},
sample_block{sample_block},
pool{std::make_shared<Poco::Data::SessionPool>(
config.getString(config_prefix + ".connector", "ODBC"),
config.getString(config_prefix + ".connection_string"))},
query_builder{dict_struct, db, table, where},
load_all_query{query_builder.composeLoadAllQuery()}
{}
const Block & sample_block);

/// copy-constructor is provided in order to support cloneability
ODBCDictionarySource(const ODBCDictionarySource & other)
: dict_struct{other.dict_struct},
db{other.db},
table{other.table},
where{other.where},
sample_block{other.sample_block},
pool{other.pool},
query_builder{dict_struct, db, table, where},
load_all_query{other.load_all_query}
{}

BlockInputStreamPtr loadAll() override
{
LOG_TRACE(log, load_all_query);
return std::make_shared<ODBCBlockInputStream>(pool->get(), load_all_query, sample_block, max_block_size);
}

BlockInputStreamPtr loadIds(const std::vector<std::uint64_t> & ids) override
{
const auto query = query_builder.composeLoadIdsQuery(ids);
return std::make_shared<ODBCBlockInputStream>(pool->get(), query, sample_block, max_block_size);
}
ODBCDictionarySource(const ODBCDictionarySource & other);

BlockInputStreamPtr loadAll() override;

BlockInputStreamPtr loadIds(const std::vector<std::uint64_t> & ids) override;

BlockInputStreamPtr loadKeys(
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows) override
{
const auto query = query_builder.composeLoadKeysQuery(key_columns, requested_rows, ExternalQueryBuilder::AND_OR_CHAIN);
return std::make_shared<ODBCBlockInputStream>(pool->get(), query, sample_block, max_block_size);
}
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows) override;

bool isModified() const override
{
return true;
}
bool isModified() const override;

bool supportsSelectiveLoad() const override { return true; }
bool supportsSelectiveLoad() const override;

DictionarySourcePtr clone() const override { return std::make_unique<ODBCDictionarySource>(*this); }
DictionarySourcePtr clone() const override;

std::string toString() const override
{
return "ODBC: " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
}
std::string toString() const override;

private:
Logger * log = &Logger::get("ODBCDictionarySource");
Expand Down
157 changes: 156 additions & 1 deletion dbms/src/Dictionaries/MySQLDictionarySource.cpp
@@ -1,3 +1,158 @@
#include <DB/Dictionaries/MySQLDictionarySource.h>

decltype(DB::MySQLDictionarySource::max_block_size) DB::MySQLDictionarySource::max_block_size;

namespace DB
{


decltype(MySQLDictionarySource::max_block_size) MySQLDictionarySource::max_block_size;


MySQLDictionarySource::MySQLDictionarySource(const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
const Block & sample_block)
: dict_struct{dict_struct_},
db{config.getString(config_prefix + ".db", "")},
table{config.getString(config_prefix + ".table")},
where{config.getString(config_prefix + ".where", "")},
dont_check_update_time{config.getBool(config_prefix + ".dont_check_update_time", false)},
sample_block{sample_block},
pool{config, config_prefix},
query_builder{dict_struct, db, table, where},
load_all_query{query_builder.composeLoadAllQuery()}
{
}

/// copy-constructor is provided in order to support cloneability
MySQLDictionarySource::MySQLDictionarySource(const MySQLDictionarySource & other)
: dict_struct{other.dict_struct},
db{other.db},
table{other.table},
where{other.where},
dont_check_update_time{other.dont_check_update_time},
sample_block{other.sample_block},
pool{other.pool},
query_builder{dict_struct, db, table, where},
load_all_query{other.load_all_query}, last_modification{other.last_modification}
{
}

BlockInputStreamPtr MySQLDictionarySource::loadAll()
{
last_modification = getLastModification();

LOG_TRACE(log, load_all_query);
return std::make_shared<MySQLBlockInputStream>(pool.Get(), load_all_query, sample_block, max_block_size);
}

BlockInputStreamPtr MySQLDictionarySource::loadIds(const std::vector<std::uint64_t> & ids)
{
/// Здесь не логгируем и не обновляем время модификации, так как запрос может быть большим, и часто задаваться.

const auto query = query_builder.composeLoadIdsQuery(ids);
return std::make_shared<MySQLBlockInputStream>(pool.Get(), query, sample_block, max_block_size);
}

BlockInputStreamPtr MySQLDictionarySource::loadKeys(
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows)
{
/// Здесь не логгируем и не обновляем время модификации, так как запрос может быть большим, и часто задаваться.

const auto query = query_builder.composeLoadKeysQuery(key_columns, requested_rows, ExternalQueryBuilder::AND_OR_CHAIN);
return std::make_shared<MySQLBlockInputStream>(pool.Get(), query, sample_block, max_block_size);
}

bool MySQLDictionarySource::isModified() const
{
if (dont_check_update_time)
return true;

return getLastModification() > last_modification;
}

bool MySQLDictionarySource::supportsSelectiveLoad() const
{
return true;
}

DictionarySourcePtr MySQLDictionarySource::clone() const
{
return std::make_unique<MySQLDictionarySource>(*this);
}

std::string MySQLDictionarySource::toString() const
{
return "MySQL: " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
}

std::string MySQLDictionarySource::quoteForLike(const std::string s)
{
std::string tmp;
tmp.reserve(s.size());

for (auto c : s)
{
if (c == '%' || c == '_' || c == '\\')
tmp.push_back('\\');
tmp.push_back(c);
}

std::string res;
{
WriteBufferFromString out(res);
writeQuoted(tmp, out);
}
return res;
}

LocalDateTime MySQLDictionarySource::getLastModification() const
{
LocalDateTime update_time{std::time(nullptr)};

if (dont_check_update_time)
return update_time;

try
{
auto connection = pool.Get();
auto query = connection->query("SHOW TABLE STATUS LIKE " + quoteForLike(table));

LOG_TRACE(log, query.str());

auto result = query.use();

size_t fetched_rows = 0;
if (auto row = result.fetch())
{
++fetched_rows;
const auto UPDATE_TIME_IDX = 12;
const auto & update_time_value = row[UPDATE_TIME_IDX];

if (!update_time_value.isNull())
{
update_time = update_time_value.getDateTime();
LOG_TRACE(log, "Got update time: " << update_time);
}

/// fetch remaining rows to avoid "commands out of sync" error
while (result.fetch())
++fetched_rows;
}

if (0 == fetched_rows)
LOG_ERROR(log, "Cannot find table in SHOW TABLE STATUS result.");

if (fetched_rows > 1)
LOG_ERROR(log, "Found more than one table in SHOW TABLE STATUS result.");
}
catch (...)
{
tryLogCurrentException("MySQLDictionarySource");
}

/// we suppose failure to get modification time is not an error, therefore return current time
return update_time;
}


}

0 comments on commit 70feb3f

Please sign in to comment.