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

Detailed Description

Set the value of a document

Function Documentation

◆ lcb_cmdstore_on_behalf_of()

lcb_STATUS lcb_cmdstore_on_behalf_of ( lcb_CMDSTORE * cmd,
const char * data,
size_t data_len )
Stability
Internal
Internal: This should never be used and is not supported.

◆ lcb_cmdstore_on_behalf_of_extra_privilege()

lcb_STATUS lcb_cmdstore_on_behalf_of_extra_privilege ( lcb_CMDSTORE * cmd,
const char * privilege,
size_t privilege_len )
Stability
Internal
Internal: This should never be used and is not supported.

Typedef Documentation

◆ lcb_RESPSTORE

typedef struct lcb_RESPSTORE_ lcb_RESPSTORE

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;
// insert only if key doesn't already exist
lcb_cmdstore_create(&cmd, LCB_STORE_INSERT);
lcb_cmdstore_key(cmd, "Key", 3);
lcb_cmdstore_value(cmd, "value", 5);
lcb_cmdstore_expiry(cmd, 60); // expire in a minute
lcb_store(instance, cookie, &cmd);
@ LCB_STORE_INSERT
Will cause the operation to fail if the key already exists in the cluster.
Definition couchbase.h:900
lcb_STATUS lcb_wait(lcb_INSTANCE *instance, lcb_WAITFLAGS flags)
Wait for completion of scheduled operations.
@ LCB_WAIT_NOCHECK
Do not check pending operations before running the event loop.
Definition couchbase.h:1870

Response

lcb_install_callback(instance, LCB_CALLBACK_STORE, store_callback);
void store_callback(lcb_INSTANCE *instance, int cbtype, const lcb_RESPBASE *rb)
{
uint64_t cas;
const lcb_RESPSTORE resp = (const lcb_RESPSTORE*)rb;
lcb_STATUS rc = lcb_respstore_status(resp);
if (rc == LCB_SUCCESS) {
lcb_respstore_cas(resp, &cas)
printf("Store success: CAS=%llx\n", cas);
} else {
printf("Store failed: %s\n", lcb_strerror_short(rc);
}
}
LCB_INTERNAL_API const char * lcb_strerror_short(lcb_STATUS error)
Get a shorter textual description of an error message.
lcb_STATUS
Error codes returned by the library.
Definition error.h:213
struct lcb_st lcb_INSTANCE
Library handle representing a connection to a cluster and its data buckets.
Definition couchbase.h:35
lcb_RESPCALLBACK lcb_install_callback(lcb_INSTANCE *instance, int cbtype, lcb_RESPCALLBACK cb)
@ LCB_CALLBACK_STORE
lcb_store()
Definition couchbase.h:472
struct lcb_RESPSTORE_ lcb_RESPSTORE
Schedule a single storage request.
Definition couchbase.h:978

Operation-specific error codes include:

  • ::LCB_ERR_DOCUMENT_NOT_FOUND if ::LCB_REPLACE was used and the key does not exist
  • ::LCB_ERR_DOCUMENT_EXISTS if ::LCB_INSERT was used and the key already exists
  • ::LCB_ERR_DOCUMENT_EXISTS if the CAS was specified (for an operation other than ::LCB_INSERT) and the item exists on the server with a different CAS
  • ::LCB_ERR_DOCUMENT_EXISTS if the item was locked and the CAS supplied did not match the locked item's CAS (or if no CAS was supplied)
  • ::LCB_ERR_NOT_STORED if an ::LCB_APPEND or ::LCB_PREPEND operation was performed and the item did not exist on the server.
  • ::LCB_ERR_VALUE_TOO_LARGE if the size of the value exceeds the cluster per-item value limit (currently 20MB).

For a 6.5 or later cluster, you should use the lcb_cmdstore_durability to make the lcb_store not return until the requested durabilty is met. If the cluster is an older version, you can use lcb_cmdstore_durability_observe.

Enumeration Type Documentation

◆ lcb_STORE_OPERATION

Values for lcb_CMDSTORE::operation.

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

Enumerator
LCB_STORE_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_STORE_UPSERT in that it will make the server unconditionally store the item, whether it exists or not.

LCB_STORE_INSERT 

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

LCB_STORE_REPLACE 

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

LCB_STORE_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_STORE_PREPEND 

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