2023-01-04 17:56:45 +00:00
|
|
|
#pragma once
|
|
|
|
#include <mutex>
|
|
|
|
#include <Interpreters/Cache/FileCache_fwd.h>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2023-01-05 21:39:17 +00:00
|
|
|
/**
|
|
|
|
* Priority of locking:
|
|
|
|
* Cache priority queue guard > key prefix guard > file segment guard.
|
|
|
|
*/
|
2023-01-04 17:56:45 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-27 12:48:46 +00:00
|
|
|
* Cache priority queue guard.
|
2023-01-05 21:39:17 +00:00
|
|
|
*/
|
2023-01-27 12:48:46 +00:00
|
|
|
struct CacheGuard
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
|
|
|
struct Lock
|
|
|
|
{
|
2023-01-27 12:48:46 +00:00
|
|
|
explicit Lock(CacheGuard & guard) : lock(guard.mutex) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
std::unique_lock<std::mutex> lock;
|
|
|
|
};
|
2023-01-27 12:48:46 +00:00
|
|
|
using LockPtr = std::shared_ptr<Lock>;
|
2023-01-04 17:56:45 +00:00
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
|
2023-01-27 12:48:46 +00:00
|
|
|
LockPtr lock() { return std::make_shared<Lock>(*this); }
|
2023-01-04 17:56:45 +00:00
|
|
|
|
2023-01-27 12:48:46 +00:00
|
|
|
CacheGuard() = default;
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-20 15:30:25 +00:00
|
|
|
/**
|
|
|
|
* Guard for cache metadata.
|
|
|
|
*/
|
|
|
|
struct CacheMetadataGuard
|
|
|
|
{
|
|
|
|
struct Lock
|
|
|
|
{
|
|
|
|
explicit Lock(CacheMetadataGuard & guard) : lock(guard.mutex) {}
|
|
|
|
std::unique_lock<std::mutex> lock;
|
|
|
|
};
|
|
|
|
using LockPtr = std::shared_ptr<Lock>;
|
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
|
|
|
|
Lock lock() { return Lock(*this); }
|
|
|
|
|
|
|
|
CacheMetadataGuard() = default;
|
|
|
|
};
|
|
|
|
|
2023-01-05 21:39:17 +00:00
|
|
|
/**
|
2023-01-27 12:48:46 +00:00
|
|
|
* Guard for a set of keys.
|
|
|
|
* One guard per key prefix (first three digits of the path hash).
|
2023-01-05 21:39:17 +00:00
|
|
|
*/
|
2023-01-27 12:48:46 +00:00
|
|
|
struct KeyGuard
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
|
|
|
struct Lock
|
|
|
|
{
|
2023-01-27 12:48:46 +00:00
|
|
|
explicit Lock(KeyGuard & guard) : lock(guard.mutex) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
std::unique_lock<std::mutex> lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
|
2023-01-27 12:48:46 +00:00
|
|
|
Lock lock() { return Lock(*this); }
|
2023-01-04 17:56:45 +00:00
|
|
|
|
2023-01-27 12:48:46 +00:00
|
|
|
KeyGuard() = default;
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
2023-01-27 12:48:46 +00:00
|
|
|
using KeyGuardPtr = std::shared_ptr<KeyGuard>;
|
2023-01-04 17:56:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Guard for a file segment.
|
|
|
|
*/
|
|
|
|
struct FileSegmentGuard
|
|
|
|
{
|
|
|
|
struct Lock
|
|
|
|
{
|
|
|
|
explicit Lock(FileSegmentGuard & guard) : lock(guard.mutex) {}
|
|
|
|
std::unique_lock<std::mutex> lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
|
|
|
|
Lock lock() { return Lock(*this); }
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|