-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrystal.cpp
More file actions
70 lines (67 loc) · 2.14 KB
/
crystal.cpp
File metadata and controls
70 lines (67 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "crystal.h"
std::string crystalserver::execute(const std::string& command) {
std::string response;
if (command.find("GET:") == 0) {
std::string key = command.substr(4);
if (cache.find(key) != cache.end()) {
response = cache[key];
}
else {
response = "Key not found in cache";
}
}
else if (command.find("SET:") == 0) {
std::string data = command.substr(4);
size_t pos = data.find(':');
if (pos != std::string::npos) {
std::string key = data.substr(0, pos);
std::string value = data.substr(pos + 1);
cache[key] = value;
response = "Key " + key + " set with value " + value;
}
else {
response = "Invalid data format";
}
}
else if (command.find("REWRITE:") == 0) {
std::string data = command.substr(8);
size_t pos = data.find(':');
if (pos != std::string::npos) {
std::string key = data.substr(0, pos);
std::string value = data.substr(pos + 1);
if (cache.find(key) != cache.end()) {
cache[key] = value;
response = "Key " + key + " rewritten with value " + value;
}
else {
response = "Key not found in cache";
}
}
else {
response = "Invalid data format";
}
}
else if (command.find("RENAME:") == 0) {
std::string data = command.substr(7);
size_t pos = data.find(':');
if (pos != std::string::npos) {
std::string oldKey = data.substr(0, pos);
std::string newKey = data.substr(pos + 1);
if (cache.find(oldKey) != cache.end()) {
cache[newKey] = cache[oldKey];
cache.erase(oldKey);
response = "Key " + oldKey + " renamed to " + newKey;
}
else {
response = "Key not found in cache";
}
}
else {
response = "Invalid data format";
}
}
else {
response = "Invalid command";
}
return response;
}