2011-10-31 17:55:06 +00:00
|
|
|
|
#pragma once
|
2010-03-18 19:32:14 +00:00
|
|
|
|
|
|
|
|
|
#include <Poco/File.h>
|
2012-11-30 04:28:13 +00:00
|
|
|
|
#include <Poco/RWLock.h>
|
2010-03-18 19:32:14 +00:00
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
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); }
|
2012-10-20 02:10:47 +00:00
|
|
|
|
protected:
|
|
|
|
|
Block readImpl();
|
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)
|
2012-06-21 18:14:38 +00:00
|
|
|
|
: plain(data_path, std::min(static_cast<size_t>(DBMS_DEFAULT_BUFFER_SIZE), Poco::File(data_path).getSize())),
|
|
|
|
|
compressed(plain)
|
2012-01-10 22:11:51 +00:00
|
|
|
|
{
|
2012-06-21 18:14:38 +00:00
|
|
|
|
if (offset)
|
|
|
|
|
plain.seek(offset);
|
2012-01-10 22:11:51 +00:00
|
|
|
|
}
|
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;
|
2012-08-29 20:07:24 +00:00
|
|
|
|
|
|
|
|
|
void addStream(const String & name, const IDataType & type, size_t level = 0);
|
|
|
|
|
void readData(const String & name, const IDataType & type, IColumn & column, size_t max_rows_to_read, size_t level = 0);
|
2010-03-18 19:32:14 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2012-11-30 04:28:13 +00:00
|
|
|
|
Poco::ScopedWriteRWLock lock;
|
2010-03-18 19:32:14 +00:00
|
|
|
|
|
2010-03-18 20:52:28 +00:00
|
|
|
|
struct Stream
|
|
|
|
|
{
|
2012-02-23 22:35:53 +00:00
|
|
|
|
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),
|
2012-09-19 18:45:01 +00:00
|
|
|
|
marks(marks_path, 4096, O_APPEND | O_CREAT | O_WRONLY)
|
|
|
|
|
{
|
|
|
|
|
plain_offset = Poco::File(data_path).getSize();
|
|
|
|
|
}
|
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;
|
2012-09-19 18:45:01 +00:00
|
|
|
|
|
|
|
|
|
size_t plain_offset; /// Сколько байт было в файле на момент создания LogBlockOutputStream.
|
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-08-29 20:07:24 +00:00
|
|
|
|
|
|
|
|
|
void addStream(const String & name, const IDataType & type, size_t level = 0);
|
|
|
|
|
void writeData(const String & name, const IDataType & type, const IColumn & column, size_t level = 0);
|
2010-03-18 19:32:14 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Реализует хранилище, подходящее для логов.
|
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;
|
2012-06-21 16:33:00 +00:00
|
|
|
|
|
2012-11-30 04:28:13 +00:00
|
|
|
|
Poco::RWLock rwlock;
|
|
|
|
|
|
2012-08-29 20:07:24 +00:00
|
|
|
|
void addFile(const String & column_name, const IDataType & type, size_t level = 0);
|
|
|
|
|
|
2012-06-21 16:33:00 +00:00
|
|
|
|
/** Прочитать файлы с засечками, если они ещё не прочитаны.
|
|
|
|
|
* Делается лениво, чтобы при большом количестве таблиц, сервер быстро стартовал.
|
|
|
|
|
*/
|
|
|
|
|
bool loaded_marks;
|
|
|
|
|
void loadMarks();
|
2010-03-18 19:32:14 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|