diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a45190..d83054e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,9 @@ add_executable(cpp_redis main.cpp controller.h tcp_server.cpp tcp_server.h - overloaded.h) + overloaded.h + datastore.h + datastore.cpp) target_link_libraries(cpp_redis PRIVATE spdlog::spdlog_header_only) \ No newline at end of file diff --git a/src/controller.cpp b/src/controller.cpp index 07f0f85..4a4345b 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -27,6 +27,10 @@ RedisType::RedisValue Controller::handleCommand(const std::vector &command) { + if (command.size() != 3) { return RedisType::SimpleError("ERR wrong number of arguments for 'set' command"); } + + auto key = extractStringFromBytes(*command[1].data, 0, (*command[0].data).size()); + auto val = extractStringFromBytes(*command[2].data, 0, (*command[0].data).size()); + + dataStore.store[key] = val; + + return RedisType::BulkString("OK"); +} +RedisType::RedisValue Controller::handleGet(const std::vector &command) { + if (command.size() != 2) { return RedisType::SimpleError("ERR wrong number of arguments for 'get' command"); } + + auto key = extractStringFromBytes(*command[1].data, 0, (*command[0].data).size()); + + auto val = dataStore.store[key]; + + return RedisType::BulkString(val); +} diff --git a/src/controller.h b/src/controller.h index 773a63d..8fd91ed 100644 --- a/src/controller.h +++ b/src/controller.h @@ -1,5 +1,6 @@ #pragma once +#include "datastore.h" #include "redis_type.h" class Controller { @@ -9,4 +10,9 @@ class Controller { private: RedisType::RedisValue handleEcho(const std::vector &command); RedisType::RedisValue handlePing(const std::vector &command); + RedisType::RedisValue handleSet(const std::vector &command); + RedisType::RedisValue handleGet(const std::vector &command); + + + DataStore dataStore; }; diff --git a/src/datastore.cpp b/src/datastore.cpp new file mode 100644 index 0000000..2aa2dd8 --- /dev/null +++ b/src/datastore.cpp @@ -0,0 +1 @@ +#include "datastore.h" \ No newline at end of file diff --git a/src/datastore.h b/src/datastore.h new file mode 100644 index 0000000..457e173 --- /dev/null +++ b/src/datastore.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +class DataStore { +public: + std::unordered_map store; +}; \ No newline at end of file