Skip to content

Instantly share code, notes, and snippets.

@ashigeru
Created February 11, 2010 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashigeru/301053 to your computer and use it in GitHub Desktop.
Save ashigeru/301053 to your computer and use it in GitHub Desktop.
QueryResultIterator
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.QueryResultIterator;
@SuppressWarnings("serial")
public class CursorReviewServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
prepare();
String cursor = null;
while (true) {
cursor = exec(resp.getWriter(), cursor);
if (cursor == null) {
break;
}
}
}
private void prepare() {
List<Entity> entities = new ArrayList<Entity>();
for (int i = 0; i < 30; i++) {
Key key = KeyFactory.createKey("CursorReview", String.format("_%04d", i));
entities.add(new Entity(key));
}
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
ds.put(entities);
}
private String exec(PrintWriter writer, String paging) {
Query query = new Query("CursorReview");
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
// カーソルを復元
FetchOptions options = FetchOptions.Builder.withLimit(10);
if (paging != null) {
options = options.cursor(Cursor.fromWebSafeString(paging));
}
QueryResultIterator<Entity> iter =
ds.prepare(query).asQueryResultIterator(options);
writer.println("Serializable?:" + (iter instanceof Serializable));
// 先頭ひとつだけ
if (iter.hasNext() == false) {
writer.println("Result is empty");
return null;
}
else {
iter.hasNext(); // カーソルを進めるか?
writer.println("First: " + iter.next().getKey());
}
Cursor cursor = iter.getCursor();
if (cursor == null) {
writer.println("Cursor is null");
return null;
}
else {
String next = cursor.toWebSafeString();
writer.println("continue..");
return next;
}
}
}
Serializable?:false
First: CursorReview("_0000")
continue..
Serializable?:false
First: CursorReview("_0001")
continue..
Serializable?:false
First: CursorReview("_0002")
continue..
Serializable?:false
First: CursorReview("_0003")
continue..
Serializable?:false
First: CursorReview("_0004")
continue..
Serializable?:false
First: CursorReview("_0005")
continue..
Serializable?:false
First: CursorReview("_0006")
continue..
Serializable?:false
First: CursorReview("_0007")
continue..
Serializable?:false
First: CursorReview("_0008")
continue..
Serializable?:false
First: CursorReview("_0009")
continue..
Serializable?:false
First: CursorReview("_0010")
continue..
Serializable?:false
First: CursorReview("_0011")
continue..
Serializable?:false
First: CursorReview("_0012")
continue..
Serializable?:false
First: CursorReview("_0013")
continue..
Serializable?:false
First: CursorReview("_0014")
continue..
Serializable?:false
First: CursorReview("_0015")
continue..
Serializable?:false
First: CursorReview("_0016")
continue..
Serializable?:false
First: CursorReview("_0017")
continue..
Serializable?:false
First: CursorReview("_0018")
continue..
Serializable?:false
First: CursorReview("_0019")
continue..
Serializable?:false
First: CursorReview("_0020")
continue..
Serializable?:false
First: CursorReview("_0021")
continue..
Serializable?:false
First: CursorReview("_0022")
continue..
Serializable?:false
First: CursorReview("_0023")
continue..
Serializable?:false
First: CursorReview("_0024")
continue..
Serializable?:false
First: CursorReview("_0025")
continue..
Serializable?:false
First: CursorReview("_0026")
continue..
Serializable?:false
First: CursorReview("_0027")
continue..
Serializable?:false
First: CursorReview("_0028")
continue..
Serializable?:false
First: CursorReview("_0029")
continue..
Serializable?:false
Result is empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment