-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgent_level.cpp
More file actions
executable file
·283 lines (266 loc) · 7.7 KB
/
gent_level.cpp
File metadata and controls
executable file
·283 lines (266 loc) · 7.7 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "prefine.h"
#include "gent_util.h"
#include "gent_level.h"
#include "gent_db.h"
#include "gent_list.h"
#include "gent_app_mgr.h"
GentLevel::GentLevel(GentConnect *c):GentCommand(c)
{
keystr = "";
remains = 0;
max_tokens = 1000;
}
GentLevel::~GentLevel()
{}
uint8_t GentLevel::Split(const string &str, const string &delimit, vector<string> &v) {
uint64_t pos,last_pos=0;
uint8_t num = 0;
while((pos = str.find_first_of(delimit,last_pos)) != string::npos){
if(pos == last_pos){
last_pos++;
}else{
v.push_back(str.substr(last_pos, pos-last_pos));
num++;
last_pos = pos+1;
}
}
size_t ter_pos = str.find_first_of("\r\n",last_pos);
if(ter_pos == string::npos) {
return 0;
}
if(ter_pos>last_pos){
string curstr = str.substr(last_pos, ter_pos-last_pos);
v.push_back(curstr);
num++;
}
/*
vector<string>::iterator iter;
for(iter=v.begin(); iter!=v.end();iter++){
cout<<"split:" << *iter << endl;
}
*/
return num;
}
int GentLevel::CommandWord() {
//vector<string> tokenList;
tokenList.clear();
uint8_t clength = Split(commandstr, " ", tokenList);
LOG(GentLog::INFO, "tokenList clength: %d", clength);
if(clength == 2 && tokenList[0] == "get") {
LOG(GentLog::INFO, "tokenList get");
commandtype = CommandType::COMM_GET;
keystr = tokenList[1];
conn->SetStatus(Status::CONN_DATA);
return 0;
}else if(clength > 2 && tokenList[0] == "get") {
if(clength > max_tokens) {
return -1;
}
keystr = "";
keys.clear();
for(vector<string>::size_type i=1; i!=tokenList.size(); i++) {
keys.push_back(tokenList[i]);
}
LOG(GentLog::INFO, "tokenList multi get");
commandtype = CommandType::COMM_GET;
conn->SetStatus(Status::CONN_DATA);
return 0;
}else if(clength == 5 && tokenList[0] == "set") {
LOG(GentLog::INFO, "the command is set and the key is %s", tokenList[1].c_str());
keystr = tokenList[1];
commandtype = CommandType::COMM_SET;
conn->SetStatus(Status::CONN_NREAD);
int vlen;
if(!GentUtil::SafeStrtol(tokenList[4].c_str(), (int32_t *)&vlen)) {
LOG(GentLog::WARN, "the length of set's command is failed");
return -1;
}
LOG(GentLog::INFO, "need read the lenth of client's data is %d,and the key is %s", vlen, tokenList[1].c_str());
rlbytes = vlen+2;
return rlbytes-remains;
return rlbytes;
}else if(clength == 2 && tokenList[0] == "del") {
LOG(GentLog::INFO, "the command is del and the key is %s", tokenList[1].c_str());
commandtype = CommandType::COMM_DEL;
keystr = tokenList[1];
conn->SetStatus(Status::CONN_DATA);
return 0;
}else if(clength == 1 && tokenList[0] == "quit") {
LOG(GentLog::INFO, "the command is quit");
commandtype = CommandType::COMM_QUIT;
conn->SetStatus(Status::CONN_CLOSE);
return 0;
}else if(clength ==2 && tokenList[0] == "stats") {
LOG(GentLog::INFO, "the command is stats");
commandtype = CommandType::COMM_STATS;
keystr = tokenList[1];
conn->SetStatus(Status::CONN_DATA);
return 0;
}else if(clength == 1 && tokenList[0] == "stats") {
LOG(GentLog::INFO, "the command is stats");
commandtype = CommandType::COMM_STATS;
keystr = "";
conn->SetStatus(Status::CONN_DATA);
return 0;
}
return -1;
}
int GentLevel::ParseCommand(const string &str) {
if(str.size() == 0) return 0;
uint64_t pos = str.find_first_of("\n");
if(pos == string::npos || pos == 0) return 0;
commandstr = str.substr(0, pos);
content = "";
remains = str.size()-pos-1;
content.assign(str.substr(pos+1,remains));
return 1;
}
int GentLevel::Process(const char *rbuf, uint64_t size, string &outstr) {
if(size <=0 ) {
outstr = "ERROR\r\n";
return 0;
}
string data = string(rbuf, size);
if(!ParseCommand(data)) {
outstr = "ERROR\r\n";
return 0;
}
int cword = CommandWord();
if(cword < 0) {
outstr = "CLIENT ERROR\r\n";
return 0;
}
if(cword == 0) return cword;
return cword;
}
void GentLevel::ProcessDel(string &outstr)
{
/*
if(!GentList::Instance()->Load(keystr)) {
outstr = "NOT_FOUND\r\n";
return;
}
*/
string nr = "";
if(!GentDb::Instance()->Get(keystr, nr)){
outstr = "NOT_FOUND\r\n";
return;
}
//GentList::Instance()->Clear(keystr);
if(!GentDb::Instance()->Del(keystr)) {
outstr = "NOT_FOUND\r\n";
}else{
outstr = "DELETED\r\n";
}
}
void GentLevel::ProcessGet(string &outstr)
{
if(keystr == "") {
ProcessMultiGet(outstr);
return;
}
string nr="";
if(!GentDb::Instance()->Get(keystr, nr))
{
outstr = "END\r\n";
}
char retbuf[200]={0};
snprintf(retbuf,200, "VALUE %s 0 %u\r\n",keystr.c_str(), (unsigned int)nr.size());
outstr = retbuf;
outstr += nr+"\r\nEND\r\n";
}
void GentLevel::ProcessMultiGet(string &outstr)
{
vector<string>::iterator iter;
for(iter=keys.begin(); iter!=keys.end(); iter++)
{
string nr="";
if(!GentDb::Instance()->Get(*iter, nr))
{
continue;
}
char retbuf[200]={0};
snprintf(retbuf,200, "VALUE %s 0 %u\r\n",(*iter).c_str(), (unsigned int)nr.size());
outstr += retbuf;
outstr += nr+"\r\n";
}
outstr += "END\r\n";
}
void GentLevel::ProcessStats(string &outstr)
{
char retbuf[200] = {0};
uint64_t num = GentDb::Instance()->Count(keystr);
uint64_t totals = GentDb::Instance()->TotalSize();
snprintf(retbuf,200,"total_connect: %u\r\ncurrent_connect: %u\r\nitem_nums: %llu\r\ntotal_size: %llu\r\n",
(unsigned int)GentAppMgr::Instance()->GetTotalConnCount(),
(unsigned int)GentAppMgr::Instance()->GetConnCount(),
(unsigned long long)num, (unsigned long long)totals);
outstr = retbuf;
}
void GentLevel::Complete(string &outstr, const char *recont, uint64_t len)
{
char buf[20]={0};
switch(commandtype)
{
case CommandType::COMM_GET:
//NOT_FOUND
//if(keystr!="" && !GentList::Instance()->Load(keystr)) {
// outstr += "END\r\n";
// break;
//}
ProcessGet(outstr);
break;
case CommandType::COMM_SET:
//NOT_STORED
LOG(GentLog::WARN, "commandtype::comm_set");
content += string(recont,len);
if(content.substr(rlbytes-2,2)!="\r\n") {
outstr = "CLIENT_ERROR bad data chunk\r\n";
LOG(GentLog::WARN, "CLIENT_ERROR bad data chunk");
}else{
string nr;
nr.assign(content.c_str(), rlbytes-2);
if(!GentDb::Instance()->Put(keystr, nr,0, 0)) {
outstr = "NOT_STORED\r\n";
}else{
//GentList::Instance()->Save(keystr);
LOG(GentLog::WARN, "commandtype::comm_set stored");
sprintf(buf,"STORED\r\n");
//outstr = "STORED\r\n";
outstr.assign(buf,8);
}
//outstr = "STORED\r\n";
}
break;
case CommandType::COMM_QUIT:
break;
case CommandType::COMM_DEL:
ProcessDel(outstr);
break;
case CommandType::COMM_STATS:
ProcessStats(outstr);
break;
default:
outstr = "ERROR\r\n";
return;
}
}
GentCommand *GentLevel::Clone(GentConnect *connect)
{
return new GentLevel(connect);
}
bool GentLevel::Init(string &msg)
{
if(!GentDb::Instance()->Init(msg))
{
LOG(GentLog::ERROR, "db init fail,%s",msg.c_str());
return false;
}
//GentList::Instance()->Init();
return true;
}
void GentLevel::AssignVal(token_t *tokens)
{
string tmp(tokens[1].value);
keystr.assign(tmp,0,tmp.size());
}