Search in sources :

Example 1 with FetchOptions

use of com.google.appengine.api.datastore.FetchOptions in project java-docs-samples by GoogleCloudPlatform.

the class ListPeopleServletTest method getFirstCursor.

private String getFirstCursor() {
    Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
    PreparedQuery pq = datastore.prepare(q);
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(ListPeopleServlet.PAGE_SIZE);
    QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
    return results.getCursor().toWebSafeString();
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery)

Example 2 with FetchOptions

use of com.google.appengine.api.datastore.FetchOptions in project java-docs-samples by GoogleCloudPlatform.

the class ListPeopleServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE);
    // If this servlet is passed a cursor parameter, let's use it.
    String startCursor = req.getParameter("cursor");
    if (startCursor != null) {
        fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
    }
    Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
    PreparedQuery pq = datastore.prepare(q);
    QueryResultList<Entity> results;
    try {
        results = pq.asQueryResultList(fetchOptions);
    } catch (IllegalArgumentException e) {
        // IllegalArgumentException happens when an invalid cursor is used.
        // A user could have manually entered a bad cursor in the URL or there
        // may have been an internal implementation detail change in App Engine.
        // Redirect to the page without the cursor parameter to show something
        // rather than an error.
        resp.sendRedirect("/people");
        return;
    }
    resp.setContentType("text/html");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter w = resp.getWriter();
    w.println("<!DOCTYPE html>");
    w.println("<meta charset=\"utf-8\">");
    w.println("<title>Cloud Datastore Cursor Sample</title>");
    w.println("<ul>");
    for (Entity entity : results) {
        w.println("<li>" + entity.getProperty("name") + "</li>");
    }
    w.println("</ul>");
    String cursorString = results.getCursor().toWebSafeString();
    // This servlet lives at '/people'.
    w.println("<a href='/people?cursor=" + cursorString + "'>Next page</a>");
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) PrintWriter(java.io.PrintWriter)

Example 3 with FetchOptions

use of com.google.appengine.api.datastore.FetchOptions in project java-docs-samples by GoogleCloudPlatform.

the class ShortTest method testDisabledDatastore.

@Test(expected = ApiProxy.CapabilityDisabledException.class)
public void testDisabledDatastore() {
    Capability testOne = new Capability("datastore_v3");
    CapabilityStatus testStatus = CapabilityStatus.DISABLED;
    // Initialize the test configuration.
    LocalCapabilitiesServiceTestConfig config = new LocalCapabilitiesServiceTestConfig().setCapabilityStatus(testOne, testStatus);
    helper = new LocalServiceTestHelper(config);
    helper.setUp();
    FetchOptions fo = FetchOptions.Builder.withLimit(10);
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    assertEquals(0, ds.prepare(new Query("yam")).countEntities(fo));
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Capability(com.google.appengine.api.capabilities.Capability) Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService) LocalServiceTestHelper(com.google.appengine.tools.development.testing.LocalServiceTestHelper) CapabilityStatus(com.google.appengine.api.capabilities.CapabilityStatus) LocalCapabilitiesServiceTestConfig(com.google.appengine.tools.development.testing.LocalCapabilitiesServiceTestConfig) Test(org.junit.Test)

Example 4 with FetchOptions

use of com.google.appengine.api.datastore.FetchOptions in project Cached-Datastore by Emperorlou.

the class CachedDatastoreService method fetchKeys.

private List<Key> fetchKeys(Query q, int limit, Cursor startCursor) {
    q.setKeysOnly();
    prepareQuery(q);
    int chunkSize = limit;
    FetchOptions fo = FetchOptions.Builder.withLimit(limit).chunkSize(chunkSize);
    if (startCursor != null)
        fo = fo.startCursor(startCursor);
    List<Key> keys = new ArrayList<>();
    QueryResultList<Entity> entities = pq.asQueryResultList(fo);
    int count = entities.size();
    if (count > limit)
        count = limit;
    for (int i = 0; i < count; i++) {
        Entity e = entities.get(i);
        keys.add(e.getKey());
    }
    lastQuery_endCursor = entities.getCursor();
    if (statsTracking)
        incrementStat(QUERYKEYCACHE_QUERIES);
    return keys;
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key)

Example 5 with FetchOptions

use of com.google.appengine.api.datastore.FetchOptions in project siena by mandubian.

the class GaePersistenceManager method doFetchList.

private <T> List<T> doFetchList(Query<T> query, int limit, int offset) {
    QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext) query.option(QueryOptionGaeContext.ID);
    if (gaeCtx == null) {
        gaeCtx = new QueryOptionGaeContext();
        query.customize(gaeCtx);
    }
    QueryOptionState state = (QueryOptionState) query.option(QueryOptionState.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType) query.option(QueryOptionFetchType.ID);
    FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
    QueryOptionPage pag = (QueryOptionPage) query.option(QueryOptionPage.ID);
    if (!pag.isPaginating()) {
        // no pagination but pageOption active
        if (pag.isActive()) {
            // if local limit is set, it overrides the pageOption.pageSize
            if (limit != Integer.MAX_VALUE) {
                gaeCtx.realPageSize = limit;
                fetchOptions.limit(gaeCtx.realPageSize);
                // pageOption is passivated to be sure it is not reused
                pag.passivate();
            } else // using pageOption.pageSize
            {
                gaeCtx.realPageSize = pag.pageSize;
                fetchOptions.limit(gaeCtx.realPageSize);
                // passivates the pageOption in stateless mode not to keep anything between 2 requests
                if (state.isStateless()) {
                    pag.passivate();
                }
            }
        } else {
            if (limit != Integer.MAX_VALUE) {
                gaeCtx.realPageSize = limit;
                fetchOptions.limit(gaeCtx.realPageSize);
            }
        }
    } else {
        // paginating so use the pagesize and don't passivate pageOption
        // local limit is not taken into account
        gaeCtx.realPageSize = pag.pageSize;
        fetchOptions.limit(gaeCtx.realPageSize);
    }
    QueryOptionOffset off = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
    // if local offset has been set, uses it
    if (offset != 0) {
        off.activate();
        off.offset = offset;
    }
    // if previousPage has detected there is no more data, simply returns an empty list
    if (gaeCtx.noMoreDataBefore) {
        return new ArrayList<T>();
    }
    if (state.isStateless()) {
        if (pag.isPaginating()) {
            if (off.isActive()) {
                gaeCtx.realOffset += off.offset;
                fetchOptions.offset(gaeCtx.realOffset);
                off.passivate();
            } else {
                fetchOptions.offset(gaeCtx.realOffset);
            }
        } else {
            // if stateless and not paginating, resets the realoffset to 0
            gaeCtx.realOffset = 0;
            if (off.isActive()) {
                gaeCtx.realOffset = off.offset;
                fetchOptions.offset(gaeCtx.realOffset);
                off.passivate();
            }
        }
        switch(fetchType.fetchType) {
            case KEYS_ONLY:
                {
                    // uses iterable as it is the only async request for prepared query for the time being
                    List<Entity> entities = prepareKeysOnly(query).asList(fetchOptions);
                    // if paginating and 0 results then no more data else resets noMoreDataAfter
                    if (pag.isPaginating()) {
                        if (entities.size() == 0) {
                            gaeCtx.noMoreDataAfter = true;
                        } else {
                            gaeCtx.noMoreDataAfter = false;
                        }
                    }
                    return mapKeysOnly(query, entities);
                }
            case NORMAL:
            default:
                {
                    // uses iterable as it is the only async request for prepared query for the time being
                    List<Entity> entities = prepare(query).asList(fetchOptions);
                    // if paginating and 0 results then no more data else resets noMoreDataAfter
                    if (pag.isPaginating()) {
                        if (entities.size() == 0) {
                            gaeCtx.noMoreDataAfter = true;
                        } else {
                            gaeCtx.noMoreDataAfter = false;
                        }
                    }
                    return map(query, entities);
                }
        }
    } else {
        if (off.isActive()) {
            // by default, we add the offset but it can be added with the realoffset
            // in case of cursor desactivated
            fetchOptions.offset(off.offset);
            gaeCtx.realOffset += off.offset;
            off.passivate();
        }
        // manages cursor limitations for IN and != operators with offsets
        if (!gaeCtx.isActive()) {
            // cursor not yet created
            switch(fetchType.fetchType) {
                case KEYS_ONLY:
                    {
                        PreparedQuery pq = prepareKeysOnly(query);
                        if (!gaeCtx.useCursor) {
                            // then uses offset (in case of IN or != operators)
                            // if(offset.isActive()){
                            // fetchOptions.offset(gaeCtx.realOffset);
                            // }
                            fetchOptions.offset(gaeCtx.realOffset);
                        }
                        // we can't use real asynchronous function with cursors
                        // so the page is extracted at once and wrapped into a SienaFuture
                        QueryResultList<Entity> entities = pq.asQueryResultList(fetchOptions);
                        // activates the GaeCtx now that it is initialised
                        gaeCtx.activate();
                        // sets the current cursor (in stateful mode, cursor is always kept for further use)
                        if (pag.isPaginating()) {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addCursor(cursor.toWebSafeString());
                            }
                            // if paginating and 0 results then no more data else resets noMoreDataAfter
                            if (entities.size() == 0) {
                                gaeCtx.noMoreDataAfter = true;
                            } else {
                                gaeCtx.noMoreDataAfter = false;
                            }
                        } else {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addAndMoveCursor(entities.getCursor().toWebSafeString());
                            }
                            // keeps track of the offset anyway if not paginating
                            gaeCtx.realOffset += entities.size();
                        }
                        return mapKeysOnly(query, entities);
                    }
                case NORMAL:
                default:
                    {
                        PreparedQuery pq = prepare(query);
                        if (!gaeCtx.useCursor) {
                            // then uses offset (in case of IN or != operators)
                            // if(offset.isActive()){
                            // fetchOptions.offset(gaeCtx.realOffset);
                            // }
                            fetchOptions.offset(gaeCtx.realOffset);
                        }
                        // we can't use real asynchronous function with cursors
                        // so the page is extracted at once and wrapped into a SienaFuture
                        QueryResultList<Entity> entities = pq.asQueryResultList(fetchOptions);
                        // activates the GaeCtx now that it is initialised
                        gaeCtx.activate();
                        // sets the current cursor (in stateful mode, cursor is always kept for further use)
                        if (pag.isPaginating()) {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addCursor(cursor.toWebSafeString());
                            }
                            // if paginating and 0 results then no more data else resets noMoreDataAfter
                            if (entities.size() == 0) {
                                gaeCtx.noMoreDataAfter = true;
                            } else {
                                gaeCtx.noMoreDataAfter = false;
                            }
                        } else {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addAndMoveCursor(entities.getCursor().toWebSafeString());
                            }
                            // keeps track of the offset anyway if not paginating
                            gaeCtx.realOffset += entities.size();
                        }
                        return map(query, entities);
                    }
            }
        } else {
            switch(fetchType.fetchType) {
                case KEYS_ONLY:
                    {
                        // we prepare the query each time
                        PreparedQuery pq = prepareKeysOnly(query);
                        QueryResultList<Entity> entities;
                        if (!gaeCtx.useCursor) {
                            // then uses offset (in case of IN or != operators)
                            // if(offset.isActive()){
                            // fetchOptions.offset(gaeCtx.realOffset);
                            // }
                            fetchOptions.offset(gaeCtx.realOffset);
                            // we can't use real asynchronous function with cursors
                            // so the page is extracted at once and wrapped into a SienaFuture
                            entities = pq.asQueryResultList(fetchOptions);
                        } else {
                            // we can't use real asynchronous function with cursors
                            // so the page is extracted at once and wrapped into a SienaFuture
                            String cursor = gaeCtx.currentCursor();
                            if (cursor != null) {
                                entities = pq.asQueryResultList(fetchOptions.startCursor(Cursor.fromWebSafeString(cursor)));
                            } else {
                                entities = pq.asQueryResultList(fetchOptions);
                            }
                        }
                        // sets the current cursor (in stateful mode, cursor is always kept for further use)
                        if (pag.isPaginating()) {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addCursor(cursor.toWebSafeString());
                            }
                            // if paginating and 0 results then no more data else resets noMoreDataAfter
                            if (entities.size() == 0) {
                                gaeCtx.noMoreDataAfter = true;
                            } else {
                                gaeCtx.noMoreDataAfter = false;
                            }
                        } else {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addAndMoveCursor(entities.getCursor().toWebSafeString());
                            }
                            // keeps track of the offset anyway if not paginating
                            gaeCtx.realOffset += entities.size();
                        }
                        return mapKeysOnly(query, entities);
                    }
                case NORMAL:
                default:
                    {
                        PreparedQuery pq = prepare(query);
                        QueryResultList<Entity> entities;
                        if (!gaeCtx.useCursor) {
                            // then uses offset (in case of IN or != operators)
                            // if(offset.isActive()){
                            // fetchOptions.offset(gaeCtx.realOffset);
                            // }
                            fetchOptions.offset(gaeCtx.realOffset);
                            // we can't use real asynchronous function with cursors
                            // so the page is extracted at once and wrapped into a SienaFuture
                            entities = pq.asQueryResultList(fetchOptions);
                        } else {
                            // we can't use real asynchronous function with cursors
                            // so the page is extracted at once and wrapped into a SienaFuture
                            String cursor = gaeCtx.currentCursor();
                            if (cursor != null) {
                                entities = pq.asQueryResultList(fetchOptions.startCursor(Cursor.fromWebSafeString(gaeCtx.currentCursor())));
                            } else {
                                entities = pq.asQueryResultList(fetchOptions);
                            }
                        }
                        // sets the current cursor (in stateful mode, cursor is always kept for further use)
                        if (pag.isPaginating()) {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addCursor(cursor.toWebSafeString());
                            }
                            // if paginating and 0 results then no more data else resets noMoreDataAfter
                            if (entities.size() == 0) {
                                gaeCtx.noMoreDataAfter = true;
                            } else {
                                gaeCtx.noMoreDataAfter = false;
                            }
                        } else {
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addAndMoveCursor(entities.getCursor().toWebSafeString());
                            }
                            // keeps track of the offset anyway
                            gaeCtx.realOffset += entities.size();
                        }
                        return map(query, entities);
                    }
            }
        }
    }
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryOptionPage(siena.core.options.QueryOptionPage) QueryResultList(com.google.appengine.api.datastore.QueryResultList) QueryOptionState(siena.core.options.QueryOptionState) ArrayList(java.util.ArrayList) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) QueryOptionFetchType(siena.core.options.QueryOptionFetchType) Cursor(com.google.appengine.api.datastore.Cursor)

Aggregations

FetchOptions (com.google.appengine.api.datastore.FetchOptions)11 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)8 Cursor (com.google.appengine.api.datastore.Cursor)6 Entity (com.google.appengine.api.datastore.Entity)6 Query (com.google.appengine.api.datastore.Query)5 QueryResultList (com.google.appengine.api.datastore.QueryResultList)4 ArrayList (java.util.ArrayList)4 QueryOptionFetchType (siena.core.options.QueryOptionFetchType)4 QueryOptionOffset (siena.core.options.QueryOptionOffset)4 QueryOptionPage (siena.core.options.QueryOptionPage)4 QueryOptionState (siena.core.options.QueryOptionState)4 QueryResultIterable (com.google.appengine.api.datastore.QueryResultIterable)3 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2 Key (com.google.appengine.api.datastore.Key)2 SienaFutureMock (siena.core.async.SienaFutureMock)2 Capability (com.google.appengine.api.capabilities.Capability)1 CapabilityStatus (com.google.appengine.api.capabilities.CapabilityStatus)1 DatastoreService (com.google.appengine.api.datastore.DatastoreService)1 LocalCapabilitiesServiceTestConfig (com.google.appengine.tools.development.testing.LocalCapabilitiesServiceTestConfig)1