-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCacheManager.h
More file actions
102 lines (89 loc) · 3.2 KB
/
FileCacheManager.h
File metadata and controls
102 lines (89 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// Created by duni on 12/01/2020.
//
#ifndef PROBLEMSOLVER_FILECHACHEMANAGER_H
#define PROBLEMSOLVER_FILECHACHEMANAGER_H
#include <fstream>
#include <mutex>
#include "CacheManager.h"
using namespace std;
template<class Problem>
class FileCacheManager : public CacheManager<Problem, string> {
private:
std::mutex fileLock;
public:
// This function checks the cache if a current solution exists and returns the result
bool doesSolutionExist(Problem p, string algorithmName) {
fileLock.lock();
ifstream file;
string strFileName = "";
char currentChar;
int i;
// Preform hash function on the problem to save the file in a unique way
std::size_t fileName = std::hash<std::string>{}(p);
strFileName = std::to_string(fileName);
// Concatenate the algorithm name which solved the problem to the result of the hash function
strFileName += algorithmName;
// Concatenate the ending of the file name
strFileName.append(".txt");
file.open(strFileName);
// Check if the wanted file exists in the cache
bool doesExist = file.good();
file.close();
fileLock.unlock();
return doesExist;
}
// This function saves the solution in the cache
void saveSolution(Problem p, string s, string algorithmName) {
fileLock.lock();
ofstream file;
string strFileName = "";
char currentChar;
int i;
// Preform hash function on the problem to save the file in a unique way
std::size_t fileName = std::hash<std::string>{}(p);
strFileName = std::to_string(fileName);
// Concatenate the algorithm name which solved the problem to the result of the hash function
strFileName += algorithmName;
// Concatenate the ending of the file name
strFileName.append(".txt");
// Open the file
file.open(strFileName, ios::app);
if (file.is_open()) {
// Write the solution into the file
file << s << endl;
//file.write((char *) &s, sizeof(s));
} else {
throw "could not open file";
}
file.close();
fileLock.unlock();
}
// This function returns the solution from the cache
string getSolution(Problem p, string algorithmName) {
fileLock.lock();
ifstream file;
string strFileName;
char currentChar;
int i;
string s;
// Preform hash function on the problem to save the file in a unique way
std::size_t fileName = std::hash<std::string>{}(p);
strFileName = std::to_string(fileName);
// Concatenate the algorithm name which solved the problem to the result of the hash function
strFileName += algorithmName;
// Concatenate the ending of the file name
strFileName.append(".txt");
// Open the file that contains the solution
file.open(strFileName, ios::in);
if (file.is_open()) {
getline(file, s);
} else {
throw "object doesnt exist in cache or files";
}
file.close();
fileLock.unlock();
return s;
}
};
#endif //PROBLEMSOLVER_FILECHACHEMANAGER_H