-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbGate.h
More file actions
74 lines (65 loc) · 2.22 KB
/
dbGate.h
File metadata and controls
74 lines (65 loc) · 2.22 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
#pragma once
#include <iostream>
#include <stdexcept>
#include <string>
#include <cstring>
#include "ldsDb.h"
#include "ldsSnapshot.h"
#include "logger.h"
extern logger LOGGER;
class dbGate {
public:
ldsDb *ledisDb;
ldsSnapshot *ledisSnapshot;
dbGate() {
ledisDb = new ldsDb{};
ledisSnapshot = new ldsSnapshot{};
}
~dbGate() {
delete ledisDb;
delete ledisSnapshot;
}
int parseAndExecute(const std::string &cmdStr, ldsRet &ret) {
try {
ldsCmd cmd = parseCmd(cmdStr);
if (cmd.cmd == CMD_EXIT) {
return -1;
}
ledisDb->execute(cmd, ret);
if (ret.type == RET_UNKNOWN) {
switch (cmd.cmd) {
case CMD_SNAPSHOT:
LOGGER.info("[COMMAND] Save");
if (cmd.args != nullptr && strlen(cmd.args) > 0)
throw std::runtime_error("Save command does not take arguments");
if (!ledisSnapshot->createSnapshot(*ledisDb))
throw std::runtime_error("Failed to create snapshot");
ret.type = RET_OK;
ret.ptr = nullptr;
break;
case CMD_RESTORE: {
LOGGER.info("[COMMAND] Restore");
auto tmpDb = ledisSnapshot->restoreSnapshot();
if (tmpDb != nullptr) {
delete ledisDb;
ledisDb = tmpDb;
ret.type = RET_OK;
ret.ptr = nullptr;
} else
throw std::runtime_error("Failed to restore snapshot");
break;
}
default:
throw std::runtime_error("Unknown command");
}
}
ledisSnapshot->addCmd(cmd);
return 1;
} catch (const std::exception &e) {
ret.type = RET_ERR;
ret.ptr = new std::string(e.what());
LOGGER.error("[ERROR] " + std::string(e.what()));
return 0;
}
}
};