Here we go, attached is the patch against trunk and jar for reusable and caching queries. The patch also contains unit tests. It seems to work quite fine even for complex queries. For about 10000 objects, when indexing can be used, the improvement are about 15-20%. Without index, improvement are about 40-50%.
The main objective is to cache subsets of queries. There is one catch though, caching and reusing queries are explicit. Otherwise, there is no existing mechanism to specify which subset of a query should be cached. It might be possible to enable reusing an existing query without touching the API but it looks rather difficult (it would require to compare constraints) and not necessarily exact.
An example on how this works
if (_cachedQuery == null) {
// creates a reusable query
_cachedQuery = db().query().cached();
// defines the subset cached
_cachedQuery.onCache().constrain(SensorObject.class);
_cachedQuery.onCache().descend("value").constrain(new Integer(10)).greater();
_cachedQuery.onCache().descend("value").constrain(new Integer(dataSize()-10)).smaller();
}
_cachedQuery.descend("type").constrain(new Integer(3));
Collection result = _cachedQuery.execute();
This will create a cached subset of SensorObject where value > 10 and < n-10. And this subset can be queried against other criterias. One point missing is the invalidation of the cache.
Erik.