How to pass in objects or arrays as parameters into UDF?


#1

I know this has been answered before. But the examples in below link have already expired. Can you help?


#2

Hi,

Let’s say you have an existing record (identified by a key = 0) that has two bins: username-bin (String) and age-bin (Integer), and you want to edit the age-bin value by doing a simple arithmetic operation using a lua function called from the C client.

UDF function:

function set_bin(record, bin_name, x, y)
     record[bin_name] = (record[bin_name] * x) + y
     aerospike:update(record)
     return record[bin_name]
end

Define the record key:

The Aerospike C Client API provides aerospike_key_apply() to apply a user-defined function against a record in the database via its key.

as_key key;
as_key_init_int64(&key, "namespace", "set", 0);

Define the argument list:

as_arraylist args;
as_arraylist_inita(&args, 3);
as_arraylist_append_str(&args, "age-bin");
as_arraylist_append_int64(&args, 1);
as_arraylist_append_int64(&args, 5);

Apply the UDF function:

as_val * result = NULL;
if (aerospike_key_apply(&as, &err, NULL, &key, "module", "set_bin", (as_list *) &args, &result) != AEROSPIKE_OK) {
     fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);
}

The returned value from the UDF function will be returned in the result parameter.

Print the new bin value:

as_integer* p_integer = as_integer_fromval(result);
if (! p_integer) {
     LOG("non-as_integer object");
     return true;
}
LOG("The new age-bin value is %" PRId64, as_integer_get(p_integer));