ClickHouse/dbms/include/DB/Storages/StorageMemory.h

100 lines
2.6 KiB
C
Raw Normal View History

2011-10-31 17:55:06 +00:00
#pragma once
#include <Poco/Mutex.h>
2011-10-31 17:55:06 +00:00
#include <DB/Core/NamesAndTypes.h>
#include <DB/Storages/IStorage.h>
#include <DB/DataStreams/IProfilingBlockInputStream.h>
#include <DB/DataStreams/IBlockOutputStream.h>
2011-10-31 17:55:06 +00:00
namespace DB
{
class StorageMemory;
class MemoryBlockInputStream : public IProfilingBlockInputStream
{
public:
MemoryBlockInputStream(const Names & column_names_, BlocksList::iterator begin_, BlocksList::iterator end_);
2011-10-31 17:55:06 +00:00
String getName() const { return "MemoryBlockInputStream"; }
String getID() const
{
std::stringstream res;
res << "Memory(" << &*begin << ", " << &*end;
for (const auto & name : column_names)
res << ", " << name;
res << ")";
return res.str();
}
2012-10-20 02:10:47 +00:00
protected:
Block readImpl();
2011-10-31 17:55:06 +00:00
private:
Names column_names;
BlocksList::iterator begin;
BlocksList::iterator end;
BlocksList::iterator it;
2011-10-31 17:55:06 +00:00
};
class MemoryBlockOutputStream : public IBlockOutputStream
{
public:
MemoryBlockOutputStream(StorageMemory & storage_);
2011-10-31 17:55:06 +00:00
void write(const Block & block);
private:
StorageMemory & storage;
};
/** Реализует хранилище в оперативке.
* Подходит для временных данных.
* В нём не поддерживаются ключи.
* Данные хранятся в виде набора блоков и никуда дополнительно не сохраняются.
*/
class StorageMemory : public IStorage
{
friend class MemoryBlockInputStream;
friend class MemoryBlockOutputStream;
public:
static StoragePtr create(const std::string & name_, NamesAndTypesListPtr columns_);
2011-10-31 17:55:06 +00:00
std::string getName() const override { return "Memory"; }
std::string getTableName() const override { return name; }
2011-10-31 17:55:06 +00:00
const NamesAndTypesList & getColumnsList() const override { return *columns; }
2011-10-31 17:55:06 +00:00
size_t getSize() const { return data.size(); }
2012-01-09 19:20:48 +00:00
BlockInputStreams 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 = DEFAULT_BLOCK_SIZE,
unsigned threads = 1) override;
2011-10-31 17:55:06 +00:00
BlockOutputStreamPtr write(ASTPtr query) override;
2011-10-31 17:55:06 +00:00
2014-03-20 13:28:49 +00:00
void drop() override;
void rename(const String & new_path_to_db, const String & new_database_name, const String & new_table_name) override { name = new_table_name; }
2011-11-05 23:31:19 +00:00
2011-10-31 17:55:06 +00:00
private:
2012-06-18 06:19:13 +00:00
String name;
2011-11-01 17:12:11 +00:00
NamesAndTypesListPtr columns;
2011-10-31 17:55:06 +00:00
/// Сами данные. list - чтобы при вставке в конец, существующие итераторы не инвалидировались.
BlocksList data;
Poco::FastMutex mutex;
StorageMemory(const std::string & name_, NamesAndTypesListPtr columns_);
2011-10-31 17:55:06 +00:00
};
}