Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Scan Applying a Record UDF

Mira Dytko edited this page Jan 20, 2022 · 3 revisions

Scan Applying a Record UDF

Use the following Aerospike PHP client APIs to apply a UDF to each record in a set in the background:

  • scanApply()
  • scan()

UDFs must be registered with the Aerospike server.

Running the Background Scan

This example uses the registered a UDF module named rec_udf with the Lua function accumulate:

function accumulate(rec, collector, source)
    rec[collector] = rec[collector] + rec[source]
    aerospike:update(rec)
end

To apply this UDF to all the records of a specified set:

$status = $db->scanApply("test", "users", "rec_udf", "accumulate", ["compensation", "income"], $scan_id);
if ($status == Aerospike::OK) {
    echo "The background scan ID is $scan_id\n";
    while(true) {
        $status = $db->scanInfo($scan_id, $info);
        if ($status == Aerospike::OK) {
            var_dump($info);
            if ($info["status"] === Aerospike::SCAN_STATUS_COMPLETED) {
                echo "Background scan is complete!";
                break;
            }
        }
        sleep(1);
    }
} else {
    echo "An error occurred while launching the background scan [{$db->errorno()}] ".$db->error();
}

The records update and compensation adds income to its value.