Question about aerospike_batch_get with binary keys


#1

Hi there,

I have a set which the key is 16-byte binary data. The code I used to insert the data is as follows:

as_key key;
    
    uint64_t keyval[2];
    keyval[0] = i;
    keyval[1] = i;        
    
    as_key_init_raw(&key, g_namespace, k_set, (uint8_t*)(keyval), 16);
            
    as_record rec;
    as_record_inita(&rec, 1);
    as_record_set_int64(&rec, "kbin1", (uint64_t)i);

    // Write a record to the database.
    if (aerospike_key_put(p_as, &err, NULL, &key, &rec) != AEROSPIKE_OK) {
        LOG("aerospike_key_put() returned %d - %s", err.code, err.message);
        return false;

I was able to verify after the insert, the records are good and key return from


if (aerospike_key_get(&as, &err, NULL, &key, &p_rec) != AEROSPIKE_OK) {}
uint8_t* keys =  as_bytes_get((as_bytes*) (key.valuep));    

is what I inserted.

Then when I do the aerospike_batch_get, I can see it got same number of records back, but each record.result is AEROSPIKE_ERR_RECORD_NOT_FOUND. The batch code is as follows:

as_batch batch; as_batch_init(&batch, g_n_keys);

for (uint32_t i = 0; i < g_n_keys; i++) {
    uint64_t keyval[2];
    keyval[0] = i;
    keyval[1] = i;
    
    as_key_init_raw(as_batch_keyat(&batch, i), g_namespace, k_set, (uint8_t*)(keyval), 16);                
}

    
// Get all of these keys - they should all be there.
if (aerospike_batch_get(&as, &err, NULL, &batch, batch_read_cb, NULL) !=
        AEROSPIKE_OK) {
    LOG("aerospike_batch_get() returned %d - %s", err.code, err.message);
    cleanup(&as);
    exit(-1);
}

Anything wrong with the code?

Thanks,

Mark

I }


#2

Here’s the example included in the C client SDK which has a batch get, with integer keys.

Thanks.


#3

Thanks a lot. That is what I used as the base for my code. In particular, I want to use raw binary data as the key. I inserted the key and then used aerospike_key_get(&as, &err, NULL, &key, &p_rec) to get the key/record back, and they look good. But the aerospike_batch_get(&as, &err, NULL, &batch, batch_read_cb, NULL) seems not giving me the right content.

Is there any example of using binary data as the key?


#4

I added a demo file at:

Can you please take a look?

Thanks, Mark


#5

I finally figured out.

The problem was in the key init for the batch. The code with the problem was:


for (uint32_t i = 0; i < g_n_keys; i++) {
    uint64_t keyval[2];
    keyval[0] = i;
    keyval[1] = i;

    as_key_init_raw(as_batch_keyat(&batch, i), g_namespace, k_set, (uint8_t*)(keyval), 16);                
}

The keyval was available only within the loop. When the loop finishes, the key in the batch was left with undesired value. By moving the keyval out of the loop, it fixes the problem.

The updated code is in github now.

I wish there is a better example or documentation explain this.

<