Created
February 11, 2010 00:45
-
-
Save ashigeru/301048 to your computer and use it in GitHub Desktop.
QueryResultList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.QueryResultList; | |
@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)); | |
} | |
QueryResultList<Entity> iter = | |
ds.prepare(query).asQueryResultList( | |
options); | |
writer.println("Serializable?:" + (iter instanceof Serializable)); | |
// 先頭ひとつだけ | |
if (iter.isEmpty()) { | |
writer.println("List is empty"); | |
return null; | |
} | |
else { | |
writer.println("First: " + iter.get(0).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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Serializable?:false | |
First: CursorReview("_0000") | |
continue.. | |
Serializable?:false | |
First: CursorReview("_0010") | |
continue.. | |
Serializable?:false | |
First: CursorReview("_0020") | |
continue.. | |
Serializable?:false | |
List is empty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment