Skip to content

Commit

Permalink
fix(tianmu):fix alter table add/drop primary key.(stoneatom#1673)
Browse files Browse the repository at this point in the history
in some cases, can insert same values by primary key after atler table add
primary key.
  • Loading branch information
hustjieke committed May 10, 2023
1 parent 57e0848 commit 92876bb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
18 changes: 13 additions & 5 deletions storage/tianmu/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_mutex> index_guard(tables_keys_mutex);
index::RCTableIndex::DropIndexTable(table);
m_table_keys.erase(table);
}
DeleteTableIndex(table, thd);
{
std::unique_lock<std::shared_mutex> mem_guard(mem_table_mutex);
RCMemTable::DropMemTable(table);
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<std::shared_mutex> 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<index::RCTableIndex> Engine::GetTableIndex(const std::string &table_path) {
std::shared_lock<std::shared_mutex> guard(tables_keys_mutex);
auto iter = m_table_keys.find(table_path);
Expand Down
3 changes: 3 additions & 0 deletions storage/tianmu/core/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<RCTable> &table, const std::vector<bool> &, 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<index::RCTableIndex> 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);
Expand Down

0 comments on commit 92876bb

Please sign in to comment.