FileManager v1.7.0
Manage access to files with async model
Loading...
Searching...
No Matches
Cache.cpp
1#include "Cache.h"
2
3#include "FileManager.h"
4#include "Exceptions/FileDoesNotExistException.h"
5
6using namespace std;
7
8namespace std
9{
10 template<>
11 struct less<pair<uint64_t, filesystem::path>>
12 {
13 constexpr bool operator()(const pair<uint64_t, filesystem::path>& left, const pair<uint64_t, filesystem::path>& right) const
14 {
15 return left.first > right.first;
16 }
17 };
18}
19
20namespace file_manager
21{
22 void Cache::updateCache()
23 {
24 priority_queue<pair<uint64_t, filesystem::path>> paths;
25 vector<pair<uint64_t, const filesystem::path*>> notExistingPaths;
26 unique_lock<mutex> cacheDataLock(cacheDataMutex);
27
28 for (const auto& [path, _] : cacheData)
29 {
30 uint64_t size = filesystem::file_size(path);
31
32 if (!filesystem::exists(path))
33 {
34 notExistingPaths.emplace_back(size, &path);
35
36 continue;
37 }
38
39 paths.emplace(size, path);
40 }
41
42 for (const auto& [size, path] : notExistingPaths)
43 {
44 currentCacheSize -= size;
45
46 cacheData.erase(*path);
47 }
48
49 while (currentCacheSize < cacheSize && paths.size())
50 {
51 const auto& [size, path] = paths.top();
52
53 currentCacheSize -= size;
54
55 cacheData.erase(path);
56
57 paths.pop();
58 }
59 }
60
61 Cache& Cache::getCache()
62 {
64 }
65
66 Cache::Cache() :
67 cacheSize(0),
68 currentCacheSize(0)
69 {
70
71 }
72
73 Cache::CacheResultCodes Cache::addCache(const filesystem::path& filePath, ios_base::openmode mode)
74 {
75 if (!filesystem::exists(filePath))
76 {
77 return CacheResultCodes::fileDoesNotExist;
78 }
79 else if (currentCacheSize + filesystem::file_size(filePath) > cacheSize)
80 {
81 return CacheResultCodes::notEnoughCacheSize;
82 }
83
84 unique_lock<mutex> dataLock(cacheDataMutex);
85
86 if (cacheData.contains(filePath))
87 {
88 return CacheResultCodes::noError;
89 }
90
91 string data = (ostringstream() << ifstream(filePath, mode).rdbuf()).str();
92
93 currentCacheSize += data.size();
94
95 cacheData.try_emplace(filePath, move(data));
96
97 return CacheResultCodes::noError;
98 }
99
100 Cache::CacheResultCodes Cache::appendCache(const filesystem::path& filePath, const vector<char>& data)
101 {
102 return this->appendCache(filePath, data.data());
103 }
104
105 Cache::CacheResultCodes Cache::appendCache(const filesystem::path& filePath, string_view data)
106 {
107 if (currentCacheSize + data.size() > cacheSize)
108 {
109 return CacheResultCodes::notEnoughCacheSize;
110 }
111
112 unique_lock<mutex> dataLock(cacheDataMutex);
113
114 if (auto it = cacheData.find(filePath); it != cacheData.end())
115 {
116 it->second += data;
117 }
118 else
119 {
120 cacheData[filePath] = data;
121 }
122
123 currentCacheSize += data.size();
124
125 return CacheResultCodes::noError;
126 }
127
128 bool Cache::contains(const filesystem::path& filePath) const
129 {
130 unique_lock<mutex> dataLock(cacheDataMutex);
131
132 return cacheData.contains(filePath);
133 }
134
135 void Cache::clear()
136 {
137 unique_lock<mutex> dataLock(cacheDataMutex);
138
139 currentCacheSize = 0;
140
141 cacheData.clear();
142 }
143
144 void Cache::clear(const filesystem::path& filePath)
145 {
146 unique_lock<mutex> dataLock(cacheDataMutex);
147 auto it = cacheData.find(filePath);
148
149 if (it == cacheData.end())
150 {
151 return;
152 }
153
154 currentCacheSize -= it->second.size();
155
156 cacheData.erase(it);
157 }
158
159 void Cache::setCacheSize(uint64_t sizeInBytes)
160 {
161 cacheSize = sizeInBytes;
162
163 if (cacheSize < currentCacheSize)
164 {
165 this->updateCache();
166 }
167 }
168
169 const string& Cache::getCacheData(const filesystem::path& filePath) const
170 {
171 unique_lock<mutex> dataLock(cacheDataMutex);
172 auto it = cacheData.find(filePath);
173
174 if (it == cacheData.end())
175 {
177 }
178
179 return it->second;
180 }
181
182 uint64_t Cache::getCacheSize() const
183 {
184 return cacheSize;
185 }
186
187 uint64_t Cache::getCurrentCacheSize() const
188 {
189 return currentCacheSize;
190 }
191
192 const string& Cache::operator [] (const filesystem::path& filePath) const
193 {
194 return this->getCacheData(filePath);
195 }
196}
CacheResultCodes
Result of adding cache.
Definition Cache.h:23
Cache & getCache()
Cache getter.
static FileManager & getInstance()
Singleton getter Also initialize thread pool with max threads for current hardware Default getter aft...