-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_state_machine.cc
174 lines (139 loc) · 4.38 KB
/
raft_state_machine.cc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "raft_state_machine.h"
#include <sstream>
#include <iomanip>
#include <cstring>
kv_command::kv_command() : kv_command(CMD_NONE, "", "") { }
kv_command::kv_command(command_type tp, const std::string &key, const std::string &value) :
cmd_tp(tp), key(key), value(value), res(std::make_shared<result>())
{
res->start = std::chrono::system_clock::now();
res->key = key;
}
kv_command::kv_command(const kv_command &cmd) :
cmd_tp(cmd.cmd_tp), key(cmd.key), value(cmd.value), res(cmd.res) {}
kv_command::~kv_command() { }
int kv_command::size() const {
// Your code here:
size_t res = std::to_string(cmd_tp).size() + 1
+ std::to_string(key.size()).size() + 1
+ key.size() + 1
+ std::to_string(value.size()).size() + 1
+ value.size();
return res;
}
void kv_command::serialize(char* buf, int size) const {
// Your code here:
if (size != this->size()) {
printf("serialize failed: size unmatch\n");
return;
}
std::string s;
s = std::to_string(cmd_tp) + " "
+ std::to_string(key.size()) + " "
+ key + " "
+ std::to_string(value.size()) + " "
+ value;
std::strncpy(buf, s.c_str(), s.size());
return;
}
void kv_command::deserialize(const char* buf, int size) {
// Your code here:
std::string s(buf, size);
std::stringstream ss(s);
int key_size = 0, value_size = 0;
int type_ = 0;
ss >> type_;
ss >> key_size;
ss.seekg(1, std::ios_base::cur);
// prevent \0
ss >> std::setw(key_size) >> key;
ss >> value_size;
ss.seekg(1, std::ios_base::cur);
ss >> std::setw(value_size) >> value;
cmd_tp = static_cast<kv_command::command_type>(type_);
return;
}
marshall& operator<<(marshall &m, const kv_command& cmd) {
// Your code here:
int type_ = cmd.cmd_tp;
m << type_ << cmd.key << cmd.value;
return m;
}
unmarshall& operator>>(unmarshall &u, kv_command& cmd) {
// Your code here:
int type_;
u >> type_;
cmd.cmd_tp = static_cast<kv_command::command_type>(type_);
u >> cmd.key;
u >> cmd.value;
return u;
}
kv_state_machine::~kv_state_machine() {
}
void kv_state_machine::apply_log(raft_command &cmd) {
kv_command &kv_cmd = dynamic_cast<kv_command&>(cmd);
std::unique_lock<std::mutex> lock(kv_cmd.res->mtx);
// Your code here:
kv_cmd.res->done = true;
auto iter = kv.find(kv_cmd.key);
switch (kv_cmd.cmd_tp)
{
case kv_command::CMD_GET:
kv_cmd.res->value = (iter == kv.end()) ? "" : iter->second;
kv_cmd.res->succ = (kv_cmd.res->value.size()) ? true : false;
break;
case kv_command::CMD_DEL:
kv_cmd.res->value = (iter == kv.end()) ? "" : iter->second;
kv.erase(iter);
kv_cmd.res->succ = (kv_cmd.res->value.size()) ? true : false;
break;
case kv_command::CMD_PUT:
kv_cmd.res->value = (iter == kv.end()) ? kv_cmd.value : iter->second;
kv[kv_cmd.key] = kv_cmd.value;
kv_cmd.res->succ = (kv_cmd.res->value == kv_cmd.value) ? true : false;
break;
default:
break;
}
kv_cmd.res->cv.notify_all();
return;
}
std::vector<char> kv_state_machine::snapshot() {
// Your code here:
std::unique_lock<std::mutex> lock(mtx);
std::vector<char> data;
std::stringstream ss;
int kv_size = kv.size();
ss << kv_size;
for (auto iter : kv) {
int key_size = iter.first.size();
int value_size = iter.second.size();
ss << " " << key_size << " " << iter.first
<< " " << value_size << " " << iter.second;
}
std::string str = ss.str();
data.assign(str.begin(), str.end());
return data;
}
void kv_state_machine::apply_snapshot(const std::vector<char>& snapshot) {
// Your code here:
std::unique_lock<std::mutex> lock(mtx);
std::string str;
str.assign(snapshot.begin(), snapshot.end());
std::stringstream ss(str);
kv.clear();
int kv_size = 0;
ss >> kv_size;
for (int i = 0; i < kv_size; ++i) {
int key_size = 0, value_size = 0;
std::string key,value;
ss >> key_size;
ss.seekg(1, std::ios_base::cur);
ss >> std::setw(key_size) >> key;
ss >> value_size;
ss.seekg(1, std::ios_base::cur);
ss >> std::setw(value_size) >> value;
kv[key] = value;
}
return;
}