ClickHouse/src/Interpreters/Cache/FileCacheFactory.cpp

52 lines
1.2 KiB
C++
Raw Normal View History

2022-01-13 11:57:56 +00:00
#include "FileCacheFactory.h"
2022-06-25 19:05:54 +00:00
#include "FileCache.h"
2022-01-13 11:57:56 +00:00
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;
}
2023-04-15 11:08:49 +00:00
FileCacheFactory::CacheByName FileCacheFactory::getAll()
{
std::lock_guard lock(mutex);
2023-04-15 11:08:49 +00:00
return caches_by_name;
}
FileCachePtr FileCacheFactory::getOrCreate(
2023-04-15 11:08:49 +00:00
const std::string & cache_name, 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
2023-04-15 11:08:49 +00:00
auto it = caches_by_name.find(cache_name);
if (it == caches_by_name.end())
2022-06-21 13:42:36 +00:00
{
2023-04-15 11:08:49 +00:00
auto cache = std::make_shared<FileCache>(file_cache_settings);
it = caches_by_name.emplace(
cache_name, std::make_unique<FileCacheData>(cache, file_cache_settings)).first;
2022-06-21 13:42:36 +00:00
}
2022-01-13 11:57:56 +00:00
2023-04-15 11:08:49 +00:00
return it->second->cache;
2022-01-13 11:57:56 +00:00
}
2023-04-15 11:08:49 +00:00
FileCacheFactory::FileCacheData FileCacheFactory::getByName(const std::string & cache_name)
2022-06-21 13:42:36 +00:00
{
2022-06-23 15:46:27 +00:00
std::lock_guard lock(mutex);
2023-04-15 11:08:49 +00:00
auto it = caches_by_name.find(cache_name);
2022-06-21 13:42:36 +00:00
if (it == caches_by_name.end())
2023-04-15 11:08:49 +00:00
throw Exception(ErrorCodes::BAD_ARGUMENTS, "There is no cache by name: {}", cache_name);
2022-06-23 15:46:27 +00:00
return *it->second;
2022-06-21 13:42:36 +00:00
}
2022-01-13 11:57:56 +00:00
}