How to create a scan and touch UDF

How to create a scan and touch UDF

Context

There are certain circumstances in which it may be necessary to simulate an operation on a record without actually modifying the record at all. An example of this would be when changing the compression level of an Aerospike namespace. The selected compression level is applied as records are written, so if the level is changed, this will not be applied to records that have already been stored. In that instance the record must be ‘touched’ to simulate an operation without changing any contents. A scan and touch UDF can iterate through the records in a namespace to touch them all.

Method

The general format for a scan and touch UDF is as follows:

function touch(rec)
   record.set_ttl(rec, -2)
   aerospike:update(rec)
end

In the example above the TTL is set to -2 and so should not modify the existing TTL and impact expirations. It should be noted that a touch will increment the generation of a record.

Although the scan and touch method is traditionally associated with an UDF as described above, in Aerospike 4.7 and higher there is the facility to do background operations as part of the scan without having to use a separate UDF. In general background operations can be considered more efficient than a scan and UDF combination. Background operations can touch the record but cannot take TTL as a parameter meaning that the namespace default would be used. This is a factor that should be considered if using the background scan / operation method to touch records in a TTL enabled namespace.

Discussion on scan background operations can be found within the various client documentation sets.

Notes

  • Potential extensions to the UDF shown above might include using predicate expression metadata filters when changing compression level to ignore new records which would have been written compressed. This saves decompress and recompress cycles on records that are already at the desired compression level.
  • Examples of background scans with predicate filters can be found on the Background UDF Example documentation page.
  • A background UDF as shown above can be run either via a scan or via query if desired.
  • Aerospike also supports touching records as part of client operations as described here.

Keywords

SCAN TOUCH UDF EXAMPLE CHANGE COMPRESSION PREDICATE

Timestamp

July 2020

© 2015 Copyright Aerospike, Inc. | All rights reserved. Creators of the Aerospike Database.