Predicates - hazelcast/hazelcast-scala GitHub Wiki
In Hazelcast the Predicate
interface is a simple functional interface for querying entries. It comes with two implementations that will dramatically improve query performance when indexing is used.
SQL-like
Hazelcast has a SQL-like predicate (SqlPredicate
) for writing WHERE-like filter statements. In hazelcast-scala, it's provided through a StringContext
macro, which is compile-time checked (provided no substitution arguments are used).
val result = imap.values(where"active AND ( age > 20 OR salary < 60000 )")
If the statement doesn't parse, the compiler will complain instead of throwing a runtime exception.
Criteria API
Similarly, the Criteria API (PredicateBuilder
/EntryObject
) can be used like this:
val result = imap.values((where("active") = true) && (where("age") > 20 || where("salary") < 60000))
To query the basic types directly (primitives, String
, etc.), use .key
or .value
:
val imap: IMap[Int, Long] = ???
val keysLessThan10 = imap.values(where.key < 10)
val valuesGreaterThan200 = imap.values(where.value > 200L)
Partial results
If queries are too big, the results can be sorted and limited to avoid excessive network traffic.
E.g. using the same map as above, we'd like to get the smallest 10 values greater than 200, and the overall 10 largest values:
val imap: IMap[Int, Long] = ???
val top10SmallestValuesGreaterThan200 = imap.values(0 to 9, where.value > 200L)(sortBy = _.value)
val top10LargestValues = imap.values(0 to 9)(sortBy = _.value, reverse = true)