| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <stddef.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <algorithm> |
| 30 | #include <typeinfo> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include "lex_string.h" |
| 34 | #include "my_compiler.h" |
| 35 | #include "sql/dd/cache/dictionary_client.h" |
| 36 | #include "sql/dd/cache/element_map.h" |
| 37 | #include "sql/dd/dd.h" |
| 38 | #include "sql/dd/impl/cache/cache_element.h" |
| 39 | #include "sql/dd/impl/cache/free_list.h" |
| 40 | #include "sql/dd/impl/cache/shared_dictionary_cache.h" |
| 41 | #include "sql/dd/impl/cache/storage_adapter.h" |
| 42 | #include "sql/dd/impl/tables/tables.h" |
| 43 | #include "sql/dd/impl/types/charset_impl.h" |
| 44 | #include "sql/dd/impl/types/collation_impl.h" |
| 45 | #include "sql/dd/impl/types/column_statistics_impl.h" |
| 46 | #include "sql/dd/impl/types/event_impl.h" |
| 47 | #include "sql/dd/impl/types/procedure_impl.h" |
| 48 | #include "sql/dd/impl/types/schema_impl.h" |
| 49 | #include "sql/dd/impl/types/table_impl.h" |
| 50 | #include "sql/dd/impl/types/tablespace_impl.h" |
| 51 | #include "sql/dd/impl/types/view_impl.h" |
| 52 | // Avoid warning about deleting ptr to incomplete type on Win |
| 53 | #include "sql/dd/properties.h" |
| 54 | #include "sql/mdl.h" |
| 55 | #include "sql/sql_base.h" |
| 56 | #include "sql/sql_class.h" |
| 57 | #include "unittest/gunit/dd.h" |
| 58 | #include "unittest/gunit/test_mdl_context_owner.h" |
| 59 | #include "unittest/gunit/test_utils.h" |
| 60 | |
| 61 | namespace dd { |
| 62 | bool operator==(const Weak_object &a, const Weak_object &b) { |
| 63 | String_type arep, brep; |
| 64 | a.debug_print(arep); |
| 65 | b.debug_print(brep); |
| 66 | |
| 67 | typedef String_type::iterator i_type; |
| 68 | typedef std::pair<i_type, i_type> mismatch_type; |
| 69 | |
| 70 | bool arep_largest = (arep.size() > brep.size()); |
| 71 | |
| 72 | String_type &largest = arep_largest ? arep : brep; |
| 73 | String_type &smallest = arep_largest ? brep : arep; |
| 74 | |
| 75 | mismatch_type mismatch = |
| 76 | std::mismatch(largest.begin(), largest.end(), smallest.begin()); |
| 77 | if (mismatch.first == largest.end()) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | String_type largediff = String_type(mismatch.first, largest.end()); |
| 82 | String_type smalldiff = String_type(mismatch.second, smallest.end()); |
| 83 | std::cout << "Debug representation not equal:\n" |
| 84 | << String_type(largest.begin(), mismatch.first) << "\n<<<\n"; |
| 85 | if (arep_largest) { |
| 86 | std::cout << largediff << "\n===\n" << smalldiff; |
| 87 | } else { |
| 88 | std::cout << smalldiff << "\n===\n" << largediff; |
| 89 | } |
| 90 | std::cout << "\n>>>\n" << std::endl; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | } // namespace dd |
| 95 | |
| 96 | using dd_unittest::nullp; |
| 97 | |
| 98 | namespace dd_cache_unittest { |
| 99 | |
| 100 | class CacheStorageTest : public ::testing::Test, public Test_MDL_context_owner { |
| 101 | public: |
| 102 | dd::Schema_impl *mysql; |
| 103 | |
| 104 | void lock_object(const dd::String_type &name) { |
| 105 | MDL_request mdl_request; |
| 106 | MDL_REQUEST_INIT(&mdl_request, MDL_key::TABLE, MYSQL_SCHEMA_NAME.str, |
| 107 | name.c_str(), MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 108 | EXPECT_FALSE(m_mdl_context.acquire_lock( |
| 109 | &mdl_request, thd()->variables.lock_wait_timeout)); |
| 110 | } |
| 111 | |
| 112 | protected: |
| 113 | CacheStorageTest() : mysql(nullptr) {} |
| 114 | |
| 115 | static void SetUpTestCase() { |
| 116 | mdl_init(); |
| 117 | table_def_init(); |
| 118 | } |
| 119 | |
| 120 | static void TearDownTestCase() { |
| 121 | dd::cache::Shared_dictionary_cache::shutdown(); |
| 122 | table_def_free(); |
| 123 | mdl_destroy(); |
| 124 | } |
| 125 | |
| 126 | void SetUp() override { |
| 127 | m_init.SetUp(); |
| 128 | // Mark this as a dd system thread to skip MDL checks/asserts in the dd |
| 129 | // cache. |
| 130 | thd()->system_thread = SYSTEM_THREAD_DD_INITIALIZE; |
| 131 | #ifndef NDEBUG |
| 132 | dd::cache::Storage_adapter::s_use_fake_storage = true; |
| 133 | #endif /* !NDEBUG */ |
| 134 | dd::cache::Dictionary_client::Auto_releaser releaser(thd()->dd_client()); |
| 135 | mysql = new dd::Schema_impl(); |
| 136 | mysql->set_name("mysql"); |
| 137 | EXPECT_FALSE(thd()->dd_client()->store<dd::Schema>(mysql)); |
| 138 | EXPECT_LT(9999u, mysql->id()); |
| 139 | thd()->dd_client()->commit_modified_objects(); |
| 140 | |
| 141 | mdl_locks_unused_locks_low_water = MDL_LOCKS_UNUSED_LOCKS_LOW_WATER_DEFAULT; |
| 142 | max_write_lock_count = ULONG_MAX; |
| 143 | m_mdl_context.init(this); |
| 144 | EXPECT_FALSE(m_mdl_context.has_locks()); |
| 145 | } |
| 146 | |
| 147 | void TearDown() override { |
| 148 | /* |
| 149 | Explicit scope + auto releaser to make sure acquired objects are |
| 150 | released before teardown of the thd. |
| 151 | */ |
| 152 | { |
| 153 | const dd::Schema *acquired_mysql = nullptr; |
| 154 | dd::cache::Dictionary_client::Auto_releaser releaser(thd()->dd_client()); |
| 155 | EXPECT_FALSE(thd()->dd_client()->acquire<dd::Schema>(mysql->id(), |
| 156 | &acquired_mysql)); |
| 157 | EXPECT_NE(nullp<const dd::Schema>(), acquired_mysql); |
| 158 | EXPECT_FALSE(thd()->dd_client()->drop(acquired_mysql)); |
| 159 | thd()->dd_client()->commit_modified_objects(); |
| 160 | } |
| 161 | delete mysql; |
| 162 | m_mdl_context.release_transactional_locks(); |
| 163 | m_mdl_context.destroy(); |
| 164 | #ifndef NDEBUG |
| 165 | dd::cache::Storage_adapter::s_use_fake_storage = false; |
| 166 | #endif /* !NDEBUG */ |
| 167 | m_init.TearDown(); |
| 168 | } |
| 169 | |
| 170 | void notify_shared_lock(MDL_context_owner *in_use, |
| 171 | bool needs_thr_lock_abort) override { |
| 172 | in_use->notify_shared_lock(nullptr, needs_thr_lock_abort); |
| 173 | } |
| 174 | |
| 175 | // Return dummy thd. |
| 176 | THD *thd() { return m_init.thd(); } |
| 177 | |
| 178 | my_testing::Server_initializer m_init; // Server initializer. |
| 179 | |
| 180 | MDL_context m_mdl_context; |
| 181 | MDL_request m_request; |
| 182 | |
| 183 | private: |
| 184 | CacheStorageTest(CacheStorageTest const &) = delete; |
| 185 | CacheStorageTest &operator=(CacheStorageTest const &) = delete; |
| 186 | }; |
| 187 | |
| 188 | template <typename T> |
| 189 | class CacheTest : public ::testing::Test { |
| 190 | protected: |
| 191 | void SetUp() override {} |
| 192 | |
| 193 | void TearDown() override {} |
| 194 | }; |
| 195 | |
| 196 | typedef ::testing::Types<dd::Charset_impl, dd::Collation_impl, |
| 197 | dd::Column_statistics_impl, dd::Schema_impl, |
| 198 | dd::Table_impl, dd::Tablespace_impl, dd::View_impl, |
| 199 | dd::Event_impl, dd::Procedure_impl> |
| 200 | DDTypes; |
| 201 | TYPED_TEST_SUITE(CacheTest, DDTypes); |
| 202 | |
| 203 | // Helper class to create objects and wrap them in elements. |
| 204 | class CacheTestHelper { |
| 205 | public: |
| 206 | template <typename T> |
| 207 | static std::vector<dd::cache::Cache_element<typename T::Cache_partition> *> |
| 208 | *create_elements(uint num) { |
| 209 | char name[2] = {'a', '\0'}; |
| 210 | std::vector<dd::cache::Cache_element<typename T::Cache_partition> *> |
| 211 | *objects = new std::vector< |
| 212 | dd::cache::Cache_element<typename T::Cache_partition> *>(); |
| 213 | for (uint id = 1; id <= num; id++, name[0]++) { |
| 214 | T *object = new T(); |
| 215 | object->set_id(id); |
| 216 | object->set_name(dd::String_type(name)); |
| 217 | dd::cache::Cache_element<typename T::Cache_partition> *element = |
| 218 | new dd::cache::Cache_element<typename T::Cache_partition>(); |
| 219 | element->set_object(object); |
| 220 | element->recreate_keys(); |
| 221 | objects->push_back(element); |
| 222 | } |
| 223 | return objects; |
| 224 | } |
| 225 | |
| 226 | template <typename T> |
| 227 | static void delete_elements( |
| 228 | std::vector<dd::cache::Cache_element<typename T::Cache_partition> *> |
| 229 | *objects) { |
| 230 | // Delete the objects and elements. |
| 231 | for (typename std::vector<dd::cache::Cache_element< |
| 232 | typename T::Cache_partition> *>::iterator it = objects->begin(); |
| 233 | it != objects->end();) { |
| 234 | // Careful about iterator invalidation. |
| 235 | dd::cache::Cache_element<typename T::Cache_partition> *element = *it; |
| 236 | it++; |
| 237 | delete (element->object()); |
| 238 | delete (element); |
| 239 | } |
| 240 | |
| 241 | // Delete the vector with the elements pointer. |
| 242 | objects->clear(); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | // Test the free list. |
| 247 | TYPED_TEST(CacheTest, FreeList) { |
| 248 | // Create a free list and assert that it is empty. |
| 249 | dd::cache::Free_list< |
| 250 | dd::cache::Cache_element<typename TypeParam::Cache_partition>> |
| 251 | free_list; |
| 252 | ASSERT_EQ(0U, free_list.length()); |
| 253 | |
| 254 | // Create objects and wrap them in elements, add to vector of pointers. |
| 255 | std::vector<dd::cache::Cache_element<typename TypeParam::Cache_partition> *> |
| 256 | *objects = CacheTestHelper::create_elements<TypeParam>(4); |
| 257 | |
| 258 | // Add the elements to the free list. |
| 259 | for (typename std::vector<dd::cache::Cache_element< |
| 260 | typename TypeParam::Cache_partition> *>::iterator it = |
| 261 | objects->begin(); |
| 262 | it != objects->end(); ++it) |
| 263 | free_list.add_last(*it); |
| 264 | |
| 265 | // Now, length should be 4. |
| 266 | ASSERT_EQ(4U, free_list.length()); |
| 267 | |
| 268 | // Retrieving the least recently used element should return the first one. |
| 269 | dd::cache::Cache_element<typename TypeParam::Cache_partition> *element = |
| 270 | free_list.get_lru(); |
| 271 | free_list.remove(element); |
| 272 | ASSERT_EQ(3U, free_list.length()); |
| 273 | ASSERT_EQ(1U, element->object()->id()); |
| 274 | ASSERT_EQ(dd::String_type("a"), element->object()->name()); |
| 275 | |
| 276 | // Now let us remove the middle of the remaining elements. |
| 277 | free_list.remove(objects->at(2)); |
| 278 | ASSERT_EQ(2U, free_list.length()); |
| 279 | |
| 280 | // Get the two last elements and verify they are what we expect. |
| 281 | element = free_list.get_lru(); |
| 282 | free_list.remove(element); |
| 283 | ASSERT_EQ(1U, free_list.length()); |
| 284 | ASSERT_EQ(2U, element->object()->id()); |
| 285 | ASSERT_EQ(dd::String_type("b"), element->object()->name()); |
| 286 | |
| 287 | element = free_list.get_lru(); |
| 288 | free_list.remove(element); |
| 289 | ASSERT_EQ(0U, free_list.length()); |
| 290 | ASSERT_EQ(4U, element->object()->id()); |
| 291 | ASSERT_EQ(dd::String_type("d"), element->object()->name()); |
| 292 | |
| 293 | // Cleanup. |
| 294 | CacheTestHelper::delete_elements<TypeParam>(objects); |
| 295 | delete objects; |
| 296 | } |
| 297 | |
| 298 | // Test the element map. Use a template function to do this for each |
| 299 | // of the key types, since we have already used the template support in |
| 300 | // gtest to handle the different object types. |
| 301 | template <typename T, typename K> |
| 302 | void element_map_test() { |
| 303 | // Create an element map and assert that it is empty. |
| 304 | dd::cache::Element_map<K, |
| 305 | dd::cache::Cache_element<typename T::Cache_partition>> |
| 306 | element_map; |
| 307 | ASSERT_EQ(0U, element_map.size()); |
| 308 | |
| 309 | // Create objects and wrap them in elements. |
| 310 | std::vector<dd::cache::Cache_element<typename T::Cache_partition> *> |
| 311 | *objects = CacheTestHelper::create_elements<T>(4); |
| 312 | |
| 313 | // Add the elements to the map. |
| 314 | for (typename std::vector<dd::cache::Cache_element< |
| 315 | typename T::Cache_partition> *>::iterator it = objects->begin(); |
| 316 | it != objects->end(); ++it) { |
| 317 | // Template disambiguator necessary. |
| 318 | const K *key = (*it)->template get_key<K>(); |
| 319 | if (key) element_map.put(*key, *it); |
| 320 | } |
| 321 | |
| 322 | // Now, the map should contain 4 elements. |
| 323 | ASSERT_EQ(4U, element_map.size()); |
| 324 | |
| 325 | // For each of the elements, make sure they are present, and that we |
| 326 | // get the same element in return. |
| 327 | // Add the elements to the map. |
| 328 | for (typename std::vector<dd::cache::Cache_element< |
| 329 | typename T::Cache_partition> *>::iterator it = objects->begin(); |
| 330 | it != objects->end(); ++it) { |
| 331 | // Template disambiguator necessary. |
| 332 | const K *key = (*it)->template get_key<K>(); |
| 333 | if (key) { |
| 334 | ASSERT_TRUE(element_map.is_present(*key)); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Remove an element, and make sure the key |
| 339 | |
| 340 | // Delete the array and the objects. |
| 341 | CacheTestHelper::delete_elements<T>(objects); |
| 342 | delete objects; |
| 343 | } |
| 344 | |
| 345 | TYPED_TEST(CacheTest, Element_map_reverse) { |
| 346 | element_map_test<TypeParam, const typename TypeParam::Cache_partition *>(); |
| 347 | } |
| 348 | |
| 349 | TYPED_TEST(CacheTest, Element_map_id_key) { |
| 350 | element_map_test<TypeParam, typename TypeParam::Id_key>(); |
| 351 | } |
| 352 | |
| 353 | TYPED_TEST(CacheTest, Element_map_name_key) { |
| 354 | element_map_test<TypeParam, typename TypeParam::Name_key>(); |
| 355 | } |
| 356 | |
| 357 | TYPED_TEST(CacheTest, Element_map_aux_key) { |
| 358 | // The aux key behavior is not uniform, and this test is therefore omitted. |
| 359 | } |
| 360 | |
| 361 | #ifndef NDEBUG |
| 362 | template <typename Intrfc_type, typename Impl_type> |
| 363 | void test_basic_store_and_get(CacheStorageTest *tst, THD *thd) { |
| 364 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 365 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 366 | |
| 367 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 368 | Intrfc_type *icreated = created.get(); |
| 369 | dd_unittest::set_attributes(created.get(), "global_test_object"); |
| 370 | |
| 371 | tst->lock_object(created->name()); |
| 372 | EXPECT_FALSE(dc->store(icreated)); |
| 373 | EXPECT_LT(9999u, icreated->id()); |
| 374 | |
| 375 | // Acquire by id. |
| 376 | const Intrfc_type *acquired = nullptr; |
| 377 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 378 | EXPECT_NE(nullp<Intrfc_type>(), acquired); |
| 379 | EXPECT_NE(icreated, acquired); |
| 380 | EXPECT_EQ(*icreated, *acquired); |
| 381 | |
| 382 | const Intrfc_type *name_acquired = nullptr; |
| 383 | EXPECT_FALSE(dc->acquire(icreated->name(), &name_acquired)); |
| 384 | EXPECT_NE(nullp<Intrfc_type>(), name_acquired); |
| 385 | EXPECT_EQ(acquired, name_acquired); |
| 386 | |
| 387 | EXPECT_FALSE(dc->drop(acquired)); |
| 388 | dc->commit_modified_objects(); |
| 389 | } |
| 390 | |
| 391 | TEST_F(CacheStorageTest, BasicStoreAndGetCharset) { |
| 392 | test_basic_store_and_get<dd::Charset, dd::Charset_impl>(this, thd()); |
| 393 | } |
| 394 | |
| 395 | TEST_F(CacheStorageTest, BasicStoreAndGetCollation) { |
| 396 | test_basic_store_and_get<dd::Collation, dd::Collation_impl>(this, thd()); |
| 397 | } |
| 398 | |
| 399 | TEST_F(CacheStorageTest, BasicStoreAndGetColumnStatistics) { |
| 400 | test_basic_store_and_get<dd::Column_statistics, dd::Column_statistics_impl>( |
| 401 | this, thd()); |
| 402 | } |
| 403 | |
| 404 | TEST_F(CacheStorageTest, BasicStoreAndGetColumnStatisticWithCloneFail) { |
| 405 | // Verify that we don't crash even if cloning of the histogram fails. |
| 406 | DBUG_SET("+d,fail_histogram_clone"); |
| 407 | test_basic_store_and_get<dd::Column_statistics, dd::Column_statistics_impl>( |
| 408 | this, thd()); |
| 409 | DBUG_SET("-d,fail_histogram_clone"); |
| 410 | } |
| 411 | |
| 412 | TEST_F(CacheStorageTest, BasicStoreAndGetSchema) { |
| 413 | test_basic_store_and_get<dd::Schema, dd::Schema_impl>(this, thd()); |
| 414 | } |
| 415 | |
| 416 | TEST_F(CacheStorageTest, BasicStoreAndGetTablespace) { |
| 417 | test_basic_store_and_get<dd::Tablespace, dd::Tablespace_impl>(this, thd()); |
| 418 | } |
| 419 | |
| 420 | template <typename Intrfc_type, typename Impl_type> |
| 421 | void test_basic_store_and_get_with_schema(CacheStorageTest *tst, THD *thd) { |
| 422 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 423 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 424 | |
| 425 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 426 | Intrfc_type *icreated = created.get(); |
| 427 | created->set_schema_id(tst->mysql->id()); |
| 428 | dd_unittest::set_attributes(created.get(), "schema_qualified_test_object", |
| 429 | *tst->mysql); |
| 430 | |
| 431 | tst->lock_object(created->name()); |
| 432 | EXPECT_FALSE(dc->store(icreated)); |
| 433 | EXPECT_LT(9999u, icreated->id()); |
| 434 | |
| 435 | // Acquire by id. |
| 436 | const Intrfc_type *acquired = nullptr; |
| 437 | EXPECT_FALSE( |
| 438 | thd->dd_client()->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 439 | EXPECT_NE(nullp<Intrfc_type>(), acquired); |
| 440 | EXPECT_NE(icreated, acquired); |
| 441 | EXPECT_EQ(*icreated, *acquired); |
| 442 | |
| 443 | // Acquire by schema-qualified name. |
| 444 | const Intrfc_type *name_acquired = nullptr; |
| 445 | EXPECT_FALSE( |
| 446 | dc->acquire(tst->mysql->name(), icreated->name(), &name_acquired)); |
| 447 | EXPECT_NE(nullp<Intrfc_type>(), name_acquired); |
| 448 | EXPECT_EQ(acquired, name_acquired); |
| 449 | |
| 450 | EXPECT_FALSE(dc->drop(acquired)); |
| 451 | dc->commit_modified_objects(); |
| 452 | } |
| 453 | |
| 454 | TEST_F(CacheStorageTest, BasicStoreAndGetTable) { |
| 455 | test_basic_store_and_get_with_schema<dd::Table, dd::Table_impl>(this, thd()); |
| 456 | } |
| 457 | |
| 458 | TEST_F(CacheStorageTest, BasicStoreAndGetView) { |
| 459 | test_basic_store_and_get_with_schema<dd::View, dd::View_impl>(this, thd()); |
| 460 | } |
| 461 | |
| 462 | TEST_F(CacheStorageTest, BasicStoreAndGetEvent) { |
| 463 | test_basic_store_and_get_with_schema<dd::Event, dd::Event_impl>(this, thd()); |
| 464 | } |
| 465 | |
| 466 | TEST_F(CacheStorageTest, BasicStoreAndGetRoutine) { |
| 467 | test_basic_store_and_get_with_schema<dd::Procedure, dd::Procedure_impl>( |
| 468 | this, thd()); |
| 469 | } |
| 470 | |
| 471 | template <typename Intrfc_type, typename Impl_type> |
| 472 | void test_acquire_for_modification(CacheStorageTest *tst, THD *thd) { |
| 473 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 474 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 475 | |
| 476 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 477 | Intrfc_type *icreated = created.get(); |
| 478 | dd_unittest::set_attributes(created.get(), "global_test_object"); |
| 479 | |
| 480 | tst->lock_object(created->name()); |
| 481 | EXPECT_FALSE(dc->store(icreated)); |
| 482 | EXPECT_LT(9999u, icreated->id()); |
| 483 | |
| 484 | const Intrfc_type *acquired = nullptr; |
| 485 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 486 | |
| 487 | // Acquire by id. |
| 488 | Intrfc_type *modified = nullptr; |
| 489 | { |
| 490 | dd::cache::Dictionary_client::Auto_releaser releaser2(dc); |
| 491 | |
| 492 | EXPECT_FALSE( |
| 493 | dc->acquire_for_modification<Intrfc_type>(icreated->id(), &modified)); |
| 494 | EXPECT_NE(nullp<Intrfc_type>(), modified); |
| 495 | |
| 496 | // acquire should return the same object |
| 497 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 498 | EXPECT_NE(nullp<Intrfc_type>(), acquired); |
| 499 | EXPECT_NE(modified, acquired); |
| 500 | EXPECT_EQ(*modified, *acquired); |
| 501 | } |
| 502 | // acquire should still return the same object |
| 503 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 504 | EXPECT_NE(nullp<Intrfc_type>(), acquired); |
| 505 | EXPECT_NE(modified, acquired); |
| 506 | EXPECT_EQ(*modified, *acquired); |
| 507 | |
| 508 | dc->commit_modified_objects(); |
| 509 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 510 | EXPECT_FALSE(dc->drop(acquired)); |
| 511 | dc->commit_modified_objects(); |
| 512 | } |
| 513 | |
| 514 | TEST_F(CacheStorageTest, AquireForModificationCharset) { |
| 515 | test_acquire_for_modification<dd::Charset, dd::Charset_impl>(this, thd()); |
| 516 | } |
| 517 | |
| 518 | TEST_F(CacheStorageTest, AquireForModificationCollation) { |
| 519 | test_acquire_for_modification<dd::Collation, dd::Collation_impl>(this, thd()); |
| 520 | } |
| 521 | |
| 522 | TEST_F(CacheStorageTest, AquireForModificationColumnStatistic) { |
| 523 | test_acquire_for_modification<dd::Column_statistics, |
| 524 | dd::Column_statistics_impl>(this, thd()); |
| 525 | } |
| 526 | |
| 527 | TEST_F(CacheStorageTest, AquireForModificationSchema) { |
| 528 | test_acquire_for_modification<dd::Schema, dd::Schema_impl>(this, thd()); |
| 529 | } |
| 530 | |
| 531 | TEST_F(CacheStorageTest, AquireForModificationTablespace) { |
| 532 | test_acquire_for_modification<dd::Tablespace, dd::Tablespace_impl>(this, |
| 533 | thd()); |
| 534 | } |
| 535 | |
| 536 | template <typename Intrfc_type, typename Impl_type> |
| 537 | void test_acquire_for_modification_with_schema(CacheStorageTest *tst, |
| 538 | THD *thd) { |
| 539 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 540 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 541 | |
| 542 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 543 | Intrfc_type *icreated = created.get(); |
| 544 | created->set_schema_id(tst->mysql->id()); |
| 545 | dd_unittest::set_attributes(created.get(), "schema_qualified_test_object", |
| 546 | *tst->mysql); |
| 547 | |
| 548 | tst->lock_object(created->name()); |
| 549 | EXPECT_FALSE(dc->store(icreated)); |
| 550 | EXPECT_LT(9999u, icreated->id()); |
| 551 | |
| 552 | const Intrfc_type *acquired = nullptr; |
| 553 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 554 | |
| 555 | // Acquire by id. |
| 556 | Intrfc_type *modified = nullptr; |
| 557 | { |
| 558 | dd::cache::Dictionary_client::Auto_releaser releaser2(dc); |
| 559 | |
| 560 | EXPECT_FALSE( |
| 561 | dc->acquire_for_modification<Intrfc_type>(icreated->id(), &modified)); |
| 562 | EXPECT_NE(nullp<Intrfc_type>(), modified); |
| 563 | |
| 564 | // acquire should return the same object |
| 565 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 566 | EXPECT_NE(nullp<Intrfc_type>(), acquired); |
| 567 | EXPECT_NE(modified, acquired); |
| 568 | EXPECT_EQ(*modified, *acquired); |
| 569 | } |
| 570 | // acquire should still return the same object |
| 571 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 572 | EXPECT_NE(nullp<Intrfc_type>(), acquired); |
| 573 | EXPECT_NE(modified, acquired); |
| 574 | EXPECT_EQ(*modified, *acquired); |
| 575 | |
| 576 | dc->commit_modified_objects(); |
| 577 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 578 | EXPECT_FALSE(dc->drop(acquired)); |
| 579 | dc->commit_modified_objects(); |
| 580 | } |
| 581 | |
| 582 | TEST_F(CacheStorageTest, AcquireForModificationTable) { |
| 583 | test_acquire_for_modification_with_schema<dd::Table, dd::Table_impl>(this, |
| 584 | thd()); |
| 585 | } |
| 586 | |
| 587 | TEST_F(CacheStorageTest, AcquireForModificationView) { |
| 588 | test_acquire_for_modification_with_schema<dd::View, dd::View_impl>(this, |
| 589 | thd()); |
| 590 | } |
| 591 | |
| 592 | TEST_F(CacheStorageTest, AcquireForModificationEvent) { |
| 593 | test_acquire_for_modification_with_schema<dd::Event, dd::Event_impl>(this, |
| 594 | thd()); |
| 595 | } |
| 596 | |
| 597 | TEST_F(CacheStorageTest, AcquireForModificationProcedure) { |
| 598 | test_acquire_for_modification_with_schema<dd::Procedure, dd::Procedure_impl>( |
| 599 | this, thd()); |
| 600 | } |
| 601 | |
| 602 | template <typename Intrfc_type, typename Impl_type> |
| 603 | void test_acquire_and_rename(CacheStorageTest *tst, THD *thd) { |
| 604 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 605 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 606 | |
| 607 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 608 | Intrfc_type *icreated = created.get(); |
| 609 | dd_unittest::set_attributes(created.get(), "old_name"); |
| 610 | |
| 611 | tst->lock_object(created->name()); |
| 612 | EXPECT_FALSE(dc->store(icreated)); |
| 613 | EXPECT_LT(9999u, icreated->id()); |
| 614 | |
| 615 | const Intrfc_type *old_const = nullptr; |
| 616 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &old_const)); |
| 617 | Intrfc_type *old_modified = nullptr; |
| 618 | EXPECT_FALSE( |
| 619 | dc->acquire_for_modification<Intrfc_type>(icreated->id(), &old_modified)); |
| 620 | |
| 621 | dd_unittest::set_attributes(old_modified, "new_name"); |
| 622 | EXPECT_FALSE(dc->update(old_modified)); |
| 623 | |
| 624 | // Should be possible to acquire with the new name. |
| 625 | Intrfc_type *new_modified = nullptr; |
| 626 | EXPECT_FALSE(dc->acquire_for_modification<Intrfc_type>(old_modified->name(), |
| 627 | &new_modified)); |
| 628 | EXPECT_NE(nullp<Intrfc_type>(), new_modified); |
| 629 | EXPECT_NE(new_modified, old_modified); |
| 630 | EXPECT_EQ(*new_modified, *old_modified); |
| 631 | |
| 632 | const Intrfc_type *new_object = nullptr; |
| 633 | EXPECT_FALSE(dc->acquire<Intrfc_type>(old_modified->name(), &new_object)); |
| 634 | EXPECT_NE(nullp<Intrfc_type>(), new_object); |
| 635 | EXPECT_EQ(new_object, old_modified); // equal due to update() above. |
| 636 | EXPECT_EQ(*new_object, *old_modified); |
| 637 | |
| 638 | // But not by the old object name. |
| 639 | EXPECT_FALSE( |
| 640 | dc->acquire_for_modification<Intrfc_type>("old_name", &old_modified)); |
| 641 | EXPECT_EQ(nullp<Intrfc_type>(), old_modified); |
| 642 | |
| 643 | EXPECT_FALSE(dc->acquire<Intrfc_type>("old_name", &old_const)); |
| 644 | EXPECT_EQ(nullp<Intrfc_type>(), old_const); |
| 645 | |
| 646 | dc->commit_modified_objects(); |
| 647 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &new_object)); |
| 648 | EXPECT_FALSE(dc->drop(new_object)); |
| 649 | dc->commit_modified_objects(); |
| 650 | } |
| 651 | |
| 652 | TEST_F(CacheStorageTest, AquireAndRenameCharset) { |
| 653 | test_acquire_and_rename<dd::Charset, dd::Charset_impl>(this, thd()); |
| 654 | } |
| 655 | |
| 656 | TEST_F(CacheStorageTest, AquireAndRenameCollation) { |
| 657 | test_acquire_and_rename<dd::Collation, dd::Collation_impl>(this, thd()); |
| 658 | } |
| 659 | |
| 660 | TEST_F(CacheStorageTest, AquireAndRenameColumnStatistic) { |
| 661 | test_acquire_and_rename<dd::Column_statistics, dd::Column_statistics_impl>( |
| 662 | this, thd()); |
| 663 | } |
| 664 | |
| 665 | TEST_F(CacheStorageTest, AquireAndRenameSchema) { |
| 666 | test_acquire_and_rename<dd::Schema, dd::Schema_impl>(this, thd()); |
| 667 | } |
| 668 | |
| 669 | TEST_F(CacheStorageTest, AquireAndRenameTablespace) { |
| 670 | test_acquire_and_rename<dd::Tablespace, dd::Tablespace_impl>(this, thd()); |
| 671 | } |
| 672 | |
| 673 | template <typename Intrfc_type, typename Impl_type> |
| 674 | void test_acquire_and_rename_with_schema(CacheStorageTest *tst, THD *thd) { |
| 675 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 676 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 677 | |
| 678 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 679 | Intrfc_type *icreated = created.get(); |
| 680 | created->set_schema_id(tst->mysql->id()); |
| 681 | dd_unittest::set_attributes(created.get(), "schema_old_name", *tst->mysql); |
| 682 | |
| 683 | tst->lock_object(created->name()); |
| 684 | EXPECT_FALSE(dc->store(icreated)); |
| 685 | EXPECT_LT(9999u, icreated->id()); |
| 686 | |
| 687 | const Intrfc_type *old_const = nullptr; |
| 688 | EXPECT_FALSE(dc->acquire(icreated->id(), &old_const)); |
| 689 | Intrfc_type *old_modified = nullptr; |
| 690 | EXPECT_FALSE(dc->acquire_for_modification(icreated->id(), &old_modified)); |
| 691 | |
| 692 | dd_unittest::set_attributes(old_modified, "schema_new_name", *tst->mysql); |
| 693 | EXPECT_FALSE(dc->update(old_modified)); |
| 694 | |
| 695 | // Should be possible to acquire with the new name. |
| 696 | Intrfc_type *new_modified = nullptr; |
| 697 | EXPECT_FALSE(dc->acquire_for_modification( |
| 698 | tst->mysql->name(), old_modified->name(), &new_modified)); |
| 699 | EXPECT_NE(nullp<Intrfc_type>(), new_modified); |
| 700 | EXPECT_NE(new_modified, old_modified); |
| 701 | EXPECT_EQ(*new_modified, *old_modified); |
| 702 | |
| 703 | const Intrfc_type *new_object = nullptr; |
| 704 | EXPECT_FALSE( |
| 705 | dc->acquire(tst->mysql->name(), old_modified->name(), &new_object)); |
| 706 | EXPECT_NE(nullp<Intrfc_type>(), new_object); |
| 707 | EXPECT_EQ(new_object, old_modified); |
| 708 | EXPECT_EQ(*new_object, *old_modified); |
| 709 | |
| 710 | // But not by the old object name. |
| 711 | EXPECT_FALSE(dc->acquire_for_modification(tst->mysql->name(), |
| 712 | "schema_old_name", &old_modified)); |
| 713 | EXPECT_EQ(nullp<Intrfc_type>(), old_modified); |
| 714 | |
| 715 | EXPECT_FALSE(dc->acquire(tst->mysql->name(), "schema_old_name", &old_const)); |
| 716 | EXPECT_EQ(nullp<Intrfc_type>(), old_const); |
| 717 | |
| 718 | dc->commit_modified_objects(); |
| 719 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &new_object)); |
| 720 | EXPECT_FALSE(dc->drop(new_object)); |
| 721 | dc->commit_modified_objects(); |
| 722 | } |
| 723 | |
| 724 | TEST_F(CacheStorageTest, AcquireAndRenameTable) { |
| 725 | test_acquire_and_rename_with_schema<dd::Table, dd::Table_impl>(this, thd()); |
| 726 | } |
| 727 | |
| 728 | TEST_F(CacheStorageTest, AcquireAndRenameView) { |
| 729 | test_acquire_and_rename_with_schema<dd::View, dd::View_impl>(this, thd()); |
| 730 | } |
| 731 | |
| 732 | TEST_F(CacheStorageTest, AcquireAndRenameEvent) { |
| 733 | test_acquire_and_rename_with_schema<dd::Event, dd::Event_impl>(this, thd()); |
| 734 | } |
| 735 | |
| 736 | TEST_F(CacheStorageTest, AcquireAndRenameProcedure) { |
| 737 | test_acquire_and_rename_with_schema<dd::Procedure, dd::Procedure_impl>(this, |
| 738 | thd()); |
| 739 | } |
| 740 | |
| 741 | template <typename Intrfc_type, typename Impl_type> |
| 742 | void test_acquire_and_move(CacheStorageTest *tst, THD *thd) { |
| 743 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 744 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 745 | |
| 746 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 747 | Intrfc_type *icreated = created.get(); |
| 748 | created->set_schema_id(tst->mysql->id()); |
| 749 | dd_unittest::set_attributes(created.get(), "schema_name", *tst->mysql); |
| 750 | |
| 751 | tst->lock_object(created->name()); |
| 752 | EXPECT_FALSE(dc->store(icreated)); |
| 753 | EXPECT_LT(9999u, icreated->id()); |
| 754 | |
| 755 | dd::Schema_impl *new_schema = new dd::Schema_impl(); |
| 756 | new_schema->set_name("schema1"); |
| 757 | EXPECT_FALSE(dc->store<dd::Schema>(new_schema)); |
| 758 | EXPECT_LT(9999u, new_schema->id()); |
| 759 | |
| 760 | const Intrfc_type *old_const = nullptr; |
| 761 | EXPECT_FALSE(dc->acquire(icreated->id(), &old_const)); |
| 762 | Intrfc_type *old_modified = nullptr; |
| 763 | EXPECT_FALSE(dc->acquire_for_modification(icreated->id(), &old_modified)); |
| 764 | |
| 765 | // Move object to a new schema, but keep object name. |
| 766 | dd_unittest::set_attributes(old_modified, created->name(), *new_schema); |
| 767 | EXPECT_FALSE(dc->update(old_modified)); |
| 768 | |
| 769 | // Should be possible to acquire in the new schema. |
| 770 | Intrfc_type *new_modified = nullptr; |
| 771 | EXPECT_FALSE(dc->acquire_for_modification(new_schema->name(), created->name(), |
| 772 | &new_modified)); |
| 773 | EXPECT_NE(nullp<Intrfc_type>(), new_modified); |
| 774 | EXPECT_NE(new_modified, old_modified); |
| 775 | EXPECT_EQ(*new_modified, *old_modified); |
| 776 | |
| 777 | const Intrfc_type *new_object = nullptr; |
| 778 | EXPECT_FALSE( |
| 779 | dc->acquire(new_schema->name(), old_modified->name(), &new_object)); |
| 780 | EXPECT_NE(nullp<Intrfc_type>(), new_object); |
| 781 | EXPECT_EQ(new_object, old_modified); |
| 782 | EXPECT_EQ(*new_object, *old_modified); |
| 783 | |
| 784 | // But not in the old schema. |
| 785 | EXPECT_FALSE(dc->acquire_for_modification(tst->mysql->name(), created->name(), |
| 786 | &old_modified)); |
| 787 | EXPECT_EQ(nullp<Intrfc_type>(), old_modified); |
| 788 | |
| 789 | EXPECT_FALSE(dc->acquire(tst->mysql->name(), created->name(), &old_const)); |
| 790 | EXPECT_EQ(nullp<Intrfc_type>(), old_const); |
| 791 | |
| 792 | dc->commit_modified_objects(); |
| 793 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &new_object)); |
| 794 | EXPECT_FALSE(dc->drop(new_object)); |
| 795 | |
| 796 | // Cleanup: Acquire and delete the new schema. |
| 797 | const dd::Schema *s = nullptr; |
| 798 | EXPECT_FALSE(dc->acquire(new_schema->id(), &s)); |
| 799 | EXPECT_FALSE(dc->drop(s)); |
| 800 | dc->commit_modified_objects(); |
| 801 | delete new_schema; |
| 802 | } |
| 803 | |
| 804 | TEST_F(CacheStorageTest, AcquireAndMoveTable) { |
| 805 | test_acquire_and_move<dd::Table, dd::Table_impl>(this, thd()); |
| 806 | } |
| 807 | |
| 808 | TEST_F(CacheStorageTest, AcquireAndMoveView) { |
| 809 | test_acquire_and_move<dd::View, dd::View_impl>(this, thd()); |
| 810 | } |
| 811 | |
| 812 | TEST_F(CacheStorageTest, AcquireAndMoveEvent) { |
| 813 | test_acquire_and_move<dd::Event, dd::Event_impl>(this, thd()); |
| 814 | } |
| 815 | |
| 816 | TEST_F(CacheStorageTest, AcquireAndMoveProcedure) { |
| 817 | test_acquire_and_move<dd::Procedure, dd::Procedure_impl>(this, thd()); |
| 818 | } |
| 819 | |
| 820 | template <typename Intrfc_type, typename Impl_type> |
| 821 | void test_acquire_and_drop(CacheStorageTest *tst, THD *thd) { |
| 822 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 823 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 824 | |
| 825 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 826 | Intrfc_type *icreated = created.get(); |
| 827 | dd_unittest::set_attributes(created.get(), "drop_test_object"); |
| 828 | |
| 829 | tst->lock_object(created->name()); |
| 830 | EXPECT_FALSE(dc->store(icreated)); |
| 831 | EXPECT_LT(9999u, icreated->id()); |
| 832 | |
| 833 | const Intrfc_type *acquired = nullptr; |
| 834 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->id(), &acquired)); |
| 835 | |
| 836 | Intrfc_type *modified = nullptr; |
| 837 | EXPECT_FALSE( |
| 838 | dc->acquire_for_modification<Intrfc_type>(icreated->id(), &modified)); |
| 839 | |
| 840 | EXPECT_FALSE(dc->drop(acquired)); |
| 841 | |
| 842 | // Should not be possible to acquire |
| 843 | EXPECT_FALSE(dc->acquire<Intrfc_type>(icreated->name(), &acquired)); |
| 844 | EXPECT_EQ(nullp<Intrfc_type>(), acquired); |
| 845 | EXPECT_FALSE( |
| 846 | dc->acquire_for_modification<Intrfc_type>(icreated->name(), &modified)); |
| 847 | EXPECT_EQ(nullp<Intrfc_type>(), modified); |
| 848 | dc->commit_modified_objects(); |
| 849 | } |
| 850 | |
| 851 | TEST_F(CacheStorageTest, AquireAndDropCharset) { |
| 852 | test_acquire_and_drop<dd::Charset, dd::Charset_impl>(this, thd()); |
| 853 | } |
| 854 | |
| 855 | TEST_F(CacheStorageTest, AquireAndDropCollation) { |
| 856 | test_acquire_and_drop<dd::Collation, dd::Collation_impl>(this, thd()); |
| 857 | } |
| 858 | |
| 859 | TEST_F(CacheStorageTest, AquireAndDropColumnStatistic) { |
| 860 | test_acquire_and_drop<dd::Column_statistics, dd::Column_statistics_impl>( |
| 861 | this, thd()); |
| 862 | } |
| 863 | |
| 864 | TEST_F(CacheStorageTest, AquireAndDropSchema) { |
| 865 | test_acquire_and_drop<dd::Schema, dd::Schema_impl>(this, thd()); |
| 866 | } |
| 867 | |
| 868 | TEST_F(CacheStorageTest, AquireAndDropTablespace) { |
| 869 | test_acquire_and_drop<dd::Tablespace, dd::Tablespace_impl>(this, thd()); |
| 870 | } |
| 871 | |
| 872 | template <typename Intrfc_type, typename Impl_type> |
| 873 | void test_acquire_and_drop_with_schema(CacheStorageTest *tst, THD *thd) { |
| 874 | dd::cache::Dictionary_client *dc = thd->dd_client(); |
| 875 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 876 | |
| 877 | std::unique_ptr<Impl_type> created(new Impl_type()); |
| 878 | Intrfc_type *icreated = created.get(); |
| 879 | created->set_schema_id(tst->mysql->id()); |
| 880 | dd_unittest::set_attributes(created.get(), "schema_drop_test_object", |
| 881 | *tst->mysql); |
| 882 | |
| 883 | tst->lock_object(created->name()); |
| 884 | EXPECT_FALSE(dc->store(icreated)); |
| 885 | EXPECT_LT(9999u, icreated->id()); |
| 886 | |
| 887 | const Intrfc_type *acquired = nullptr; |
| 888 | EXPECT_FALSE(dc->acquire(icreated->id(), &acquired)); |
| 889 | |
| 890 | Intrfc_type *modified = nullptr; |
| 891 | EXPECT_FALSE(dc->acquire_for_modification(icreated->id(), &modified)); |
| 892 | |
| 893 | EXPECT_FALSE(dc->drop(acquired)); |
| 894 | |
| 895 | // Should not be possible to acquire |
| 896 | EXPECT_FALSE(dc->acquire(tst->mysql->name(), icreated->name(), &acquired)); |
| 897 | EXPECT_EQ(nullp<Intrfc_type>(), acquired); |
| 898 | EXPECT_FALSE(dc->acquire_for_modification(tst->mysql->name(), |
| 899 | icreated->name(), &modified)); |
| 900 | EXPECT_EQ(nullp<Intrfc_type>(), modified); |
| 901 | dc->commit_modified_objects(); |
| 902 | } |
| 903 | |
| 904 | TEST_F(CacheStorageTest, AcquireAndDropTable) { |
| 905 | test_acquire_and_drop_with_schema<dd::Table, dd::Table_impl>(this, thd()); |
| 906 | } |
| 907 | |
| 908 | TEST_F(CacheStorageTest, AcquireAndDropView) { |
| 909 | test_acquire_and_drop_with_schema<dd::View, dd::View_impl>(this, thd()); |
| 910 | } |
| 911 | |
| 912 | TEST_F(CacheStorageTest, AcquireAndDropEvent) { |
| 913 | test_acquire_and_drop_with_schema<dd::Event, dd::Event_impl>(this, thd()); |
| 914 | } |
| 915 | |
| 916 | TEST_F(CacheStorageTest, AcquireAndDropProcedure) { |
| 917 | test_acquire_and_drop_with_schema<dd::Procedure, dd::Procedure_impl>(this, |
| 918 | thd()); |
| 919 | } |
| 920 | |
| 921 | TEST_F(CacheStorageTest, CommitNewObject) { |
| 922 | dd::cache::Dictionary_client *dc = thd()->dd_client(); |
| 923 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 924 | |
| 925 | dd::Table_impl *created = new dd::Table_impl(); |
| 926 | dd::Table *icreated = created; |
| 927 | created->set_schema_id(mysql->id()); |
| 928 | dd_unittest::set_attributes(created, "new_object", *mysql); |
| 929 | lock_object(created->name()); |
| 930 | EXPECT_FALSE(dc->store(icreated)); |
| 931 | EXPECT_LT(9999u, icreated->id()); |
| 932 | |
| 933 | dc->commit_modified_objects(); |
| 934 | delete created; |
| 935 | } |
| 936 | |
| 937 | TEST_F(CacheStorageTest, DoubleUpdate) { |
| 938 | dd::cache::Dictionary_client *dc = thd()->dd_client(); |
| 939 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 940 | |
| 941 | std::unique_ptr<dd::Table> created(new dd::Table_impl()); |
| 942 | created->set_name("old_name"); |
| 943 | created->set_engine("innodb"); |
| 944 | lock_object(created->name()); |
| 945 | EXPECT_FALSE(dc->store(created.get())); |
| 946 | EXPECT_LT(9999u, created->id()); |
| 947 | |
| 948 | dd::Table *new_obj = nullptr; |
| 949 | EXPECT_FALSE(dc->acquire_for_modification(created->id(), &new_obj)); |
| 950 | |
| 951 | new_obj->set_name("new name"); |
| 952 | EXPECT_FALSE(dc->update(new_obj)); |
| 953 | |
| 954 | new_obj->set_name("newer name"); |
| 955 | EXPECT_FALSE(dc->update(new_obj)); |
| 956 | |
| 957 | dc->commit_modified_objects(); |
| 958 | } |
| 959 | |
| 960 | TEST_F(CacheStorageTest, DuplicateSePrivateId) { |
| 961 | dd::cache::Dictionary_client *dc = thd()->dd_client(); |
| 962 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 963 | |
| 964 | std::unique_ptr<dd::Table> ibd_tab(dd::create_object<dd::Table>()); |
| 965 | dd_unittest::set_attributes(ibd_tab.get(), "innodb_table_object", *mysql); |
| 966 | ibd_tab->set_engine("innodb"); |
| 967 | ibd_tab->set_se_private_id(123); |
| 968 | lock_object(ibd_tab->name()); |
| 969 | |
| 970 | std::unique_ptr<dd::Table> ndb_tab(dd::create_object<dd::Table>()); |
| 971 | dd_unittest::set_attributes(ndb_tab.get(), "ndb_table_object", *mysql); |
| 972 | ndb_tab->set_engine("ndb"); |
| 973 | ndb_tab->set_se_private_id(123); |
| 974 | lock_object(ndb_tab->name()); |
| 975 | |
| 976 | EXPECT_FALSE(dc->store(ibd_tab.get())); |
| 977 | EXPECT_FALSE(dc->store(ndb_tab.get())); |
| 978 | |
| 979 | // Acquire and drop the objects |
| 980 | const dd::Table *ibd_tab_ptr = nullptr; |
| 981 | EXPECT_FALSE( |
| 982 | dc->acquire<dd::Table>("mysql", "innodb_table_object", &ibd_tab_ptr)); |
| 983 | EXPECT_NE(nullp<const dd::Table>(), ibd_tab_ptr); |
| 984 | EXPECT_FALSE(dc->drop(ibd_tab_ptr)); |
| 985 | |
| 986 | const dd::Table *ndb_tab_ptr = nullptr; |
| 987 | EXPECT_FALSE( |
| 988 | dc->acquire<dd::Table>("mysql", "ndb_table_object", &ndb_tab_ptr)); |
| 989 | EXPECT_NE(nullp<const dd::Table>(), ndb_tab_ptr); |
| 990 | EXPECT_FALSE(dc->drop(ndb_tab_ptr)); |
| 991 | |
| 992 | dc->commit_modified_objects(); |
| 993 | } |
| 994 | |
| 995 | TEST_F(CacheStorageTest, GetTableBySePrivateId) { |
| 996 | dd::cache::Dictionary_client *dc = thd()->dd_client(); |
| 997 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 998 | |
| 999 | std::unique_ptr<dd::Table> obj(dd::create_object<dd::Table>()); |
| 1000 | dd_unittest::set_attributes(obj.get(), "table_object", *mysql); |
| 1001 | obj->set_engine("innodb"); |
| 1002 | obj->set_se_private_id(0xEEEE); // Storing some magic number |
| 1003 | |
| 1004 | dd::Partition *part_obj = obj->add_partition(); |
| 1005 | part_obj->set_name("table_part2"); |
| 1006 | part_obj->set_se_private_id(0xAFFF); |
| 1007 | part_obj->set_engine("innodb"); |
| 1008 | part_obj->set_number(3); |
| 1009 | part_obj->set_comment("Partition comment"); |
| 1010 | part_obj->set_tablespace_id(1); |
| 1011 | |
| 1012 | lock_object(obj->name()); |
| 1013 | EXPECT_FALSE(dc->store(obj.get())); |
| 1014 | |
| 1015 | dd::String_type schema_name; |
| 1016 | dd::String_type table_name; |
| 1017 | |
| 1018 | EXPECT_FALSE(dc->get_table_name_by_se_private_id("innodb", 0xEEEE, |
| 1019 | &schema_name, &table_name)); |
| 1020 | EXPECT_EQ(dd::String_type("mysql"), schema_name); |
| 1021 | EXPECT_EQ(obj->name(), table_name); |
| 1022 | |
| 1023 | // Get table object. |
| 1024 | const dd::Table *tab = nullptr; |
| 1025 | |
| 1026 | EXPECT_FALSE(dc->acquire<dd::Table>(schema_name, table_name, &tab)); |
| 1027 | EXPECT_NE(nullp<const dd::Table>(), tab); |
| 1028 | if (tab) { |
| 1029 | EXPECT_EQ(tab->name(), table_name); |
| 1030 | EXPECT_EQ(*obj, *tab); |
| 1031 | |
| 1032 | const dd::Table *obj2 = nullptr; |
| 1033 | EXPECT_FALSE(dc->acquire<dd::Table>("mysql", obj->name(), &obj2)); |
| 1034 | EXPECT_NE(nullp<const dd::Table>(), tab); |
| 1035 | EXPECT_EQ(*obj, *obj2); |
| 1036 | |
| 1037 | EXPECT_FALSE(dc->drop(obj2)); |
| 1038 | dc->commit_modified_objects(); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | TEST_F(CacheStorageTest, TestRename) { |
| 1043 | dd::cache::Dictionary_client &dc = *thd()->dd_client(); |
| 1044 | dd::cache::Dictionary_client::Auto_releaser releaser_1(&dc); |
| 1045 | |
| 1046 | std::unique_ptr<dd::Table> temp_table_object(dd::create_object<dd::Table>()); |
| 1047 | dd_unittest::set_attributes(temp_table_object.get(), "temp_table", *mysql); |
| 1048 | |
| 1049 | lock_object(temp_table_object->name()); |
| 1050 | EXPECT_FALSE(dc.store(temp_table_object.get())); |
| 1051 | |
| 1052 | { |
| 1053 | // Disable foreign key checks, we need to set this before |
| 1054 | // call to open_tables(). |
| 1055 | thd()->variables.option_bits |= OPTION_NO_FOREIGN_KEY_CHECKS; |
| 1056 | |
| 1057 | dd::cache::Dictionary_client::Auto_releaser releaser_2(&dc); |
| 1058 | |
| 1059 | // Get 'test.temp_table' dd object. Schema id for 'mysql' is 1. |
| 1060 | const dd::Schema *sch = nullptr; |
| 1061 | EXPECT_FALSE(dc.acquire<dd::Schema>(mysql->id(), &sch)); |
| 1062 | EXPECT_NE(nullp<const dd::Schema>(), sch); |
| 1063 | |
| 1064 | const dd::Table *t = nullptr; |
| 1065 | EXPECT_FALSE(dc.acquire<dd::Table>(sch->name(), "temp_table", &t)); |
| 1066 | EXPECT_NE(nullp<const dd::Table>(), t); |
| 1067 | if (t) { |
| 1068 | dd::Table *temp_table = nullptr; |
| 1069 | EXPECT_FALSE(dc.acquire_for_modification(t->id(), &temp_table)); |
| 1070 | |
| 1071 | temp_table->set_name("updated_table_name"); |
| 1072 | |
| 1073 | // Change name of columns and indexes |
| 1074 | for (const dd::Column *c : *temp_table->columns()) |
| 1075 | const_cast<dd::Column *>(c)->set_name(c->name() + "_changed"); |
| 1076 | for (dd::Index *i : *temp_table->indexes()) |
| 1077 | i->set_name(i->name() + "_changed"); |
| 1078 | |
| 1079 | // Store the object. |
| 1080 | lock_object(temp_table->name()); |
| 1081 | EXPECT_FALSE(dc.update(temp_table)); |
| 1082 | |
| 1083 | // Enable foreign key checks |
| 1084 | thd()->variables.option_bits &= ~OPTION_NO_FOREIGN_KEY_CHECKS; |
| 1085 | } |
| 1086 | { |
| 1087 | // Get id of original object to be modified |
| 1088 | const dd::Table *temp_table = nullptr; |
| 1089 | EXPECT_FALSE( |
| 1090 | dc.acquire<dd::Table>("mysql", "updated_table_name", &temp_table)); |
| 1091 | EXPECT_NE(nullp<const dd::Table>(), temp_table); |
| 1092 | EXPECT_FALSE( |
| 1093 | dc.acquire<dd::Table>(sch->name(), "updated_table_name", &t)); |
| 1094 | if (t) { |
| 1095 | EXPECT_FALSE(dc.drop(t)); |
| 1096 | } |
| 1097 | } |
| 1098 | if (t) { |
| 1099 | // The old name is not available anymnore. |
| 1100 | t = nullptr; |
| 1101 | EXPECT_FALSE(dc.acquire<dd::Table>(sch->name(), "temp_table", &t)); |
| 1102 | EXPECT_EQ(nullp<const dd::Table>(), t); |
| 1103 | } |
| 1104 | dc.commit_modified_objects(); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | TEST_F(CacheStorageTest, TestSchema) { |
| 1109 | dd::cache::Dictionary_client &dc = *thd()->dd_client(); |
| 1110 | dd::cache::Dictionary_client::Auto_releaser releaser(&dc); |
| 1111 | |
| 1112 | std::unique_ptr<dd::Schema_impl> s(new dd::Schema_impl()); |
| 1113 | s->set_name("schema1"); |
| 1114 | EXPECT_FALSE(dc.store<dd::Schema>(s.get())); |
| 1115 | EXPECT_LT(9999u, s->id()); |
| 1116 | |
| 1117 | std::unique_ptr<dd::Table_impl> t(new dd::Table_impl()); |
| 1118 | t->set_name("table1"); |
| 1119 | t->set_schema_id(s->id()); |
| 1120 | EXPECT_FALSE(dc.store<dd::Table>(t.get())); |
| 1121 | EXPECT_LT(9999u, t->id()); |
| 1122 | |
| 1123 | s->set_id(-1); |
| 1124 | s->set_name("schema2"); |
| 1125 | EXPECT_FALSE(dc.store<dd::Schema>(s.get())); |
| 1126 | EXPECT_LT(9999u, s->id()); |
| 1127 | |
| 1128 | { |
| 1129 | // Get Schema object for "schema1" and "schema2". |
| 1130 | const dd::Schema *s1 = nullptr; |
| 1131 | const dd::Schema *s2 = nullptr; |
| 1132 | |
| 1133 | EXPECT_FALSE(dc.acquire<dd::Schema>("schema1", &s1)); |
| 1134 | EXPECT_FALSE(dc.acquire<dd::Schema>("schema2", &s2)); |
| 1135 | |
| 1136 | if (s1 && s2) { |
| 1137 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, "schema1", "table1", |
| 1138 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 1139 | |
| 1140 | m_mdl_context.acquire_lock(&m_request, |
| 1141 | thd()->variables.lock_wait_timeout); |
| 1142 | |
| 1143 | // Get "schema1.table1" table from cache. |
| 1144 | const dd::Table *s1_t1 = nullptr; |
| 1145 | EXPECT_FALSE(dc.acquire<dd::Table>("schema1", "table1", &s1_t1)); |
| 1146 | EXPECT_NE(nullp<const dd::Table>(), s1_t1); |
| 1147 | |
| 1148 | // Try to get "schema2.table1"(non existing) table. |
| 1149 | const dd::Table *s2_t1 = nullptr; |
| 1150 | EXPECT_FALSE(dc.acquire<dd::Table>("schema2", "table1", &s2_t1)); |
| 1151 | EXPECT_EQ(nullp<const dd::Table>(), s2_t1); |
| 1152 | |
| 1153 | EXPECT_FALSE(dc.drop(s1_t1)); |
| 1154 | EXPECT_FALSE(dc.drop(s2)); |
| 1155 | EXPECT_FALSE(dc.drop(s1)); |
| 1156 | } |
| 1157 | } |
| 1158 | dc.commit_modified_objects(); |
| 1159 | } |
| 1160 | |
| 1161 | // Testing Transaction::max_se_private_id() method. |
| 1162 | // Tables |
| 1163 | TEST_F(CacheStorageTest, TestTransactionMaxSePrivateId) { |
| 1164 | dd::cache::Dictionary_client &dc = *thd()->dd_client(); |
| 1165 | dd::cache::Dictionary_client::Auto_releaser releaser(&dc); |
| 1166 | |
| 1167 | std::unique_ptr<dd::Table> tab1(dd::create_object<dd::Table>()); |
| 1168 | std::unique_ptr<dd::Table> tab2(dd::create_object<dd::Table>()); |
| 1169 | std::unique_ptr<dd::Table> tab3(dd::create_object<dd::Table>()); |
| 1170 | |
| 1171 | dd_unittest::set_attributes(tab1.get(), "table1", *mysql); |
| 1172 | tab1->set_se_private_id(5); |
| 1173 | tab1->set_engine("innodb"); |
| 1174 | lock_object(tab1->name()); |
| 1175 | EXPECT_FALSE(dc.store(tab1.get())); |
| 1176 | |
| 1177 | dd_unittest::set_attributes(tab2.get(), "table2", *mysql); |
| 1178 | tab2->set_se_private_id(10); |
| 1179 | tab2->set_engine("innodb"); |
| 1180 | lock_object(tab2->name()); |
| 1181 | EXPECT_FALSE(dc.store(tab2.get())); |
| 1182 | |
| 1183 | dd_unittest::set_attributes(tab3.get(), "table3", *mysql); |
| 1184 | tab3->set_se_private_id(20); |
| 1185 | tab3->set_engine("unknown"); |
| 1186 | lock_object(tab3->name()); |
| 1187 | EXPECT_FALSE(dc.store(tab3.get())); |
| 1188 | |
| 1189 | dd::Table *tab1_new = nullptr; |
| 1190 | EXPECT_FALSE( |
| 1191 | dc.acquire_uncached_table_by_se_private_id("innodb", 5, &tab1_new)); |
| 1192 | EXPECT_NE(nullp<dd::Table>(), tab1_new); |
| 1193 | |
| 1194 | dd::Table *tab2_new = nullptr; |
| 1195 | EXPECT_FALSE( |
| 1196 | dc.acquire_uncached_table_by_se_private_id("innodb", 10, &tab2_new)); |
| 1197 | EXPECT_NE(nullp<dd::Table>(), tab2_new); |
| 1198 | |
| 1199 | dd::Table *tab3_new = nullptr; |
| 1200 | EXPECT_FALSE( |
| 1201 | dc.acquire_uncached_table_by_se_private_id("unknown", 20, &tab3_new)); |
| 1202 | EXPECT_NE(nullp<dd::Table>(), tab3_new); |
| 1203 | |
| 1204 | // Drop the objects |
| 1205 | const dd::Table *tab1_new_c = nullptr; |
| 1206 | const dd::Table *tab2_new_c = nullptr; |
| 1207 | const dd::Table *tab3_new_c = nullptr; |
| 1208 | EXPECT_FALSE(dc.acquire<dd::Table>("mysql", "table1", &tab1_new_c)); |
| 1209 | EXPECT_FALSE(dc.acquire<dd::Table>("mysql", "table2", &tab2_new_c)); |
| 1210 | EXPECT_FALSE(dc.acquire<dd::Table>("mysql", "table3", &tab3_new_c)); |
| 1211 | EXPECT_FALSE(dc.drop(tab1_new_c)); |
| 1212 | EXPECT_FALSE(dc.drop(tab2_new_c)); |
| 1213 | EXPECT_FALSE(dc.drop(tab3_new_c)); |
| 1214 | dc.commit_modified_objects(); |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | The following test cases have been commented out since they test |
| 1219 | Dictionary_client member functions which bypass Storage_adapter and |
| 1220 | access the dictionary tables directly. |
| 1221 | */ |
| 1222 | |
| 1223 | // Test fetching multiple objects. |
| 1224 | // Dictionary_client::fetch_catalog_components() |
| 1225 | // Needs working Dictionary_impl::instance() |
| 1226 | // TEST_F(CacheStorageTest, TestFetchCatalogComponents) |
| 1227 | // { |
| 1228 | // dd::cache::Dictionary_client &dc= *thd()->dd_client(); |
| 1229 | // dd::cache::Dictionary_client::Auto_releaser releaser(&dc); |
| 1230 | |
| 1231 | // std::unique_ptr<dd::Iterator<const dd::Schema> > schemas; |
| 1232 | // EXPECT_FALSE(dc.fetch_catalog_components(&schemas)); |
| 1233 | |
| 1234 | // while (true) |
| 1235 | // { |
| 1236 | // const dd::Schema *s= schemas->next(); |
| 1237 | |
| 1238 | // if (!s) |
| 1239 | // { |
| 1240 | // break; |
| 1241 | // } |
| 1242 | // } |
| 1243 | // } |
| 1244 | |
| 1245 | // |
| 1246 | // Dictionary_client::fetch_schema_components() |
| 1247 | // |
| 1248 | // TEST_F(CacheStorageTest, TestFetchSchemaComponents) |
| 1249 | // { |
| 1250 | // dd::cache::Dictionary_client &dc= *thd()->dd_client(); |
| 1251 | // dd::cache::Dictionary_client::Auto_releaser releaser(&dc); |
| 1252 | |
| 1253 | // const dd::Schema *s= NULL; |
| 1254 | // EXPECT_FALSE(dc.acquire<dd::Schema>("mysql", &s)); |
| 1255 | // EXPECT_NE(nullp<dd::Schema>(), s); |
| 1256 | |
| 1257 | // std::unique_ptr<dd::Iterator<const dd::Table> > tables; |
| 1258 | // EXPECT_FALSE(dc.fetch_schema_components(s, &tables)); |
| 1259 | |
| 1260 | // while (true) |
| 1261 | // { |
| 1262 | // const dd::Table *t= tables->next(); |
| 1263 | |
| 1264 | // if (!t) |
| 1265 | // break; |
| 1266 | // } |
| 1267 | // } |
| 1268 | |
| 1269 | // |
| 1270 | // Dictionary_client::fetch_global_components() |
| 1271 | // |
| 1272 | // TEST_F(CacheStorageTest, TestFetchGlobalComponents) |
| 1273 | // { |
| 1274 | // dd::cache::Dictionary_client &dc= *thd()->dd_client(); |
| 1275 | // dd::cache::Dictionary_client::Auto_releaser releaser(&dc); |
| 1276 | |
| 1277 | // // Create a new tablespace. |
| 1278 | // dd::Object_id tablespace_id [[maybe_unused]]; |
| 1279 | // { |
| 1280 | // std::unique_ptr<dd::Tablespace> obj(dd::create_object<dd::Tablespace>()); |
| 1281 | // dd_unittest::set_attributes(obj.get(), "test_tablespace"); |
| 1282 | |
| 1283 | // //lock_object(thd, obj); |
| 1284 | // EXPECT_FALSE(dc.store(obj.get())); |
| 1285 | // tablespace_id= obj->id(); |
| 1286 | // } |
| 1287 | // ha_commit_trans(thd(), false, true); |
| 1288 | |
| 1289 | // // List all tablespaces |
| 1290 | // { |
| 1291 | // std::unique_ptr<dd::Iterator<const dd::Tablespace> > tablespaces; |
| 1292 | // EXPECT_FALSE(dc.fetch_global_components(&tablespaces)); |
| 1293 | |
| 1294 | // while (true) |
| 1295 | // { |
| 1296 | // const dd::Tablespace *t= tablespaces->next(); |
| 1297 | |
| 1298 | // if (!t) |
| 1299 | // break; |
| 1300 | // } |
| 1301 | // } |
| 1302 | |
| 1303 | // // Drop tablespace |
| 1304 | // { |
| 1305 | // MDL_request mdl_request; |
| 1306 | // MDL_REQUEST_INIT(&mdl_request, MDL_key::TABLESPACE, |
| 1307 | // "test_tablespace", "", |
| 1308 | // MDL_EXCLUSIVE, |
| 1309 | // MDL_TRANSACTION); |
| 1310 | // (void) thd()->mdl_context.acquire_lock(&mdl_request, |
| 1311 | // thd()->variables.lock_wait_timeout); |
| 1312 | // const dd::Tablespace *obj= NULL; |
| 1313 | // EXPECT_FALSE(dc.acquire<dd::Tablespace>(tablespace_id, &obj)); |
| 1314 | // EXPECT_NE(nullp<const dd::Tablespace>(), obj); |
| 1315 | // EXPECT_FALSE(dc.drop(const_cast<dd::Tablespace*>(obj))); |
| 1316 | // } |
| 1317 | // EXPECT_FALSE(ha_commit_trans(thd(), false, true)); |
| 1318 | // } |
| 1319 | |
| 1320 | TEST_F(CacheStorageTest, TestCacheLookup) { |
| 1321 | dd::cache::Dictionary_client &dc = *thd()->dd_client(); |
| 1322 | dd::cache::Dictionary_client::Auto_releaser releaser(&dc); |
| 1323 | |
| 1324 | dd::String_type obj_name = |
| 1325 | dd::Table::DD_table::instance().name() + dd::String_type("_cacheissue"); |
| 1326 | dd::Object_id id; |
| 1327 | // |
| 1328 | // Create table object |
| 1329 | // |
| 1330 | { |
| 1331 | std::unique_ptr<dd::Table> obj(dd::create_object<dd::Table>()); |
| 1332 | dd_unittest::set_attributes(obj.get(), obj_name, *mysql); |
| 1333 | |
| 1334 | obj->set_engine("innodb"); |
| 1335 | obj->set_se_private_id(0xFFFA); // Storing some magic number |
| 1336 | |
| 1337 | lock_object(obj->name()); |
| 1338 | EXPECT_FALSE(dc.store(obj.get())); |
| 1339 | } |
| 1340 | |
| 1341 | // |
| 1342 | // Step 1: |
| 1343 | // Get Table object given se_private_id = 0xFFFA |
| 1344 | // This should release the object reference in cache. |
| 1345 | // |
| 1346 | { |
| 1347 | dd::String_type sch_name, tab_name; |
| 1348 | EXPECT_FALSE(dc.get_table_name_by_se_private_id("innodb", 0xFFFA, &sch_name, |
| 1349 | &tab_name)); |
| 1350 | EXPECT_LT(0u, sch_name.size()); |
| 1351 | EXPECT_LT(0u, tab_name.size()); |
| 1352 | |
| 1353 | const dd::Table *obj = nullptr; |
| 1354 | EXPECT_FALSE(dc.acquire<dd::Table>(sch_name, tab_name, &obj)); |
| 1355 | EXPECT_NE(nullp<const dd::Table>(), obj); |
| 1356 | id = obj->id(); |
| 1357 | } |
| 1358 | |
| 1359 | // |
| 1360 | // Step 2: |
| 1361 | // Get Table object given id and drop it. |
| 1362 | // This should remove object from the cache and delete it. |
| 1363 | // |
| 1364 | |
| 1365 | // |
| 1366 | // Get from cache and then drop it. |
| 1367 | // |
| 1368 | { |
| 1369 | const dd::Table *obj = nullptr; |
| 1370 | EXPECT_FALSE(dc.acquire<dd::Table>(id, &obj)); |
| 1371 | EXPECT_NE(nullp<const dd::Table>(), obj); |
| 1372 | EXPECT_FALSE(dc.drop(obj)); |
| 1373 | } |
| 1374 | |
| 1375 | // |
| 1376 | // Step 3: |
| 1377 | // Again, try to get Table object with se_private_id = 0xFFFA |
| 1378 | // This should not get object reference in cache which was stored |
| 1379 | // by Step 1. Make sure we get no object. |
| 1380 | // |
| 1381 | |
| 1382 | { |
| 1383 | dd::String_type sch_name, tab_name; |
| 1384 | EXPECT_FALSE(dc.get_table_name_by_se_private_id("innodb", 0XFFFA, &sch_name, |
| 1385 | &tab_name)); |
| 1386 | EXPECT_EQ(0u, sch_name.size()); |
| 1387 | EXPECT_EQ(0u, tab_name.size()); |
| 1388 | } |
| 1389 | dc.commit_modified_objects(); |
| 1390 | } |
| 1391 | |
| 1392 | TEST_F(CacheStorageTest, TestTriggers) { |
| 1393 | dd::cache::Dictionary_client &dc = *thd()->dd_client(); |
| 1394 | dd::cache::Dictionary_client::Auto_releaser releaser(&dc); |
| 1395 | |
| 1396 | dd::String_type obj_name = |
| 1397 | dd::Table::DD_table::instance().name() + dd::String_type("_trigs"); |
| 1398 | dd::Object_id id [[maybe_unused]]; |
| 1399 | |
| 1400 | // |
| 1401 | // Create table object |
| 1402 | // |
| 1403 | { |
| 1404 | std::unique_ptr<dd::Table> obj(dd::create_object<dd::Table>()); |
| 1405 | dd_unittest::set_attributes(obj.get(), obj_name, *mysql); |
| 1406 | |
| 1407 | // |
| 1408 | // Store table trigger information |
| 1409 | // |
| 1410 | |
| 1411 | dd::Trigger *trig_obj = |
| 1412 | obj->add_trigger(dd::Trigger::enum_action_timing::AT_AFTER, |
| 1413 | dd::Trigger::enum_event_type::ET_INSERT); |
| 1414 | trig_obj->set_name(obj->name() + "trigger1"); |
| 1415 | trig_obj->set_definer("definer_username", "definer_hostname"); |
| 1416 | trig_obj->set_client_collation_id(1); |
| 1417 | trig_obj->set_connection_collation_id(1); |
| 1418 | trig_obj->set_schema_collation_id(1); |
| 1419 | |
| 1420 | dd::Trigger *trig_obj2 = obj->add_trigger_preceding( |
| 1421 | trig_obj, dd::Trigger::enum_action_timing::AT_AFTER, |
| 1422 | dd::Trigger::enum_event_type::ET_INSERT); |
| 1423 | trig_obj2->set_name(obj->name() + "trigger2"); |
| 1424 | trig_obj2->set_definer("definer_username", "definer_hostname"); |
| 1425 | trig_obj2->set_client_collation_id(1); |
| 1426 | trig_obj2->set_connection_collation_id(1); |
| 1427 | trig_obj2->set_schema_collation_id(1); |
| 1428 | |
| 1429 | dd::Trigger *trig_obj3 = obj->add_trigger_following( |
| 1430 | trig_obj2, dd::Trigger::enum_action_timing::AT_AFTER, |
| 1431 | dd::Trigger::enum_event_type::ET_INSERT); |
| 1432 | trig_obj3->set_name(obj->name() + "trigger3"); |
| 1433 | trig_obj3->set_definer("definer_username", "definer_hostname"); |
| 1434 | trig_obj3->set_client_collation_id(1); |
| 1435 | trig_obj3->set_connection_collation_id(1); |
| 1436 | trig_obj3->set_schema_collation_id(1); |
| 1437 | |
| 1438 | dd::Trigger *trig_obj1 = |
| 1439 | obj->add_trigger(dd::Trigger::enum_action_timing::AT_BEFORE, |
| 1440 | dd::Trigger::enum_event_type::ET_UPDATE); |
| 1441 | trig_obj1->set_name("newtrigger1"); |
| 1442 | trig_obj1->set_definer("definer_username", "definer_hostname"); |
| 1443 | trig_obj1->set_client_collation_id(1); |
| 1444 | trig_obj1->set_connection_collation_id(1); |
| 1445 | trig_obj1->set_schema_collation_id(1); |
| 1446 | |
| 1447 | lock_object(obj->name()); |
| 1448 | EXPECT_FALSE(dc.store(obj.get())); |
| 1449 | id = obj->id(); |
| 1450 | } |
| 1451 | |
| 1452 | // |
| 1453 | // Test iterator. |
| 1454 | // |
| 1455 | |
| 1456 | { |
| 1457 | const dd::Table *obj = nullptr; |
| 1458 | EXPECT_FALSE(dc.acquire<dd::Table>(id, &obj)); |
| 1459 | EXPECT_NE(nullp<const dd::Table>(), obj); |
| 1460 | |
| 1461 | // Validate number of triggers expected. |
| 1462 | const dd::Table::Trigger_collection &tc = obj->triggers(); |
| 1463 | dd::Table::Trigger_collection::const_iterator it = tc.begin(); |
| 1464 | EXPECT_TRUE((*it)->name() == "newtrigger0"); |
| 1465 | ++it; |
| 1466 | EXPECT_TRUE((*it)->name() == "tables_trigstrigger2"); |
| 1467 | ++it; |
| 1468 | EXPECT_TRUE((*it)->name() == "tables_trigstrigger3"); |
| 1469 | ++it; |
| 1470 | EXPECT_TRUE((*it)->name() == "tables_trigstrigger1"); |
| 1471 | ++it; |
| 1472 | EXPECT_TRUE((*it)->name() == "newtrigger1"); |
| 1473 | ++it; |
| 1474 | EXPECT_TRUE(it == tc.end()); |
| 1475 | |
| 1476 | EXPECT_FALSE(dc.drop(const_cast<dd::Table *>(obj))); |
| 1477 | } |
| 1478 | dc.commit_modified_objects(); |
| 1479 | } |
| 1480 | |
| 1481 | TEST_F(CacheStorageTest, PartitionTest) { |
| 1482 | dd::cache::Dictionary_client *dc = thd()->dd_client(); |
| 1483 | dd::cache::Dictionary_client::Auto_releaser releaser(dc); |
| 1484 | |
| 1485 | std::unique_ptr<dd::Table> obj(dd::create_object<dd::Table>()); |
| 1486 | // dd_unittest::set_attributes(obj.get(), "part_table1", *mysql); |
| 1487 | dd::String_type table_name = "part_table1"; |
| 1488 | obj->set_name(table_name); |
| 1489 | |
| 1490 | obj->set_schema_id(mysql->id()); |
| 1491 | obj->set_collation_id(1); |
| 1492 | obj->set_tablespace_id(1); |
| 1493 | obj->set_engine("innodb"); |
| 1494 | |
| 1495 | // |
| 1496 | // Create a new column |
| 1497 | // |
| 1498 | dd::Column *col_obj1 = obj->add_column(); |
| 1499 | col_obj1->set_name(table_name + "col2"); |
| 1500 | col_obj1->set_default_value_null(true); |
| 1501 | col_obj1->set_collation_id(1); |
| 1502 | |
| 1503 | dd::Partition *part_obj1 = obj->add_partition(); |
| 1504 | part_obj1->set_name("p1"); |
| 1505 | part_obj1->set_se_private_id(0xAFFF); |
| 1506 | part_obj1->set_engine("innodb"); |
| 1507 | part_obj1->set_number(0); |
| 1508 | part_obj1->set_comment("Partition comment"); |
| 1509 | part_obj1->set_tablespace_id(1); |
| 1510 | |
| 1511 | dd::Partition *sub_part_obj1 = part_obj1->add_subpartition(); |
| 1512 | sub_part_obj1->set_name("p1s1"); |
| 1513 | sub_part_obj1->set_se_private_id(0xAFFF); |
| 1514 | sub_part_obj1->set_engine("innodb"); |
| 1515 | sub_part_obj1->set_number(0); |
| 1516 | sub_part_obj1->set_comment("Partition comment"); |
| 1517 | sub_part_obj1->set_tablespace_id(1); |
| 1518 | |
| 1519 | dd::Partition *sub_part_obj2 = part_obj1->add_subpartition(); |
| 1520 | sub_part_obj2->set_name("p1s2"); |
| 1521 | sub_part_obj2->set_se_private_id(0xAFFF); |
| 1522 | sub_part_obj2->set_engine("innodb"); |
| 1523 | sub_part_obj2->set_number(1); |
| 1524 | sub_part_obj2->set_comment("Partition comment"); |
| 1525 | sub_part_obj2->set_tablespace_id(1); |
| 1526 | |
| 1527 | dd::Partition *part_obj2 = obj->add_partition(); |
| 1528 | part_obj2->set_name("p2"); |
| 1529 | part_obj2->set_se_private_id(0xAFFF); |
| 1530 | part_obj2->set_engine("innodb"); |
| 1531 | part_obj2->set_number(1); |
| 1532 | part_obj2->set_comment("Partition comment"); |
| 1533 | part_obj2->set_tablespace_id(1); |
| 1534 | |
| 1535 | sub_part_obj1 = part_obj2->add_subpartition(); |
| 1536 | sub_part_obj1->set_name("p2s1"); |
| 1537 | sub_part_obj1->set_se_private_id(0xAFFF); |
| 1538 | sub_part_obj1->set_engine("innodb"); |
| 1539 | sub_part_obj1->set_number(0); |
| 1540 | sub_part_obj1->set_comment("Partition comment"); |
| 1541 | sub_part_obj1->set_tablespace_id(1); |
| 1542 | |
| 1543 | sub_part_obj2 = part_obj2->add_subpartition(); |
| 1544 | sub_part_obj2->set_name("p2s2"); |
| 1545 | sub_part_obj2->set_se_private_id(0xAFFF); |
| 1546 | sub_part_obj2->set_engine("innodb"); |
| 1547 | sub_part_obj2->set_number(1); |
| 1548 | sub_part_obj2->set_comment("Partition comment"); |
| 1549 | sub_part_obj2->set_tablespace_id(1); |
| 1550 | |
| 1551 | lock_object(obj->name()); |
| 1552 | EXPECT_FALSE(dc->store(obj.get())); |
| 1553 | |
| 1554 | // Get table object. |
| 1555 | const dd::Table *tab = nullptr; |
| 1556 | |
| 1557 | EXPECT_FALSE(dc->acquire<dd::Table>("mysql", table_name, &tab)); |
| 1558 | EXPECT_NE(nullp<const dd::Table>(), tab); |
| 1559 | if (tab) { |
| 1560 | EXPECT_EQ(tab->name(), table_name); |
| 1561 | EXPECT_EQ(*obj, *tab); |
| 1562 | |
| 1563 | int i = 1; |
| 1564 | // Verify the partition info |
| 1565 | for (const dd::Partition *p : tab->partitions()) { |
| 1566 | dd::Stringstream_type part_name; |
| 1567 | part_name << "p" << i++; |
| 1568 | EXPECT_EQ(p->name(), part_name.str()); |
| 1569 | int j = 1; |
| 1570 | for (const dd::Partition *s : p->subpartitions()) { |
| 1571 | dd::Stringstream_type sub_part_name; |
| 1572 | sub_part_name << part_name.str() << "s" << j++; |
| 1573 | EXPECT_EQ(s->name(), sub_part_name.str()); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | const dd::Table *obj2 = nullptr; |
| 1578 | EXPECT_FALSE(dc->acquire<dd::Table>("mysql", table_name, &obj2)); |
| 1579 | EXPECT_NE(nullp<const dd::Table>(), tab); |
| 1580 | EXPECT_EQ(*obj, *obj2); |
| 1581 | |
| 1582 | EXPECT_FALSE(dc->drop(obj2)); |
| 1583 | dc->commit_modified_objects(); |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | TEST_F(CacheStorageTest, CloneInternalPointersTest) { |
| 1588 | std::unique_ptr<dd::Table> tab{dd::create_object<dd::Table>()}; |
| 1589 | |
| 1590 | dd::Column *col1 = tab->add_column(); |
| 1591 | dd::Index *index1 = tab->add_index(); |
| 1592 | index1->add_element(col1); |
| 1593 | |
| 1594 | dd::Column *col2 = tab->add_column(); |
| 1595 | dd::Index *index2 = tab->add_index(); |
| 1596 | index2->add_element(col2); |
| 1597 | |
| 1598 | tab->add_partition()->add_index(index1); |
| 1599 | tab->add_partition()->add_index(index2); |
| 1600 | |
| 1601 | std::unique_ptr<dd::Table> clone{tab->clone()}; |
| 1602 | const dd::Table &cr = *clone; |
| 1603 | const dd::Table::Column_collection &clone_cols = cr.columns(); |
| 1604 | EXPECT_EQ(2u, clone_cols.size()); |
| 1605 | const dd::Table::Index_collection &clone_indices = cr.indexes(); |
| 1606 | EXPECT_EQ(2u, clone_indices.size()); |
| 1607 | const dd::Table::Partition_collection &clone_parts = cr.partitions(); |
| 1608 | EXPECT_EQ(2u, clone_parts.size()); |
| 1609 | |
| 1610 | EXPECT_EQ(clone_cols[0], &clone_indices[0]->elements().front()->column()); |
| 1611 | EXPECT_EQ(clone_cols[1], &clone_indices[1]->elements().front()->column()); |
| 1612 | |
| 1613 | EXPECT_EQ(clone_indices[0], &clone_parts[0]->indexes().front()->index()); |
| 1614 | EXPECT_EQ(clone_indices[1], &clone_parts[1]->indexes().front()->index()); |
| 1615 | } |
| 1616 | |
| 1617 | #endif /* !NDEBUG */ |
| 1618 | } // namespace dd_cache_unittest |