Couchbase C Client  2.10.6
Asynchronous C Client for Couchbase
Create/Update

Detailed Description

Set the value of a document.

Functions

lcb_error_t lcb_store3 (lcb_t instance, const void *cookie, const lcb_CMDSTORE *cmd)
 Schedule a single storage request. More...
 

Macros

#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 buffer. More...
 
#define LCB_CMD_SET_VALUEIOV(scmd, iovs, niovs)
 Set value from a series of input buffers. This may be used when the input buffer is not contiguous. Using this call eliminates the need for creating a temporary contiguous buffer in which to store the value. More...
 

Enumerations

enum  lcb_storage_t
 Values for lcb_CMDSTORE::operation. More...
 

Function Documentation

◆ lcb_store3()

lcb_error_t lcb_store3 ( lcb_t  instance,
const void *  cookie,
const lcb_CMDSTORE cmd 
)

Schedule a single storage request.

Stability
Committed:
Parameters
instancethe handle
cookiepointer to associate with the command
cmdthe command structure
Returns
LCB_SUCCESS on success, error code on failure

Request

lcb_CMDSTORE cmd = { 0 };
LCB_CMD_SET_KEY(&cmd, "Key", 3);
LCB_CMD_SET_VALUE(&cmd, "value", 5);
cmd.operation = LCB_ADD; // Only create if it does not exist
cmd.exptime = 60; // expire in a minute
lcb_store3(instance, cookie, &cmd);

### Response

lcb_install_callback3(instance, LCB_CALLBACK_STORE, store_callback);
void store_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb)
{
if (rb->rc == LCB_SUCCESS) {
printf("Store success: CAS=%llx\n", rb->cas);
} else {
printf("Store failed: %s\n", lcb_strerror(NULL, rb->rc);
}
}

Operation-specific error codes include:

Note
After a successful store operation you can use lcb_endure3_ctxnew() to wait for the item to be persisted and/or replicated to other nodes.
Examples
example/crypto/openssl_symmetric_encrypt.c, example/libeventdirect/main.c, example/minimal/minimal.c, example/minimal/query.c, and example/tracing/tracing.c.

Data Structure Documentation

◆ lcb_CMDSTORE

struct lcb_CMDSTORE

Command for storing an item to the server.

This command must contain the key to mutate, the value which should be set (or appended/prepended) in the lcb_CMDSTORE::value field (see LCB_CMD_SET_VALUE()) and the operation indicating the mutation type (lcb_CMDSTORE::operation).

Warning
exptime cannot be used with operation set to LCB_APPEND or LCB_PREPEND.
Examples
example/crypto/openssl_symmetric_encrypt.c, example/libeventdirect/main.c, example/minimal/minimal.c, example/minimal/query.c, example/tracing/tracing.c, and example/tracing/views.c.
Data Fields
lcb_U32 cmdflags Common flags for the command.

These modify the command itself. Currently the lower 16 bits of this field are reserved, and the higher 16 bits are used for individual commands.

lcb_U32 exptime Specify the expiration time.

This is either an absolute Unix time stamp or a relative offset from now, in seconds. If the value of this number is greater than the value of thirty days in seconds, then it is a Unix timestamp. This field is used in mutation operations (lcb_store3()) to indicate the lifetime of the item. It is used in lcb_get3() with the lcb_CMDGET::lock option to indicate the lock expiration itself.

lcb_U64 cas The known CAS of the item.

This is passed to mutation to commands to ensure the item is only changed if the server-side CAS value matches the one specified here. For other operations (such as lcb_CMDENDURE) this is used to ensure that the item has been persisted/replicated to a number of servers with the value specified here.

lcb_KEYBUF key The key for the document itself.

This should be set via LCB_CMD_SET_KEY()

lcb_KEYBUF _hashkey
Stability
Volatile:
lcb_VALBUF value Value to store on the server.

The value may be set using the LCB_CMD_SET_VALUE() or LCB_CMD_SET_VALUEIOV() API

lcb_U32 flags Format flags used by clients to determine the underlying encoding of the value.

This value is also returned during retrieval operations in the lcb_RESPGET::itmflags field

lcb_datatype_t datatype Do not set this value for now.
lcb_storage_t operation Controls how the operation is perfomed.

See the documentation for lcb_storage_t for the options. There is no default value for this field.

◆ lcb_RESPSTORE

struct lcb_RESPSTORE

Response structure for lcb_store3()

Data Fields
void * cookie Application-defined pointer passed as the cookie parameter when scheduling the command.
const void * key Key for request.
lcb_SIZE nkey Size of key.
lcb_CAS cas CAS for response (if applicable)
lcb_error_t rc Status code.
lcb_U16 version ABI version for response.
lcb_U16 rflags Response specific flags.

see lcb_RESPFLAGS

lcb_storage_t op The type of operation which was performed.

Macro Definition Documentation

◆ 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 buffer.

Stability
Committed:
Parameters
scmdan lcb_CMDSTORE pointer
valbufthe buffer for the value
vallenthe length of the buffer

The buffer needs to remain valid only until the command is passed to the lcb_store3() function.

Examples
example/crypto/openssl_symmetric_encrypt.c, example/libeventdirect/main.c, example/minimal/minimal.c, example/minimal/query.c, example/observe/durability.c, example/subdoc/subdoc-xattrs.c, and example/tracing/tracing.c.

◆ LCB_CMD_SET_VALUEIOV

#define LCB_CMD_SET_VALUEIOV (   scmd,
  iovs,
  niovs 
)

Set value from a series of input buffers. This may be used when the input buffer is not contiguous. Using this call eliminates the need for creating a temporary contiguous buffer in which to store the value.

Stability
Committed:
Parameters
scmdthe command which needs a value
iovsan array of lcb_IOV structures
niovsnumber of items in the array.

The buffers (and the IOV array itself) need to remain valid only until the scheduler function is called. Once the scheduling function is called, the buffer contents are copied into the library's internal buffers.

Enumeration Type Documentation

◆ lcb_storage_t

Values for lcb_CMDSTORE::operation.

Storing an item in couchbase is only one operation with a different set of attributes / constraints.

Enumerator
LCB_ADD 

Will cause the operation to fail if the key already exists in the cluster.

LCB_REPLACE 

Will cause the operation to fail unless the key already exists in the cluster.

LCB_SET 

Unconditionally store the item in the cluster.

LCB_UPSERT 

The default storage mode.

This constant was added in version 2.6.2 for the sake of maintaining a default storage mode, eliminating the need for simple storage operations to explicitly define lcb_CMDSTORE::operation. Behaviorally it is identical to LCB_SET in that it will make the server unconditionally store the item, whether it exists or not.

LCB_APPEND 

Rather than setting the contents of the entire document, take the value specified in lcb_CMDSTORE::value and append it to the existing bytes in the value.

LCB_PREPEND 

Like LCB_APPEND, but prepends the new value to the existing value.