Couchbase C Client  2.10.7
Asynchronous C Client for Couchbase
example/observe/durability.c

Show how to enforce durability requirements for store operations.

/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2017 Couchbase, Inc.
*
* Licensed 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.
*/
/*
* BUILD:
* cc -o durability durability.c -lcouchbase
*
* RUN:
* ./durability [ CONNSTRING [ PASSWORD [ USERNAME ] ] ]
*
* # use default durability check method
* ./durability couchbase://localhost
*
* # force durability check method based on sequence numbers
* ./durability couchbase://localhost?fetch_mutation_tokens=true&dur_mutation_tokens=true
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <libcouchbase/api3.h>
#define fail(msg) \
fprintf(stderr, "%s\n", msg); \
exit(EXIT_FAILURE)
#define fail2(msg, err) \
fprintf(stderr, "%s\n", msg); \
fprintf(stderr, "Error was 0x%x (%s)\n", err, lcb_strerror(NULL, err)); \
exit(EXIT_FAILURE)
static void store_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb)
{
const lcb_RESPSTOREDUR *resp = (const lcb_RESPSTOREDUR *)rb;
fprintf(stderr, "Got status of operation: 0x%02x, %s\n", resp->rc, lcb_strerror(NULL, resp->rc));
fprintf(stderr, "Stored: %s\n", resp->store_ok ? "true" : "false");
fprintf(stderr, "Number of roundtrips: %d\n", resp->dur_resp->nresponses);
fprintf(stderr, "In memory on master: %s\n", resp->dur_resp->exists_master ? "true" : "false");
fprintf(stderr, "Persisted on master: %s\n", resp->dur_resp->persisted_master ? "true" : "false");
fprintf(stderr, "Nodes have value replicated: %d\n", resp->dur_resp->nreplicated);
fprintf(stderr, "Nodes have value persisted (including master): %d\n", resp->dur_resp->npersisted);
}
int main(int argc, char *argv[])
{
lcb_t instance;
lcb_CMDSTOREDUR cmd = {0};
struct lcb_create_st create_options = {0};
const char *key = "foo";
const char *value = "{\"val\":42}";
create_options.version = 3;
if (argc > 1) {
create_options.v.v3.connstr = argv[1];
}
if (argc > 2) {
create_options.v.v3.passwd = argv[2];
}
if (argc > 3) {
create_options.v.v3.username = argv[3];
}
if ((err = lcb_create(&instance, &create_options)) != LCB_SUCCESS) {
fail2("cannot create connection instance", err);
}
if ((err = lcb_connect(instance)) != LCB_SUCCESS) {
fail2("Couldn't schedule connection", err);
}
lcb_wait(instance);
if ((err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS) {
fail2("Couldn't get initial cluster configuration", err);
}
lcb_install_callback3(instance, LCB_CALLBACK_STOREDUR, store_callback);
LCB_CMD_SET_KEY(&cmd, key, strlen(key));
LCB_CMD_SET_VALUE(&cmd, value, strlen(value));
/* replicate and persist on all nodes */
cmd.replicate_to = -1;
cmd.persist_to = -1;
lcb_storedur3(instance, NULL, &cmd);
lcb_wait(instance);
lcb_destroy(instance);
return EXIT_SUCCESS;
}
lcb_wait
lcb_error_t lcb_wait(lcb_t instance)
Wait for the execution of all batched requests.
lcb_RESPSTOREDUR::dur_resp
const lcb_RESPENDURE * dur_resp
Internal durability response structure.
Definition: couchbase.h:1707
lcb_RESPSTOREDUR::rc
lcb_error_t rc
Status code.
Definition: couchbase.h:1705
lcb_strerror
const char * lcb_strerror(lcb_t instance, lcb_error_t error)
Get a textual descrtiption for the given error code.
lcb_RESPENDURE::nreplicated
lcb_U8 nreplicated
Total number of replica nodes to which this mutation has been replicated.
Definition: couchbase.h:1547
lcb_create_st3::passwd
const char * passwd
Password for bucket.
Definition: couchbase.h:293
lcb_RESPENDURE::persisted_master
lcb_U8 persisted_master
True if item was persisted on the master node.
Definition: couchbase.h:1535
lcb_get_bootstrap_status
lcb_error_t lcb_get_bootstrap_status(lcb_t instance)
Gets the initial bootstrap status.
lcb_create_st3::connstr
const char * connstr
Connection string.
Definition: couchbase.h:282
lcb_RESPENDURE::npersisted
lcb_U8 npersisted
Total number of nodes (including master) on which this mutation has been persisted.
Definition: couchbase.h:1541
lcb_RESPENDURE::exists_master
lcb_U8 exists_master
Whether this item exists in the master in its current form.
Definition: couchbase.h:1529
lcb_connect
lcb_error_t lcb_connect(lcb_t instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
lcb_create_st3::username
const char * username
Username to use for authentication.
Definition: couchbase.h:288
lcb_CMDSTOREDUR
Command structure for lcb_storedur3() This is much like lcb_CMDSTORE, but also includes durability op...
Definition: couchbase.h:1679
lcb_error_t
lcb_error_t
Error codes returned by the library.
Definition: error.h:476
lcb_RESPSTOREDUR::store_ok
int store_ok
If the rc field is not LCB_SUCCESS, this field indicates what failed.
Definition: couchbase.h:1713
lcb_create_st::lcb_CRST_u::v3
struct lcb_create_st3 v3
Use this field.
Definition: couchbase.h:338
lcb_create_st
Wrapper structure for lcb_create()
Definition: couchbase.h:328
lcb_RESPSTOREDUR
Response structure for `LCB_CALLBACK_STOREDUR.
Definition: couchbase.h:1704
LCB_CMD_SET_KEY
#define LCB_CMD_SET_KEY(cmd, keybuf, keylen)
Set the key for the command.
Definition: couchbase.h:556
lcb_t
struct lcb_st * lcb_t
Definition: couchbase.h:41
lcb_create
lcb_error_t lcb_create(lcb_t *instance, const struct lcb_create_st *options)
Create an instance of lcb.
couchbase.h
LCB_SUCCESS
@ LCB_SUCCESS
Success.
Definition: error.h:478
lcb_RESPBASE
Base response structure for callbacks.
Definition: couchbase.h:625
lcb_destroy
void lcb_destroy(lcb_t instance)
Destroy (and release all allocated resources) an instance of lcb.
LCB_CMD_SET_VALUE
#define LCB_CMD_SET_VALUE(scmd, valbuf, vallen)
Set the value buffer for the command. This may be used when the new value is a single contiguous buff...
Definition: couchbase.h:1139
lcb_create_st::version
int version
Indicates which field in the lcb_CRST_u union should be used.
Definition: couchbase.h:330
lcb_install_callback3
lcb_RESPCALLBACK lcb_install_callback3(lcb_t instance, int cbtype, lcb_RESPCALLBACK cb)
lcb_CMDSTOREDUR::persist_to
char persist_to
Number of nodes to persist to.
Definition: couchbase.h:1691
lcb_storedur3
lcb_error_t lcb_storedur3(lcb_t instance, const void *cookie, const lcb_CMDSTOREDUR *cmd)
lcb_RESPENDURE::nresponses
lcb_U16 nresponses
Total number of polls (i.e.
Definition: couchbase.h:1523
lcb_CMDSTOREDUR::replicate_to
char replicate_to
Number of nodes to replicate to.
Definition: couchbase.h:1698