Skip to content

Commit

Permalink
fix set bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mleleszi committed Jun 12, 2024
1 parent 9d0c95b commit 99f4cf4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,19 @@ RedisType::RedisValue Controller::handlePing(const std::vector<RedisType::BulkSt
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());
auto key = extractStringFromBytes(*command[1].data, 0, (*command[1].data).size());
auto val = extractStringFromBytes(*command[2].data, 0, (*command[2].data).size());

dataStore.set(key, val);

return RedisType::SimpleString("OK");
}

// TODO: WORKS INCORRECTLY FOR INTEGERS
// TODO: set and get works incorrectly when ends with number??
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 key = extractStringFromBytes(*command[1].data, 0, (*command[1].data).size());

auto valueOpt = dataStore.get(key);

Expand Down
1 change: 1 addition & 0 deletions src/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ TCPServer::TCPServer(const Controller &controller) : controller{controller} {
}
}

// TODO: why can it start if the port is already taken?
[[noreturn]] void TCPServer::start(const std::string &address = "0.0.0.0", int port = 6379) {
struct sockaddr_in server_addr {};
server_addr.sin_family = AF_INET;
Expand Down
24 changes: 24 additions & 0 deletions tests/controller_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,27 @@ TEST(ControllerTests, HandleGET) {

ASSERT_EQ(bulkData, stringToByteVector("val"));
}

TEST(ControllerTests, HandleGETEndWithNum) {
Controller controller;

std::vector<RedisType::BulkString> setCommand{
RedisType::BulkString("SET"),
RedisType::BulkString("key2"),
RedisType::BulkString("val2"),
};

controller.handleCommand(setCommand);

std::vector<RedisType::BulkString> getCommand{
RedisType::BulkString("GET"),
RedisType::BulkString("key2"),
};

auto result = controller.handleCommand(getCommand);

ASSERT_TRUE(std::holds_alternative<RedisType::BulkString>(result));
auto bulkData = *std::get<RedisType::BulkString>(result).data;

ASSERT_EQ(bulkData, stringToByteVector("val2"));
}

0 comments on commit 99f4cf4

Please sign in to comment.