Search in sources :

Example 11 with QueryOptionPage

use of siena.core.options.QueryOptionPage in project siena by mandubian.

the class GaePersistenceManagerAsync method doFetchIterable.

private <T> SienaFuture<Iterable<T>> doFetchIterable(QueryAsync<T> query, int limit, int offset) {
    QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext) query.option(QueryOptionGaeContext.ID);
    QueryOptionState state = (QueryOptionState) query.option(QueryOptionState.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType) query.option(QueryOptionFetchType.ID);
    if (gaeCtx == null) {
        gaeCtx = new QueryOptionGaeContext();
        query.customize(gaeCtx);
    }
    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 SienaFutureMock<Iterable<T>>(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 = off.offset;
            if (off.isActive()) {
                fetchOptions.offset(gaeCtx.realOffset);
                off.passivate();
            }
        }
        switch(fetchType.fetchType) {
            case ITER:
            default:
                {
                    // uses iterable as it is the only async request for prepared query for the time being
                    Iterable<Entity> entities = prepare(query).asIterable(fetchOptions);
                    return new GaeSienaFutureIterableMapper<T>(this, entities, query);
                }
        }
    } 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		
        if (!gaeCtx.isActive()) {
            // cursor not yet created
            switch(fetchType.fetchType) {
                case ITER:
                default:
                    {
                        PreparedQuery pq = prepare(query);
                        if (pag.isPaginating()) {
                            // in case of pagination, we need to allow asynchronous calls such as:
                            // QueryAsync<MyClass> query = pm.createQuery(MyClass).paginate(5).stateful().order("name");
                            // SienaFuture<Iterable<MyClass>> future1 = query.iter();
                            // SienaFuture<Iterable<MyClass>> future2 = query.nextPage().iter();
                            // Iterable<MyClass> it = future1.get().iterator();
                            // while(it.hasNext()) { // do it }
                            // it = future2.get().iterator();
                            // while(it.hasNext()) { // do it }
                            // so we can't use the asQueryResultIterable as the cursor is not moved to the end of the current page
                            // but moved at each call of iterable.iterator().next()
                            // thus we use the List in this case to be able to move directly to the next page with cursors
                            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(gaeCtx.useCursor){
                            Cursor cursor = entities.getCursor();
                            if (cursor != null) {
                                gaeCtx.addCursor(cursor.toWebSafeString());
                            }
                            return new GaeSienaFutureIterableMapper<T>(this, entities, query);
                        } else {
                            // if not paginating, we simply use the queryresultiterable and moves the current cursor
                            // while iterating
                            QueryResultIterable<Entity> entities = pq.asQueryResultIterable(fetchOptions);
                            // activates the GaeCtx now that it is initialised
                            gaeCtx.activate();
                            return new GaeSienaFutureIterableMapperWithCursor<T>(this, entities, query);
                        }
                    }
            }
        } else {
            switch(fetchType.fetchType) {
                case ITER:
                default:
                    {
                        PreparedQuery pq = prepare(query);
                        if (pag.isPaginating()) {
                            // in case of pagination, we need to allow asynchronous calls such as:
                            // QueryAsync<MyClass> query = pm.createQuery(MyClass).paginate(5).stateful().order("name");
                            // SienaFuture<Iterable<MyClass>> future1 = query.iter();
                            // SienaFuture<Iterable<MyClass>> future2 = query.nextPage().iter();
                            // Iterable<MyClass> it = future1.get().iterator();
                            // while(it.hasNext()) { // do it }
                            // it = future2.get().iterator();
                            // while(it.hasNext()) { // do it }
                            // so we can't use the asQueryResultIterable as the cursor is not moved to the end of the current page
                            // but moved at each call of iterable.iterator().next()
                            // thus we use the List in this case to be able to move directly to the next page with cursors
                            QueryResultList<Entity> entities;
                            if (!gaeCtx.useCursor) {
                                // then uses offset (in case of IN or != operators)
                                //if(offset.isActive()){
                                //	fetchOptions.offset(offset.offset);
                                //}
                                fetchOptions.offset(gaeCtx.realOffset);
                                entities = pq.asQueryResultList(fetchOptions);
                            } else {
                                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(gaeCtx.useCursor){
                                gaeCtx.addCursor(entities.getCursor().toWebSafeString());
                            //}
                            }
                            return new GaeSienaFutureIterableMapper<T>(this, entities, query);
                        } else {
                            // if not paginating, we simply use the queryresultiterable and moves the current cursor
                            // while iterating
                            QueryResultIterable<Entity> entities;
                            if (!gaeCtx.useCursor) {
                                // then uses offset (in case of IN or != operators)
                                //if(offset.isActive()){
                                //	fetchOptions.offset(offset.offset);
                                //}
                                fetchOptions.offset(gaeCtx.realOffset);
                                entities = pq.asQueryResultIterable(fetchOptions);
                            } else {
                                String cursor = gaeCtx.currentCursor();
                                if (cursor != null) {
                                    entities = pq.asQueryResultIterable(fetchOptions.startCursor(Cursor.fromWebSafeString(gaeCtx.currentCursor())));
                                } else {
                                    entities = pq.asQueryResultIterable(fetchOptions);
                                }
                            }
                            return new GaeSienaFutureIterableMapperWithCursor<T>(this, entities, query);
                        }
                    }
            }
        }
    }
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryResultIterable(com.google.appengine.api.datastore.QueryResultIterable) QueryResultList(com.google.appengine.api.datastore.QueryResultList) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) QueryOptionFetchType(siena.core.options.QueryOptionFetchType) Cursor(com.google.appengine.api.datastore.Cursor) SienaFutureMock(siena.core.async.SienaFutureMock) QueryResultIterable(com.google.appengine.api.datastore.QueryResultIterable) QueryOptionPage(siena.core.options.QueryOptionPage) QueryOptionState(siena.core.options.QueryOptionState)

Example 12 with QueryOptionPage

use of siena.core.options.QueryOptionPage in project siena by mandubian.

the class BaseQueryData method defaultOptions.

public static Map<Integer, QueryOption> defaultOptions() {
    return new HashMap<Integer, QueryOption>() {

        private static final long serialVersionUID = -7438657296637379900L;

        {
            put(QueryOptionPage.ID, new QueryOptionPage(0));
            put(QueryOptionOffset.ID, new QueryOptionOffset(0));
            put(QueryOptionState.ID, new QueryOptionState());
            //the fetch type is activated by default and set to NORMAL
            put(QueryOptionFetchType.ID, (new QueryOptionFetchType()).activate());
        }
    };
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryOptionPage(siena.core.options.QueryOptionPage) HashMap(java.util.HashMap) QueryOptionState(siena.core.options.QueryOptionState) QueryOptionFetchType(siena.core.options.QueryOptionFetchType)

Example 13 with QueryOptionPage

use of siena.core.options.QueryOptionPage in project siena by mandubian.

the class BaseQueryData method optionOffset.

protected void optionOffset(int offset) {
    QueryOptionPage pagOpt = (QueryOptionPage) (options.get(QueryOptionPage.ID));
    QueryOptionOffset offOpt = (QueryOptionOffset) options.get(QueryOptionOffset.ID);
    //QueryOptionState stateOpt = (QueryOptionState)(options.get(QueryOptionState.ID));
    offOpt.activate();
    offOpt.offsetType = QueryOptionOffset.OffsetType.MANUAL;
    offOpt.offset = offset;
    // deactivates the pagination in any case
    pagOpt.pageType = QueryOptionPage.PageType.MANUAL;
//if(offset!=0){
// if stateful mode, adds the offset to current offset
//if(stateOpt.isStateful()){
//	offOpt.offset += offset;
//}
// if stateless mode, simply replaces the offset
//	else {
//	offOpt.offset = offset;
//if(!pagOpt.isManual()){
//	pagOpt.pageSize = 0;
//}
//}
//}
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryOptionPage(siena.core.options.QueryOptionPage)

Example 14 with QueryOptionPage

use of siena.core.options.QueryOptionPage in project siena by mandubian.

the class BaseQueryData method optionPaginate.

protected void optionPaginate(int pageSize) {
    // sets the pagination
    QueryOptionPage opt = (QueryOptionPage) (options.get(QueryOptionPage.ID));
    QueryOptionOffset offOpt = (QueryOptionOffset) options.get(QueryOptionOffset.ID);
    //QueryOptionState stateOpt = (QueryOptionState)(options.get(QueryOptionState.ID)).activate();
    // can't change pagination after it has been initialized because it breaks all the cursor mechanism
    /*if(opt.isActive() && opt.isPaginating()){
			throw new SienaException("Can't change pagination after it has been initialized...");
		}*/
    opt.activate();
    opt.pageSize = pageSize;
    opt.pageType = QueryOptionPage.PageType.PAGINATING;
    // resets offset to be sure nothing changes the pagination mechanism
    offOpt.offsetType = QueryOptionOffset.OffsetType.PAGINATING;
//offOpt.offset = 0;
/*if(stateOpt.isStateful()){
			offOpt.passivate();
		}else {
			offOpt.activate();
		}*/
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryOptionPage(siena.core.options.QueryOptionPage)

Example 15 with QueryOptionPage

use of siena.core.options.QueryOptionPage in project siena by mandubian.

the class SdbMappingUtils method nextPage.

public static <T> void nextPage(QueryData<T> query) {
    QueryOptionPage pag = (QueryOptionPage) query.option(QueryOptionPage.ID);
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext) query.option(QueryOptionSdbContext.ID);
    QueryOptionOffset off = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
    if (sdbCtx == null) {
        sdbCtx = new QueryOptionSdbContext();
        query.options().put(sdbCtx.type, sdbCtx);
    }
    // if no more data after, doesn't try to go after
    if (sdbCtx.noMoreDataAfter) {
        return;
    }
    // if no more data before, removes flag to be able and stay there
    if (sdbCtx.noMoreDataBefore) {
        sdbCtx.noMoreDataBefore = false;
        return;
    }
    if (pag.isPaginating()) {
        if (sdbCtx.hasToken()) {
            if (sdbCtx.nextToken() == null) {
                // in this case, doesn't advance to the next page 
                // and stays at the offset of the beginning of the 
                // last page
                sdbCtx.noMoreDataAfter = true;
            } else {
                // follows the real offset and doesn't forget to add the off.offset
                if (off.isActive())
                    sdbCtx.realOffset += pag.pageSize + off.offset;
                else
                    sdbCtx.realOffset += pag.pageSize;
                // uses offset
                if (sdbCtx.currentTokenOffset() <= sdbCtx.realOffset) {
                    off.activate();
                    off.offset = sdbCtx.realOffset - sdbCtx.currentTokenOffset();
                } else // if currentokenoffset is greater than previous page realoffset
                // go to previous page again
                {
                    nextPage(query);
                }
            }
        } else {
            // no token yet, so uses the offset to go to next page
            QueryOptionOffset offset = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
            offset.activate();
            offset.offset += pag.pageSize;
            // follows the real offset
            sdbCtx.realOffset += pag.pageSize;
        }
    } else {
        // throws exception because it's impossible to reuse nextPage when paginating has been interrupted, the cases are too many
        throw new SienaException("Can't use nextPage after pagination has been interrupted...");
    }
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryOptionPage(siena.core.options.QueryOptionPage) SienaException(siena.SienaException)

Aggregations

QueryOptionPage (siena.core.options.QueryOptionPage)22 QueryOptionOffset (siena.core.options.QueryOptionOffset)18 QueryOptionState (siena.core.options.QueryOptionState)15 SienaException (siena.SienaException)9 ArrayList (java.util.ArrayList)5 QueryOptionFetchType (siena.core.options.QueryOptionFetchType)5 Cursor (com.google.appengine.api.datastore.Cursor)4 FetchOptions (com.google.appengine.api.datastore.FetchOptions)4 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)4 QueryResultList (com.google.appengine.api.datastore.QueryResultList)4 QueryResultIterable (com.google.appengine.api.datastore.QueryResultIterable)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 SienaFutureMock (siena.core.async.SienaFutureMock)2 HashMap (java.util.HashMap)1 List (java.util.List)1