ClickHouse/dbms/include/DB/Storages/StorageLog.h
2011-08-09 15:57:33 +00:00

112 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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>
#include <DB/IO/ReadBufferFromIStream.h>
#include <DB/IO/WriteBufferFromOStream.h>
#include <DB/IO/CompressedReadBuffer.h>
#include <DB/IO/CompressedWriteBuffer.h>
#include <DB/Storages/IStorage.h>
namespace DB
{
using Poco::SharedPtr;
class StorageLog;
class LogBlockInputStream : public IBlockInputStream
{
public:
LogBlockInputStream(size_t block_size_, const Names & column_names_, StorageLog & storage_);
Block read();
private:
size_t block_size;
const Names & column_names;
StorageLog & storage;
struct Stream
{
Stream(const std::string & path)
: istr(path, std::ios::in | std::ios::binary), plain(istr), compressed(plain) {}
Poco::FileInputStream istr;
ReadBufferFromIStream plain;
CompressedReadBuffer compressed;
};
typedef std::map<std::string, SharedPtr<Stream> > FileStreams;
FileStreams streams;
};
class LogBlockOutputStream : public IBlockOutputStream
{
public:
LogBlockOutputStream(StorageLog & storage_);
void write(const Block & block);
private:
StorageLog & storage;
struct Stream
{
Stream(const std::string & path)
: ostr(path, std::ios::out | std::ios::ate | std::ios::binary), plain(ostr), compressed(plain) {}
Poco::FileOutputStream ostr;
WriteBufferFromOStream plain;
CompressedWriteBuffer compressed;
};
typedef std::map<std::string, SharedPtr<Stream> > FileStreams;
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"; }
SharedPtr<IBlockInputStream> read(
const Names & column_names,
const ptree & query,
size_t max_block_size = DEFAULT_BLOCK_SIZE);
SharedPtr<IBlockOutputStream> write(
const ptree & query);
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