2013-02-08 17:06:29 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <statdaemons/OptimizedRegularExpression.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Interpreters/Context.h>
|
|
|
|
|
#include <DB/Storages/IStorage.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
2013-05-15 12:32:40 +00:00
|
|
|
|
/** То и дело объединяет таблицы, подходящие под регэксп, в таблицы типа Chunks.
|
2013-02-08 17:06:29 +00:00
|
|
|
|
* После объндинения заменяет исходные таблицы таблицами типа ChunkRef.
|
|
|
|
|
* При чтении ведет себя как таблица типа Merge.
|
|
|
|
|
*/
|
|
|
|
|
class StorageChunkMerger : public IStorage
|
|
|
|
|
{
|
2014-02-11 18:38:21 +00:00
|
|
|
|
typedef std::vector<StoragePtr> Storages;
|
2013-02-08 17:06:29 +00:00
|
|
|
|
public:
|
|
|
|
|
static StoragePtr create(
|
|
|
|
|
const std::string & this_database_,/// Имя БД для этой таблицы.
|
|
|
|
|
const std::string & name_, /// Имя таблицы.
|
|
|
|
|
NamesAndTypesListPtr columns_, /// Список столбцов.
|
2014-10-03 15:30:10 +00:00
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
|
const ColumnDefaults & column_defaults_,
|
2013-02-08 17:06:29 +00:00
|
|
|
|
const String & source_database_, /// В какой БД искать таблицы-источники.
|
|
|
|
|
const String & table_name_regexp_, /// Регексп имён таблиц-источников.
|
|
|
|
|
const std::string & destination_name_prefix_, /// Префикс имен создаваемых таблиц типа Chunks.
|
|
|
|
|
size_t chunks_to_merge_, /// Сколько чанков сливать в одну группу.
|
2013-05-05 18:02:05 +00:00
|
|
|
|
Context & context_); /// Известные таблицы.
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
2014-10-03 17:55:36 +00:00
|
|
|
|
std::string getName() const override { return "ChunkMerger"; }
|
|
|
|
|
std::string getTableName() const override { return name; }
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
2014-11-12 10:37:47 +00:00
|
|
|
|
const NamesAndTypesList & getColumnsListImpl() const override { return *columns; }
|
2014-10-03 17:55:36 +00:00
|
|
|
|
NameAndTypePair getColumn(const String & column_name) const override;
|
|
|
|
|
bool hasColumn(const String & column_name) const override;
|
2014-01-16 14:52:13 +00:00
|
|
|
|
|
2013-02-08 17:06:29 +00:00
|
|
|
|
BlockInputStreams read(
|
|
|
|
|
const Names & column_names,
|
|
|
|
|
ASTPtr query,
|
2014-12-17 11:53:17 +00:00
|
|
|
|
const Context & context,
|
2013-02-08 17:06:29 +00:00
|
|
|
|
const Settings & settings,
|
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
|
|
|
|
size_t max_block_size = DEFAULT_BLOCK_SIZE,
|
2014-10-03 17:55:36 +00:00
|
|
|
|
unsigned threads = 1) override;
|
2013-09-30 01:29:19 +00:00
|
|
|
|
|
2014-10-03 17:55:36 +00:00
|
|
|
|
void shutdown() override;
|
2014-02-11 18:38:21 +00:00
|
|
|
|
|
2014-10-03 17:55:36 +00:00
|
|
|
|
~StorageChunkMerger() override;
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
2013-02-08 17:06:29 +00:00
|
|
|
|
private:
|
|
|
|
|
String this_database;
|
|
|
|
|
String name;
|
|
|
|
|
NamesAndTypesListPtr columns;
|
|
|
|
|
String source_database;
|
|
|
|
|
OptimizedRegularExpression table_name_regexp;
|
|
|
|
|
std::string destination_name_prefix;
|
|
|
|
|
size_t chunks_to_merge;
|
2013-05-05 18:02:05 +00:00
|
|
|
|
Context & context;
|
2013-09-14 05:14:22 +00:00
|
|
|
|
Settings settings;
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
|
|
|
|
std::thread merge_thread;
|
2013-07-28 01:15:52 +00:00
|
|
|
|
Poco::Event cancel_merge_thread;
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
2013-02-08 17:06:29 +00:00
|
|
|
|
Logger * log;
|
2013-10-22 19:11:03 +00:00
|
|
|
|
volatile bool shutdown_called;
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
2014-01-27 13:52:01 +00:00
|
|
|
|
/// Название виртуального столбца, отвечающего за имя таблицы, из которой идет чтение. (Например "_table")
|
2014-01-16 14:52:13 +00:00
|
|
|
|
String _table_column_name;
|
|
|
|
|
|
2013-02-08 17:06:29 +00:00
|
|
|
|
StorageChunkMerger(
|
|
|
|
|
const std::string & this_database_,
|
|
|
|
|
const std::string & name_,
|
|
|
|
|
NamesAndTypesListPtr columns_,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
|
const ColumnDefaults & column_defaults_,
|
2013-02-08 17:06:29 +00:00
|
|
|
|
const String & source_database_,
|
|
|
|
|
const String & table_name_regexp_,
|
|
|
|
|
const std::string & destination_name_prefix_,
|
|
|
|
|
size_t chunks_to_merge_,
|
|
|
|
|
Context & context_);
|
2014-10-03 17:55:36 +00:00
|
|
|
|
|
2013-02-08 17:06:29 +00:00
|
|
|
|
void mergeThread();
|
|
|
|
|
bool maybeMergeSomething();
|
|
|
|
|
Storages selectChunksToMerge();
|
2013-05-15 13:47:09 +00:00
|
|
|
|
bool mergeChunks(const Storages & chunks);
|
2013-07-28 01:15:52 +00:00
|
|
|
|
|
2014-10-03 17:55:36 +00:00
|
|
|
|
Block getBlockWithVirtualColumns(const Storages & selected_tables) const;
|
|
|
|
|
|
2013-05-15 12:32:40 +00:00
|
|
|
|
typedef std::set<std::string> TableNames;
|
|
|
|
|
/// Какие таблицы типа Chunks сейчас пишет хоть один ChunkMerger.
|
|
|
|
|
/// Нужно смотреть, залочив mutex из контекста.
|
|
|
|
|
static TableNames currently_written_groups;
|
2013-02-08 17:06:29 +00:00
|
|
|
|
};
|
2014-10-16 01:21:03 +00:00
|
|
|
|
|
2013-02-08 17:06:29 +00:00
|
|
|
|
}
|