2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2011-10-31 17:55:06 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/IBlockInputStream.h>
|
2015-01-18 08:25:56 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageMemory.h>
|
2017-12-30 00:36:06 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
2011-10-31 17:55:06 +00:00
|
|
|
|
2018-06-05 19:46:49 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2011-10-31 17:55:06 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-30 00:36:06 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
class MemoryBlockInputStream : public IBlockInputStream
|
2011-10-31 17:55:06 +00:00
|
|
|
{
|
2015-01-18 08:25:56 +00:00
|
|
|
public:
|
2018-01-06 18:10:44 +00:00
|
|
|
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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "Memory"; }
|
2011-10-31 17:55:06 +00:00
|
|
|
|
2018-02-18 03:23:48 +00:00
|
|
|
Block getHeader() const override { return storage.getSampleBlockForColumns(column_names); }
|
2018-01-06 18:10:44 +00:00
|
|
|
|
2015-01-18 08:25:56 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
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:
|
2017-04-01 07:20:54 +00:00
|
|
|
Names column_names;
|
|
|
|
BlocksList::iterator begin;
|
|
|
|
BlocksList::iterator end;
|
|
|
|
BlocksList::iterator it;
|
2018-01-06 18:10:44 +00:00
|
|
|
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:
|
2017-09-07 21:04:48 +00:00
|
|
|
explicit MemoryBlockOutputStream(StorageMemory & storage_) : storage(storage_) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-19 00:45:32 +00:00
|
|
|
Block getHeader() const override { return storage.getSampleBlock(); }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void write(const Block & block) override
|
|
|
|
{
|
|
|
|
storage.check(block, true);
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(storage.mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
storage.data.push_back(block);
|
|
|
|
}
|
2015-01-18 08:25:56 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
StorageMemory & storage;
|
2015-01-18 08:25:56 +00:00
|
|
|
};
|
2011-10-31 17:55:06 +00:00
|
|
|
|
|
|
|
|
2019-08-24 21:20:20 +00:00
|
|
|
StorageMemory::StorageMemory(String database_name_, String table_name_, ColumnsDescription columns_description_, ConstraintsDescription constraints_)
|
|
|
|
: database_name(std::move(database_name_)), table_name(std::move(table_name_))
|
2014-09-30 03:08:47 +00:00
|
|
|
{
|
2019-08-24 21:20:20 +00:00
|
|
|
setColumns(std::move(columns_description_));
|
|
|
|
setConstraints(std::move(constraints_));
|
2014-09-30 03:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-09 19:20:48 +00:00
|
|
|
BlockInputStreams StorageMemory::read(
|
2017-04-01 07:20:54 +00:00
|
|
|
const Names & column_names,
|
2017-12-01 21:13:25 +00:00
|
|
|
const SelectQueryInfo & /*query_info*/,
|
2018-09-08 11:29:23 +00:00
|
|
|
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
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
check(column_names);
|
2012-05-30 04:45:49 +00:00
|
|
|
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2012-11-30 04:28:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = data.size();
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-06-02 15:54:39 +00:00
|
|
|
if (num_streams > size)
|
|
|
|
num_streams = size;
|
2012-05-30 04:45:49 +00:00
|
|
|
|
2017-04-01 07:20:54 +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)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
BlocksList::iterator begin = data.begin();
|
|
|
|
BlocksList::iterator end = data.begin();
|
2012-11-30 04:28:13 +00:00
|
|
|
|
2017-06-02 15:54:39 +00:00
|
|
|
std::advance(begin, stream * size / num_streams);
|
|
|
|
std::advance(end, (stream + 1) * size / num_streams);
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2018-01-06 18:10:44 +00:00
|
|
|
res.push_back(std::make_shared<MemoryBlockInputStream>(column_names, begin, end, *this));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2011-10-31 17:55:06 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2011-10-31 17:55:06 +00:00
|
|
|
BlockOutputStreamPtr StorageMemory::write(
|
2019-02-27 18:26:24 +00:00
|
|
|
const ASTPtr & /*query*/, const Context & /*context*/)
|
2011-10-31 17:55:06 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<MemoryBlockOutputStream>(*this);
|
2011-10-31 17:55:06 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
|
2019-08-27 20:43:08 +00:00
|
|
|
void StorageMemory::drop(TableStructureWriteLockHolder &)
|
2011-11-05 23:31:19 +00:00
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
data.clear();
|
2011-11-05 23:31:19 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 20:43:08 +00:00
|
|
|
void StorageMemory::truncate(const ASTPtr &, const Context &, TableStructureWriteLockHolder &)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-04-21 00:35:20 +00:00
|
|
|
data.clear();
|
|
|
|
}
|
|
|
|
|
2017-12-30 00:36:06 +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);
|
|
|
|
|
2019-08-24 21:20:20 +00:00
|
|
|
return StorageMemory::create(args.database_name, args.table_name, args.columns, args.constraints);
|
2017-12-30 00:36:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:55:06 +00:00
|
|
|
}
|