ClickHouse/dbms/src/Storages/StorageLog.cpp

104 lines
2.5 KiB
C++
Raw Normal View History

2010-03-18 19:32:14 +00:00
#include <map>
#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_)
{
}
Block LogBlockInputStream::read()
{
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 20:52:28 +00:00
streams.insert(std::make_pair(*it, new Stream(storage.files[*it].path())));
2010-03-18 19:32:14 +00:00
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;
column.type = (*storage.columns)[*it];
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_)
{
}
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 std::string & name = block.getByPosition(i).name;
2010-03-18 20:52:28 +00:00
streams.insert(std::make_pair(name, new Stream(storage.files[name].path())));
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
}
}
StorageLog::StorageLog(const std::string & path_, const std::string & name_, SharedPtr<NamesAndTypes> columns_,
const std::string & extension_)
: path(path_), name(name_), columns(columns_), extension(extension_)
{
/// создаём файлы, если их нет
Poco::File dir(path + name + '/');
dir.createDirectories();
for (NamesAndTypes::const_iterator it = columns->begin(); it != columns->end(); ++it)
{
if (files.end() != files.find(it->first))
throw Exception("Duplicate column with name " + it->first + " in constructor of StorageLog.",
ErrorCodes::DUPLICATE_COLUMN);
files.insert(std::make_pair(it->first, Poco::File(path + name + '/' + it->first + extension)));
}
}
SharedPtr<IBlockInputStream> 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);
}
SharedPtr<IBlockOutputStream> StorageLog::write(
2011-08-15 01:12:57 +00:00
ASTPtr query)
2010-03-18 19:32:14 +00:00
{
return new LogBlockOutputStream(*this);
}
}