/* Copyright (c) 2015, 2024, Oracle and/or its affiliates. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is designed to work with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have either included with the program or referenced in the documentation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "sql/dd/impl/cache/shared_dictionary_cache.h" #include #include #include "sql/dd/impl/cache/shared_multi_map.h" #include "sql/dd/impl/cache/storage_adapter.h" // Storage_adapter #include "sql/mysqld.h" #include "sql/sql_class.h" // THD::is_error() namespace dd { namespace cache { template class Cache_element; Shared_dictionary_cache *Shared_dictionary_cache::instance() { static Shared_dictionary_cache s_cache; return &s_cache; } void Shared_dictionary_cache::init() { instance()->m_map()->set_capacity(collation_capacity); instance()->m_map()->set_capacity(charset_capacity); // Set capacity to have room for all connections to leave an element // unused in the cache to avoid frequent cache misses while e.g. // opening a table. instance()->m_map()->set_capacity(max_connections); instance()->m_map()->set_capacity(event_capacity); instance()->m_map()->set_capacity(stored_program_def_size); instance()->m_map()->set_capacity(schema_def_size); instance()->m_map()->set_capacity( column_statistics_capacity); instance()->m_map()->set_capacity( spatial_reference_system_capacity); instance()->m_map()->set_capacity(tablespace_def_size); instance()->m_map()->set_capacity(resource_group_capacity); } void Shared_dictionary_cache::shutdown() { instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); instance()->m_map()->shutdown(); } // Don't call this function anywhere except upgrade scenario. void Shared_dictionary_cache::reset(bool keep_dd_entities) { shutdown(); if (!keep_dd_entities) Storage_adapter::instance()->erase_all(); init(); } // Workaround to be used during recovery at server restart. bool Shared_dictionary_cache::reset_tables_and_tablespaces(THD *thd) { return (instance()->m_map()->reset(thd) || instance()->m_map()->reset(thd)); } // Get an element from the cache, given the key. template bool Shared_dictionary_cache::get(THD *thd, const K &key, Cache_element **element) { bool error = false; assert(element); if (m_map()->get(key, element)) { // Handle cache miss. const T *new_object = nullptr; error = get_uncached(thd, key, ISO_READ_COMMITTED, &new_object); // Add the new object, and assign the output element, even in the case of // a miss error (needed to remove the missed key). m_map()->put(&key, new_object, element); } return error; } // Read an object directly from disk, given the key. template bool Shared_dictionary_cache::get_uncached(THD *thd, const K &key, enum_tx_isolation isolation, const T **object) const { assert(object); bool error = Storage_adapter::get(thd, key, isolation, false, object); assert(!error || thd->is_system_thread() || thd->killed || thd->is_error()); return error; } // Add an object to the shared cache. template void Shared_dictionary_cache::put(const T *object, Cache_element **element) { assert(object); // Cast needed to help the compiler choose the correct template instance.. m_map()->put(static_cast(nullptr), object, element); } // Explicitly instantiate the types for the various usages. template bool Shared_dictionary_cache::get( THD *thd, const Abstract_table::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Abstract_table::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Abstract_table::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Abstract_table::Id_key &, enum_tx_isolation, const Abstract_table **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Abstract_table::Name_key &, enum_tx_isolation, const Abstract_table **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Abstract_table::Aux_key &, enum_tx_isolation, const Abstract_table **) const; template void Shared_dictionary_cache::put( const Abstract_table *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Charset::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Charset::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Charset::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Charset::Id_key &, enum_tx_isolation, const Charset **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Charset::Name_key &, enum_tx_isolation, const Charset **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Charset::Aux_key &, enum_tx_isolation, const Charset **) const; template void Shared_dictionary_cache::put(const Charset *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Collation::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Collation::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Collation::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached< Collation::Id_key, Collation>(THD *thd, const Collation::Id_key &, enum_tx_isolation, const Collation **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Collation::Name_key &, enum_tx_isolation, const Collation **) const; template bool Shared_dictionary_cache::get_uncached< Collation::Aux_key, Collation>(THD *thd, const Collation::Aux_key &, enum_tx_isolation, const Collation **) const; template void Shared_dictionary_cache::put( const Collation *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Event::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Event::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Event::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Event::Id_key &, enum_tx_isolation, const Event **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Event::Name_key &, enum_tx_isolation, const Event **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Event::Aux_key &, enum_tx_isolation, const Event **) const; template void Shared_dictionary_cache::put(const Event *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Routine::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Routine::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Routine::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Routine::Id_key &, enum_tx_isolation, const Routine **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Routine::Name_key &, enum_tx_isolation, const Routine **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Routine::Aux_key &, enum_tx_isolation, const Routine **) const; template void Shared_dictionary_cache::put(const Routine *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Schema::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Schema::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Schema::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Schema::Id_key &, enum_tx_isolation, const Schema **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Schema::Name_key &, enum_tx_isolation, const Schema **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Schema::Aux_key &, enum_tx_isolation, const Schema **) const; template void Shared_dictionary_cache::put(const Schema *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Spatial_reference_system::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Spatial_reference_system::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Spatial_reference_system::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached< Spatial_reference_system::Id_key, Spatial_reference_system>( THD *thd, const Spatial_reference_system::Id_key &, enum_tx_isolation, const Spatial_reference_system **) const; template bool Shared_dictionary_cache::get_uncached< Spatial_reference_system::Name_key, Spatial_reference_system>( THD *thd, const Spatial_reference_system::Name_key &, enum_tx_isolation, const Spatial_reference_system **) const; template bool Shared_dictionary_cache::get_uncached< Spatial_reference_system::Aux_key, Spatial_reference_system>( THD *thd, const Spatial_reference_system::Aux_key &, enum_tx_isolation, const Spatial_reference_system **) const; template void Shared_dictionary_cache::put( const Spatial_reference_system *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Column_statistics::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Column_statistics::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Column_statistics::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Column_statistics::Id_key &, enum_tx_isolation, const Column_statistics **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Column_statistics::Name_key &, enum_tx_isolation, const Column_statistics **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Column_statistics::Aux_key &, enum_tx_isolation, const Column_statistics **) const; template void Shared_dictionary_cache::put( const Column_statistics *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Tablespace::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Tablespace::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Tablespace::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Tablespace::Id_key &, enum_tx_isolation, const Tablespace **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Tablespace::Name_key &, enum_tx_isolation, const Tablespace **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Tablespace::Aux_key &, enum_tx_isolation, const Tablespace **) const; template void Shared_dictionary_cache::put( const Tablespace *, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Resource_group::Id_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Resource_group::Name_key &, Cache_element **); template bool Shared_dictionary_cache::get( THD *thd, const Resource_group::Aux_key &, Cache_element **); template bool Shared_dictionary_cache::get_uncached( THD *thd, const Resource_group::Id_key &, enum_tx_isolation, const Resource_group **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Resource_group::Name_key &, enum_tx_isolation, const Resource_group **) const; template bool Shared_dictionary_cache::get_uncached( THD *thd, const Resource_group::Aux_key &, enum_tx_isolation, const Resource_group **) const; template void Shared_dictionary_cache::put( const Resource_group *, Cache_element **); } // namespace cache } // namespace dd