ClickHouse/dbms/src/Storages/StorageMemory.cpp

152 lines
3.8 KiB
C++
Raw Normal View History

2011-10-31 17:55:06 +00:00
#include <map>
#include <Common/Exception.h>
2011-10-31 17:55:06 +00:00
#include <DataStreams/IProfilingBlockInputStream.h>
2015-01-18 08:25:56 +00:00
#include <Storages/StorageMemory.h>
#include <Storages/StorageFactory.h>
2011-10-31 17:55:06 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
2015-01-18 08:25:56 +00:00
class MemoryBlockInputStream : public IProfilingBlockInputStream
2011-10-31 17:55:06 +00:00
{
2015-01-18 08:25:56 +00:00
public:
MemoryBlockInputStream(const Names & column_names_, BlocksList::iterator begin_, BlocksList::iterator end_, const StorageMemory & storage_)
: column_names(column_names_), begin(begin_), end(end_), it(begin), storage(storage_) {}
2011-10-31 17:55:06 +00:00
String getName() const override { return "Memory"; }
2011-10-31 17:55:06 +00:00
Block getHeader() const override { return storage.getSampleBlockForColumns(column_names); }
2015-01-18 08:25:56 +00:00
protected:
Block readImpl() override
{
if (it == end)
{
return Block();
}
else
{
Block src = *it;
Block res;
/// Add only required columns to `res`.
for (size_t i = 0, size = column_names.size(); i < size; ++i)
res.insert(src.getByName(column_names[i]));
++it;
return res;
}
}
2015-01-18 08:25:56 +00:00
private:
Names column_names;
BlocksList::iterator begin;
BlocksList::iterator end;
BlocksList::iterator it;
const StorageMemory & storage;
2015-01-18 08:25:56 +00:00
};
2011-10-31 17:55:06 +00:00
2015-01-18 08:25:56 +00:00
class MemoryBlockOutputStream : public IBlockOutputStream
2011-10-31 17:55:06 +00:00
{
2015-01-18 08:25:56 +00:00
public:
explicit MemoryBlockOutputStream(StorageMemory & storage_) : storage(storage_) {}
Block getHeader() const override { return storage.getSampleBlock(); }
void write(const Block & block) override
{
storage.check(block, true);
std::lock_guard<std::mutex> lock(storage.mutex);
storage.data.push_back(block);
}
2015-01-18 08:25:56 +00:00
private:
StorageMemory & storage;
2015-01-18 08:25:56 +00:00
};
2011-10-31 17:55:06 +00:00
StorageMemory::StorageMemory(
const std::string & name_,
const NamesAndTypesList & columns_,
const NamesAndTypesList & materialized_columns_,
const NamesAndTypesList & alias_columns_,
const ColumnDefaults & column_defaults_)
: IStorage{columns_, materialized_columns_, alias_columns_, column_defaults_},
name(name_)
{
}
2012-01-09 19:20:48 +00:00
BlockInputStreams StorageMemory::read(
const Names & column_names,
2017-12-01 21:13:25 +00:00
const SelectQueryInfo & /*query_info*/,
const Context & /*context*/,
QueryProcessingStage::Enum & processed_stage,
2017-12-01 21:13:25 +00:00
size_t /*max_block_size*/,
2017-06-02 15:54:39 +00:00
unsigned num_streams)
2011-10-31 17:55:06 +00:00
{
check(column_names);
processed_stage = QueryProcessingStage::FetchColumns;
2012-05-30 04:45:49 +00:00
std::lock_guard<std::mutex> lock(mutex);
size_t size = data.size();
2017-06-02 15:54:39 +00:00
if (num_streams > size)
num_streams = size;
2012-05-30 04:45:49 +00:00
BlockInputStreams res;
2012-05-30 04:45:49 +00:00
2017-06-02 15:54:39 +00:00
for (size_t stream = 0; stream < num_streams; ++stream)
{
BlocksList::iterator begin = data.begin();
BlocksList::iterator end = data.begin();
2017-06-02 15:54:39 +00:00
std::advance(begin, stream * size / num_streams);
std::advance(end, (stream + 1) * size / num_streams);
res.push_back(std::make_shared<MemoryBlockInputStream>(column_names, begin, end, *this));
}
return res;
2011-10-31 17:55:06 +00:00
}
2011-10-31 17:55:06 +00:00
BlockOutputStreamPtr StorageMemory::write(
2017-12-01 21:13:25 +00:00
const ASTPtr & /*query*/, const Settings & /*settings*/)
2011-10-31 17:55:06 +00:00
{
return std::make_shared<MemoryBlockOutputStream>(*this);
2011-10-31 17:55:06 +00:00
}
2011-11-05 23:31:19 +00:00
2014-03-20 13:28:49 +00:00
void StorageMemory::drop()
2011-11-05 23:31:19 +00:00
{
std::lock_guard<std::mutex> lock(mutex);
data.clear();
2011-11-05 23:31:19 +00:00
}
void registerStorageMemory(StorageFactory & factory)
{
factory.registerStorage("Memory", [](const StorageFactory::Arguments & args)
{
if (!args.engine_args.empty())
throw Exception(
"Engine " + args.engine_name + " doesn't support any arguments (" + toString(args.engine_args.size()) + " given)",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
return StorageMemory::create(args.table_name, args.columns, args.materialized_columns, args.alias_columns, args.column_defaults);
});
}
2011-10-31 17:55:06 +00:00
}