-
Notifications
You must be signed in to change notification settings - Fork 209
/
kv_service_tools.cpp
280 lines (263 loc) · 8.12 KB
/
kv_service_tools.cpp
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <fcntl.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include "common/proto/signature_info.pb.h"
#include "interface/kv/kv_client.h"
#include "platform/config/resdb_config_utils.h"
using resdb::GenerateReplicaInfo;
using resdb::GenerateResDBConfig;
using resdb::KVClient;
using resdb::ReplicaInfo;
using resdb::ResDBConfig;
void ShowUsage() {
printf(
"--config: config path\n"
"--cmd "
"set/get/set_with_version/get_with_version/get_key_range/"
"get_key_range_with_version/get_top/get_history\n"
"--key key\n"
"--value value, if cmd is a get operation\n"
"--version version of the value, if cmd is vesion based\n"
"--min_key the min key if cmd is get_key_range\n"
"--max_key the max key if cmd is get_key_range\n"
"--min_version, if cmd is get_history\n"
"--max_version, if cmd is get_history\n"
"--top, if cmd is get_top\n"
"\n"
"More examples can be found from README.\n");
}
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"config", required_argument, NULL, 'c'},
{"cmd", required_argument, NULL, 'f'},
{"key", required_argument, NULL, 'K'},
{"value", required_argument, NULL, 'V'},
{"version", required_argument, NULL, 'v'},
{"min_version", required_argument, NULL, 's'},
{"max_version", required_argument, NULL, 'S'},
{"min_key", required_argument, NULL, 'y'},
{"max_key", required_argument, NULL, 'Y'},
{"top", required_argument, NULL, 't'},
};
void OldAPI(char** argv) {
std::string client_config_file = argv[1];
std::string cmd = argv[2];
std::string key;
if (cmd != "getvalues") {
key = argv[3];
}
std::string value;
if (cmd == "set") {
value = argv[4];
}
std::string key2;
if (cmd == "getrange") {
key2 = argv[4];
}
ResDBConfig config = GenerateResDBConfig(client_config_file);
config.SetClientTimeoutMs(100000);
KVClient client(config);
if (cmd == "set") {
int ret = client.Set(key, value);
printf("client set ret = %d\n", ret);
} else if (cmd == "get") {
auto res = client.Get(key);
if (res != nullptr) {
printf("client get value = %s\n", res->c_str());
} else {
printf("client get value fail\n");
}
} else if (cmd == "getvalues") {
auto res = client.GetAllValues();
if (res != nullptr) {
printf("client getvalues value = %s\n", res->c_str());
} else {
printf("client getvalues value fail\n");
}
} else if (cmd == "getrange") {
auto res = client.GetRange(key, key2);
if (res != nullptr) {
printf("client getrange value = %s\n", res->c_str());
} else {
printf("client getrange value fail\n");
}
}
}
int main(int argc, char** argv) {
std::string key;
int version = -1;
int option_index = 0;
int min_version = -1, max_version = -1;
std::string min_key, max_key;
std::string value;
std::string client_config_file;
int top = 0;
char c;
std::string cmd;
if (argc >= 3) {
cmd = argv[2];
if (cmd == "get" || cmd == "set" || cmd == "getvalues" ||
cmd == "getrange") {
OldAPI(argv);
return 0;
}
}
while ((c = getopt_long(argc, argv, "h", long_options, &option_index)) !=
-1) {
switch (c) {
case 'c':
client_config_file = optarg;
break;
case 'f':
cmd = optarg;
break;
case 'K':
key = optarg;
break;
case 'V':
value = optarg;
break;
case 'v':
version = strtoull(optarg, NULL, 10);
break;
case 's':
min_version = strtoull(optarg, NULL, 10);
break;
case 'S':
max_version = strtoull(optarg, NULL, 10);
break;
case 'y':
min_key = optarg;
break;
case 'Y':
max_key = optarg;
break;
case 't':
top = strtoull(optarg, NULL, 10);
break;
case 'h':
ShowUsage();
break;
}
}
ResDBConfig config = GenerateResDBConfig(client_config_file);
config.SetClientTimeoutMs(100000);
KVClient client(config);
if (cmd == "set_with_version") {
if (key.empty() || value.empty() || version < 0) {
ShowUsage();
return 0;
}
int ret = client.Set(key, value, version);
printf("set key = %s, value = %s, version = %d done, ret = %d\n",
key.c_str(), value.c_str(), version, ret);
if (ret == 0) {
usleep(100000);
auto res = client.Get(key, 0);
if (res != nullptr) {
printf("current value = %s\n", res->DebugString().c_str());
} else {
printf("get value fail\n");
}
}
} else if (cmd == "set") {
if (key.empty() || value.empty()) {
ShowUsage();
return 0;
}
int ret = client.Set(key, value);
printf("set key = %s, value = %s done, ret = %d\n", key.c_str(),
value.c_str(), ret);
} else if (cmd == "get_with_version") {
auto res = client.Get(key, version);
if (res != nullptr) {
printf("get key = %s, value = %s\n", key.c_str(),
res->DebugString().c_str());
} else {
printf("get value fail\n");
}
} else if (cmd == "get") {
auto res = client.Get(key);
if (res != nullptr) {
printf("get key = %s value = %s\n", key.c_str(), res->c_str());
} else {
printf("get value fail\n");
}
} else if (cmd == "get_top") {
auto res = client.GetKeyTopHistory(key, top);
if (res != nullptr) {
printf("key = %s, top %d\n value = %s\n", key.c_str(), top,
res->DebugString().c_str());
} else {
printf("get key = %s top %d value fail\n", key.c_str(), top);
}
} else if (cmd == "get_history") {
if (key.empty() || min_version < 0 || max_version < 0 ||
max_version < min_version) {
ShowUsage();
return 0;
}
auto res = client.GetKeyHistory(key, min_version, max_version);
if (res != nullptr) {
printf(
"get history key = %s, min version = %d, max version = %d\n value = "
"%s\n",
key.c_str(), min_version, max_version, res->DebugString().c_str());
} else {
printf(
"get history key = %s, min version = %d, max version = %d value "
"fail\n",
key.c_str(), min_version, max_version);
}
} else if (cmd == "get_key_range") {
if (min_key.empty() || max_key.empty() || min_key > max_key) {
ShowUsage();
return 0;
}
auto res = client.GetRange(min_key, max_key);
if (res != nullptr) {
printf("getrange min key = %s, max key = %s\n value = %s\n",
min_key.c_str(), max_key.c_str(), (*res).c_str());
} else {
printf("getrange value fail, min key = %s, max key = %s\n",
min_key.c_str(), max_key.c_str());
}
} else if (cmd == "get_key_range_with_version") {
if (min_key.empty() || max_key.empty() || min_key > max_key) {
ShowUsage();
return 0;
}
printf("min key = %s max key = %s\n", min_key.c_str(), max_key.c_str());
auto res = client.GetKeyRange(min_key, max_key);
if (res != nullptr) {
printf("getrange min key = %s, max key = %s\n value = %s\n",
min_key.c_str(), max_key.c_str(), res->DebugString().c_str());
} else {
printf("getrange value fail, min key = %s, max key = %s\n",
min_key.c_str(), max_key.c_str());
}
} else {
ShowUsage();
}
}