Apply a UDF
Single record
UDFs that execute on a single record are Record UDFs. The record may or may not exist in the database, which allows the UDF to update or create a record.
To invoke a Record UDF, use Client.execute_udf()
:
pub fn execute_udf(&self, policy: &WritePolicy, key: &Key, udf_name: &str, function_name: &str, args: Option<&[Value]>) -> Result<Option<Value>>;
Where,
policy
— The policy governing the operation.key
— The key of the record on which to invoke the function.udf_name
— The UDF module that contains the function to invoke.function_name
— The function to invoke.args
— The optional function arguments.
This example defines a UDF in the module examples.lua:
function readBin(r, name) return r[name]end
readBin
returns the value of record r in bin name.
The client application can invoke readBin
on a record:
let result = client.execute_udf(&WritePolicy::default(), &key, "examples", "readBin", as_values!("name"));
key
specifies the record to pass to the UDF as the parameter r
.
Multiple Arguments
If the UDF accepts multiple arguments, add each argument to client.execute_udf()
.
For example, if the following UDF is defined in example.lua:
function multiplyAndAdd(r, a, b) return r['factor'] * a + b;end
(This multiplies the bin factor by a and adds b, and returns results to the caller.)
Then, to invoke multiplyAndAdd()
from Rust, run:
let result = client.execute_udf(&WritePolicy::default(), &key, "examples", "multiplyAndAdd", as_values!(10, 5));