-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathArchipelago_DataStorageSim.cpp
More file actions
84 lines (76 loc) · 3.02 KB
/
Archipelago_DataStorageSim.cpp
File metadata and controls
84 lines (76 loc) · 3.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <cstdint>
#include <cstdio>
#include <functional>
#include <json/value.h>
#include <json/writer.h>
#include <map>
#include <string>
extern Json::Value sp_save_root;
extern std::string sp_save_path;
extern Json::FastWriter writer;
extern const int AP_OFFLINE_SLOT;
void WriteFileJSON(Json::Value root, std::string path);
bool parse_response(std::string msg, std::string &request);
void operation_replace(std::string key, Json::Value new_val) {
Json::Value& store = sp_save_root["store"];
store[key] = new_val;
}
void operation_or(std::string key, Json::Value new_val) {
Json::Value& store = sp_save_root["store"];
// Semi-broken implementation. Assumes integer or bool values atm
if (store[key].isInt64()) {
int64_t value = store[key].asInt64();
store[key] = value | new_val.asInt64();
} else if (store[key].isBool()) {
bool value = store[key].asBool();
store[key] = value || new_val.asBool();
}
}
void operation_default(std::string key, Json::Value new_val) {
// Noop as default is set in resolveDataStorageOp.
}
static std::map<const std::string, const std::function<void(std::string,Json::Value)>> known_operations = {
{"replace", operation_replace},
{"or", operation_or},
{"default", operation_default},
};
void resolveDataStorageOp(Json::Value dataOp) {
Json::Value& store = sp_save_root["store"];
if (dataOp["cmd"] == "Set") {
std::string key = dataOp["key"].asString();
Json::Value original_value = store.get(key, Json::nullValue);
if (!store.get(key, Json::nullValue) && dataOp.get("default", Json::nullValue)) {
// Key not in store. Set default
store[key] = dataOp["default"];
}
for (Json::Value atomicOp : dataOp["operations"]) {
std::string operation_str = atomicOp["operation"].asString();
Json::Value newVal = atomicOp["value"];
if (known_operations.count(operation_str))
known_operations[operation_str](key, newVal);
else
printf("AP: Unimplement Datastore operation \"%s\" in offline session!\n", operation_str.c_str());
}
WriteFileJSON(sp_save_root, sp_save_path);
if (dataOp["want_reply"].asBool()) {
Json::Value fake_msg;
std::string req;
fake_msg[0]["cmd"] = "SetReply";
fake_msg[0]["key"] = key;
fake_msg[0]["value"] = store[key];
fake_msg[0]["original_value"] = original_value;
fake_msg[0]["slot"] = AP_OFFLINE_SLOT;
parse_response(writer.write(fake_msg), req);
}
} else if (dataOp["cmd"] == "Get") {
Json::Value fake_msg;
std::string req;
fake_msg[0]["cmd"] = "Retrieved";
fake_msg[0]["keys"] = Json::objectValue;
for (Json::Value key_v : dataOp["keys"]) {
std::string key = key_v.asString();
fake_msg[0]["keys"][key] = sp_save_root.get(key, Json::nullValue);
}
parse_response(writer.write(fake_msg), req);
}
}