-
Notifications
You must be signed in to change notification settings - Fork 10
/
worker.cc
349 lines (328 loc) · 11 KB
/
worker.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "worker.h"
namespace superfastmatch{
struct RESTRequest{
const HTTPClient::Method& verb;
const string& path;
const map<string, string>& reqheads;
const string& reqbody;
map<string,string> values;
string resource;
string first_id;
string second_id;
bool first_is_numeric;
bool second_is_numeric;
string cursor;
bool cursor_is_numeric;
RESTRequest(const HTTPClient::Method& method,
const string& path,
const map<string, string>& reqheads,
const string& reqbody,
const map<string, string>& misc):
verb(method),
path(path),
reqheads(reqheads),
reqbody(reqbody)
{
if (method==HTTPClient::MPOST|| method==HTTPClient::MPUT){
wwwformtomap(reqbody,&values);
}
vector<string> sections,queries,parts;
kc::strsplit(path, '/', §ions);
if (sections.size()>1){
resource = sections[1];
}
if (sections.size()>2){
first_id = sections[2];
first_is_numeric=isNumeric(first_id);
}
else{
first_is_numeric=false;
}
if (sections.size()>3){
second_id = sections[3];
second_is_numeric = isNumeric(second_id);
}
else{
second_is_numeric=false;
}
for (map<string, string>::const_iterator it = misc.begin();it!=misc.end();it++){
if (it->first=="query"){
kc::strsplit(it->second,"&",&queries);
for (vector<string>::const_iterator it=queries.begin();it!=queries.end();it++){
kc::strsplit(*it,"=",&parts);
if ((parts.size()==2) && (parts[0]=="cursor")){
cursor=parts[1];
cursor_is_numeric=isNumeric(cursor);
}
}
}
}
}
};
struct RESTResponse{
map<string, string>& resheads;
TemplateDictionary dict;
string template_name;
int32_t code;
stringstream message;
RESTResponse(map<string,string>& resheads):
resheads(resheads),
dict("response"){}
};
Worker::Worker(Registry* registry):
registry_(registry)
{ }
void Worker::process_idle(HTTPServer* serv) {
// serv->log(Logger::INFO,"Idle");
}
void Worker::process_timer(HTTPServer* serv) {
Queue queue(registry_);
if(!registry_->getPostings()->isReady()){
registry_->getPostings()->init();
}
else if (queue.process()){
serv->log(Logger::INFO,"Finished processing command queue");
};
}
int32_t Worker::process(HTTPServer* serv, HTTPServer::Session* sess,
const string& path, HTTPClient::Method method,
const map<string, string>& reqheads,
const string& reqbody,
map<string, string>& resheads,
string& resbody,
const map<string, string>& misc)
{
double start = kyotocabinet::time();
RESTRequest req(method,path,reqheads,reqbody,misc);
RESTResponse res(resheads);
if(req.resource=="document"){
process_document(req,res);
}
else if(req.resource=="association"){
process_association(req,res);
}
else if(req.resource=="index"){
process_index(req,res);
}
else if(req.resource=="queue"){
process_queue(req,res);
}
else if(req.resource=="echo"){
process_echo(req,res);
}
else if(req.resource=="heap"){
process_heap(req,res);
}
else if(req.resource=="histograms"){
process_histograms(req,res);
}
else if(req.resource=="status"){
process_status(req,res);
}
else if(req.resource=="help"){
process_help(req,res);
}
else if(req.resource=="search"){
process_search(req,res);
}else{
process_search(req,res);
}
res.dict.AddIncludeDictionary("HEADER")->SetFilename(HEADER);
res.dict.AddIncludeDictionary("FOOTER")->SetFilename(FOOTER);
registry_->getTemplateCache()->ExpandWithData(res.template_name,DO_NOT_STRIP,&res.dict,NULL,&resbody);
res.message << " Response Time: " << setiosflags(ios::fixed) << setprecision(4) << kyotocabinet::time()-start << " secs";
if (res.code==500 || res.code==404){
serv->log(Logger::ERROR,res.message.str().c_str());
}else{
serv->log(Logger::INFO,res.message.str().c_str());
}
return res.code;
}
void Worker::process_search(const RESTRequest& req,RESTResponse& res){
res.dict.SetTemplateGlobalValue("TITLE","Search");
switch (req.verb){
case HTTPClient::MPOST:{
DocumentPtr doc=registry_->getDocumentManager()->createTemporaryDocument(req.reqbody);
registry_->getPostings()->fill_search_dictionary(doc,&res.dict);
res.code=200;
res.template_name=RESULTS_PAGE;
}
break;
case HTTPClient::MGET:
res.template_name=SEARCH_PAGE;
res.code=200;
break;
default:
res.template_name=ERROR_PAGE;
res.code=500;
break;
}
}
void Worker::process_document(const RESTRequest& req,RESTResponse& res){
res.dict.SetTemplateGlobalValue("TITLE","Document");
if (req.first_is_numeric && req.second_is_numeric){
uint32_t doctype = kc::atoi(req.first_id.data());
uint32_t docid = kc::atoi(req.second_id.data());
Queue queue(registry_);
switch(req.verb){
case HTTPClient::MGET:
case HTTPClient::MHEAD:
{
DocumentPtr doc=registry_->getDocumentManager()->getDocument(doctype,docid);
if (doc){
res.message << "Getting document: " << doc;
if(req.verb==HTTPClient::MGET){
res.template_name=DOCUMENT_PAGE;
doc->fill_document_dictionary(&res.dict);
}
res.code=200;
}else{
res.message << "Error getting document: " << doc;
res.code=404;
}
}
break;
case HTTPClient::MPUT:
case HTTPClient::MPOST:{
uint64_t queue_id = queue.add_document(doctype,docid,req.reqbody,req.verb==HTTPClient::MPUT);
res.message << "Queued document: (" << doctype << "," << docid << ") for indexing queue id:"<< queue_id;
res.dict.SetIntValue("QUEUE_ID",queue_id);
res.dict.SetIntValue("DOC_TYPE",doctype);
res.dict.SetIntValue("DOC_ID",docid);
res.dict.SetValue("ACTION","insertion into index");
res.template_name=QUEUED_PAGE;
res.code=202;
}
break;
case HTTPClient::MDELETE:{
uint64_t queue_id = queue.delete_document(doctype,docid);
res.message << "Queued document: (" << doctype << "," << docid << ") for deleting with queue id:" << queue_id;
res.dict.SetIntValue("QUEUE_ID",queue_id);
res.dict.SetIntValue("DOC_TYPE",doctype);
res.dict.SetIntValue("DOC_ID",docid);
res.dict.SetValue("ACTION","deletion from index");
res.code=202;
}
break;
default:
res.template_name=ERROR_PAGE;
res.code=500;
break;
}
}
else if (req.first_is_numeric){
DocumentCursor cursor(registry_);
uint32_t doctype=kc::atoi(req.first_id.c_str());
uint32_t docid=0;
if (req.cursor_is_numeric){
docid=kc::atoi(req.cursor.c_str());
}
cursor.fill_list_dictionary(&res.dict,doctype,docid);
res.template_name=DOCUMENTS_PAGE;
res.code=200;
}
else{
DocumentCursor cursor(registry_);
uint32_t docid=0;
if (req.cursor_is_numeric){
docid=kc::atoi(req.cursor.c_str());
}
cursor.fill_list_dictionary(&res.dict,0,docid);
res.template_name=DOCUMENTS_PAGE;
res.code=200;
}
}
void Worker::process_help(const RESTRequest& req,RESTResponse& res){
res.dict.SetTemplateGlobalValue("TITLE","Help");
res.template_name=HELP_PAGE;
res.code=200;
}
void Worker::process_association(const RESTRequest& req, RESTResponse& res){
res.dict.SetTemplateGlobalValue("TITLE","Association");
Queue queue(registry_);
switch (req.verb){
case HTTPClient::MPOST:{
queue.addAssociations();
res.code=200;
break;
}
default:
res.code=500;
break;
}
}
void Worker::process_index(const RESTRequest& req,RESTResponse& res){
res.dict.SetTemplateGlobalValue("TITLE","Index");
switch(req.verb){
case HTTPClient::MGET:
if (req.cursor_is_numeric){
registry_->getPostings()->fill_list_dictionary(&res.dict,kc::atoi(req.cursor.c_str()));
}else{
registry_->getPostings()->fill_list_dictionary(&res.dict,0);
}
res.template_name=INDEX_PAGE;
res.code=200;
break;
default:
res.template_name=ERROR_PAGE;
res.code=500;
break;
}
}
void Worker::process_queue(const RESTRequest& req,RESTResponse& res){
Queue queue(registry_);
res.dict.SetTemplateGlobalValue("TITLE","Queue");
switch (req.verb){
case HTTPClient::MGET:
queue.fill_list_dictionary(&res.dict);
res.template_name=QUEUE_PAGE;
res.code=200;
break;
default:
res.template_name=ERROR_PAGE;
res.code=500;
break;
}
}
void Worker::process_heap(const RESTRequest& req, RESTResponse& res){
// MallocExtensionWriter out;
// MallocExtension::instance()->GetHeapSample(&out);
// res.dict.SetValue("BODY",out);
res.template_name=EMPTY_PAGE;
res.code=200;
}
void Worker::process_echo(const RESTRequest& req,RESTResponse& res){
for (map<string, string>::const_iterator it = req.reqheads.begin();it != req.reqheads.end(); it++) {
if (!it->first.empty()){
TemplateDictionary* header_dict=res.dict.AddSectionDictionary("HEADER");
header_dict->SetValue("KEY",it->first);
header_dict->SetValue("VALUE",it->second);
}
}
res.dict.SetValue("BODY",req.reqbody);
res.template_name=ECHO_PAGE;
res.code=200;
}
void Worker::process_histograms(const RESTRequest& req,RESTResponse& res){
res.dict.SetTemplateGlobalValue("TITLE","Histograms");
registry_->getPostings()->fill_histogram_dictionary(&res.dict);
res.template_name=HISTOGRAMS_PAGE;
res.code=200;
}
void Worker::process_status(const RESTRequest& req,RESTResponse& res){
size_t memory;
const int kBufferSize = 16 << 12;
char* buffer = new char[kBufferSize];
// MallocExtension::instance()->GetNumericProperty("generic.current_allocated_bytes",&memory);
// MallocExtension::instance()->GetStats(buffer,kBufferSize);
res.dict.SetFormattedValue("MEMORY","%.4f",double(memory)/1024/1024/1024);
res.dict.SetValue("MEMORY_STATS",string(buffer));
res.dict.SetIntValue("WHITESPACE_HASH",registry_->getWhiteSpaceHash());
delete [] buffer;
registry_->fill_status_dictionary(&res.dict);
registry_->getPostings()->fill_status_dictionary(&res.dict);
res.dict.SetTemplateGlobalValue("TITLE","Status");
res.template_name=STATUS_PAGE;
res.code=200;
}
}