2018-05-21 13:49:54 +00:00
|
|
|
#pragma once
|
2019-05-17 14:34:25 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
|
|
|
#include <Interpreters/StorageID.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Storages/IStorage_fwd.h>
|
2018-05-21 13:49:54 +00:00
|
|
|
#include <Common/ActionLock.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
|
2018-05-21 13:49:54 +00:00
|
|
|
#include <mutex>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <unordered_map>
|
2018-05-21 13:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Holds ActionLocks for tables
|
|
|
|
/// Does not store pointers to tables
|
2021-04-10 23:33:54 +00:00
|
|
|
class ActionLocksManager : WithContext
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-04-10 23:33:54 +00:00
|
|
|
explicit ActionLocksManager(ContextPtr context);
|
2020-05-28 23:01:18 +00:00
|
|
|
|
2018-05-21 13:49:54 +00:00
|
|
|
/// 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);
|
2018-05-21 13:49:54 +00:00
|
|
|
|
|
|
|
/// 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);
|
2018-05-21 13:49:54 +00:00
|
|
|
|
|
|
|
/// Removes all locks of non-existing tables
|
|
|
|
void cleanExpired();
|
|
|
|
|
|
|
|
private:
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|