Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objectify paging with Cursors

I have this method in my RPC service:

@Override
public Entrata[] getEntrate(int from, int to) {
    List<Entrata> data = entrateDao.list();
    return data.toArray(new Entrata[0]);
}

As you can see, I am not using the two parameters, which, in a SQL world, I would use as LIMIT and OFFSET.

It's not completely clear what I have to do now, I started reading this: http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Cursors

I think I have to do a query.startCursor(<my_"from"_parameter>)

Then iterate for "TO" times, the page size.

All right? Can you help me with some snippets? :)

like image 899
Fabio B. Avatar asked Aug 11 '11 13:08

Fabio B.


1 Answers

From docs: Cursors let you take a "checkpoint" in a query result set, store the checkpoint elsewhere, and then resume from where you left off late

As you need just limit/offset, you have to use limit() and offset() method of Objectify Query. Like:

ob.query(Entrata.class).limit(to - from).offset(from)

Or, when you have cursor:

String cursor = // get it from request
Query<Entrata> query = ob.query(Entrata.class);
Query q = query.startCursor(Cursor.fromWebSafeString(cursor));
q.limit(x);
QueryResultIterator<Entrate> iterator = query.iterator()
List<Entrate> data = // fetch data
String newCursor = iterrator.getStartCursor().toWebSafeString()
return new EntrataListWithCursor(data, cursor);
like image 64
Igor Artamonov Avatar answered Oct 13 '22 15:10

Igor Artamonov