Exforsys

Home arrow Reviews arrow Oracle Coherence

Oracle Coherence: Built-in Filters

Author: Packt Publishing     Published on: 26th Jun 2010

Fortunately, Coherence provides a number of built-in filters that make custom filter implementation unnecessary in the vast majority of cases.

Ads

Built-in filters

Most queries can be expressed in terms of object attributes and standard logical and relational operators, such as AND, OR, equals, less than, greater than, and so on. For example, if we wanted to find all the transactions for an account, it would be much easier if we could just execute the query analogous to the select * from Transactions where account_id = 123 SQL statement than to write a custom filter that checks if the accountId attribute is equal to 123.

The good news is that Coherence has a number of built-in filters that allow us to do exactly that. The following table lists all the filters from the com.tangosol.util.filter package that you can use to construct custom queries:

----------------------------------------------------------------------------------
Filter Class Description
----------------------------------------------------------------------------------
EqualsFilter Compares two values for equality.
NotEqualsFilter Compares two values for inequality.
IsNullFilter Checks if the value is null.
IsNotNullFilter Checks if the value is not null.
LessFilter Checks if the first value is smaller than the second value.
LessEqualsFilter Checks if the first value is smaller than or equal to the second value.
GreaterFilter Checks if the first value is greater than the second value.
GreaterEqualsFilter Checks if the first value is greater than or equal to the second value.
BetweenFilter Checks if the value is within certain range.
LikeFilter Checks if the value matches specified pattern, similar to the SQL LIKE operator.
InFilter Checks if the value is within a specified collection.
InKeySetFilter Checks if the entry key is within a specified collection.
ContainsFilter Checks if the collection contains a specified element.
ContainsAllFilter Checks if the collection contains all of the elements from another collection.
ContainsAnyFilter Checks if the collection contains any of the elements from another collection.
AndFilter Returns the logical and of two other filters.
OrFilter Returns the logical or of two other filters.
XorFilter Returns the logical exclusive or of two other filters.
NotFilter Negates the results of another filter.
LimitFilter Allows paging through the results of another filter.
AlwaysFilter Returns true.
NeverFilter Returns false.
AllFilter Returns logical and of all filters in a filter array.
AnyFilter Returns logical or of all filters in a filter array.

As you can see, pretty much all of the standard Java logical operators and SQL predicates are covered. This will allow us to construct query expressions as complex as the ones we can define in Java code or the SQL where clause.

The bad news is that there is no query language in Coherence that allows you to specify a query as a string. Instead, you need to create the expression tree for the query programmatically, which can make things a bit tedious.

For example, the where clause of the SQL statement we specified earlier, select * from Transactions where account_id = 123, can be represented by the following Coherence filter definition:

Sample Code
  1.     Filter filter = new EqualsFilter("getId.getAccountId", 123)
Copyright exforsys.com


In this case it is not too bad: we simply create an instance of an EqualsFilter that will extract the value of an accountId attribute from a Transaction.Id instance and compare it with 123. However, if we modify the query to filter transactions by date as well, the filter expression that we need to create becomes slightly more complex:

Filter filter = new AndFilter( new EqualsFilter("getId.getAccountId", accountId), new BetweenFilter("getTime", from, to));

Ads

If you need to combine several logical expressions, this can quickly get out of hand, so we will look for a way to simplify filter creation shortly. But first, let's talk about something we used in the examples without paying much attention to it--value extractors.



 
This tutorial is part of a Oracle Coherence tutorial series. Read it from the beginning and learn yourself.

Oracle Coherence

 

Comments