Couchbase C Client  3.0.7
Asynchronous C Client for Couchbase
utils.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2011-2020 Couchbase, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef LCB_UTILS_H
19 #define LCB_UTILS_H
20 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define LCB_CONFIG_MCD_PORT 11210
33 #define LCB_CONFIG_MCD_SSL_PORT 11207
34 #define LCB_CONFIG_HTTP_PORT 8091
35 #define LCB_CONFIG_HTTP_SSL_PORT 18091
36 #define LCB_CONFIG_MCCOMPAT_PORT 11211
37 
52 #define LCB_CMD_SET_KEY(cmd, keybuf, keylen) LCB_KREQ_SIMPLE(&(cmd)->key, keybuf, keylen)
53 
71 #define LCB_CMD_BASE \
72  \
75  lcb_U32 cmdflags; \
76  \
77  \
85  lcb_U32 exptime; \
86  \
87  \
92  lcb_U64 cas; \
93  \
94  \
95  lcb_U32 cid; \
96  const char *scope; \
97  size_t nscope; \
98  const char *collection; \
99  size_t ncollection; \
100  \
101  lcb_KEYBUF key; \
102  \
103  \
104  lcb_U32 timeout; \
105  \
106  lcbtrace_SPAN *pspan
107 
119 typedef struct {
120  LCB_CMD_BASE;
121 } lcb_CMDCBFLUSH;
122 
123 typedef struct {
124  lcb_STATUS rc;
125  void *cookie;
126  lcb_U16 rflags;
127 } lcb_RESPCBFLUSH;
128 
151 LIBCOUCHBASE_API
152 lcb_STATUS lcb_cbflush3(lcb_INSTANCE *instance, void *cookie, const lcb_CMDCBFLUSH *cmd);
153 
232 enum lcb_timeunit_t {
233  LCB_TIMEUNIT_NSEC = 0,
234  LCB_TIMEUNIT_USEC = 1,
235  LCB_TIMEUNIT_MSEC = 2,
236  LCB_TIMEUNIT_SEC = 3
237 };
238 typedef enum lcb_timeunit_t lcb_timeunit_t;
239 
252 LIBCOUCHBASE_API
254 
263 LIBCOUCHBASE_API
265 
281 typedef void (*lcb_timings_callback)(lcb_INSTANCE *instance, const void *cookie, lcb_timeunit_t timeunit, lcb_U32 min,
282  lcb_U32 max, lcb_U32 total, lcb_U32 maxtotal);
283 
293 LIBCOUCHBASE_API
294 lcb_STATUS lcb_get_timings(lcb_INSTANCE *instance, const void *cookie, lcb_timings_callback callback);
297 typedef enum {
299  LCB_DUMP_VBCONFIG = 0x01,
301  LCB_DUMP_PKTINFO = 0x02,
303  LCB_DUMP_BUFINFO = 0x04,
305  LCB_DUMP_METRICS = 0x08,
307  LCB_DUMP_ALL = 0xff
308 } lcb_DUMPFLAGS;
309 
324 LIBCOUCHBASE_API
325 void lcb_dump(lcb_INSTANCE *instance, FILE *fp, lcb_U32 flags);
326 
328 struct lcb_histogram_st;
329 typedef struct lcb_histogram_st lcb_HISTOGRAM;
330 
336 LIBCOUCHBASE_API
337 lcb_HISTOGRAM *lcb_histogram_create(void);
338 
343 LIBCOUCHBASE_API
344 void lcb_histogram_destroy(lcb_HISTOGRAM *hg);
345 
352 LIBCOUCHBASE_API
353 void lcb_histogram_record(lcb_HISTOGRAM *hg, lcb_U64 duration);
354 
355 typedef void (*lcb_HISTOGRAM_CALLBACK)(const void *cookie, lcb_timeunit_t timeunit, lcb_U32 min, lcb_U32 max,
356  lcb_U32 total, lcb_U32 maxtotal);
357 
365 LIBCOUCHBASE_API
366 void lcb_histogram_read(const lcb_HISTOGRAM *hg, const void *cookie, lcb_HISTOGRAM_CALLBACK cb);
367 
378 LIBCOUCHBASE_API
379 void lcb_histogram_print(lcb_HISTOGRAM *hg, FILE *stream);
380 
392 LIBCOUCHBASE_API
393 const char *lcb_resp_get_error_context(int cbtype, const lcb_RESPBASE *rb);
394 
405 LIBCOUCHBASE_API
406 const char *lcb_resp_get_error_ref(int cbtype, const lcb_RESPBASE *rb);
407 
413 /*
414  * @addtogroup lcb-collection-api
415  * @{
416  */
417 
418 typedef struct lcb_RESPGETMANIFEST_ lcb_RESPGETMANIFEST;
419 
420 LIBCOUCHBASE_API lcb_STATUS lcb_respgetmanifest_status(const lcb_RESPGETMANIFEST *resp);
421 LIBCOUCHBASE_API lcb_STATUS lcb_respgetmanifest_cookie(const lcb_RESPGETMANIFEST *resp, void **cookie);
422 LIBCOUCHBASE_API lcb_STATUS lcb_respgetmanifest_value(const lcb_RESPGETMANIFEST *resp, const char **json,
423  size_t *json_len);
424 
425 typedef struct lcb_CMDGETMANIFEST_ lcb_CMDGETMANIFEST;
426 
427 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetmanifest_create(lcb_CMDGETMANIFEST **cmd);
428 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetmanifest_destroy(lcb_CMDGETMANIFEST *cmd);
429 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetmanifest_timeout(lcb_CMDGETMANIFEST *cmd, uint32_t timeout);
430 LIBCOUCHBASE_API lcb_STATUS lcb_getmanifest(lcb_INSTANCE *instance, void *cookie, const lcb_CMDGETMANIFEST *cmd);
431 
432 typedef struct lcb_RESPGETCID_ lcb_RESPGETCID;
433 
434 LIBCOUCHBASE_API lcb_STATUS lcb_respgetcid_status(const lcb_RESPGETCID *resp);
435 LIBCOUCHBASE_API lcb_STATUS lcb_respgetcid_cookie(const lcb_RESPGETCID *resp, void **cookie);
436 LIBCOUCHBASE_API lcb_STATUS lcb_respgetcid_manifest_id(const lcb_RESPGETCID *resp, uint64_t *id);
437 LIBCOUCHBASE_API lcb_STATUS lcb_respgetcid_collection_id(const lcb_RESPGETCID *resp, uint32_t *id);
438 LIBCOUCHBASE_API lcb_STATUS lcb_respgetcid_scoped_collection(const lcb_RESPGETCID *resp, const char **name,
439  size_t *name_len);
440 
441 typedef struct lcb_CMDGETCID_ lcb_CMDGETCID;
442 
443 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetcid_create(lcb_CMDGETCID **cmd);
444 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetcid_destroy(lcb_CMDGETCID *cmd);
445 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetcid_scope(lcb_CMDGETCID *cmd, const char *scope, size_t scope_len);
446 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetcid_collection(lcb_CMDGETCID *cmd, const char *collection, size_t collection_len);
447 LIBCOUCHBASE_API lcb_STATUS lcb_cmdgetcid_timeout(lcb_CMDGETCID *cmd, uint32_t timeout);
448 
449 LIBCOUCHBASE_API lcb_STATUS lcb_getcid(lcb_INSTANCE *instance, void *cookie, const lcb_CMDGETCID *cmd);
467 typedef enum {
469  LCB_NODE_HTCONFIG = 0x01,
473  LCB_NODE_VIEWS = 0x04,
475  LCB_NODE_CONNECTED = 0x08,
476 
481  LCB_NODE_NEVERNULL = 0x10,
482 
485 
490  LCB_NODE_HTCONFIG_ANY = 0x11
492 
495 #define LCB_GETNODE_UNAVAILABLE "invalid_host:0"
496 
539 LIBCOUCHBASE_API
540 const char *lcb_get_node(lcb_INSTANCE *instance, lcb_GETNODETYPE type, unsigned index);
541 
559 LIBCOUCHBASE_API
560 const char *lcb_get_keynode(lcb_INSTANCE *instance, const void *key, size_t nkey);
561 
570 LIBCOUCHBASE_API
571 lcb_S32 lcb_get_num_replicas(lcb_INSTANCE *instance);
572 
579 LIBCOUCHBASE_API
580 lcb_S32 lcb_get_num_nodes(lcb_INSTANCE *instance);
581 
599 LIBCOUCHBASE_API
600 const char *const *lcb_get_server_list(lcb_INSTANCE *instance);
601 
609 LIBCOUCHBASE_API
610 void *lcb_mem_alloc(lcb_SIZE size);
611 
613 LIBCOUCHBASE_API
614 void lcb_mem_free(void *ptr);
615 
625 LCB_INTERNAL_API
626 void lcb_run_loop(lcb_INSTANCE *instance);
627 
629 LCB_INTERNAL_API
630 void lcb_stop_loop(lcb_INSTANCE *instance);
631 
633 /* This returns the library's idea of time */
634 LCB_INTERNAL_API
635 lcb_U64 lcb_nstime(void);
636 
637 #ifdef __cplusplus
638 }
639 #endif
640 #endif /* LCB_UTILS_H */
LCB_TIMEUNIT_NSEC
@ LCB_TIMEUNIT_NSEC
Time is in nanoseconds.
Definition: utils.h:220
lcb_timeunit_t
lcb_timeunit_t
Time units reported by lcb_get_timings()
Definition: utils.h:219
lcb_get_node
const char * lcb_get_node(lcb_INSTANCE *instance, lcb_GETNODETYPE type, unsigned index)
Return a string of host:port for a node of the given type.
lcb_histogram_print
void lcb_histogram_print(lcb_HISTOGRAM *hg, FILE *stream)
Print the histogram to the specified FILE.
LCB_TIMEUNIT_SEC
@ LCB_TIMEUNIT_SEC
Time is in seconds.
Definition: utils.h:223
LCB_NODE_CONNECTED
@ LCB_NODE_CONNECTED
Only return a node which is connected, or a node which is known to be up.
Definition: utils.h:462
LCB_NODE_NEVERNULL
@ LCB_NODE_NEVERNULL
Specifying this flag adds additional semantics which instruct the library to search additional resour...
Definition: utils.h:468
lcb_mem_alloc
void * lcb_mem_alloc(lcb_SIZE size)
Functions to allocate and free memory related to libcouchbase.
lcb_get_num_nodes
lcb_S32 lcb_get_num_nodes(lcb_INSTANCE *instance)
Get the number of the nodes in the cluster.
lcb_DUMPFLAGS
lcb_DUMPFLAGS
Definition: utils.h:284
lcb_GETNODETYPE
lcb_GETNODETYPE
Type of node to retrieve for the lcb_get_node() function.
Definition: utils.h:454
LCB_DUMP_METRICS
@ LCB_DUMP_METRICS
Dump various metrics information.
Definition: utils.h:292
lcb_get_timings
lcb_STATUS lcb_get_timings(lcb_INSTANCE *instance, const void *cookie, lcb_timings_callback callback)
Get the timings histogram.
LCB_DUMP_PKTINFO
@ LCB_DUMP_PKTINFO
Dump information about each packet.
Definition: utils.h:288
lcb_histogram_read
void lcb_histogram_read(const lcb_HISTOGRAM *hg, const void *cookie, lcb_HISTOGRAM_CALLBACK cb)
lcb_get_server_list
const char *const * lcb_get_server_list(lcb_INSTANCE *instance)
Get a list of nodes in the cluster.
lcb_histogram_create
lcb_HISTOGRAM * lcb_histogram_create(void)
lcb_enable_timings
lcb_STATUS lcb_enable_timings(lcb_INSTANCE *instance)
Start recording timing metrics for the different operations.
lcb_resp_get_error_context
const char * lcb_resp_get_error_context(int cbtype, const lcb_RESPBASE *rb)
LCB_NODE_HTCONFIG_CONNECTED
@ LCB_NODE_HTCONFIG_CONNECTED
Equivalent to LCB_NODE_HTCONFIG|LCB_NODE_CONNECTED
Definition: utils.h:471
lcb_timings_callback
void(* lcb_timings_callback)(lcb_INSTANCE *instance, const void *cookie, lcb_timeunit_t timeunit, lcb_U32 min, lcb_U32 max, lcb_U32 total, lcb_U32 maxtotal)
The following function is called for each bucket in the timings histogram when you call lcb_get_timin...
Definition: utils.h:268
lcb_STATUS
lcb_STATUS
Error codes returned by the library.
Definition: error.h:207
lcb_INSTANCE
struct lcb_st lcb_INSTANCE
Library handle representing a connection to a cluster and its data buckets.
Definition: couchbase.h:35
lcb_disable_timings
lcb_STATUS lcb_disable_timings(lcb_INSTANCE *instance)
Stop recording (and release all resources from previous measurements) timing metrics.
lcb_histogram_record
void lcb_histogram_record(lcb_HISTOGRAM *hg, lcb_U64 duration)
LCB_DUMP_ALL
@ LCB_DUMP_ALL
Dump everything.
Definition: utils.h:294
lcb_mem_free
void lcb_mem_free(void *ptr)
Use this to free memory allocated with lcb_mem_alloc.
LCB_NODE_HTCONFIG_ANY
@ LCB_NODE_HTCONFIG_ANY
Equivalent to LCB_NODE_HTCONFIG|LCB_NODE_NEVERNULL.
Definition: utils.h:477
lcb_histogram_destroy
void lcb_histogram_destroy(lcb_HISTOGRAM *hg)
LCB_DUMP_VBCONFIG
@ LCB_DUMP_VBCONFIG
Dump the raw vbucket configuration.
Definition: utils.h:286
LCB_TIMEUNIT_MSEC
@ LCB_TIMEUNIT_MSEC
Time is in milliseconds.
Definition: utils.h:222
lcb_get_num_replicas
lcb_S32 lcb_get_num_replicas(lcb_INSTANCE *instance)
Get the number of the replicas in the cluster.
lcb_get_keynode
const char * lcb_get_keynode(lcb_INSTANCE *instance, const void *key, size_t nkey)
Get the target server for a given key.
lcb_cbflush3
lcb_STATUS lcb_cbflush3(lcb_INSTANCE *instance, void *cookie, const lcb_CMDCBFLUSH *cmd)
LCB_DUMP_BUFINFO
@ LCB_DUMP_BUFINFO
Dump memory usage/reservation information about buffers.
Definition: utils.h:290
lcb_stop_loop
LCB_INTERNAL_API void lcb_stop_loop(lcb_INSTANCE *instance)
lcb_nstime
LCB_INTERNAL_API lcb_U64 lcb_nstime(void)
LCB_NODE_VIEWS
@ LCB_NODE_VIEWS
Get a view (CAPI) node.
Definition: utils.h:460
lcb_resp_get_error_ref
const char * lcb_resp_get_error_ref(int cbtype, const lcb_RESPBASE *rb)
LCB_TIMEUNIT_USEC
@ LCB_TIMEUNIT_USEC
Time is in microseconds.
Definition: utils.h:221
LCB_NODE_DATA
@ LCB_NODE_DATA
Get a data (memcached) node.
Definition: utils.h:458
lcb_run_loop
LCB_INTERNAL_API void lcb_run_loop(lcb_INSTANCE *instance)
LCB_NODE_HTCONFIG
@ LCB_NODE_HTCONFIG
Get an HTTP configuration (Rest API) node.
Definition: utils.h:456
lcb_dump
void lcb_dump(lcb_INSTANCE *instance, FILE *fp, lcb_U32 flags)
Write a textual dump to a file.