Skip to content

Commit

Permalink
✨ feat: add command keys
Browse files Browse the repository at this point in the history
  • Loading branch information
camargo2019 committed Sep 9, 2024
1 parent bbe4968 commit 41607c1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:

## 📚 Comandos Suportados

| Comando | Descrição |
|------------------------|----------------------------------------------------------|
| Comando | Descrição |
|------------------------|-----------------------------------------------------------|
| `SET key value` | Define um valor para a chave |
| `SET key value expire` | Define um valor com tempo de expiração |
| `GET key` | Retorna o valor associado à chave |
| `DEL key` | Remove uma chave |
| `AUTH user password` | Autentica o usuário com a senha |
| `USE database` | Especifica qual banco de dados será utilizado |
| `KEYS` | Lista todas as chaves registradas |

## 🌟 Contribuindo

Expand Down
10 changes: 10 additions & 0 deletions core/cache/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ bool Cache::del(std::string db, std::string key){
return false;
}

std::vector<std::string> Cache::keys(std::string db){
std::vector<std::string> keys;

for (const auto& row: cache_[db]){
keys.push_back(row.first);
}

return keys;
}

void Cache::save(){
std::ofstream file("data/databases.dat");

Expand Down
1 change: 1 addition & 0 deletions core/cache/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
std::string get(std::string db, std::string key);
bool set(std::string db, std::string key, std::string value, int expire = -1);
bool del(std::string db, std::string key);
std::vector<std::string> keys (std::string db);
void save();

private:
Expand Down
19 changes: 18 additions & 1 deletion core/manager/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ void Manager::invokeAction(){
invokeDel(args);
}

if (std::find(commands.keys.begin(), commands.keys.end(), command) != commands.keys.end()) {
invokeKeys(args);
}

data_.clear();
}

Expand Down Expand Up @@ -161,7 +165,7 @@ void Manager::invokeUse(std::vector<std::string> args){
}

void Manager::invokeAuth(std::vector<std::string> args){
if (!user.user.empty() && !user.db.empty()) return result(std::string("ERROR: you are not authenticated"));
if (!user.user.empty() && !user.db.empty()) return result(std::string("ERROR: you are authenticated"));
if (args[0].empty() || args[1].empty()) return result(std::string("ERROR: use AUTH USER PASS"));

for (const auto& auth : ConfigConn.auth.basic) {
Expand All @@ -176,6 +180,19 @@ void Manager::invokeAuth(std::vector<std::string> args){
result(std::string("ERROR: failed to authenticate"));
}

void Manager::invokeKeys(std::vector<std::string> args){
if (user.user.empty() && user.db.empty()) return result(std::string("ERROR: you need to authenticate"));
if (user.db == "*") return result(std::string("ERROR: use the `USE DB` command to choose the name of the database instance"));

std::string messageKeys;
std::vector<std::string> keys = cache_.keys(user.db);
for (const std::string& key : keys) {
messageKeys += key + "\n";
}

return result(std::string("SUCCESS: \r\n" + messageKeys));
}

void Manager::scheduleSave(){
if (!saveRunning_){
saveRunning_ = true;
Expand Down
2 changes: 2 additions & 0 deletions core/manager/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
std::vector<std::string> get = {"GET", "get"};
std::vector<std::string> auth = {"AUTH", "auth"};
std::vector<std::string> use = {"USE", "use"};
std::vector<std::string> keys = {"KEYS", "keys"};
};


Expand All @@ -59,6 +60,7 @@
void invokeGet(std::vector<std::string> args);
void invokeAuth(std::vector<std::string> args);
void invokeUse(std::vector<std::string> args);
void invokeKeys(std::vector<std::string> args);
void scheduleSave();

boost::asio::ip::tcp::socket socket_;
Expand Down

0 comments on commit 41607c1

Please sign in to comment.