ClickHouse/dbms/src/Storages/StorageMemory.cpp

98 lines
1.8 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_)
2012-05-30 04:45:49 +00:00
: 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();
else
return *it++;
}
MemoryBlockOutputStream::MemoryBlockOutputStream(StorageMemory & storage_)
: storage(storage_)
{
}
void MemoryBlockOutputStream::write(const Block & block)
{
storage.check(block);
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_)
{
}
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,
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));
}
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(*this);
}
2011-11-05 23:31:19 +00:00
void StorageMemory::drop()
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
2011-11-05 23:31:19 +00:00
data.clear();
}
2011-10-31 17:55:06 +00:00
}