ClickHouse/dbms/include/DB/Storages/StorageLog.h

143 lines
4.1 KiB
C
Raw Normal View History

2011-10-31 17:55:06 +00:00
#pragma once
2010-03-18 19:32:14 +00:00
#include <Poco/File.h>
#include <DB/Core/NamesAndTypes.h>
2011-10-24 12:10:59 +00:00
#include <DB/IO/ReadBufferFromFile.h>
#include <DB/IO/WriteBufferFromFile.h>
2010-06-04 18:25:25 +00:00
#include <DB/IO/CompressedReadBuffer.h>
#include <DB/IO/CompressedWriteBuffer.h>
2010-03-18 19:32:14 +00:00
#include <DB/Storages/IStorage.h>
2011-09-04 21:23:19 +00:00
#include <DB/DataStreams/IProfilingBlockInputStream.h>
2010-03-18 19:32:14 +00:00
namespace DB
{
class StorageLog;
2012-01-10 22:11:51 +00:00
/** Смещение до каждой некоторой пачки значений.
* Эти пачки имеют одинаковый размер в разных столбцах.
* Они нужны, чтобы можно было читать данные в несколько потоков.
*/
struct Mark
{
size_t rows; /// Сколько строк содержится в этой пачке и всех предыдущих.
size_t offset; /// Смещение до пачки в сжатом файле.
};
typedef std::vector<Mark> Marks;
2011-09-04 21:23:19 +00:00
class LogBlockInputStream : public IProfilingBlockInputStream
2010-03-18 19:32:14 +00:00
{
public:
2012-01-10 22:11:51 +00:00
LogBlockInputStream(size_t block_size_, const Names & column_names_, StorageLog & storage_, size_t mark_number_, size_t rows_limit_);
2011-09-04 21:23:19 +00:00
Block readImpl();
String getName() const { return "LogBlockInputStream"; }
2012-01-10 22:11:51 +00:00
BlockInputStreamPtr clone() { return new LogBlockInputStream(block_size, column_names, storage, mark_number, rows_limit); }
2010-03-18 19:32:14 +00:00
private:
size_t block_size;
2011-08-28 05:13:24 +00:00
Names column_names;
2010-03-18 19:32:14 +00:00
StorageLog & storage;
2012-01-10 22:11:51 +00:00
size_t mark_number; /// С какой засечки читать данные
size_t rows_limit; /// Максимальное количество строк, которых можно прочитать
size_t rows_read;
2010-03-18 19:32:14 +00:00
2010-03-18 20:52:28 +00:00
struct Stream
{
2012-01-10 22:11:51 +00:00
Stream(const std::string & data_path, size_t offset)
: plain(data_path), compressed(plain)
{
plain.seek(offset);
}
2010-03-18 20:52:28 +00:00
2011-10-24 12:10:59 +00:00
ReadBufferFromFile plain;
2010-06-04 18:25:25 +00:00
CompressedReadBuffer compressed;
2010-03-18 20:52:28 +00:00
};
typedef std::map<std::string, SharedPtr<Stream> > FileStreams;
2010-03-18 19:32:14 +00:00
FileStreams streams;
};
class LogBlockOutputStream : public IBlockOutputStream
{
public:
LogBlockOutputStream(StorageLog & storage_);
void write(const Block & block);
2011-10-24 12:10:59 +00:00
BlockOutputStreamPtr clone() { return new LogBlockOutputStream(storage); }
2010-03-18 19:32:14 +00:00
private:
StorageLog & storage;
2010-03-18 20:52:28 +00:00
struct Stream
{
Stream(const std::string & data_path, const std::string & marks_path) :
plain(data_path, DBMS_DEFAULT_BUFFER_SIZE, O_APPEND | O_CREAT | O_WRONLY),
2012-04-14 00:18:07 +00:00
compressed(plain),
marks(marks_path, DBMS_DEFAULT_BUFFER_SIZE, O_APPEND | O_CREAT | O_WRONLY) {}
2010-03-18 20:52:28 +00:00
2011-10-24 12:10:59 +00:00
WriteBufferFromFile plain;
2010-06-04 18:25:25 +00:00
CompressedWriteBuffer compressed;
2012-01-09 19:20:48 +00:00
WriteBufferFromFile marks;
2010-03-18 20:52:28 +00:00
};
typedef std::map<std::string, SharedPtr<Stream> > FileStreams;
2010-03-18 19:32:14 +00:00
FileStreams streams;
};
/** Реализует хранилище, подходящее для логов.
2012-01-09 19:20:48 +00:00
* Ключи не поддерживаются.
2010-03-18 19:32:14 +00:00
* Данные хранятся в сжатом виде.
*/
class StorageLog : public IStorage
{
friend class LogBlockInputStream;
friend class LogBlockOutputStream;
public:
/** Подцепить таблицу с соответствующим именем, по соответствующему пути (с / на конце),
* (корректность имён и путей не проверяется)
* состоящую из указанных столбцов; создать файлы, если их нет.
*/
2012-01-09 19:20:48 +00:00
StorageLog(const std::string & path_, const std::string & name_, NamesAndTypesListPtr columns_);
2010-03-18 19:32:14 +00:00
std::string getName() const { return "Log"; }
2011-08-15 02:24:44 +00:00
std::string getTableName() const { return name; }
2011-11-01 17:12:11 +00:00
const NamesAndTypesList & getColumnsList() const { return *columns; }
2010-03-18 19:32:14 +00:00
2012-01-09 19:20:48 +00:00
BlockInputStreams read(
2011-08-09 15:57:33 +00:00
const Names & column_names,
2011-08-15 01:12:57 +00:00
ASTPtr query,
2012-05-22 18:32:45 +00:00
QueryProcessingStage::Enum & processed_stage,
2012-01-09 19:20:48 +00:00
size_t max_block_size = DEFAULT_BLOCK_SIZE,
2012-05-30 04:45:49 +00:00
unsigned threads = 1);
2010-03-18 19:32:14 +00:00
2011-08-28 02:22:23 +00:00
BlockOutputStreamPtr write(
2011-08-15 01:12:57 +00:00
ASTPtr query);
2010-03-18 19:32:14 +00:00
2011-11-05 23:31:19 +00:00
void drop();
2012-06-18 06:19:13 +00:00
void rename(const String & new_path_to_db, const String & new_name);
2011-11-05 23:31:19 +00:00
2010-03-18 19:32:14 +00:00
private:
2012-06-18 06:19:13 +00:00
String path;
String name;
2011-11-01 17:12:11 +00:00
NamesAndTypesListPtr columns;
2010-03-18 19:32:14 +00:00
2012-01-10 22:11:51 +00:00
/// Данные столбца
struct ColumnData
{
Poco::File data_file;
Poco::File marks_file;
Marks marks;
};
2012-06-18 06:19:13 +00:00
typedef std::map<String, ColumnData> Files_t;
2010-03-18 19:32:14 +00:00
Files_t files;
};
}