@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String source = req.getParameter("source"); ImagesService imagesService = ImagesServiceFactory.getImagesService(); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); List<Filter> filters = new ArrayList<Filter>(); Query query = new Query(GAEFeedRepository.FEED_ITEM_KIND); filters.add(new Query.FilterPredicate("source", FilterOperator.EQUAL, source)); filters.add(new Query.FilterPredicate("img1A", FilterOperator.EQUAL, 1)); filters.add(new Query.FilterPredicate("img2A", FilterOperator.EQUAL, 1)); query.setFilter(CompositeFilterOperator.and(filters)); query.addSort("publishedDate", SortDirection.DESCENDING); PreparedQuery pq = datastore.prepare(query); int pageSize = 30; resp.setContentType("text/html"); resp.getWriter().println(" <ul>"); FetchOptions fetchOptions = FetchOptions.Builder.withLimit(pageSize); String startCursor = req.getParameter("cursor"); // If this servlet is passed a cursor parameter, let's use it if (startCursor != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor)); } QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions); for (Entity entity : results) { resp.getWriter() .println( "<li>" + entity.getProperty("imageLink") + " / " + imagesService.getServingUrl( ServingUrlOptions.Builder.withBlobKey((BlobKey) entity.getProperty("img2"))) + " / <img height=40 width=40 src=\"" + imagesService.getServingUrl( ServingUrlOptions.Builder.withBlobKey((BlobKey) entity.getProperty("img2"))) + "\" />"); } resp.getWriter().println("</li> </entity></ul> "); String cursor = results.getCursor().toWebSafeString(); // Assuming this servlet lives at '/people' resp.getWriter() .println( "<a href=\"/p8admin/ListFeedItems?cursor=" + cursor + "&source=" + source + "\">Next page</a>"); }
/** * This method lists all the entities inserted in datastore. It uses HTTP GET method and paging * support. * * @return A CollectionResponse class containing the list of all entities persisted and a cursor * to the next page. * @throws UnauthorizedException */ @ApiMethod( name = "findAll", scopes = {Config.EMAIL_SCOPE}, clientIds = {Config.CHROME_CLIENT_ID, Config.WEB_CLIENT_ID, Config.API_EXPLORER_CLIENT_ID}) public CollectionResponse<ServiceResponse> findAll( @Named("_name") String _name, @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit, User user) throws UnauthorizedException { if (user == null) { throw new UnauthorizedException("UnauthorizedException # User is Null."); } DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); if (limit == null) { limit = 10; } FetchOptions fetchOptions = FetchOptions.Builder.withLimit(limit); if (cursorString != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(cursorString)); } Query q = new Query(_name); q.addSort("_updatedAt", SortDirection.DESCENDING); PreparedQuery pq = datastore.prepare(q); QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions); List<ServiceResponse> responses = new ArrayList<ServiceResponse>(); ServiceResponse res = null; for (Entity entity : results) { res = new ServiceResponse(); // TODO add properties from entity to service response res.set_id(entity.getKey().getId()); res.set_createdAt((Date) entity.getProperty(_createdAt)); res.set_createdBy((String) entity.getProperty(_createdBy)); res.set_upatedAt((Date) entity.getProperty(_updatedAt)); res.set_updatedBy((String) entity.getProperty(_updatedBy)); res.set_status((String) entity.getProperty(_status)); Text dataText = (Text) entity.getProperty(data); res.setData(dataText.getValue()); responses.add(res); } cursorString = results.getCursor().toWebSafeString(); return CollectionResponse.<ServiceResponse>builder() .setItems(responses) .setNextPageToken(cursorString) .build(); }
public static void iterate( Query q, long maxElements, int batchSize, Cursor startCursor, Callback callback) { batchSize = (int) Math.min(Math.min(maxElements, batchSize), 1000); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); PreparedQuery pq = ds.prepare(q); QueryResultList<Entity> list = null; if (startCursor == null) { list = pq.asQueryResultList(withLimit(batchSize)); } else { list = pq.asQueryResultList(withLimit(batchSize).startCursor(startCursor)); } long totalElements = 0; Cursor cursor = list.getCursor(); while (totalElements < maxElements && list.size() > 0) { for (Entity entity : list) { totalElements++; try { callback.withEntity( entity, DatastoreServiceFactory.getDatastoreService(), list.getCursor(), totalElements); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); break; } if (totalElements >= maxElements) break; } if (totalElements >= maxElements) break; // wait before fetching new data try { Thread.sleep(WAIT); } catch (InterruptedException e) { System.err.println(e.getMessage()); } list = pq.asQueryResultList( withLimit((int) Math.min(batchSize, maxElements - totalElements)) .startCursor(cursor)); cursor = list.getCursor(); } }