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

115 lines
2.9 KiB
C
Raw Normal View History

2010-03-18 19:32:14 +00:00
#ifndef DBMS_STORAGES_STORAGE_LOG_H
#define DBMS_STORAGES_STORAGE_LOG_H
#include <map>
#include <Poco/SharedPtr.h>
#include <Poco/File.h>
#include <Poco/FileStream.h>
#include <DB/Core/NamesAndTypes.h>
2010-06-04 18:25:25 +00:00
#include <DB/IO/ReadBufferFromIStream.h>
#include <DB/IO/WriteBufferFromOStream.h>
#include <DB/IO/CompressedReadBuffer.h>
#include <DB/IO/CompressedWriteBuffer.h>
2010-03-18 19:32:14 +00:00
#include <DB/Storages/IStorage.h>
namespace DB
{
using Poco::SharedPtr;
class StorageLog;
class LogBlockInputStream : public IBlockInputStream
{
public:
2011-08-09 15:57:33 +00:00
LogBlockInputStream(size_t block_size_, const Names & column_names_, StorageLog & storage_);
2010-03-18 19:32:14 +00:00
Block read();
private:
size_t block_size;
2011-08-09 15:57:33 +00:00
const 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)
2010-06-04 18:25:25 +00:00
: istr(path, std::ios::in | std::ios::binary), plain(istr), compressed(plain) {}
2010-03-18 20:52:28 +00:00
2010-06-04 18:25:25 +00:00
Poco::FileInputStream istr;
ReadBufferFromIStream plain;
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);
private:
StorageLog & storage;
2010-03-18 20:52:28 +00:00
struct Stream
{
Stream(const std::string & path)
2010-06-04 18:25:25 +00:00
: ostr(path, std::ios::out | std::ios::ate | std::ios::binary), plain(ostr), compressed(plain) {}
2010-03-18 20:52:28 +00:00
2010-06-04 18:25:25 +00:00
Poco::FileOutputStream ostr;
WriteBufferFromOStream plain;
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:
/** Подцепить таблицу с соответствующим именем, по соответствующему пути (с / на конце),
* (корректность имён и путей не проверяется)
* состоящую из указанных столбцов; создать файлы, если их нет.
*/
StorageLog(const std::string & path_, const std::string & name_, SharedPtr<NamesAndTypes> columns_,
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; }
const NamesAndTypes & getColumns() 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
private:
const std::string path;
const std::string name;
SharedPtr<NamesAndTypes> columns;
const std::string extension;
typedef std::map<std::string, Poco::File> Files_t;
Files_t files;
};
}
#endif