1111#include < Storages/StorageDictionary.h>
1212#include < IO/WriteBufferFromFile.h>
1313#include < Poco/File.h>
14- #include < ext/scope_guard.h >
14+ #include < boost/smart_ptr/make_shared_object.hpp >
1515
1616
1717namespace CurrentStatusInfo
@@ -61,9 +61,9 @@ void DatabaseWithDictionaries::attachDictionary(const String & dictionary_name,
6161
6262 CurrentStatusInfo::set (CurrentStatusInfo::DictionaryStatus, full_name, static_cast <Int8>(ExternalLoaderStatus::NOT_LOADED));
6363
64- // / ExternalLoader::reloadConfig() will find out that the dictionary's config has been added
65- // / and in case `dictionaries_lazy_load == false` it will load the dictionary.
66- external_loader-> reloadConfig ( getDatabaseName (), full_name);
64+ // / We want ExternalLoader::reloadConfig() to find out that the dictionary's config
65+ // / has been added and in case `dictionaries_lazy_load == false` to load the dictionary.
66+ reloadDictionaryConfig ( full_name);
6767}
6868
6969void DatabaseWithDictionaries::detachDictionary (const String & dictionary_name)
@@ -98,9 +98,9 @@ void DatabaseWithDictionaries::detachDictionaryImpl(const String & dictionary_na
9898
9999 CurrentStatusInfo::unset (CurrentStatusInfo::DictionaryStatus, full_name);
100100
101- // / ExternalLoader::reloadConfig() will find out that the dictionary's config has been removed
102- // / and therefore it will unload the dictionary.
103- external_loader-> reloadConfig ( getDatabaseName (), full_name);
101+ // / We want ExternalLoader::reloadConfig() to find out that the dictionary's config
102+ // / has been removed and to unload the dictionary.
103+ reloadDictionaryConfig ( full_name);
104104}
105105
106106void DatabaseWithDictionaries::createDictionary (const Context & context, const String & dictionary_name, const ASTPtr & query)
@@ -122,7 +122,7 @@ void DatabaseWithDictionaries::createDictionary(const Context & context, const S
122122
123123 // / A dictionary with the same full name could be defined in *.xml config files.
124124 String full_name = getDatabaseName () + " ." + dictionary_name;
125- if (external_loader-> getCurrentStatus (full_name) != ExternalLoader::Status::NOT_EXIST)
125+ if (external_loader. getCurrentStatus (full_name) != ExternalLoader::Status::NOT_EXIST)
126126 throw Exception (
127127 " Dictionary " + backQuote (getDatabaseName ()) + " ." + backQuote (dictionary_name) + " already exists." ,
128128 ErrorCodes::DICTIONARY_ALREADY_EXISTS);
@@ -153,15 +153,15 @@ void DatabaseWithDictionaries::createDictionary(const Context & context, const S
153153
154154 // / Add a temporary repository containing the dictionary.
155155 // / We need this temp repository to try loading the dictionary before actually attaching it to the database.
156- auto temp_repository = external_loader-> addConfigRepository (std::make_unique<ExternalLoaderTempConfigRepository>(
156+ auto temp_repository = external_loader. addConfigRepository (std::make_unique<ExternalLoaderTempConfigRepository>(
157157 getDatabaseName (), dictionary_metadata_tmp_path, getDictionaryConfigurationFromAST (query->as <const ASTCreateQuery &>())));
158158
159159 bool lazy_load = context.getConfigRef ().getBool (" dictionaries_lazy_load" , true );
160160 if (!lazy_load)
161161 {
162162 // / load() is called here to force loading the dictionary, wait until the loading is finished,
163163 // / and throw an exception if the loading is failed.
164- external_loader-> load (full_name);
164+ external_loader. load (full_name);
165165 }
166166
167167 auto config = getDictionaryConfigurationFromAST (query->as <const ASTCreateQuery &>());
@@ -176,8 +176,8 @@ void DatabaseWithDictionaries::createDictionary(const Context & context, const S
176176 Poco::File (dictionary_metadata_tmp_path).renameTo (dictionary_metadata_path);
177177
178178 // / ExternalDictionariesLoader doesn't know we renamed the metadata path.
179- // / So we have to manually call reloadConfig() here.
180- external_loader-> reloadConfig ( getDatabaseName (), full_name);
179+ // / That's why we have to call ExternalLoader:: reloadConfig() here.
180+ reloadDictionaryConfig ( full_name);
181181
182182 // / Everything's ok.
183183 succeeded = true ;
@@ -291,28 +291,38 @@ bool DatabaseWithDictionaries::empty() const
291291 return tables.empty () && dictionaries.empty ();
292292}
293293
294+ void DatabaseWithDictionaries::reloadDictionaryConfig (const String & full_name)
295+ {
296+ // / Ensure that this database is attached to ExternalLoader as a config repository.
297+ if (!database_as_config_repo_for_external_loader.load ())
298+ database_as_config_repo_for_external_loader = boost::make_shared<ext::scope_guard>(
299+ external_loader.addConfigRepository (std::make_unique<ExternalLoaderDatabaseConfigRepository>(*this )));
300+
301+ external_loader.reloadConfig (getDatabaseName (), full_name);
302+ }
303+
304+
294305void DatabaseWithDictionaries::shutdown ()
295306{
296307 {
297308 std::lock_guard lock (mutex);
298309 dictionaries.clear ();
299310 }
300- detachFromExternalDictionariesLoader ();
311+
312+ // / Invoke removing the database from ExternalLoader.
313+ database_as_config_repo_for_external_loader = nullptr ;
314+
301315 DatabaseOnDisk::shutdown ();
302316}
303317
304- DatabaseWithDictionaries::~DatabaseWithDictionaries () = default ;
305318
306- void DatabaseWithDictionaries::attachToExternalDictionariesLoader (Context & context)
319+ DatabaseWithDictionaries::DatabaseWithDictionaries (
320+ const String & name, const String & metadata_path_, const String & data_path_, const String & logger, const Context & context)
321+ : DatabaseOnDisk(name, metadata_path_, data_path_, logger, context)
322+ , external_loader(context.getExternalDictionariesLoader())
307323{
308- external_loader = &context.getExternalDictionariesLoader ();
309- database_as_config_repo_for_external_loader
310- = external_loader->addConfigRepository (std::make_unique<ExternalLoaderDatabaseConfigRepository>(*this , context));
311324}
312325
313- void DatabaseWithDictionaries::detachFromExternalDictionariesLoader ()
314- {
315- database_as_config_repo_for_external_loader = {};
316- }
326+ DatabaseWithDictionaries::~DatabaseWithDictionaries () = default ;
317327
318328}
0 commit comments