Skip to content

Commit

Permalink
Simple get and set impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mleleszi committed Jun 8, 2024
1 parent 1fb0c66 commit b55cef8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 23 additions & 0 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ RedisType::RedisValue Controller::handleCommand(const std::vector<RedisType::Bul
return handleEcho(command);
} else if (commandType == "PING") {
return handlePing(command);
} else if (commandType == "SET") {
return handleSet(command);
} else if (commandType == "GET") {
return handleGet(command);
}

return RedisType::SimpleError("ERR unsupported command");
Expand All @@ -43,3 +47,22 @@ RedisType::RedisValue Controller::handlePing(const std::vector<RedisType::BulkSt

return RedisType::BulkString("PONG");
}
RedisType::RedisValue Controller::handleSet(const std::vector<RedisType::BulkString> &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<RedisType::BulkString> &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);
}
6 changes: 6 additions & 0 deletions src/controller.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "datastore.h"
#include "redis_type.h"

class Controller {
Expand All @@ -9,4 +10,9 @@ class Controller {
private:
RedisType::RedisValue handleEcho(const std::vector<RedisType::BulkString> &command);
RedisType::RedisValue handlePing(const std::vector<RedisType::BulkString> &command);
RedisType::RedisValue handleSet(const std::vector<RedisType::BulkString> &command);
RedisType::RedisValue handleGet(const std::vector<RedisType::BulkString> &command);


DataStore dataStore;
};
1 change: 1 addition & 0 deletions src/datastore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "datastore.h"
8 changes: 8 additions & 0 deletions src/datastore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <unordered_map>

class DataStore {
public:
std::unordered_map<std::string, std::string> store;
};

0 comments on commit b55cef8

Please sign in to comment.