ClickHouse/dbms/src/Storages/StorageLog.cpp

107 lines
2.7 KiB
C++
Raw Normal View History

2010-03-18 19:32:14 +00:00
#include <map>
2011-11-05 23:31:19 +00:00
#include <DB/Common/escapeForFileName.h>
2010-03-18 19:32:14 +00:00
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
#include <DB/Storages/StorageLog.h>
namespace DB
{
using Poco::SharedPtr;
2011-08-09 15:57:33 +00:00
LogBlockInputStream::LogBlockInputStream(size_t block_size_, const Names & column_names_, StorageLog & storage_)
2010-03-18 19:32:14 +00:00
: block_size(block_size_), column_names(column_names_), storage(storage_)
{
2011-10-31 06:37:12 +00:00
for (Names::const_iterator it = column_names.begin(); it != column_names.end(); ++it)
streams.insert(std::make_pair(*it, new Stream(storage.files[*it].path())));
2010-03-18 19:32:14 +00:00
}
2011-09-04 21:23:19 +00:00
Block LogBlockInputStream::readImpl()
2010-03-18 19:32:14 +00:00
{
Block res;
2011-08-09 15:57:33 +00:00
for (Names::const_iterator it = column_names.begin(); it != column_names.end(); ++it)
2010-03-18 19:32:14 +00:00
{
ColumnWithNameAndType column;
column.name = *it;
2011-11-01 17:12:11 +00:00
column.type = storage.getDataTypeByName(*it);
2010-03-18 19:32:14 +00:00
column.column = column.type->createColumn();
2010-03-18 20:52:28 +00:00
column.type->deserializeBinary(*column.column, streams[column.name]->compressed, block_size);
2010-03-18 19:32:14 +00:00
2010-05-24 18:58:14 +00:00
if (column.column->size())
res.insert(column);
2010-03-18 19:32:14 +00:00
}
return res;
}
LogBlockOutputStream::LogBlockOutputStream(StorageLog & storage_)
: storage(storage_)
{
2011-11-01 17:12:11 +00:00
for (NamesAndTypesList::const_iterator it = storage.columns->begin(); it != storage.columns->end(); ++it)
2011-10-31 06:37:12 +00:00
streams.insert(std::make_pair(it->first, new Stream(storage.files[it->first].path())));
2010-03-18 19:32:14 +00:00
}
void LogBlockOutputStream::write(const Block & block)
{
2011-08-15 02:24:44 +00:00
storage.check(block);
2010-03-18 19:32:14 +00:00
for (size_t i = 0; i < block.columns(); ++i)
{
const ColumnWithNameAndType & column = block.getByPosition(i);
2010-03-18 20:52:28 +00:00
column.type->serializeBinary(*column.column, streams[column.name]->compressed);
2010-03-18 19:32:14 +00:00
}
}
2011-11-01 17:12:11 +00:00
StorageLog::StorageLog(const std::string & path_, const std::string & name_, NamesAndTypesListPtr columns_,
2010-03-18 19:32:14 +00:00
const std::string & extension_)
: path(path_), name(name_), columns(columns_), extension(extension_)
{
/// создаём файлы, если их нет
2011-11-05 23:31:19 +00:00
Poco::File(path + escapeForFileName(name) + '/').createDirectories();
2010-03-18 19:32:14 +00:00
2011-11-01 17:12:11 +00:00
for (NamesAndTypesList::const_iterator it = columns->begin(); it != columns->end(); ++it)
2010-03-18 19:32:14 +00:00
{
if (files.end() != files.find(it->first))
throw Exception("Duplicate column with name " + it->first + " in constructor of StorageLog.",
ErrorCodes::DUPLICATE_COLUMN);
2011-11-05 23:31:19 +00:00
files.insert(std::make_pair(it->first, Poco::File(path + escapeForFileName(name) + '/' + escapeForFileName(it->first) + extension)));
2010-03-18 19:32:14 +00:00
}
}
2011-08-28 02:22:23 +00:00
BlockInputStreamPtr StorageLog::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)
{
2011-08-15 02:24:44 +00:00
check(column_names);
2010-03-18 19:32:14 +00:00
return new LogBlockInputStream(max_block_size, column_names, *this);
}
2011-08-28 02:22:23 +00:00
BlockOutputStreamPtr StorageLog::write(
2011-08-15 01:12:57 +00:00
ASTPtr query)
2010-03-18 19:32:14 +00:00
{
return new LogBlockOutputStream(*this);
}
2011-11-05 23:31:19 +00:00
void StorageLog::drop()
{
for (Files_t::iterator it = files.begin(); it != files.end(); ++it)
it->second.remove();
}
2010-03-18 19:32:14 +00:00
}