ClickHouse/dbms/Interpreters/ActionLocksManager.h

51 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
#include <Core/Types.h>
#include <Storages/IStorage_fwd.h>
#include <Common/ActionLock.h>
2020-03-13 10:30:55 +00:00
#include <Interpreters/StorageID.h>
#include <mutex>
#include <unordered_map>
namespace DB
{
class Context;
/// Holds ActionLocks for tables
/// Does not store pointers to tables
class ActionLocksManager
{
public:
explicit ActionLocksManager(Context & global_context_) : global_context(global_context_) {}
/// Adds new locks for each table
void add(StorageActionBlockType action_type);
/// Add new lock for a table if it has not been already added
2020-03-04 20:29:52 +00:00
void add(const StorageID & table_id, StorageActionBlockType action_type);
2020-01-24 16:20:36 +00:00
void add(const StoragePtr & table, StorageActionBlockType action_type);
/// Remove locks for all tables
void remove(StorageActionBlockType action_type);
/// Removes a lock for a table if it exists
2020-03-04 20:29:52 +00:00
void remove(const StorageID & table_id, StorageActionBlockType action_type);
2020-01-24 16:20:36 +00:00
void remove(const StoragePtr & table, StorageActionBlockType action_type);
/// Removes all locks of non-existing tables
void cleanExpired();
private:
Context & global_context;
using StorageRawPtr = const IStorage *;
using Locks = std::unordered_map<size_t, ActionLock>;
using StorageLocks = std::unordered_map<StorageRawPtr, Locks>;
mutable std::mutex mutex;
StorageLocks storage_locks;
};
}