DEV Community

Eugene R. for Aerospike

Posted on • Edited on

Dealing with Predicate Expression Filters in Aerospike REST Client (Part 1)

If you're already an Aerospike user, you’ve likely developed with one of the client libraries available on GitHub, and might be familiar with Predicate Expression filters, which became available for all transactions starting from Aerospike Server v4.8.

I want to show how you would use PredExp filters with the REST client.

So what is the PredExp filter and why do we need it? Predicate expression filters are applied to scan, query and key-value results on the server. Meaning, if an expression evaluates to false, the transaction is ignored.

The REST client uses the Java client under the covers, where PredExp filters should be declared as an array in postfix notation. This form is hard to read and not intuitive. Having that in mind, how can we send such a construction using the REST API?

To solve this issue, I created a sort of DSL in infix notation, which uses well known logical operators and supports a special filters syntax.

A lot of theory till now, let's jump to some examples.

Here’s an example of a Java client PredExp filter:

PredExp.integerBin("bin2"),
PredExp.integerValue(126),
PredExp.integerGreater(),
PredExp.integerBin("bin2"),
PredExp.integerValue(140),
PredExp.integerLessEq(),
PredExp.and(2),
PredExp.integerBin("bin2"),
PredExp.integerValue(360),
PredExp.integerEqual(),
PredExp.or(2)
Enter fullscreen mode Exit fullscreen mode

Which actually expresses the following expression:

(bin2 > 126 and bin2 <= 140) or bin2 == 360
Enter fullscreen mode Exit fullscreen mode

Isn't the latter more concise and intuitive?

What is left, is to Base64 encode it, and send it as a GET parameter with the request.

Want the bin value to be a String? Just wrap it with double quotes like this. For non-numeric values we can omit the quotes:

(bin2 == "126" and bin2 != "140") or bin2 == ten
Enter fullscreen mode Exit fullscreen mode

Before I wrap up, let's talk about some PredExp use cases.

  • Let's say you have a counter, but you only want to increment it if the value is under a certain number (for example 100).

  • Don't alter the record if its expiration time is above or under some particular value (here you'll need to write a special filter VOID_TIME which is not covered in this post).

This was a very simple example of how to use Predicate Expression Filters in the Aerospike REST Client. In my next post, I plan to write about special filter operators and their syntax.

If you still haven't tinkered with the Aerospike REST client, you can find it here:
https://github.com/aerospike/aerospike-client-rest

Take care, stay tuned and continue to Aerospike. Till next time.

Top comments (1)

Collapse
 
kportertx profile image
Kevin Porter

Minor correction, predexp on all transactions started with 4.7.0.2.

"[AER-6101] - (KVS) Added predicate filter support for batch, read, write, delete, and record UDF transactions."