提交 93a9e646 编写于 作者: A Alexey Zatelepin

fix usage of possibly invalidated iterator [#CLICKHOUSE-2]

上级 e936a57a
......@@ -320,21 +320,22 @@ private:
};
/** Кладёт элемент в map, в деструкторе - удаляет.
* Если элемент уже есть - кидает исключение.
*/
/// Puts an element into the map, erases it in the destructor.
/// If the element already exists in the map, throws an exception containing provided message.
class DDLGuard
{
/// Имя объекта -> сообщение.
using Map = std::unordered_map<String, String>;
public:
/// Element name -> message.
/// NOTE: using std::map here (and not std::unordered_map) to avoid iterator invalidation on insertion.
using Map = std::map<String, String>;
DDLGuard(Map & map_, std::mutex & mutex_, std::unique_lock<std::mutex> && lock, const String & elem, const String & message);
~DDLGuard();
private:
Map & map;
Map::iterator it;
std::mutex & mutex;
public:
DDLGuard(Map & map_, std::mutex & mutex_, std::unique_lock<std::mutex> && lock, const String & elem, const String & message);
~DDLGuard();
};
}
......@@ -140,7 +140,7 @@ struct ContextShared
/// database -> table -> exception_message
/// На время выполнения операции, сюда помещается элемент, и возвращается объект, который в деструкторе удаляет элемент.
/// В случае, если элемент уже есть - кидается исключение. См. class DDLGuard ниже.
using DDLGuards = std::unordered_map<String, std::unordered_map<String, String>>;
using DDLGuards = std::unordered_map<String, DDLGuard::Map>;
DDLGuards ddl_guards;
/// Если вы захватываете mutex и ddl_guards_mutex, то захватывать их нужно строго в этом порядке.
mutable std::mutex ddl_guards_mutex;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册