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
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-04 17:56:45 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
2023-01-20 15:30:25 +00:00
|
|
|
/**
|
|
|
|
* Guard for cache metadata.
|
|
|
|
*/
|
|
|
|
struct CacheMetadataGuard
|
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-20 15:30:25 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-20 15:30:25 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-20 15:30:25 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-04 17:56:45 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
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
|
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
struct Lock : public std::unique_lock<std::mutex>
|
2023-01-04 17:56:45 +00:00
|
|
|
{
|
2023-01-31 13:48:51 +00:00
|
|
|
explicit Lock(std::mutex & mutex_) : std::unique_lock<std::mutex>(mutex_) {}
|
2023-01-04 17:56:45 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 13:48:51 +00:00
|
|
|
Lock lock() { return Lock(mutex); }
|
2023-01-04 17:56:45 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|