Skip to content

Commit

Permalink
Handle EXISTS command
Browse files Browse the repository at this point in the history
  • Loading branch information
mleleszi committed Jun 11, 2024
1 parent 9af929e commit 9d0c95b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <algorithm>
#include <iostream>
#include <numeric>

#include "controller.h"
#include "protocol.h"
Expand Down Expand Up @@ -31,6 +32,8 @@ RedisType::RedisValue Controller::handleCommand(const std::vector<RedisType::Bul
return handleSet(command);
} else if (commandType == "GET") {
return handleGet(command);
} else if (commandType == "EXISTS") {
return handleExists(command);
}

return RedisType::SimpleError("ERR unsupported command");
Expand All @@ -57,6 +60,9 @@ RedisType::RedisValue Controller::handleSet(const std::vector<RedisType::BulkStr

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"); }

Expand All @@ -68,3 +74,14 @@ RedisType::RedisValue Controller::handleGet(const std::vector<RedisType::BulkStr

return RedisType::BulkString();
}
RedisType::RedisValue Controller::handleExists(const std::vector<RedisType::BulkString> &command) {
if (command.size() < 2) { return RedisType::SimpleError("ERR wrong number of arguments for 'exists' command"); }

int count =
std::accumulate(command.begin() + 1, command.end(), 0, [this](int acc, const RedisType::BulkString &cmd) {
auto key = extractStringFromBytes(*cmd.data, 0, cmd.data->size());
return acc + (dataStore.exists(key) ? 1 : 0);
});

return RedisType::Integer(count);
}
1 change: 1 addition & 0 deletions src/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Controller {
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);
RedisType::RedisValue handleExists(const std::vector<RedisType::BulkString> &command);


DataStore dataStore;
Expand Down
2 changes: 2 additions & 0 deletions src/datastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class DataStore {

void set(const std::string &key, const std::string &val) { store[key] = val; }

bool exists(const std::string &key) { return store.contains(key); }


private:
std::unordered_map<std::string, std::string> store;
Expand Down
1 change: 1 addition & 0 deletions src/redis_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace RedisType {

struct Integer {
std::int64_t data;
Integer(const int64_t &data) : data(data) {}
};

struct SimpleError {
Expand Down

0 comments on commit 9d0c95b

Please sign in to comment.