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;
|
|
|
|
|
|
2011-09-04 21:23:19 +00:00
|
|
|
|
class LogBlockInputStream : public IProfilingBlockInputStream
|
2010-03-18 19:32:14 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2011-08-09 15:57:33 +00:00
|
|
|
|
LogBlockInputStream(size_t block_size_, const Names & column_names_, StorageLog & storage_);
|
2011-09-04 21:23:19 +00:00
|
|
|
|
Block readImpl();
|
|
|
|
|
String getName() const { return "LogBlockInputStream"; }
|
2011-10-24 12:10:59 +00:00
|
|
|
|
BlockInputStreamPtr clone() { return new LogBlockInputStream(block_size, column_names, storage); }
|
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;
|
|
|
|
|
|
2010-03-18 20:52:28 +00:00
|
|
|
|
struct Stream
|
|
|
|
|
{
|
|
|
|
|
Stream(const std::string & path)
|
2011-10-24 12:10:59 +00:00
|
|
|
|
: plain(path), compressed(plain) {}
|
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 & path)
|
2012-01-05 18:35:22 +00:00
|
|
|
|
: plain(path), compressed(plain, CompressionMethod::LZ4) {}
|
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;
|
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 StorageLog : public IStorage
|
|
|
|
|
{
|
|
|
|
|
friend class LogBlockInputStream;
|
|
|
|
|
friend class LogBlockOutputStream;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/** Подцепить таблицу с соответствующим именем, по соответствующему пути (с / на конце),
|
|
|
|
|
* (корректность имён и путей не проверяется)
|
|
|
|
|
* состоящую из указанных столбцов; создать файлы, если их нет.
|
|
|
|
|
*/
|
2011-11-01 17:12:11 +00:00
|
|
|
|
StorageLog(const std::string & path_, const std::string & name_, NamesAndTypesListPtr columns_,
|
2010-03-18 19:32:14 +00:00
|
|
|
|
const std::string & extension_ = ".bin");
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-08-28 02:22:23 +00:00
|
|
|
|
BlockInputStreamPtr read(
|
2011-08-09 15:57:33 +00:00
|
|
|
|
const Names & column_names,
|
2011-08-15 01:12:57 +00:00
|
|
|
|
ASTPtr query,
|
2010-03-18 19:32:14 +00:00
|
|
|
|
size_t max_block_size = DEFAULT_BLOCK_SIZE);
|
|
|
|
|
|
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();
|
|
|
|
|
|
2010-03-18 19:32:14 +00:00
|
|
|
|
private:
|
|
|
|
|
const std::string path;
|
|
|
|
|
const std::string name;
|
2011-11-01 17:12:11 +00:00
|
|
|
|
NamesAndTypesListPtr columns;
|
2010-03-18 19:32:14 +00:00
|
|
|
|
const std::string extension;
|
|
|
|
|
|
|
|
|
|
typedef std::map<std::string, Poco::File> Files_t;
|
|
|
|
|
Files_t files;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|