-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.cc
executable file
·101 lines (95 loc) · 3.08 KB
/
sample.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
#include "ins_sdk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace galaxy::ins::sdk;
void my_watch_cb(const WatchParam& param, SDKError error) {
InsSDK* sdk = static_cast<InsSDK*>(param.context);
printf("key: %s\n", param.key.c_str());
printf("value: %s\n", param.value.c_str());
printf("deleted: %s\n", param.deleted ?"true":"false");
printf("error code: %d\n", static_cast<int>(error));
if (sdk) {
printf("watch again\n");
SDKError er;
sdk->Watch(param.key, my_watch_cb, (void*)sdk, &er);
}
}
void on_session_timeout(void* context) {
(void)context;
fprintf(stderr, "in session timeout\n");
exit(1);
}
int main(int argc, char* argv[]) {
std::vector<std::string> members;
if (argc < 2) {
fprintf(stderr, "./sample [read|write]\n");
return 1;
}
if (strcmp("write", argv[1]) == 0) {
fprintf(stderr, "write test\n");
InsSDK::ParseFlagFromArgs(argc, argv, &members);
InsSDK sdk(members);
char key_buf[1024] = {'\0'};
char value_buf[1024] = {'\0'};
SDKError err;
for (int i=1; i<=100000; i++) {
snprintf(key_buf, sizeof(key_buf), "key_%d", i);
snprintf(value_buf, sizeof(value_buf), "value_%d", i);
sdk.Put(key_buf, value_buf, &err);
if (err == kClusterDown) {
i--;
printf("try put again: %s\n", key_buf);
sleep(2);
continue;
}
printf("%s\n", key_buf);
fflush(stdout);
}
} else if(strcmp("read", argv[1]) == 0) {
fprintf(stderr, "read test\n");
InsSDK::ParseFlagFromArgs(argc, argv, &members);
InsSDK sdk(members);
char key_buf[1024] = {'\0'};
char value_buf[1024] = {'\0'};
SDKError err;
for (int i=1; i<=100000; i++) {
snprintf(key_buf, sizeof(key_buf), "key_%d", i);
snprintf(value_buf, sizeof(value_buf), "value_%d", i);
std::string value;
sdk.Get(key_buf, &value, &err);
if (err == kClusterDown) {
i--;
printf("try get again: %s", key_buf);
sleep(2);
continue;
}
if (err == kOK) {
printf("%s\n", value.c_str());
} else if (err == kNoSuchKey) {
printf("NOT FOUND\n");
}
fflush(stdout);
}
} else if(strcmp("watch", argv[1]) == 0) {
fprintf(stderr, "watch test\n");
InsSDK::ParseFlagFromArgs(argc, argv, &members);
InsSDK sdk(members);
sdk.RegisterSessionTimeout(on_session_timeout, NULL);
char key_buf[1024] = {'\0'};
SDKError err;
for (int i=1; i<=1000; i++) {
snprintf(key_buf, sizeof(key_buf), "key_%d", i);
sdk.Watch(key_buf, my_watch_cb, &sdk, &err);
}
while (true) {
sleep(1);
}
}
return 0;
}