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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
#include "IStorageBackend.hpp"
#include <string>
#include <vector>
#include <fstream>
#include <memory>
#include <unordered_map>
namespace TestProject {
/**
* @brief File-based storage implementation
*
* This implementation stores data in a simple text file format.
* Data persists between program runs but has slower access times.
*/
class FileStorage : public IStorageBackend {
public:
/**
* @brief Construct a new FileStorage instance
* @param filename The file to use for storage (default: "storage.txt")
*/
explicit FileStorage(const std::string& filename = "storage.txt");
/**
* @brief Destructor - ensures data is flushed to disk
*/
~FileStorage() override;
/**
* @brief Store a key-value pair to file
* @param key The key to store
* @param value The value to associate with the key
* @return true if successful, false on I/O error
*/
bool store(const std::string& key, const std::string& value) override;
/**
* @brief Retrieve a value by key from file
* @param key The key to look up
* @return The associated value, or empty string if not found
*/
std::string retrieve(const std::string& key) const override;
/**
* @brief Remove a key-value pair from file
* @param key The key to remove
* @return true if the key was found and removed, false otherwise
*/
bool remove(const std::string& key) override;
/**
* @brief List all stored keys in file
* @return Vector of all keys in the storage
*/
std::vector<std::string> listKeys() const override;
/**
* @brief Clear all stored data from file
*/
void clear() override;
/**
* @brief Get the backend type name
* @return String identifying this as file storage
*/
std::string getBackendType() const override;
#ifdef ENABLE_DEBUG_LOGGING
/**
* @brief Get debug information about file storage state
* @return Debug information string
*/
std::string getDebugInfo() const override;
#endif
/**
* @brief Get the storage filename
* @return The filename used for storage
*/
const std::string& getFilename() const;
/**
* @brief Check if the storage file exists
* @return true if the file exists and is accessible
*/
bool fileExists() const;
/**
* @brief Flush any pending changes to disk
* @return true if successful, false on I/O error
*/
bool flush();
private:
std::string filename_;
mutable size_t read_count_ = 0; // Track read operations for debugging
mutable size_t write_count_ = 0; // Track write operations for debugging
/**
* @brief Load all data from file into memory temporarily
* @return Map of key-value pairs from file
*/
std::unordered_map<std::string, std::string> loadFromFile() const;
/**
* @brief Save all data from memory map to file
* @param data The data to save
* @return true if successful, false on I/O error
*/
bool saveToFile(const std::unordered_map<std::string, std::string>& data);
/**
* @brief Escape special characters in strings for file storage
* @param str The string to escape
* @return Escaped string
*/
std::string escapeString(const std::string& str) const;
/**
* @brief Unescape special characters from file storage
* @param str The string to unescape
* @return Unescaped string
*/
std::string unescapeString(const std::string& str) const;
};
} // namespace TestProject