diff --git a/dbms/include/DB/Storages/IStorage.h b/dbms/include/DB/Storages/IStorage.h index cbee61c7900..29e022289c8 100644 --- a/dbms/include/DB/Storages/IStorage.h +++ b/dbms/include/DB/Storages/IStorage.h @@ -47,7 +47,7 @@ using StoragePtr = std::shared_ptr; * - структура хранения данных (сжатие, etc.) * - конкуррентный доступ к данным (блокировки, etc.) */ -class IStorage : private boost::noncopyable, public ITableDeclaration +class IStorage : public std::enable_shared_from_this, private boost::noncopyable, public ITableDeclaration { public: /// Основное имя типа таблицы (например, StorageMergeTree). @@ -106,7 +106,7 @@ public: */ TableStructureReadLockPtr lockStructure(bool will_modify_data) { - TableStructureReadLockPtr res = std::make_shared(thisPtr(), true, will_modify_data); + TableStructureReadLockPtr res = std::make_shared(shared_from_this(), true, will_modify_data); if (is_dropped) throw Exception("Table is dropped", ErrorCodes::TABLE_IS_DROPPED); return res; @@ -270,19 +270,6 @@ public: */ virtual void shutdown() {} - /** Возвращает владеющий указатель на себя. - */ - std::shared_ptr thisPtr() - { - std::shared_ptr res = this_ptr.lock(); - if (!res) - { - res.reset(this); - this_ptr = res; - } - return res; - } - bool is_dropped{false}; /// Поддерживается ли индекс в секции IN @@ -293,10 +280,9 @@ public: protected: using ITableDeclaration::ITableDeclaration; + using std::enable_shared_from_this::shared_from_this; private: - std::weak_ptr this_ptr; - /// Брать следующие два лока всегда нужно в этом порядке. /** Берется на чтение на все время запроса INSERT и на все время слияния кусков (для MergeTree). diff --git a/dbms/include/DB/Storages/StorageBuffer.h b/dbms/include/DB/Storages/StorageBuffer.h index c6bdbca7581..03b07cd862a 100644 --- a/dbms/include/DB/Storages/StorageBuffer.h +++ b/dbms/include/DB/Storages/StorageBuffer.h @@ -3,6 +3,8 @@ #include #include +#include + #include #include #include @@ -31,8 +33,9 @@ namespace DB * При уничтожении таблицы типа Buffer и при завершении работы, все данные сбрасываются. * Данные в буфере не реплицируются, не логгируются на диск, не индексируются. При грубом перезапуске сервера, данные пропадают. */ -class StorageBuffer : public IStorage +class StorageBuffer : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; friend class BufferBlockInputStream; friend class BufferBlockOutputStream; diff --git a/dbms/include/DB/Storages/StorageCloud.h b/dbms/include/DB/Storages/StorageCloud.h index 4eff063bf48..f81d2524794 100644 --- a/dbms/include/DB/Storages/StorageCloud.h +++ b/dbms/include/DB/Storages/StorageCloud.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -13,8 +15,10 @@ class DatabaseCloud; /** Облачная таблица. Может находиться только в облачной базе данных. * При записи в таблицу, данные записываются в локальные таблицы на нескольких серверах облака. */ -class StorageCloud : public IStorage +class StorageCloud : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create( DatabasePtr & database_ptr_, diff --git a/dbms/include/DB/Storages/StorageDistributed.h b/dbms/include/DB/Storages/StorageDistributed.h index 47f1dc409e3..e8870028152 100644 --- a/dbms/include/DB/Storages/StorageDistributed.h +++ b/dbms/include/DB/Storages/StorageDistributed.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -18,8 +20,9 @@ namespace DB * Можно передать один адрес, а не несколько. * В этом случае, таблицу можно считать удалённой, а не распределённой. */ -class StorageDistributed : public IStorage +class StorageDistributed : private ext::share_ptr_helper, public IStorage { + friend class ext::share_ptr_helper; friend class DistributedBlockOutputStream; friend class DirectoryMonitor; diff --git a/dbms/include/DB/Storages/StorageJoin.h b/dbms/include/DB/Storages/StorageJoin.h index e62b4a9325e..ba2c22f181f 100644 --- a/dbms/include/DB/Storages/StorageJoin.h +++ b/dbms/include/DB/Storages/StorageJoin.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -14,8 +16,10 @@ namespace DB * * При использовании, JOIN должен быть соответствующего типа (ANY|ALL LEFT|INNER ...). */ -class StorageJoin : public StorageSetOrJoinBase +class StorageJoin : private ext::share_ptr_helper, public StorageSetOrJoinBase { +friend class ext::share_ptr_helper; + public: static StoragePtr create( const String & path_, @@ -27,11 +31,10 @@ public: const NamesAndTypesList & alias_columns_, const ColumnDefaults & column_defaults_) { - return (new StorageJoin{ - path_, name_, - key_names_, kind_, strictness_, - columns_, - materialized_columns_, alias_columns_, column_defaults_})->thisPtr(); + return ext::share_ptr_helper::make_shared( + path_, name_, key_names_, kind_, strictness_, + columns_, materialized_columns_, alias_columns_, column_defaults_ + ); } String getName() const override { return "Join"; } diff --git a/dbms/include/DB/Storages/StorageLog.h b/dbms/include/DB/Storages/StorageLog.h index 4c1e9d91cd5..4082f69175c 100644 --- a/dbms/include/DB/Storages/StorageLog.h +++ b/dbms/include/DB/Storages/StorageLog.h @@ -2,6 +2,8 @@ #include +#include + #include #include @@ -35,8 +37,9 @@ using Marks = std::vector; * Ключи не поддерживаются. * Данные хранятся в сжатом виде. */ -class StorageLog : public IStorage +class StorageLog : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; friend class LogBlockInputStream; friend class LogBlockOutputStream; diff --git a/dbms/include/DB/Storages/StorageMaterializedView.h b/dbms/include/DB/Storages/StorageMaterializedView.h index cc627598070..18b6ca22ca7 100644 --- a/dbms/include/DB/Storages/StorageMaterializedView.h +++ b/dbms/include/DB/Storages/StorageMaterializedView.h @@ -1,12 +1,16 @@ #pragma once +#include + #include namespace DB { -class StorageMaterializedView : public StorageView { +class StorageMaterializedView : private ext::share_ptr_helper, public StorageView +{ +friend class ext::share_ptr_helper; public: static StoragePtr create( diff --git a/dbms/include/DB/Storages/StorageMemory.h b/dbms/include/DB/Storages/StorageMemory.h index ed065e3978d..183261d9a73 100644 --- a/dbms/include/DB/Storages/StorageMemory.h +++ b/dbms/include/DB/Storages/StorageMemory.h @@ -2,6 +2,8 @@ #include +#include + #include #include #include @@ -18,8 +20,9 @@ class StorageMemory; * В нём не поддерживаются ключи. * Данные хранятся в виде набора блоков и никуда дополнительно не сохраняются. */ -class StorageMemory : public IStorage +class StorageMemory : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; friend class MemoryBlockInputStream; friend class MemoryBlockOutputStream; diff --git a/dbms/include/DB/Storages/StorageMerge.h b/dbms/include/DB/Storages/StorageMerge.h index 32d1c61240d..05e10e64ca1 100644 --- a/dbms/include/DB/Storages/StorageMerge.h +++ b/dbms/include/DB/Storages/StorageMerge.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -10,8 +12,10 @@ namespace DB /** Таблица, представляющая собой объединение произвольного количества других таблиц. * У всех таблиц должна быть одинаковая структура. */ -class StorageMerge : public IStorage +class StorageMerge : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create( const std::string & name_, /// Имя таблицы. diff --git a/dbms/include/DB/Storages/StorageMergeTree.h b/dbms/include/DB/Storages/StorageMergeTree.h index 4d128f77841..fbe7f65881b 100644 --- a/dbms/include/DB/Storages/StorageMergeTree.h +++ b/dbms/include/DB/Storages/StorageMergeTree.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -14,8 +16,9 @@ namespace DB /** См. описание структуры данных в MergeTreeData. */ -class StorageMergeTree : public IStorage +class StorageMergeTree : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; friend class MergeTreeBlockOutputStream; public: diff --git a/dbms/include/DB/Storages/StorageNull.h b/dbms/include/DB/Storages/StorageNull.h index 3db738a7874..73d420851ec 100644 --- a/dbms/include/DB/Storages/StorageNull.h +++ b/dbms/include/DB/Storages/StorageNull.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -12,8 +14,10 @@ namespace DB /** При записи, ничего не делает. * При чтении, возвращает пустоту. */ -class StorageNull : public IStorage +class StorageNull : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create( const std::string & name_, @@ -22,7 +26,7 @@ public: const NamesAndTypesList & alias_columns_, const ColumnDefaults & column_defaults_) { - return (new StorageNull{name_, columns_, materialized_columns_, alias_columns_, column_defaults_})->thisPtr(); + return make_shared(name_, columns_, materialized_columns_, alias_columns_, column_defaults_); } std::string getName() const override { return "Null"; } diff --git a/dbms/include/DB/Storages/StorageReplicatedMergeTree.h b/dbms/include/DB/Storages/StorageReplicatedMergeTree.h index 8c57bb2a4a0..0739fcfc0d2 100644 --- a/dbms/include/DB/Storages/StorageReplicatedMergeTree.h +++ b/dbms/include/DB/Storages/StorageReplicatedMergeTree.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -67,8 +69,10 @@ namespace DB * в качестве времени будет браться время создания соответствующего куска на какой-либо из реплик. */ -class StorageReplicatedMergeTree : public IStorage +class StorageReplicatedMergeTree : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: /** Если !attach, либо создает новую таблицу в ZK, либо добавляет реплику в существующую таблицу. */ diff --git a/dbms/include/DB/Storages/StorageSet.h b/dbms/include/DB/Storages/StorageSet.h index 8ec3f9bb393..f196f6d0270 100644 --- a/dbms/include/DB/Storages/StorageSet.h +++ b/dbms/include/DB/Storages/StorageSet.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -14,8 +16,9 @@ namespace DB /** Общая часть StorageSet и StorageJoin. */ -class StorageSetOrJoinBase : public IStorage +class StorageSetOrJoinBase : private ext::share_ptr_helper, public IStorage { + friend class ext::share_ptr_helper; friend class SetOrJoinBlockOutputStream; public: @@ -78,8 +81,10 @@ private: * а также записаны в файл-бэкап, для восстановления после перезапуска. * Чтение из таблицы напрямую невозможно - возможно лишь указание в правой части оператора IN. */ -class StorageSet : public StorageSetOrJoinBase +class StorageSet : private ext::share_ptr_helper, public StorageSetOrJoinBase { +friend class ext::share_ptr_helper; + public: static StoragePtr create( const String & path_, @@ -89,9 +94,7 @@ public: const NamesAndTypesList & alias_columns_, const ColumnDefaults & column_defaults_) { - return (new StorageSet{ - path_, name_, columns_, - materialized_columns_, alias_columns_, column_defaults_})->thisPtr(); + return ext::share_ptr_helper::make_shared(path_, name_, columns_, materialized_columns_, alias_columns_, column_defaults_); } String getName() const override { return "Set"; } diff --git a/dbms/include/DB/Storages/StorageStripeLog.h b/dbms/include/DB/Storages/StorageStripeLog.h index 133671a4987..f5c27b268ba 100644 --- a/dbms/include/DB/Storages/StorageStripeLog.h +++ b/dbms/include/DB/Storages/StorageStripeLog.h @@ -1,6 +1,9 @@ #pragma once #include + +#include + #include #include @@ -14,8 +17,9 @@ namespace DB /** Реализует хранилище, подходящее для маленьких кусочков лога. * При этом, хранит все столбцы в одном файле формата Native, с расположенным рядом индексом. */ -class StorageStripeLog : public IStorage +class StorageStripeLog : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; friend class StripeLogBlockInputStream; friend class StripeLogBlockOutputStream; diff --git a/dbms/include/DB/Storages/StorageTinyLog.h b/dbms/include/DB/Storages/StorageTinyLog.h index b85a7e3a1fe..920fcf2b309 100644 --- a/dbms/include/DB/Storages/StorageTinyLog.h +++ b/dbms/include/DB/Storages/StorageTinyLog.h @@ -2,6 +2,8 @@ #include +#include + #include #include @@ -15,8 +17,9 @@ namespace DB /** Реализует хранилище, подходящее для маленьких кусочков лога. * Отличается от StorageLog отсутствием файлов с засечками. */ -class StorageTinyLog : public IStorage +class StorageTinyLog : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; friend class TinyLogBlockInputStream; friend class TinyLogBlockOutputStream; diff --git a/dbms/include/DB/Storages/StorageView.h b/dbms/include/DB/Storages/StorageView.h index 44d79835e05..3a6d14e2b15 100644 --- a/dbms/include/DB/Storages/StorageView.h +++ b/dbms/include/DB/Storages/StorageView.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -7,8 +9,9 @@ namespace DB { -class StorageView : public IStorage +class StorageView : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; public: static StoragePtr create( diff --git a/dbms/include/DB/Storages/System/StorageSystemClusters.h b/dbms/include/DB/Storages/System/StorageSystemClusters.h index e293425ebda..452ecbd8983 100644 --- a/dbms/include/DB/Storages/System/StorageSystemClusters.h +++ b/dbms/include/DB/Storages/System/StorageSystemClusters.h @@ -1,5 +1,7 @@ #pragma once +#include + #include namespace DB @@ -11,8 +13,10 @@ class Context; * that allows to obtain information about available clusters * (which may be specified in Distributed tables). */ -class StorageSystemClusters : public IStorage +class StorageSystemClusters : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: StorageSystemClusters(const std::string & name_, Context & context_); static StoragePtr create(const std::string & name_, Context & context_); diff --git a/dbms/include/DB/Storages/System/StorageSystemColumns.h b/dbms/include/DB/Storages/System/StorageSystemColumns.h index c9b03c15cd4..ad220c57043 100644 --- a/dbms/include/DB/Storages/System/StorageSystemColumns.h +++ b/dbms/include/DB/Storages/System/StorageSystemColumns.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,9 @@ namespace DB /** Реализует системную таблицу columns, которая позволяет получить информацию * о столбцах каждой таблицы для всех баз данных. */ -class StorageSystemColumns : public IStorage +class StorageSystemColumns : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemDatabases.h b/dbms/include/DB/Storages/System/StorageSystemDatabases.h index 36d2f72290c..da7516691f6 100644 --- a/dbms/include/DB/Storages/System/StorageSystemDatabases.h +++ b/dbms/include/DB/Storages/System/StorageSystemDatabases.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу databases, которая позволяет получить информацию о всех БД. */ -class StorageSystemDatabases : public IStorage +class StorageSystemDatabases : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemDictionaries.h b/dbms/include/DB/Storages/System/StorageSystemDictionaries.h index e65da014d9d..3549a57cb8f 100644 --- a/dbms/include/DB/Storages/System/StorageSystemDictionaries.h +++ b/dbms/include/DB/Storages/System/StorageSystemDictionaries.h @@ -1,12 +1,16 @@ #pragma once +#include + #include namespace DB { -class StorageSystemDictionaries : public IStorage +class StorageSystemDictionaries : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name); diff --git a/dbms/include/DB/Storages/System/StorageSystemEvents.h b/dbms/include/DB/Storages/System/StorageSystemEvents.h index 05d1ae6c966..95f5d351a05 100644 --- a/dbms/include/DB/Storages/System/StorageSystemEvents.h +++ b/dbms/include/DB/Storages/System/StorageSystemEvents.h @@ -1,5 +1,7 @@ #pragma once +#include + #include @@ -8,8 +10,10 @@ namespace DB /** Реализует системную таблицу events, которая позволяет получить информацию для профайлинга. */ -class StorageSystemEvents : public IStorage +class StorageSystemEvents : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemFunctions.h b/dbms/include/DB/Storages/System/StorageSystemFunctions.h index d093c61fc81..bdc6b534c45 100644 --- a/dbms/include/DB/Storages/System/StorageSystemFunctions.h +++ b/dbms/include/DB/Storages/System/StorageSystemFunctions.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу functions, которая позволяет получить список * всех обычных и агрегатных функций. */ -class StorageSystemFunctions : public IStorage +class StorageSystemFunctions : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemMerges.h b/dbms/include/DB/Storages/System/StorageSystemMerges.h index 81da8174d33..de1b82c8e01 100644 --- a/dbms/include/DB/Storages/System/StorageSystemMerges.h +++ b/dbms/include/DB/Storages/System/StorageSystemMerges.h @@ -1,12 +1,16 @@ #pragma once +#include + #include namespace DB { -class StorageSystemMerges : public IStorage +class StorageSystemMerges : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name); diff --git a/dbms/include/DB/Storages/System/StorageSystemMetrics.h b/dbms/include/DB/Storages/System/StorageSystemMetrics.h index 0077f67d341..bb6e35f6014 100644 --- a/dbms/include/DB/Storages/System/StorageSystemMetrics.h +++ b/dbms/include/DB/Storages/System/StorageSystemMetrics.h @@ -1,5 +1,7 @@ #pragma once +#include + #include @@ -8,8 +10,10 @@ namespace DB /** Реализует системную таблицу metrics, которая позволяет получить информацию о работе сервера. */ -class StorageSystemMetrics : public IStorage +class StorageSystemMetrics : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemNumbers.h b/dbms/include/DB/Storages/System/StorageSystemNumbers.h index 88f90b3d9c8..727d89a7d05 100644 --- a/dbms/include/DB/Storages/System/StorageSystemNumbers.h +++ b/dbms/include/DB/Storages/System/StorageSystemNumbers.h @@ -1,5 +1,7 @@ #pragma once +#include + #include @@ -10,8 +12,10 @@ namespace DB * Таблица содержит единственный столбец number UInt64. * Из этой таблицы можно прочитать все натуральные числа, начиная с 0 (до 2^64 - 1, а потом заново). */ -class StorageSystemNumbers : public IStorage +class StorageSystemNumbers : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_, bool multithreaded_ = false); diff --git a/dbms/include/DB/Storages/System/StorageSystemOne.h b/dbms/include/DB/Storages/System/StorageSystemOne.h index 2e34e84bb92..775ddfad725 100644 --- a/dbms/include/DB/Storages/System/StorageSystemOne.h +++ b/dbms/include/DB/Storages/System/StorageSystemOne.h @@ -1,5 +1,7 @@ #pragma once +#include + #include @@ -12,8 +14,10 @@ namespace DB * Используется, если в запросе не указана таблица. * Аналог таблицы DUAL в Oracle и MySQL. */ -class StorageSystemOne : public IStorage +class StorageSystemOne : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemParts.h b/dbms/include/DB/Storages/System/StorageSystemParts.h index 062e3d8bfe2..b9d762cdc7f 100644 --- a/dbms/include/DB/Storages/System/StorageSystemParts.h +++ b/dbms/include/DB/Storages/System/StorageSystemParts.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,12 +11,13 @@ namespace DB /** Реализует системную таблицу tables, которая позволяет получить информацию о всех таблицах. */ -class StorageSystemParts : public IStorage +class StorageSystemParts : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; public: static StoragePtr create(const std::string & name_); - std::string getName() const override{ return "SystemParts"; } + std::string getName() const override { return "SystemParts"; } std::string getTableName() const override { return name; } const NamesAndTypesList & getColumnsListImpl() const override { return columns; } diff --git a/dbms/include/DB/Storages/System/StorageSystemProcesses.h b/dbms/include/DB/Storages/System/StorageSystemProcesses.h index 3b6d1836d93..71b15ea2bb5 100644 --- a/dbms/include/DB/Storages/System/StorageSystemProcesses.h +++ b/dbms/include/DB/Storages/System/StorageSystemProcesses.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу processes, которая позволяет получить информацию о запросах, исполняющихся в данный момент. */ -class StorageSystemProcesses : public IStorage +class StorageSystemProcesses : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemReplicas.h b/dbms/include/DB/Storages/System/StorageSystemReplicas.h index 2016c1026fb..cd09cdbe543 100644 --- a/dbms/include/DB/Storages/System/StorageSystemReplicas.h +++ b/dbms/include/DB/Storages/System/StorageSystemReplicas.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу replicas, которая позволяет получить информацию о статусе реплицируемых таблиц. */ -class StorageSystemReplicas : public IStorage +class StorageSystemReplicas : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemReplicationQueue.h b/dbms/include/DB/Storages/System/StorageSystemReplicationQueue.h index dbe992beb7c..acf4e911a63 100644 --- a/dbms/include/DB/Storages/System/StorageSystemReplicationQueue.h +++ b/dbms/include/DB/Storages/System/StorageSystemReplicationQueue.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу replication_queue, которая позволяет посмотреть очереди репликации для реплицируемых таблиц. */ -class StorageSystemReplicationQueue : public IStorage +class StorageSystemReplicationQueue : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemSettings.h b/dbms/include/DB/Storages/System/StorageSystemSettings.h index b5cd52b64ee..7725a03eb58 100644 --- a/dbms/include/DB/Storages/System/StorageSystemSettings.h +++ b/dbms/include/DB/Storages/System/StorageSystemSettings.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу settings, которая позволяет получить информацию о текущих настройках. */ -class StorageSystemSettings : public IStorage +class StorageSystemSettings : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemTables.h b/dbms/include/DB/Storages/System/StorageSystemTables.h index da871cad699..2f10b72c3ed 100644 --- a/dbms/include/DB/Storages/System/StorageSystemTables.h +++ b/dbms/include/DB/Storages/System/StorageSystemTables.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу tables, которая позволяет получить информацию о всех таблицах. */ -class StorageSystemTables : public IStorage +class StorageSystemTables : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/include/DB/Storages/System/StorageSystemZooKeeper.h b/dbms/include/DB/Storages/System/StorageSystemZooKeeper.h index de00a1c2920..e32a059c6ae 100644 --- a/dbms/include/DB/Storages/System/StorageSystemZooKeeper.h +++ b/dbms/include/DB/Storages/System/StorageSystemZooKeeper.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -9,8 +11,10 @@ namespace DB /** Реализует системную таблицу zookeeper, которая позволяет просматривать данные в ZooKeeper в целях отладки. */ -class StorageSystemZooKeeper : public IStorage +class StorageSystemZooKeeper : private ext::share_ptr_helper, public IStorage { +friend class ext::share_ptr_helper; + public: static StoragePtr create(const std::string & name_); diff --git a/dbms/src/Interpreters/tests/in_join_subqueries_preprocessor.cpp b/dbms/src/Interpreters/tests/in_join_subqueries_preprocessor.cpp index 97c816eaf52..85a9532daeb 100644 --- a/dbms/src/Interpreters/tests/in_join_subqueries_preprocessor.cpp +++ b/dbms/src/Interpreters/tests/in_join_subqueries_preprocessor.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -15,12 +17,14 @@ #include /// Упрощённый вариант класса StorageDistributed. -class StorageDistributedFake : public DB::IStorage +class StorageDistributedFake : private ext::share_ptr_helper, public DB::IStorage { +friend class ext::share_ptr_helper; + public: static DB::StoragePtr create(const std::string & remote_database_, const std::string & remote_table_, size_t shard_count_) { - return (new StorageDistributedFake{remote_database_, remote_table_, shard_count_})->thisPtr(); + return make_shared(remote_database_, remote_table_, shard_count_); } std::string getName() const override { return "DistributedFake"; } diff --git a/dbms/src/Storages/StorageBuffer.cpp b/dbms/src/Storages/StorageBuffer.cpp index 28d519cea78..0837adb3c38 100644 --- a/dbms/src/Storages/StorageBuffer.cpp +++ b/dbms/src/Storages/StorageBuffer.cpp @@ -30,9 +30,9 @@ StoragePtr StorageBuffer::create(const std::string & name_, NamesAndTypesListPtr size_t num_shards_, const Thresholds & min_thresholds_, const Thresholds & max_thresholds_, const String & destination_database_, const String & destination_table_) { - return (new StorageBuffer{ + return make_shared( name_, columns_, materialized_columns_, alias_columns_, column_defaults_, - context_, num_shards_, min_thresholds_, max_thresholds_, destination_database_, destination_table_})->thisPtr(); + context_, num_shards_, min_thresholds_, max_thresholds_, destination_database_, destination_table_);; } diff --git a/dbms/src/Storages/StorageCloud.cpp b/dbms/src/Storages/StorageCloud.cpp index e88aba97f78..15dd0eb0f45 100644 --- a/dbms/src/Storages/StorageCloud.cpp +++ b/dbms/src/Storages/StorageCloud.cpp @@ -37,7 +37,7 @@ StoragePtr StorageCloud::create( const NamesAndTypesList & alias_columns_, const ColumnDefaults & column_defaults_) { - return (new StorageCloud{database_ptr_, name_, columns_, materialized_columns_, alias_columns_, column_defaults_})->thisPtr(); + return make_shared(database_ptr_, name_, columns_, materialized_columns_, alias_columns_, column_defaults_); } diff --git a/dbms/src/Storages/StorageDistributed.cpp b/dbms/src/Storages/StorageDistributed.cpp index ab939cc39a1..1c0fd705e61 100644 --- a/dbms/src/Storages/StorageDistributed.cpp +++ b/dbms/src/Storages/StorageDistributed.cpp @@ -131,13 +131,13 @@ StoragePtr StorageDistributed::create( const ASTPtr & sharding_key_, const String & data_path_) { - return (new StorageDistributed{ + return make_shared( name_, columns_, materialized_columns_, alias_columns_, column_defaults_, remote_database_, remote_table_, context_.getCluster(cluster_name), context_, sharding_key_, data_path_ - })->thisPtr(); + ); } @@ -149,15 +149,15 @@ StoragePtr StorageDistributed::create( std::shared_ptr & owned_cluster_, Context & context_) { - auto res = new StorageDistributed{ + auto res = make_shared( name_, columns_, remote_database_, remote_table_, *owned_cluster_, context_ - }; + ); /// Захватываем владение объектом-кластером. res->owned_cluster = owned_cluster_; - return res->thisPtr(); + return res; } BlockInputStreams StorageDistributed::read( diff --git a/dbms/src/Storages/StorageLog.cpp b/dbms/src/Storages/StorageLog.cpp index 26501bbe8ae..45f03ee5694 100644 --- a/dbms/src/Storages/StorageLog.cpp +++ b/dbms/src/Storages/StorageLog.cpp @@ -496,11 +496,11 @@ StoragePtr StorageLog::create( const ColumnDefaults & column_defaults_, size_t max_compress_block_size_) { - return (new StorageLog{ + return make_shared( path_, name_, columns_, materialized_columns_, alias_columns_, column_defaults_, max_compress_block_size_ - })->thisPtr(); + ); } StoragePtr StorageLog::create( @@ -509,11 +509,11 @@ StoragePtr StorageLog::create( NamesAndTypesListPtr columns_, size_t max_compress_block_size_) { - return (new StorageLog{ + return make_shared( path_, name_, columns_, - {}, {}, ColumnDefaults{}, + NamesAndTypesList{}, NamesAndTypesList{}, ColumnDefaults{}, max_compress_block_size_ - })->thisPtr(); + ); } diff --git a/dbms/src/Storages/StorageMaterializedView.cpp b/dbms/src/Storages/StorageMaterializedView.cpp index 9925e90f35d..c66af851cd4 100644 --- a/dbms/src/Storages/StorageMaterializedView.cpp +++ b/dbms/src/Storages/StorageMaterializedView.cpp @@ -28,11 +28,10 @@ StoragePtr StorageMaterializedView::create( const ColumnDefaults & column_defaults_, bool attach_) { - return (new StorageMaterializedView{ - table_name_, database_name_, context_, query_, - columns_, materialized_columns_, alias_columns_, column_defaults_, - attach_ - })->thisPtr(); + return ext::share_ptr_helper::make_shared( + table_name_, database_name_, context_, query_, columns_, + materialized_columns_, alias_columns_, column_defaults_, attach_ + ); } StorageMaterializedView::StorageMaterializedView( diff --git a/dbms/src/Storages/StorageMemory.cpp b/dbms/src/Storages/StorageMemory.cpp index 9376f2087c6..60e4d53dea6 100644 --- a/dbms/src/Storages/StorageMemory.cpp +++ b/dbms/src/Storages/StorageMemory.cpp @@ -98,9 +98,7 @@ StoragePtr StorageMemory::create( const std::string & name_, NamesAndTypesListPtr columns_) { - return (new StorageMemory{ - name_, columns_ - })->thisPtr(); + return make_shared(name_, columns_); } StoragePtr StorageMemory::create( @@ -110,10 +108,7 @@ StoragePtr StorageMemory::create( const NamesAndTypesList & alias_columns_, const ColumnDefaults & column_defaults_) { - return (new StorageMemory{ - name_, columns_, - materialized_columns_, alias_columns_, column_defaults_ - })->thisPtr(); + return make_shared(name_, columns_, materialized_columns_, alias_columns_, column_defaults_); } diff --git a/dbms/src/Storages/StorageMerge.cpp b/dbms/src/Storages/StorageMerge.cpp index 3338c984afa..5c4802f7379 100644 --- a/dbms/src/Storages/StorageMerge.cpp +++ b/dbms/src/Storages/StorageMerge.cpp @@ -53,10 +53,10 @@ StoragePtr StorageMerge::create( const String & table_name_regexp_, const Context & context_) { - return (new StorageMerge{ + return make_shared( name_, columns_, source_database_, table_name_regexp_, context_ - })->thisPtr(); + ); } StoragePtr StorageMerge::create( @@ -69,10 +69,10 @@ StoragePtr StorageMerge::create( const String & table_name_regexp_, const Context & context_) { - return (new StorageMerge{ + return make_shared( name_, columns_, materialized_columns_, alias_columns_, column_defaults_, source_database_, table_name_regexp_, context_ - })->thisPtr(); + ); } NameAndTypePair StorageMerge::getColumn(const String & column_name) const diff --git a/dbms/src/Storages/StorageMergeTree.cpp b/dbms/src/Storages/StorageMergeTree.cpp index 3c4cb66ffd1..7343e2f65bb 100644 --- a/dbms/src/Storages/StorageMergeTree.cpp +++ b/dbms/src/Storages/StorageMergeTree.cpp @@ -90,17 +90,15 @@ StoragePtr StorageMergeTree::create( bool has_force_restore_data_flag_, const MergeTreeSettings & settings_) { - auto res = new StorageMergeTree{ + auto res = make_shared( path_, database_name_, table_name_, columns_, materialized_columns_, alias_columns_, column_defaults_, context_, primary_expr_ast_, date_column_name_, sampling_expression_, index_granularity_, merging_params_, has_force_restore_data_flag_, settings_ - }; - StoragePtr res_ptr = res->thisPtr(); + ); + res->merge_task_handle = res->background_pool.addTask(std::bind(&StorageMergeTree::mergeTask, res.get(), std::placeholders::_1)); - res->merge_task_handle = res->background_pool.addTask(std::bind(&StorageMergeTree::mergeTask, res, std::placeholders::_1)); - - return res_ptr; + return res; } diff --git a/dbms/src/Storages/StorageReplicatedMergeTree.cpp b/dbms/src/Storages/StorageReplicatedMergeTree.cpp index 110026e4e8d..3c6d8ded179 100644 --- a/dbms/src/Storages/StorageReplicatedMergeTree.cpp +++ b/dbms/src/Storages/StorageReplicatedMergeTree.cpp @@ -345,15 +345,14 @@ StoragePtr StorageReplicatedMergeTree::create( bool has_force_restore_data_flag_, const MergeTreeSettings & settings_) { - auto res = new StorageReplicatedMergeTree{ + auto res = make_shared( zookeeper_path_, replica_name_, attach, path_, database_name_, name_, columns_, materialized_columns_, alias_columns_, column_defaults_, context_, primary_expr_ast_, date_column_name_, sampling_expression_, index_granularity_, - merging_params_, has_force_restore_data_flag_, settings_}; - - StoragePtr res_ptr = res->thisPtr(); + merging_params_, has_force_restore_data_flag_, settings_); + StoragePtr res_ptr = res; auto get_endpoint_holder = [&res](InterserverIOEndpointPtr endpoint) { @@ -393,7 +392,7 @@ StoragePtr StorageReplicatedMergeTree::create( } } - return res_ptr; + return res; } diff --git a/dbms/src/Storages/StorageStripeLog.cpp b/dbms/src/Storages/StorageStripeLog.cpp index 4d68937959a..9dc53d485f8 100644 --- a/dbms/src/Storages/StorageStripeLog.cpp +++ b/dbms/src/Storages/StorageStripeLog.cpp @@ -211,11 +211,11 @@ StoragePtr StorageStripeLog::create( bool attach, size_t max_compress_block_size_) { - return (new StorageStripeLog{ + return make_shared( path_, name_, columns_, materialized_columns_, alias_columns_, column_defaults_, attach, max_compress_block_size_ - })->thisPtr(); + ); } diff --git a/dbms/src/Storages/StorageTinyLog.cpp b/dbms/src/Storages/StorageTinyLog.cpp index 975b7e1d97c..e891e26215e 100644 --- a/dbms/src/Storages/StorageTinyLog.cpp +++ b/dbms/src/Storages/StorageTinyLog.cpp @@ -394,11 +394,11 @@ StoragePtr StorageTinyLog::create( bool attach, size_t max_compress_block_size_) { - return (new StorageTinyLog{ + return make_shared( path_, name_, columns_, materialized_columns_, alias_columns_, column_defaults_, attach, max_compress_block_size_ - })->thisPtr(); + ); } diff --git a/dbms/src/Storages/StorageView.cpp b/dbms/src/Storages/StorageView.cpp index 73fb43551e8..d5a40ede6ce 100644 --- a/dbms/src/Storages/StorageView.cpp +++ b/dbms/src/Storages/StorageView.cpp @@ -25,10 +25,10 @@ StoragePtr StorageView::create( const NamesAndTypesList & alias_columns_, const ColumnDefaults & column_defaults_) { - return (new StorageView{ + return make_shared( table_name_, database_name_, context_, query_, columns_, materialized_columns_, alias_columns_, column_defaults_ - })->thisPtr(); + ); } diff --git a/dbms/src/Storages/System/StorageSystemClusters.cpp b/dbms/src/Storages/System/StorageSystemClusters.cpp index 9941abf7ef8..8dd7a7d81d6 100644 --- a/dbms/src/Storages/System/StorageSystemClusters.cpp +++ b/dbms/src/Storages/System/StorageSystemClusters.cpp @@ -31,7 +31,7 @@ StorageSystemClusters::StorageSystemClusters(const std::string & name_, Context StoragePtr StorageSystemClusters::create(const std::string & name_, Context & context_) { - return (new StorageSystemClusters{name_, context_})->thisPtr(); + return make_shared(name_, context_); } BlockInputStreams StorageSystemClusters::read( diff --git a/dbms/src/Storages/System/StorageSystemColumns.cpp b/dbms/src/Storages/System/StorageSystemColumns.cpp index e9c840640ec..9a8cb8b1449 100644 --- a/dbms/src/Storages/System/StorageSystemColumns.cpp +++ b/dbms/src/Storages/System/StorageSystemColumns.cpp @@ -29,7 +29,7 @@ StorageSystemColumns::StorageSystemColumns(const std::string & name_) StoragePtr StorageSystemColumns::create(const std::string & name_) { - return (new StorageSystemColumns{name_})->thisPtr(); + return make_shared(name_); } BlockInputStreams StorageSystemColumns::read( diff --git a/dbms/src/Storages/System/StorageSystemDatabases.cpp b/dbms/src/Storages/System/StorageSystemDatabases.cpp index a931357a30b..0d73e3ef365 100644 --- a/dbms/src/Storages/System/StorageSystemDatabases.cpp +++ b/dbms/src/Storages/System/StorageSystemDatabases.cpp @@ -21,7 +21,7 @@ StorageSystemDatabases::StorageSystemDatabases(const std::string & name_) StoragePtr StorageSystemDatabases::create(const std::string & name_) { - return (new StorageSystemDatabases(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemDictionaries.cpp b/dbms/src/Storages/System/StorageSystemDictionaries.cpp index c4eb1c5ff57..40a90435bc3 100644 --- a/dbms/src/Storages/System/StorageSystemDictionaries.cpp +++ b/dbms/src/Storages/System/StorageSystemDictionaries.cpp @@ -40,7 +40,7 @@ StorageSystemDictionaries::StorageSystemDictionaries(const std::string & name) StoragePtr StorageSystemDictionaries::create(const std::string & name) { - return (new StorageSystemDictionaries{name})->thisPtr(); + return make_shared(name); } BlockInputStreams StorageSystemDictionaries::read( diff --git a/dbms/src/Storages/System/StorageSystemEvents.cpp b/dbms/src/Storages/System/StorageSystemEvents.cpp index a13b9b8f4cc..a8b995f701f 100644 --- a/dbms/src/Storages/System/StorageSystemEvents.cpp +++ b/dbms/src/Storages/System/StorageSystemEvents.cpp @@ -22,7 +22,7 @@ StorageSystemEvents::StorageSystemEvents(const std::string & name_) StoragePtr StorageSystemEvents::create(const std::string & name_) { - return (new StorageSystemEvents(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemFunctions.cpp b/dbms/src/Storages/System/StorageSystemFunctions.cpp index 561b5a82a31..93454772cbc 100644 --- a/dbms/src/Storages/System/StorageSystemFunctions.cpp +++ b/dbms/src/Storages/System/StorageSystemFunctions.cpp @@ -21,7 +21,7 @@ StorageSystemFunctions::StorageSystemFunctions(const std::string & name_) StoragePtr StorageSystemFunctions::create(const std::string & name_) { - return (new StorageSystemFunctions{name_})->thisPtr(); + return make_shared(name_); } BlockInputStreams StorageSystemFunctions::read( diff --git a/dbms/src/Storages/System/StorageSystemMerges.cpp b/dbms/src/Storages/System/StorageSystemMerges.cpp index 6bb3e2b775b..d78111e05ff 100644 --- a/dbms/src/Storages/System/StorageSystemMerges.cpp +++ b/dbms/src/Storages/System/StorageSystemMerges.cpp @@ -31,7 +31,7 @@ StorageSystemMerges::StorageSystemMerges(const std::string & name) StoragePtr StorageSystemMerges::create(const std::string & name) { - return (new StorageSystemMerges{name})->thisPtr(); + return make_shared(name); } BlockInputStreams StorageSystemMerges::read( diff --git a/dbms/src/Storages/System/StorageSystemMetrics.cpp b/dbms/src/Storages/System/StorageSystemMetrics.cpp index b699d3e6cca..5cbfd4c8ee1 100644 --- a/dbms/src/Storages/System/StorageSystemMetrics.cpp +++ b/dbms/src/Storages/System/StorageSystemMetrics.cpp @@ -22,7 +22,7 @@ StorageSystemMetrics::StorageSystemMetrics(const std::string & name_) StoragePtr StorageSystemMetrics::create(const std::string & name_) { - return (new StorageSystemMetrics(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemNumbers.cpp b/dbms/src/Storages/System/StorageSystemNumbers.cpp index 2dc575815dc..01b0144719a 100644 --- a/dbms/src/Storages/System/StorageSystemNumbers.cpp +++ b/dbms/src/Storages/System/StorageSystemNumbers.cpp @@ -55,7 +55,7 @@ StorageSystemNumbers::StorageSystemNumbers(const std::string & name_, bool multi StoragePtr StorageSystemNumbers::create(const std::string & name_, bool multithreaded_) { - return (new StorageSystemNumbers(name_, multithreaded_))->thisPtr(); + return make_shared(name_, multithreaded_); } diff --git a/dbms/src/Storages/System/StorageSystemOne.cpp b/dbms/src/Storages/System/StorageSystemOne.cpp index 8f5a08b2d78..0ce7f1113e4 100644 --- a/dbms/src/Storages/System/StorageSystemOne.cpp +++ b/dbms/src/Storages/System/StorageSystemOne.cpp @@ -17,7 +17,7 @@ StorageSystemOne::StorageSystemOne(const std::string & name_) StoragePtr StorageSystemOne::create(const std::string & name_) { - return (new StorageSystemOne(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemParts.cpp b/dbms/src/Storages/System/StorageSystemParts.cpp index 8fe7d922fbf..9e9f562c536 100644 --- a/dbms/src/Storages/System/StorageSystemParts.cpp +++ b/dbms/src/Storages/System/StorageSystemParts.cpp @@ -43,7 +43,7 @@ StorageSystemParts::StorageSystemParts(const std::string & name_) StoragePtr StorageSystemParts::create(const std::string & name_) { - return (new StorageSystemParts(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemProcesses.cpp b/dbms/src/Storages/System/StorageSystemProcesses.cpp index ccc6cc7a5e5..edf05b5bf0d 100644 --- a/dbms/src/Storages/System/StorageSystemProcesses.cpp +++ b/dbms/src/Storages/System/StorageSystemProcesses.cpp @@ -28,7 +28,7 @@ StorageSystemProcesses::StorageSystemProcesses(const std::string & name_) StoragePtr StorageSystemProcesses::create(const std::string & name_) { - return (new StorageSystemProcesses(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemReplicas.cpp b/dbms/src/Storages/System/StorageSystemReplicas.cpp index f6f7dc51159..e673791b8e3 100644 --- a/dbms/src/Storages/System/StorageSystemReplicas.cpp +++ b/dbms/src/Storages/System/StorageSystemReplicas.cpp @@ -47,7 +47,7 @@ StorageSystemReplicas::StorageSystemReplicas(const std::string & name_) StoragePtr StorageSystemReplicas::create(const std::string & name_) { - return (new StorageSystemReplicas(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemReplicationQueue.cpp b/dbms/src/Storages/System/StorageSystemReplicationQueue.cpp index bd60b35c1fb..791d2af91c4 100644 --- a/dbms/src/Storages/System/StorageSystemReplicationQueue.cpp +++ b/dbms/src/Storages/System/StorageSystemReplicationQueue.cpp @@ -49,7 +49,7 @@ StorageSystemReplicationQueue::StorageSystemReplicationQueue(const std::string & StoragePtr StorageSystemReplicationQueue::create(const std::string & name_) { - return (new StorageSystemReplicationQueue(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemSettings.cpp b/dbms/src/Storages/System/StorageSystemSettings.cpp index 4fc9d6a08b1..c625d4e2557 100644 --- a/dbms/src/Storages/System/StorageSystemSettings.cpp +++ b/dbms/src/Storages/System/StorageSystemSettings.cpp @@ -21,7 +21,7 @@ StorageSystemSettings::StorageSystemSettings(const std::string & name_) StoragePtr StorageSystemSettings::create(const std::string & name_) { - return (new StorageSystemSettings(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemTables.cpp b/dbms/src/Storages/System/StorageSystemTables.cpp index a3853705ed6..d2bb2ec6536 100644 --- a/dbms/src/Storages/System/StorageSystemTables.cpp +++ b/dbms/src/Storages/System/StorageSystemTables.cpp @@ -28,7 +28,7 @@ StorageSystemTables::StorageSystemTables(const std::string & name_) StoragePtr StorageSystemTables::create(const std::string & name_) { - return (new StorageSystemTables(name_))->thisPtr(); + return make_shared(name_); } diff --git a/dbms/src/Storages/System/StorageSystemZooKeeper.cpp b/dbms/src/Storages/System/StorageSystemZooKeeper.cpp index 1c8ee0a5439..f323394c05f 100644 --- a/dbms/src/Storages/System/StorageSystemZooKeeper.cpp +++ b/dbms/src/Storages/System/StorageSystemZooKeeper.cpp @@ -37,7 +37,7 @@ StorageSystemZooKeeper::StorageSystemZooKeeper(const std::string & name_) StoragePtr StorageSystemZooKeeper::create(const std::string & name_) { - return (new StorageSystemZooKeeper(name_))->thisPtr(); + return make_shared(name_); } diff --git a/libs/libcommon/include/ext/share_ptr_helper.hpp b/libs/libcommon/include/ext/share_ptr_helper.hpp new file mode 100644 index 00000000000..52ce4688400 --- /dev/null +++ b/libs/libcommon/include/ext/share_ptr_helper.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +namespace ext +{ + +/** + * Class AllocateShared allow to make std::shared_ptr from T with private constructor. + * Derive you T class from AllocateShared, define him as friend and call allocate_shared()/make_shared() method. +**/ +template +class share_ptr_helper +{ +protected: +typedef typename std::remove_const::type TNoConst; + + template + struct Deleter + { + void operator()(typename TAlloc::value_type * ptr) + { + std::allocator_traits::destroy(alloc, ptr); + } + TAlloc alloc; + }; + +///see std::allocate_shared +template +static std::shared_ptr allocate_shared(const TAlloc & alloc, TArgs && ... args) +{ + TAlloc alloc_copy(alloc); + return std::shared_ptr(new (std::allocator_traits::allocate(alloc_copy, 1)) TNoConst(std::forward(args)...), Deleter(), alloc_copy); +} + +template +static std::shared_ptr make_shared(TArgs && ... args) +{ + return allocate_shared(std::allocator(), std::forward(args)...); +} +}; + +} +