Loading...
Searching...
No Matches
kv_service_tools.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <fcntl.h>
21#include <getopt.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <fstream>
27
28#include "common/proto/signature_info.pb.h"
31
34using resdb::KVClient;
35using resdb::ReplicaInfo;
37
38void ShowUsage() {
39 printf(
40 "--config: config path\n"
41 "--cmd "
42 "set/get/set_with_version/get_with_version/get_key_range/"
43 "get_key_range_with_version/get_top/get_history\n"
44 "--key key\n"
45 "--value value, if cmd is a get operation\n"
46 "--version version of the value, if cmd is vesion based\n"
47 "--min_key the min key if cmd is get_key_range\n"
48 "--max_key the max key if cmd is get_key_range\n"
49 "--min_version, if cmd is get_history\n"
50 "--max_version, if cmd is get_history\n"
51 "--top, if cmd is get_top\n"
52 "\n"
53 "More examples can be found from README.\n");
54}
55
56static struct option long_options[] = {
57 {"help", no_argument, NULL, 'h'},
58 {"config", required_argument, NULL, 'c'},
59 {"cmd", required_argument, NULL, 'f'},
60 {"key", required_argument, NULL, 'K'},
61 {"value", required_argument, NULL, 'V'},
62 {"version", required_argument, NULL, 'v'},
63 {"min_version", required_argument, NULL, 's'},
64 {"max_version", required_argument, NULL, 'S'},
65 {"min_key", required_argument, NULL, 'y'},
66 {"max_key", required_argument, NULL, 'Y'},
67 {"top", required_argument, NULL, 't'},
68};
69
70void OldAPI(char** argv) {
71 std::string client_config_file = argv[1];
72 std::string cmd = argv[2];
73 std::string key;
74 if (cmd != "getvalues") {
75 key = argv[3];
76 }
77 std::string value;
78 if (cmd == "set") {
79 value = argv[4];
80 }
81
82 std::string key2;
83 if (cmd == "getrange") {
84 key2 = argv[4];
85 }
86
87 ResDBConfig config = GenerateResDBConfig(client_config_file);
88
89 config.SetClientTimeoutMs(100000);
90
91 KVClient client(config);
92
93 if (cmd == "set") {
94 int ret = client.Set(key, value);
95 printf("client set ret = %d\n", ret);
96 } else if (cmd == "get") {
97 auto res = client.Get(key);
98 if (res != nullptr) {
99 printf("client get value = %s\n", res->c_str());
100 } else {
101 printf("client get value fail\n");
102 }
103 } else if (cmd == "getvalues") {
104 auto res = client.GetAllValues();
105 if (res != nullptr) {
106 printf("client getvalues value = %s\n", res->c_str());
107 } else {
108 printf("client getvalues value fail\n");
109 }
110 } else if (cmd == "getrange") {
111 auto res = client.GetRange(key, key2);
112 if (res != nullptr) {
113 printf("client getrange value = %s\n", res->c_str());
114 } else {
115 printf("client getrange value fail\n");
116 }
117 }
118}
119
120int main(int argc, char** argv) {
121 std::string key;
122 int version = -1;
123 int option_index = 0;
124 int min_version = -1, max_version = -1;
125 std::string min_key, max_key;
126 std::string value;
127 std::string client_config_file;
128 int top = 0;
129 char c;
130 std::string cmd;
131
132 if (argc >= 3) {
133 cmd = argv[2];
134 if (cmd == "get" || cmd == "set" || cmd == "getvalues" ||
135 cmd == "getrange") {
136 OldAPI(argv);
137 return 0;
138 }
139 }
140
141 while ((c = getopt_long(argc, argv, "h", long_options, &option_index)) !=
142 -1) {
143 switch (c) {
144 case 'c':
145 client_config_file = optarg;
146 break;
147 case 'f':
148 cmd = optarg;
149 break;
150 case 'K':
151 key = optarg;
152 break;
153 case 'V':
154 value = optarg;
155 break;
156 case 'v':
157 version = strtoull(optarg, NULL, 10);
158 break;
159 case 's':
160 min_version = strtoull(optarg, NULL, 10);
161 break;
162 case 'S':
163 max_version = strtoull(optarg, NULL, 10);
164 break;
165 case 'y':
166 min_key = optarg;
167 break;
168 case 'Y':
169 max_key = optarg;
170 break;
171 case 't':
172 top = strtoull(optarg, NULL, 10);
173 break;
174 case 'h':
175 ShowUsage();
176 break;
177 }
178 }
179
180 ResDBConfig config = GenerateResDBConfig(client_config_file);
181
182 config.SetClientTimeoutMs(100000);
183 KVClient client(config);
184 if (cmd == "set_with_version") {
185 if (key.empty() || value.empty() || version < 0) {
186 ShowUsage();
187 return 0;
188 }
189 int ret = client.Set(key, value, version);
190 printf("set key = %s, value = %s, version = %d done, ret = %d\n",
191 key.c_str(), value.c_str(), version, ret);
192 if (ret == 0) {
193 usleep(100000);
194 auto res = client.Get(key, 0);
195 if (res != nullptr) {
196 printf("current value = %s\n", res->DebugString().c_str());
197 } else {
198 printf("get value fail\n");
199 }
200 }
201 } else if (cmd == "set") {
202 if (key.empty() || value.empty()) {
203 ShowUsage();
204 return 0;
205 }
206 int ret = client.Set(key, value);
207 printf("set key = %s, value = %s done, ret = %d\n", key.c_str(),
208 value.c_str(), ret);
209 } else if (cmd == "get_with_version") {
210 auto res = client.Get(key, version);
211 if (res != nullptr) {
212 printf("get key = %s, value = %s\n", key.c_str(),
213 res->DebugString().c_str());
214 } else {
215 printf("get value fail\n");
216 }
217 } else if (cmd == "get") {
218 auto res = client.Get(key);
219 if (res != nullptr) {
220 printf("get key = %s value = %s\n", key.c_str(), res->c_str());
221 } else {
222 printf("get value fail\n");
223 }
224 } else if (cmd == "get_top") {
225 auto res = client.GetKeyTopHistory(key, top);
226 if (res != nullptr) {
227 printf("key = %s, top %d\n value = %s\n", key.c_str(), top,
228 res->DebugString().c_str());
229 } else {
230 printf("get key = %s top %d value fail\n", key.c_str(), top);
231 }
232 } else if (cmd == "get_history") {
233 if (key.empty() || min_version < 0 || max_version < 0 ||
234 max_version < min_version) {
235 ShowUsage();
236 return 0;
237 }
238 auto res = client.GetKeyHistory(key, min_version, max_version);
239 if (res != nullptr) {
240 printf(
241 "get history key = %s, min version = %d, max version = %d\n value = "
242 "%s\n",
243 key.c_str(), min_version, max_version, res->DebugString().c_str());
244 } else {
245 printf(
246 "get history key = %s, min version = %d, max version = %d value "
247 "fail\n",
248 key.c_str(), min_version, max_version);
249 }
250 } else if (cmd == "get_key_range") {
251 if (min_key.empty() || max_key.empty() || min_key > max_key) {
252 ShowUsage();
253 return 0;
254 }
255 auto res = client.GetRange(min_key, max_key);
256 if (res != nullptr) {
257 printf("getrange min key = %s, max key = %s\n value = %s\n",
258 min_key.c_str(), max_key.c_str(), (*res).c_str());
259 } else {
260 printf("getrange value fail, min key = %s, max key = %s\n",
261 min_key.c_str(), max_key.c_str());
262 }
263 } else if (cmd == "get_key_range_with_version") {
264 if (min_key.empty() || max_key.empty() || min_key > max_key) {
265 ShowUsage();
266 return 0;
267 }
268 printf("min key = %s max key = %s\n", min_key.c_str(), max_key.c_str());
269 auto res = client.GetKeyRange(min_key, max_key);
270 if (res != nullptr) {
271 printf("getrange min key = %s, max key = %s\n value = %s\n",
272 min_key.c_str(), max_key.c_str(), res->DebugString().c_str());
273 } else {
274 printf("getrange value fail, min key = %s, max key = %s\n",
275 min_key.c_str(), max_key.c_str());
276 }
277 } else {
278 ShowUsage();
279 }
280}
int Set(const std::string &key, const std::string &data, int version)
Definition kv_client.cpp:77
std::unique_ptr< Items > GetKeyTopHistory(const std::string &key, int top_number)
std::unique_ptr< std::string > GetRange(const std::string &min_key, const std::string &max_key)
Definition kv_client.cpp:62
std::unique_ptr< Items > GetKeyHistory(const std::string &key, int min_version, int max_version)
std::unique_ptr< ValueInfo > Get(const std::string &key, int version)
Definition kv_client.cpp:87
std::unique_ptr< Items > GetKeyRange(const std::string &min_key, const std::string &max_key)
std::unique_ptr< std::string > GetAllValues()
Definition kv_client.cpp:50
void SetClientTimeoutMs(int timeout_ms)
ReplicaInfo GenerateReplicaInfo(int id, const std::string &ip, int port)
std::unique_ptr< ResDBConfig > GenerateResDBConfig(const std::string &config_file, const std::string &private_key_file, const std::string &cert_file, std::optional< ReplicaInfo > self_info, std::optional< ConfigGenFunc > gen_func)
int main(int argc, char **argv)
void ShowUsage()
void OldAPI(char **argv)