-
Notifications
You must be signed in to change notification settings - Fork 45
/
retrieving.cc
92 lines (77 loc) · 2.75 KB
/
retrieving.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
#include <libcouchbase/couchbase.h>
#include <libcouchbase/api3.h>
#include <string>
#include <string.h>
#include <iostream>
struct Result {
std::string value;
lcb_error_t status;
Result() : status(LCB_SUCCESS) {}
};
extern "C" {
static void get_callback(lcb_t, int, const lcb_RESPBASE *rb)
{
// "cast" to specific callback type
const lcb_RESPGET *resp = reinterpret_cast< const lcb_RESPGET * >(rb);
Result *my_result = reinterpret_cast< Result * >(rb->cookie);
my_result->status = resp->rc;
my_result->value.clear(); // Remove any prior value
if (resp->rc == LCB_SUCCESS) {
my_result->value.assign(reinterpret_cast< const char * >(resp->value), resp->nvalue);
}
}
}
int main(int, char **)
{
lcb_create_st crst = {};
lcb_t instance;
lcb_error_t rc;
crst.version = 3;
crst.v.v3.connstr = "couchbase://127.0.0.1/travel-sample";
crst.v.v3.username = "testuser";
crst.v.v3.passwd = "password";
lcb_create(&instance, &crst);
lcb_connect(instance);
lcb_wait(instance);
rc = lcb_get_bootstrap_status(instance);
if (rc != LCB_SUCCESS) {
printf("Unable to bootstrap cluster: %s\n", lcb_strerror_short(rc));
exit(1);
}
// Store a key first, so we know it will exist later on. In real production
// environments, we'd also want to install a callback for storage operations
// so we know if they succeeded
lcb_CMDSTORE scmd = {};
const char *key = "a_key";
const char *value = "{\"some\":\"json\"}";
LCB_CMD_SET_KEY(&scmd, key, strlen(key));
LCB_CMD_SET_VALUE(&scmd, value, strlen(value));
scmd.operation = LCB_SET; // Upsert
lcb_sched_enter(instance);
lcb_store3(instance, NULL, &scmd);
lcb_sched_leave(instance);
lcb_wait(instance);
// Install the callback for GET operations. Note this can be done at any
// time before the operation is scheduled
lcb_install_callback3(instance, LCB_CALLBACK_GET, get_callback);
Result my_result;
lcb_CMDGET gcmd = {};
LCB_CMD_SET_KEY(&gcmd, key, strlen(key));
lcb_sched_enter(instance);
lcb_get3(instance, &my_result, &gcmd);
lcb_sched_leave(instance);
lcb_wait(instance);
std::cout << "Status for getting " << key << ": ";
std::cout << lcb_strerror(NULL, my_result.status);
std::cout << ". Value: " << my_result.value << std::endl;
// Let's see what happens if we get a key that isn't yet stored:
key = "non-exist-key";
LCB_CMD_SET_KEY(&gcmd, key, strlen(key));
lcb_sched_enter(instance);
lcb_get3(instance, &my_result, &gcmd);
lcb_sched_leave(instance);
lcb_wait(instance);
std::cout << "Status for getting " << key << ": ";
std::cout << lcb_strerror(NULL, my_result.status) << std::endl;
lcb_destroy(instance);
}