10#ifndef COMMAND_PARSER_HPP
11#define COMMAND_PARSER_HPP
69 static std::vector<std::string> split(
const std::string &str) {
70 std::vector<std::string> tokens;
72 bool inQuotes =
false;
73 for (
size_t i = 0; i < str.length(); ++i) {
77 }
else if (std::isspace(c) && !inQuotes) {
79 tokens.push_back(token);
87 tokens.push_back(token);
102 static bool validate_set(
const std::vector<std::string> &tokens,
ValidationResult &result) {
103 if (tokens.size() != 3) {
105 result.
errmsg =
"wrong number of arguments for 'set' command";
108 if (tokens[1].empty()) {
110 result.
errmsg =
"invalid key";
113 result.
key = tokens[1];
114 result.
value = tokens[2];
129 static bool validate_get(
const std::vector<std::string> &tokens,
ValidationResult &result) {
130 if (tokens.size() != 2) {
132 result.
errmsg =
"wrong number of arguments for 'get' command";
135 if (tokens[1].empty()) {
137 result.
errmsg =
"invalid key";
140 result.
key = tokens[1];
155 static bool validate_del(
const std::vector<std::string> &tokens,
ValidationResult &result) {
156 if (tokens.size() != 2) {
158 result.
errmsg =
"wrong number of arguments for 'del' command";
161 if (tokens[1].empty()) {
163 result.
errmsg =
"invalid key";
166 result.
key = tokens[1];
180 return "Available commands:\n"
181 " SET <key> <value> - Set key to hold the string value\n"
182 " GET <key> - Get the value of key\n"
183 " DEL <key> - Delete a key\n"
184 " help - Show this help menu\n"
185 " exit - Exit the program\n"
186 " clear - Clear the screen\n";
214 std::string
parser(
const std::string &line) {
216 std::vector<std::string> tokens = split(line);
217 if (tokens.empty()) {
222 std::string command = tokens[0];
223 std::transform(command.begin(), command.end(), command.begin(), ::tolower);
225 if (command ==
"set") {
226 if (!validate_set(tokens, result)) {
231 }
else if (command ==
"get") {
232 if (!validate_get(tokens, result)) {
235 std::pair<bool, std::string> found_value =
db.get(result.
key);
236 if (!found_value.first) {
239 return found_value.second +
"\n";
240 }
else if (command ==
"del") {
241 if (!validate_del(tokens, result)) {
244 std::pair<bool, std::string> found_value =
db.get(result.
key);
245 if (!found_value.first) {
246 return "key \"" + result.
key +
"\" not found\n";
248 db.remove(result.
key);
250 }
else if (command ==
"help") {
252 }
else if (command ==
"exit") {
254 }
else if (command ==
"clear") {
255 return "\033[2J\033[1;1H";
259 }
catch (
const std::exception &e) {
LSMTree db
Database engine instance.
Definition command_parser.hpp:196
CommandParser()=default
Default constructor for CommandParser.
std::string parser(const std::string &line)
Parses and executes a command.
Definition command_parser.hpp:214
LSM Tree implementation.
Definition lsm.hpp:37
static std::string simpleString(const std::string &str)
Converts a simple string to RESP format.
Definition resp_encoder.hpp:30
static std::string error(const std::string &message)
Converts an error message to RESP format.
Definition resp_encoder.hpp:44
static std::string integer(int value)
Used to encode an integer to RESP format.
Definition resp_encoder.hpp:57
Holds the result of command validation.
Definition command_parser.hpp:27
bool success
Indicates whether the validation was successful.
Definition command_parser.hpp:32
ValidationResult()
Default constructor initializes the result to failure.
Definition command_parser.hpp:48
std::string errmsg
Error message if validation failed.
Definition command_parser.hpp:36
std::string key
Key associated with the command.
Definition command_parser.hpp:40
std::string value
Value associated with the command.
Definition command_parser.hpp:44
Used to encode messages into RESP (REdis Serialization Protocol) format.