ClickHouse/dbms/Storages/StorageMemory.cpp

172 lines
4.2 KiB
C++
Raw Normal View History

#include <Common/Exception.h>
2011-10-31 17:55:06 +00:00
#include <DataStreams/IBlockInputStream.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
2018-06-05 19:46:49 +00:00
#include <IO/WriteHelpers.h>
#include <Processors/Sources/SourceWithProgress.h>
#include <Processors/Pipe.h>
2018-06-05 19:46:49 +00:00
2011-10-31 17:55:06 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
class MemorySource : public SourceWithProgress
2011-10-31 17:55:06 +00:00
{
2015-01-18 08:25:56 +00:00
public:
MemorySource(Names column_names_, BlocksList::iterator begin_, BlocksList::iterator end_, const StorageMemory & storage)
2020-01-31 16:36:57 +00:00
: SourceWithProgress(storage.getSampleBlockForColumns(column_names_))
, column_names(std::move(column_names_)), begin(begin_), end(end_), it(begin) {}
2011-10-31 17:55:06 +00:00
String getName() const override { return "Memory"; }
2011-10-31 17:55:06 +00:00
2015-01-18 08:25:56 +00:00
protected:
Chunk generate() override
{
if (it == end)
{
return {};
}
else
{
Block src = *it;
Columns columns;
columns.reserve(column_names.size());
/// Add only required columns to `res`.
for (const auto & name : column_names)
columns.emplace_back(src.getByName(name).column);
++it;
return Chunk(std::move(columns), src.rows());
}
}
2015-01-18 08:25:56 +00:00
private:
Names column_names;
BlocksList::iterator begin;
BlocksList::iterator end;
BlocksList::iterator it;
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);
2019-01-02 06:44:36 +00:00
std::lock_guard 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
2019-12-04 16:06:55 +00:00
StorageMemory::StorageMemory(const StorageID & table_id_, ColumnsDescription columns_description_, ConstraintsDescription constraints_)
: IStorage(table_id_)
{
2019-08-24 21:20:20 +00:00
setColumns(std::move(columns_description_));
setConstraints(std::move(constraints_));
}
Pipes 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);
2012-05-30 04:45:49 +00:00
2019-01-02 06:44:36 +00:00
std::lock_guard 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
Pipes pipes;
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);
pipes.emplace_back(std::make_shared<MemorySource>(column_names, begin, end, *this));
}
return pipes;
2011-10-31 17:55:06 +00:00
}
2011-10-31 17:55:06 +00:00
BlockOutputStreamPtr StorageMemory::write(
const ASTPtr & /*query*/, const Context & /*context*/)
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
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);
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();
}
std::optional<UInt64> StorageMemory::totalRows() const
{
UInt64 rows = 0;
std::lock_guard lock(mutex);
for (auto & buffer : data)
rows += buffer.rows();
return rows;
}
std::optional<UInt64> StorageMemory::totalBytes() const
{
UInt64 bytes = 0;
std::lock_guard lock(mutex);
for (auto & buffer : data)
bytes += buffer.bytes();
return bytes;
}
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-12-04 16:06:55 +00:00
return StorageMemory::create(args.table_id, args.columns, args.constraints);
});
}
2011-10-31 17:55:06 +00:00
}