forked from ideawu/ssdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend_dump.cpp
More file actions
executable file
·90 lines (75 loc) · 1.76 KB
/
Copy pathbackend_dump.cpp
File metadata and controls
executable file
·90 lines (75 loc) · 1.76 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
#include <pthread.h>
#include "backend_dump.h"
BackendDump::BackendDump(const SSDB *ssdb){
this->ssdb = ssdb;
}
BackendDump::~BackendDump(){
log_debug("BackendDump finalized");
}
void BackendDump::proc(const Link *link){
log_info("accept dump client");
struct run_arg *arg = new run_arg();
arg->link = link;
arg->backend = this;
pthread_t tid;
int err = pthread_create(&tid, NULL, &BackendDump::_run_thread, arg);
if(err != 0){
log_error("can't create thread: %s", strerror(err));
delete link;
}
}
void* BackendDump::_run_thread(void *arg){
struct run_arg *p = (struct run_arg*)arg;
const BackendDump *backend = p->backend;
Link *link = (Link *)p->link;
delete p;
//
link->noblock(false);
const std::vector<Bytes>* req = link->last_recv();
std::string start = "";
if(req->size() > 1){
Bytes b = req->at(1);
start.assign(b.data(), b.size());
}
std::string end = "";
if(req->size() > 2){
Bytes b = req->at(2);
end.assign(b.data(), b.size());
}
int limit = 10;
if(req->size() > 3){
Bytes b = req->at(3);
limit = b.Int();
}
Buffer *output = link->output;
int count = 0;
bool quit = false;
Iterator *it = backend->ssdb->iterator(start, end, limit);
link->send("begin");
while(!quit){
if(!it->next()){
quit = true;
char buf[20];
snprintf(buf, sizeof(buf), "%d", count);
link->send("end", buf);
}else{
count ++;
Bytes key = it->key();
Bytes val = it->val();
output->append_record("set");
output->append_record(key);
output->append_record(val);
output->append('\n');
if(output->size() < output->total()/2){
continue;
}
}
if(link->flush() == -1){
log_info("fd: %d, send error", link->fd());
break;
}
}
log_info("fd: %d, delete link", link->fd());
delete link;
return (void *)NULL;
}