ClickHouse/dbms/src/Storages/StorageMemory.cpp

116 lines
2.3 KiB
C++
Raw Normal View History

2011-10-31 17:55:06 +00:00
#include <map>
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
#include <DB/Storages/StorageMemory.h>
namespace DB
{
using Poco::SharedPtr;
MemoryBlockInputStream::MemoryBlockInputStream(const Names & column_names_, BlocksList::iterator begin_, BlocksList::iterator end_, StoragePtr owned_storage)
: IProfilingBlockInputStream(owned_storage), column_names(column_names_), begin(begin_), end(end_), it(begin)
2011-10-31 17:55:06 +00:00
{
}
Block MemoryBlockInputStream::readImpl()
{
2012-05-30 04:45:49 +00:00
if (it == end)
{
2011-10-31 17:55:06 +00:00
return Block();
}
2011-10-31 17:55:06 +00:00
else
{
Block src = *it;
Block res;
/// Добавляем только нужные столбцы в res.
for (size_t i = 0, size = column_names.size(); i < size; ++i)
res.insert(src.getByName(column_names[i]));
++it;
return res;
}
2011-10-31 17:55:06 +00:00
}
MemoryBlockOutputStream::MemoryBlockOutputStream(StoragePtr owned_storage)
2013-02-20 23:25:28 +00:00
: IBlockOutputStream(owned_storage), storage(dynamic_cast<StorageMemory &>(*owned_storage))
2011-10-31 17:55:06 +00:00
{
}
void MemoryBlockOutputStream::write(const Block & block)
{
2013-02-25 10:23:31 +00:00
storage.check(block, true);
Poco::ScopedLock<Poco::FastMutex> lock(storage.mutex);
2011-10-31 17:55:06 +00:00
storage.data.push_back(block);
}
2011-11-01 17:12:11 +00:00
StorageMemory::StorageMemory(const std::string & name_, NamesAndTypesListPtr columns_)
2011-10-31 17:55:06 +00:00
: name(name_), columns(columns_)
{
}
StoragePtr StorageMemory::create(const std::string & name_, NamesAndTypesListPtr columns_)
{
return (new StorageMemory(name_, columns_))->thisPtr();
}
2011-10-31 17:55:06 +00:00
2012-01-09 19:20:48 +00:00
BlockInputStreams StorageMemory::read(
2011-10-31 17:55:06 +00:00
const Names & column_names,
ASTPtr query,
const Settings & settings,
2012-05-22 18:32:45 +00:00
QueryProcessingStage::Enum & processed_stage,
2012-01-09 19:20:48 +00:00
size_t max_block_size,
2012-05-30 04:45:49 +00:00
unsigned threads)
2011-10-31 17:55:06 +00:00
{
check(column_names);
2012-05-22 18:32:45 +00:00
processed_stage = QueryProcessingStage::FetchColumns;
2012-05-30 04:45:49 +00:00
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
size_t size = data.size();
if (threads > size)
threads = size;
2012-05-30 04:45:49 +00:00
BlockInputStreams res;
for (size_t thread = 0; thread < threads; ++thread)
{
BlocksList::iterator begin = data.begin();
BlocksList::iterator end = data.begin();
std::advance(begin, thread * size / threads);
std::advance(end, (thread + 1) * size / threads);
res.push_back(new MemoryBlockInputStream(column_names, begin, end, thisPtr()));
}
2012-05-30 04:45:49 +00:00
return res;
2011-10-31 17:55:06 +00:00
}
BlockOutputStreamPtr StorageMemory::write(
ASTPtr query)
{
return new MemoryBlockOutputStream(thisPtr());
2011-10-31 17:55:06 +00:00
}
2011-11-05 23:31:19 +00:00
void StorageMemory::dropImpl()
2011-11-05 23:31:19 +00:00
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
2011-11-05 23:31:19 +00:00
data.clear();
}
2011-10-31 17:55:06 +00:00
}