AerospikeC client manual --- data scanning-record changes
Record change Aerospike provides the ability to scan records in the database and change each record data through user-defined functions (udfs.
In SQL, the UPDATE statement for all records in the change table without conditions is:
UPDATE test.demoSET a = 1, b = 2, c = 3
Aeropike provides a function similar to the preceding SQL statement, which allows you to apply a function to each record and use the function to change the record data. This function, called UDF, is applied to a single record. It can provide parameters to read and write the bin value of the record and perform computation.
Currently, scanning and applying udfs to each record can only be performed in the background. That is to say, the client sends the scan request to the database without waiting for the result. Scan requests are put into the queue and executed in the database. No results are returned to the client. The client will obtain an id to identify the task corresponding to the scan request sent. You can use this id to check the scan status.
Define Scan
In addition to defining a UDF that can be applied to each record, the definition of the Change Record content is the same as that of the normal scan, and the background scan operation: aerospike_scan_background () is used ().
As we did in the record scan section, we first build a scan object, but do not need to select bin, and add an as_scan_apply_each () operation:
as_scan scan;as_scan_init(&scan, "test", "demo");as_scan_apply_each(&scan, "mymodule", "mytransform", NULL);
Explanation:
Row 3-create an as_scan object on the stack.
Row 3-initialize the scan. The namespace of the scanned data is named "test" and the set name is "demo".
Row 3-adds a user-defined function (UDF), which is applied to each scanned record. The UDF mytransform () is defined in the module mymodule. In this example, the UDF does not need any parameters.
Defining the Record UDF definition Record UDF
The following is the mytransform" Record UDF, defined inmymodule`. It is a pretty simple Record UDF, mimicking the SQL statement above.
The UDF defined in the module mymodule is named mytransform, which is quite simple and imitates the behavior of the preceding SQL statement.
function mytransform(rec) rec['a'] = 1 rec['b'] = 2 rec['c'] = 3 aerospike:update(rec)end
A more elaborate function might be to increment "a", calculate "b" based on "a", and calculate "c" based on the values of "a" and "b":
A slightly more complex function can add "a", calculate "B" based on "a", and calculate "c" based on "a" and "B ":
function mytransform(rec) rec['a'] = rec['a'] + 1 rec['b'] = rec['a'] * 2 rec['c'] = rec['a'] + rec['b'] aerospike:update(rec)end
Scan
This scan can be executed using areospike_scan_background:
uint64_t scan_id = 0;if (aerospike_scan_background(&as, &err, NULL, &scan, &scan_id) != AEROSPIKE_OK) { fprintf(stderr, "error(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}
Scan_id is used to query the scan status. The value of scand_id is 0, so the scan operation generates and sets the value of scan_id.
Check the scan status
You can use aerospike_scan_info () to query the scan status. The status information is filled in an as_scan_info object.
as_scan_info scan_info;if (aerospike_scan_info(&as, &err, NULL, scan_id, &scan_info) != AEROSPIKE_OK) { fprintf(stderr, "error(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}