diff --git a/kv_store.hpp b/kv_store.hpp index f1ccb5e..17a848f 100644 --- a/kv_store.hpp +++ b/kv_store.hpp @@ -5,20 +5,29 @@ #include #include #include +#include +#include using std::string; class KeyValueStore { public: - void set(const string& key, const string& value) { + using Value = std::variant; + + void set(const string& key, const Value& value) { std::lock_guard lock(mutex_); data_[key] = value; } - std::optional get(const string& key) { + template + std::optional get(const string& key) { std::lock_guard lock(mutex_); auto it = data_.find(key); - return it != data_.end() ? std::optional{it->second} : std::nullopt; + if (it == data_.end()) return std::nullopt; + if (auto* ptr = std::get_if(&it->second)) { + return *ptr; + } + return std::nullopt; } bool del(const string& key) { @@ -43,8 +52,17 @@ public: string line; while (std::getline(file, line)) { size_t eq = line.find('='); - if (eq != string::npos) { - data_[line.substr(0, eq)] = line.substr(eq + 1); + if (eq == string::npos || eq < 2) continue; + + string key = line.substr(2, eq - 2); + string val_str = line.substr(eq + 1); + char type = line[0]; + + switch (type) { + case 'i': data_[key] = std::stoi(val_str); break; + case 'd': data_[key] = std::stod(val_str); break; + case 's': data_[key] = val_str; break; + case 'b': data_[key] = (val_str == "1"); break; } } } @@ -55,12 +73,27 @@ public: if (!file.is_open()) return; for (const auto& [key, value] : data_) { - file << key << "=" << value << "\n"; + file << serialize_entry(key, value) << "\n"; } } private: - std::unordered_map data_; + string serialize_entry(const string& key, const Value& value) { + return std::visit([&key](const auto& val) -> string { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return "i:" + key + "=" + std::to_string(val); + } else if constexpr (std::is_same_v) { + return "d:" + key + "=" + std::to_string(val); + } else if constexpr (std::is_same_v) { + return "s:" + key + "=" + val; + } else if constexpr (std::is_same_v) { + return "b:" + key + "=" + (val ? "1" : "0"); + } + }, value); + } + + std::unordered_map data_; mutable std::mutex mutex_; string filename_ = "store.txt"; }; diff --git a/main.cpp b/main.cpp index 8f020b0..d339e9f 100644 --- a/main.cpp +++ b/main.cpp @@ -61,9 +61,9 @@ int main() { }); router.get("/admin/counter", [](const HttpRequest& req, HttpResponse& res) { - auto current = server->store.get("visit_count"); - int count = current ? std::stoi(current.value()) : 0; - server->store.set("visit_count", std::to_string(count + 1)); + auto current = server->store.get("visit_count"); + int count = current ? current.value() : 0; + server->store.set("visit_count", count + 1); res.set_text("Counter: " + std::to_string(count + 1)); });