JDO : JDOQL In-Memory queries

The typical use of a JDOQL query is to translate it into the native query language of the datastore and return objects matched by the query. Sometimes you want to query over a set of objects that you have to hand, or for some datastores it is simply impossible to support the full JDOQL syntax in the datastore native query language . In these situation we need to evaluate the query in-memory . In the latter case of the datastore not supported the full JDOQL syntax we evaluate as much as we can in the datastore and then instantiate those objects and evaluate further in-memory. Here we document the current capabilities of in-memory evaluation in DataNucleus.

To enable evaluation in memory you specify the query extension datanucleus.query.evaluateInMemory to true as follows

query.addExtension("datanucleus.query.evaluateInMemory","true");

This is also useful where you have a Collection of (persisted) objects and want to run a query over the Collection. Simply turn on in-memory evaluation, and supply the candidate collection to the query, and no communication with the datastore will be needed.

Specify candidates to query over

With JDO you can define a set of candidate objects that should be queried, rather than just going to the datastore to retrieve those objects. When you specify this you will automatically be switched to evaluate the query in-memory. You set the candidates like this

Query query = pm.newQuery(...);
query.setCandidates(myCandidates);
List<Product> results = (List<Product>)query.execute();


Methods

JDOQL defines support for some methods within its syntax. The following are supported for in-memory evaluation currently.

Java Type Method Description
java.lang. String startsWith(String) Returns if the string starts with the passed string
java.lang. String endsWith(String) Returns if the string ends with the passed string
java.lang. String indexOf(String) Returns the first position of the passed string
java.lang. String indexOf(String, int) Returns the position of the passed string, after the passed position
java.lang. String length() Returns the length of the passed string
java.lang. String substring(int) Returns the substring starting from the passed position
java.lang. String substring(int, int) Returns the substring between the passed positions
java.lang. String toLowerCase() Returns the string in lowercase
java.lang. String toUpperCase() Retuns the string in UPPERCASE
java.lang. String matches(String pattern) Returns whether string matches the passed expression. The pattern argument follows the rules of java.lang.String.matches method.
java.lang. String trim() Trims leading and trailing whitespace from the string
java.lang. Enum toString() Returns the string form of the enum
java.lang. Enum ordinal() Returns the ordinal form of the enum
java.util. Collection isEmpty() Returns whether the collection is empty
java.util. Collection contains(value) Returns whether the collection contains the passed element
java.util. Collection size() Returns the number of elements in the collection
java.util. List get(position) Returns the List element at this position
java.util. Map isEmpty() Returns whether the map is empty
java.util. Map containsKey(key) Returns whether the map contains the passed key
java.util. Map containsValue(value) Returns whether the map contains the passed value
java.util. Map get(key) Returns the value from the map with the passed key
java.util. Map size() Returns the number of entries in the map
java.util. Date getTime() Returns the millisecs of the passed date
java.util. Date getDay() Returns the day (of the month) of the passed date
java.util. Date getMonth() Returns the month (of the year) of the passed date
java.util. Date getYear() Returns the year of the passed date
java.util. Date getHour() Returns the hour (of the day) of the passed date
java.util. Date getMinute() Returns the minute (of the hour) of the passed date
java.util. Date getSecond() Returns the second (of the minute) of the passed date
java.lang. Math abs(number) Returns the absolute value of the passed number
java.lang. Math sqrt(number) Returns the square root of the passed number
javax.jdo. JDOHelper getObjectId(object) Returns the object identity of the passed persistent object
javax.jdo. JDOHelper getVersion(object) Returns the current version of the passed persistent object

Support for in-memory evaluation of query methods is provided via a plugin-point so can be extended easily.