diff --git a/storage/tianmu/core/engine.cpp b/storage/tianmu/core/engine.cpp index 34b6313d9..335c2412c 100644 --- a/storage/tianmu/core/engine.cpp +++ b/storage/tianmu/core/engine.cpp @@ -780,11 +780,7 @@ void Engine::Rollback(THD *thd, bool all, bool force_error_message) { } void Engine::DeleteTable(const char *table, [[maybe_unused]] const dd::Table *table_def, [[maybe_unused]] THD *thd) { - { - std::unique_lock index_guard(tables_keys_mutex); - index::RCTableIndex::DropIndexTable(table); - m_table_keys.erase(table); - } + DeleteTableIndex(table, thd); { std::unique_lock mem_guard(mem_table_mutex); RCMemTable::DropMemTable(table); @@ -1109,6 +1105,7 @@ void Engine::RenameTable([[maybe_unused]] Transaction *trans_, const std::string filter_cache.RemoveIf([id](const FilterCoordinate &c) { return c[0] == int(id); }); system::RenameFile(tianmu_data_dir / (from + common::TIANMU_EXT), tianmu_data_dir / (to + common::TIANMU_EXT)); RenameRdbTable(from, to); + DeleteTableIndex(from, thd); UnregisterMemTable(from, to); TIANMU_LOG(LogCtl_Level::INFO, "Rename table %s to %s", from.c_str(), to.c_str()); } @@ -1986,6 +1983,17 @@ void Engine::AddTableIndex(const std::string &table_path, TABLE *table, [[maybe_ } } +void Engine::DeleteTableIndex(const std::string &table_path, [[maybe_unused]] THD *thd) { + std::unique_lock index_guard(tables_keys_mutex); + if (index::RCTableIndex::FindIndexTable(table_path)) { + index::RCTableIndex::DropIndexTable(table_path); + } + auto iter = m_table_keys.find(table_path); + if (iter != m_table_keys.end()) { + m_table_keys.erase(table_path); + } +} + std::shared_ptr Engine::GetTableIndex(const std::string &table_path) { std::shared_lock guard(tables_keys_mutex); auto iter = m_table_keys.find(table_path); diff --git a/storage/tianmu/core/engine.h b/storage/tianmu/core/engine.h index 503182cac..a152af112 100644 --- a/storage/tianmu/core/engine.h +++ b/storage/tianmu/core/engine.h @@ -113,7 +113,10 @@ class Engine final { void UnRegisterTable(const std::string &table_path); void GetTableIterator(const std::string &table_path, RCTable::Iterator &iter_begin, RCTable::Iterator &iter_end, std::shared_ptr &table, const std::vector &, THD *thd); + // support for add primary key void AddTableIndex(const std::string &table_path, TABLE *table, THD *thd); + // support for delete primary key + void DeleteTableIndex(const std::string &table_path, THD *thd); std::shared_ptr GetTableIndex(const std::string &table_path); bool has_pk(TABLE *table) const { return table->s->primary_key != MAX_INDEXES; } void RenameRdbTable(const std::string &from, const std::string &to);