3#include "FileManager.h"
4#include "Exceptions/FileDoesNotExistException.h"
11 struct less<pair<uint64_t, filesystem::path>>
13 constexpr bool operator()(
const pair<uint64_t, filesystem::path>& left,
const pair<uint64_t, filesystem::path>& right)
const
15 return left.first > right.first;
22 void Cache::updateCache()
24 priority_queue<pair<uint64_t, filesystem::path>> paths;
25 vector<pair<uint64_t, const filesystem::path*>> notExistingPaths;
26 unique_lock<mutex> cacheDataLock(cacheDataMutex);
28 for (
const auto& [path, _] : cacheData)
30 uint64_t size = filesystem::file_size(path);
32 if (!filesystem::exists(path))
34 notExistingPaths.emplace_back(size, &path);
39 paths.emplace(size, path);
42 for (
const auto& [size, path] : notExistingPaths)
44 currentCacheSize -= size;
46 cacheData.erase(*path);
49 while (currentCacheSize < cacheSize && paths.size())
51 const auto& [size, path] = paths.top();
53 currentCacheSize -= size;
55 cacheData.erase(path);
61 Cache& Cache::getCache()
75 if (!filesystem::exists(filePath))
77 return CacheResultCodes::fileDoesNotExist;
79 else if (currentCacheSize + filesystem::file_size(filePath) > cacheSize)
81 return CacheResultCodes::notEnoughCacheSize;
84 unique_lock<mutex> dataLock(cacheDataMutex);
86 if (cacheData.contains(filePath))
88 return CacheResultCodes::noError;
91 string data = (ostringstream() << ifstream(filePath, mode).rdbuf()).str();
93 currentCacheSize += data.size();
95 cacheData.try_emplace(filePath, move(data));
97 return CacheResultCodes::noError;
102 return this->appendCache(filePath, data.data());
105 Cache::CacheResultCodes Cache::appendCache(
const filesystem::path& filePath, string_view data)
107 if (currentCacheSize + data.size() > cacheSize)
109 return CacheResultCodes::notEnoughCacheSize;
112 unique_lock<mutex> dataLock(cacheDataMutex);
114 if (
auto it = cacheData.find(filePath); it != cacheData.end())
120 cacheData[filePath] = data;
123 currentCacheSize += data.size();
125 return CacheResultCodes::noError;
128 bool Cache::contains(
const filesystem::path& filePath)
const
130 unique_lock<mutex> dataLock(cacheDataMutex);
132 return cacheData.contains(filePath);
137 unique_lock<mutex> dataLock(cacheDataMutex);
139 currentCacheSize = 0;
144 void Cache::clear(
const filesystem::path& filePath)
146 unique_lock<mutex> dataLock(cacheDataMutex);
147 auto it = cacheData.find(filePath);
149 if (it == cacheData.end())
154 currentCacheSize -= it->second.size();
159 void Cache::setCacheSize(uint64_t sizeInBytes)
161 cacheSize = sizeInBytes;
163 if (cacheSize < currentCacheSize)
169 const string& Cache::getCacheData(
const filesystem::path& filePath)
const
171 unique_lock<mutex> dataLock(cacheDataMutex);
172 auto it = cacheData.find(filePath);
174 if (it == cacheData.end())
182 uint64_t Cache::getCacheSize()
const
187 uint64_t Cache::getCurrentCacheSize()
const
189 return currentCacheSize;
192 const string& Cache::operator [] (
const filesystem::path& filePath)
const
194 return this->getCacheData(filePath);
CacheResultCodes
Result of adding cache.
Cache & getCache()
Cache getter.
static FileManager & getInstance()
Singleton getter Also initialize thread pool with max threads for current hardware Default getter aft...
Thrown if file does not exist.