kv_operations_set_copy()

#include <kvstore.h>

kv_error_t 
kv_operations_set_copy(kv_operations_t **operations) 

ユーザー指定のバッファ、文字列および構造がコピーされるように、指定されたkv_operations_tを構成します。通常、このAPIに提供されるバッファ、テキスト文字列および構造は、アプリケーションによって所有されます。(APIは、使用時に単に問題のメモリーを指し示します。)これは、メモリーの割当てやメモリーの内容のコピーを回避するため、ユーザーが指定した短期間のメモリーに非常に効率的です。

ただし、ユーザーが指定するデータをより長時間保持する必要がある場合(この期間を超えると、ユーザー指定のメモリーが有効範囲外になるか、再利用さらには割当て解除される可能性があります)、操作リストの作成直後にこの関数を呼び出します。そうすることにより、APIはユーザーが指定したすべてのメモリーの内容を、APIが所有するメモリーにコピーします。このようにして、アプリケーションは指定するメモリーに適切なあらゆる処理を実行できるようになり、APIはそのメモリーの内容を必要な時間だけ保持できるようになります。

たとえば、次のようにストア内のレコードを反復処理して操作リストを作成したとします。

// 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); 

パラメータ

  • operations

    operationsパラメータは、コピー用に構成する操作リストを参照します。