kv_operations_set_copy()

#include <kvstore.h>

kv_error_t 
kv_operations_set_copy(kv_operations_t **operations) 

Configures the supplied kv_operations_t so that user-supplied buffers, strings and structures are copied. Normally, buffers, text strings, and structures provided to this API are owned by the application. (The API simply points to the memory in question when making use of it.) This is highly efficient for short-lived user-supplied memory because it avoids memory allocation/copying of the memory's content.

However, if there is a need to retain the data supplied by the user for longer periods of time (beyond which the user-supplied memory might go out of scope, or otherwise be reused or even deallocated), then call this function immediately after creating the operations list. Doing so will cause the API to copy the contents of all user-supplied memory to memory owned by the API. In this way, the application can do whatever is appropriate with the memory it supplies, while the API is able to retain the contents of that memory for however long it needs that content.

As an example, suppose you were creating an operations list by iterating over records in the store, like this:

// Create an operation
// Store open skipped for brevity
kv_create_operations(store, &operations);

// Tell the operations list to copy keys/values
kv_operations_set_copy(operations);

// Create a store iterator that walks over every record in the store
err = kv_store_iterator(store, NULL, &iter, NULL, 0,
                        KV_DIRECTION_UNORDERED, 0, NULL, 0);
if (err != KV_SUCCESS) {
    fprintf(stderr, "Error obtaining store iterator: %d\n", err);
    goto done;
}

// Step through the iterator, doing work on each record's value.
// If kv_operations_set_copy() had not been called, iter_key and
// iter_value would go out of scope with each step through the store
// iterator. This would cause unpredictable results when it came time
// to execute the sequence of operations.
while (kv_iterator_next(iter,
                        (const kv_key_t **)&iter_key,
                        (const kv_value_t **)&iter_value)
                         == KV_SUCCESS) {
    // Do some work to iter_value
    kv_create_put_op(operations,  iter_key,  iter_value, 0);
}

if (iter)
    kv_release_iterator(&iter);

kv_execute(store, operations, &results, 0, 0); 

Parameters

  • operations

    The operations parameter references the operations list which you want to configure for copy.