2022-01-13 11:57:56 +00:00
|
|
|
#include "FileCacheFactory.h"
|
|
|
|
#include "FileCache.h"
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-01-22 22:56:24 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2022-01-13 11:57:56 +00:00
|
|
|
FileCacheFactory & FileCacheFactory::instance()
|
|
|
|
{
|
|
|
|
static FileCacheFactory ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:30:25 +00:00
|
|
|
FileCacheFactory::CacheByBasePath FileCacheFactory::getAll()
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
return caches;
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:56:38 +00:00
|
|
|
const FileCacheSettings & FileCacheFactory::getSettings(const std::string & cache_base_path)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2022-03-23 17:11:52 +00:00
|
|
|
auto * cache_data = getImpl(cache_base_path, lock);
|
2022-03-21 13:56:38 +00:00
|
|
|
if (cache_data)
|
|
|
|
return cache_data->settings;
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "No cache found by path: {}", cache_base_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileCacheFactory::CacheData * FileCacheFactory::getImpl(const std::string & cache_base_path, std::lock_guard<std::mutex> &)
|
2022-01-13 11:57:56 +00:00
|
|
|
{
|
|
|
|
auto it = caches.find(cache_base_path);
|
|
|
|
if (it == caches.end())
|
|
|
|
return nullptr;
|
2022-03-21 13:56:38 +00:00
|
|
|
return &it->second;
|
2022-01-13 11:57:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:30:25 +00:00
|
|
|
FileCachePtr FileCacheFactory::get(const std::string & cache_base_path)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
2022-03-23 17:11:52 +00:00
|
|
|
|
2022-03-21 13:56:38 +00:00
|
|
|
auto * cache_data = getImpl(cache_base_path, lock);
|
|
|
|
if (cache_data)
|
|
|
|
return cache_data->cache;
|
2022-03-21 11:30:25 +00:00
|
|
|
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "No cache found by path: {}", cache_base_path);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:06:13 +00:00
|
|
|
FileCachePtr FileCacheFactory::getOrCreate(
|
2022-03-14 19:15:07 +00:00
|
|
|
const std::string & cache_base_path, const FileCacheSettings & file_cache_settings)
|
2022-01-13 11:57:56 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
2022-03-23 17:11:52 +00:00
|
|
|
|
2022-03-21 13:56:38 +00:00
|
|
|
auto * cache_data = getImpl(cache_base_path, lock);
|
|
|
|
if (cache_data)
|
|
|
|
return cache_data->cache;
|
2022-01-13 11:57:56 +00:00
|
|
|
|
2022-03-21 13:56:38 +00:00
|
|
|
auto cache = std::make_shared<LRUFileCache>(cache_base_path, file_cache_settings);
|
|
|
|
caches.emplace(cache_base_path, CacheData(cache, file_cache_settings));
|
2022-01-13 11:57:56 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|