The code snippet:
Code:
public void testTotalCount(Cache cache) {
List<Person> personList = new ArrayList<Persion>();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Attribute<Date> create_time = cache.getSearchAttribute("create_time");
//get the first 1000 results
Query q = cache.createQuery();
q.maxResults(1000);
q.includeKeys();
q.includeValues();
Criteria c1 = create_time.between(df.parse("2012-09-01"),df.parse("2012-09-31"));
q.addCriteria(c1).addOrderBy(create_time, Direction.ASCENDING);
Results rs = q.execute();
for(Result r : rs.all()) {
personList.add((Person)r.getValue());
}
//get the total count
Query qC = cache.createQuery();
qC.addCriteria(c1);
qC.includeAggregator(Aggregators.count());
Results cRs = qC.execute();
int count = (Integer) cRs.all().get(0).getAggregatorResults().get(0);
}
So,I execute the query twice. I can't sure whether it is right or not?