1#pragma once
2
3#include <Core/Types.h>
4#include <Storages/IStorage_fwd.h>
5#include <Common/ActionLock.h>
6
7#include <mutex>
8#include <unordered_map>
9
10
11namespace DB
12{
13
14class Context;
15
16/// Holds ActionLocks for tables
17/// Does not store pointers to tables
18class ActionLocksManager
19{
20public:
21 explicit ActionLocksManager(Context & global_context_) : global_context(global_context_) {}
22
23 /// Adds new locks for each table
24 void add(StorageActionBlockType action_type);
25 /// Add new lock for a table if it has not been already added
26 void add(const String & database_name, const String & table_name, StorageActionBlockType action_type);
27
28 /// Remove locks for all tables
29 void remove(StorageActionBlockType action_type);
30 /// Removes a lock for a table if it exists
31 void remove(const String & database_name, const String & table_name, StorageActionBlockType action_type);
32
33 /// Removes all locks of non-existing tables
34 void cleanExpired();
35
36private:
37 Context & global_context;
38
39 using StorageRawPtr = const IStorage *;
40 using Locks = std::unordered_map<size_t, ActionLock>;
41 using StorageLocks = std::unordered_map<StorageRawPtr, Locks>;
42
43 mutable std::mutex mutex;
44 StorageLocks storage_locks;
45};
46
47}
48