Package com.google.appengine.api.datastore

Examples of com.google.appengine.api.datastore.FetchOptions


    @Test
    public void testStartEndCursor() {
        int limit = total / testDat.length;
        Query query = new Query(kindName, rootKey);
        query.addSort("name", Query.SortDirection.ASCENDING);
        FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit);
        // fetch 1st page and get cursor1
        QueryResultList<Entity> nextBatch = service.prepare(query)
            .asQueryResultList(fetchOption);
        Cursor cursor1 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString());
        // fetch 2nd page and get cursor2
        nextBatch = service.prepare(query).asQueryResultList(fetchOption.startCursor(cursor1));
        Cursor cursor2 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString());
        // cursor1 as start and cursor2 as end and 15 in limit -- -- should return 2nd page.
        checkPage(query, cursor1, cursor2, limit, limit, testDat[1], testDat[1]);
        // cursor1 as start and cursor2 as end and 30 in limit -- should return 2nd page.
        checkPage(query, cursor1, cursor2, 2 * limit, limit, testDat[1], testDat[1]);
View Full Code Here


        Assert.assertEquals(e1, iter.next());
    }

    private Cursor checkPage(Query query, Cursor stCursor, Cursor endCursor, int limit, int exptRet,
                             String chkSt, String chkEnd) {
        FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit);
        if (stCursor != null) {
            fetchOption = fetchOption.startCursor(stCursor);
        }
        if (endCursor != null) {
            fetchOption = fetchOption.endCursor(endCursor);
        }
        QueryResultList<Entity> nextBatch = service.prepare(query)
            .asQueryResultList(fetchOption);
        assertEquals(exptRet, nextBatch.size());
        if (chkSt != null) {
View Full Code Here

            assertTrue(e.getProperties().containsKey(property));
        }
    }

    private void checkQueryWithLimit(int limit) {
        FetchOptions fo = FetchOptions.Builder.withLimit(limit);
        Query query = new Query(kindName, rootKey);
        query.addProjection(new PropertyProjection("stringData", String.class));
        List<Entity> results = service.prepare(query).asList(fo);
        assertEquals(limit, results.size());
    }
View Full Code Here

        };
    }


    protected FetchOptions getFetchOptions( int offset, int count ) {
        FetchOptions fo = withOffset( offset );

        if (isLimit( count )) {
            fo = fo.limit( count );
        }

        return fo;
    }
View Full Code Here

    public static Iterable<Rule> fetch(Query q, int limit) {
        if (limit < 0) {
            limit = MAX_RULES;
        }

        FetchOptions fo = FetchOptions.Builder.withLimit(limit);
        return DatastoreUtil.query(q, fo, MAPPER);
    }
View Full Code Here

    public static Iterable<Key> fetchKeys(Query q, int limit) {
        if (limit < 0) {
            limit = MAX_RULES;
        }

        FetchOptions fo = FetchOptions.Builder.withLimit(limit);
        return DatastoreUtil.queryKeys(q, fo, MAPPER);
    }
View Full Code Here

      offset = opts.getOffset();
      limit = opts.getLimit();
    }
    // We'll need to apply the offset and the limit in-memory but we can at least
    // make use of the chunk and prefetch size.
    FetchOptions optsWithoutOffsetAndLimit =
        getFetchOptionsWithoutOffsetAndLimit(opts, chunkSize, prefetchSize);
    String keyProperty = qd.primaryDatastoreQuery.getSortPredicates().get(0).getPropertyName();
    Iterable<Entity> primaryResult;
    Iterator<Entity> joinResult;
    if (optsWithoutOffsetAndLimit == null) {
View Full Code Here

                                       mergeJoin(keyProperty, primaryResult, joinResult));
  }

  private FetchOptions getFetchOptionsWithoutOffsetAndLimit(
      FetchOptions opts, Integer chunkSize, Integer prefetchSize) {
    FetchOptions optsWithoutOffsetAndLimit = null;
    if (chunkSize != null) {
      optsWithoutOffsetAndLimit = FetchOptions.Builder.withChunkSize(opts.getChunkSize());
      if (prefetchSize != null) {
        optsWithoutOffsetAndLimit.prefetchSize(opts.getPrefetchSize());
      }
    } else if (prefetchSize != null) {
      optsWithoutOffsetAndLimit = FetchOptions.Builder.withPrefetchSize(prefetchSize);
    }
    return optsWithoutOffsetAndLimit;
View Full Code Here

          qd.primaryDatastoreQuery.getSortPredicates().isEmpty()) {
        // only execute a batch get if there aren't any other
        // filters or sorts
        return fulfillBatchGetQuery(ds, qd, mconn);
      } else if (qd.joinQuery != null) {
        FetchOptions opts = buildFetchOptions(fromInclNo, toExclNo);
        JoinHelper joinHelper = new JoinHelper();
        return wrapEntityQueryResult(
            joinHelper.executeJoinQuery(qd, this, ds, opts),
            qd.resultTransformer, ds, mconn, null);
      } else {
        latestDatastoreQuery = qd.primaryDatastoreQuery;
        Transaction txn = null;
        // give users a chance to opt-out of having their query execute in a txn
        if (extensions == null ||
            !extensions.containsKey(DatastoreManager.EXCLUDE_QUERY_FROM_TXN) ||
            !(Boolean)extensions.get(DatastoreManager.EXCLUDE_QUERY_FROM_TXN)) {
          // If this is an ancestor query, execute it in the current transaction
          txn = qd.primaryDatastoreQuery.getAncestor() != null ? ds.getCurrentTransaction(null) : null;
        }
        PreparedQuery preparedQuery = ds.prepare(txn, qd.primaryDatastoreQuery);
        FetchOptions opts = buildFetchOptions(fromInclNo, toExclNo);
        if (qd.resultType == ResultType.COUNT) {
          return fulfillCountQuery(preparedQuery, opts);
        } else {
          if (qd.resultType == ResultType.KEYS_ONLY || isBulkDelete()) {
            qd.primaryDatastoreQuery.setKeysOnly();
View Full Code Here

   * Build a FetchOptions instance using the provided params.
   * @return A FetchOptions instance built using the provided params,
   * or {@code null} if neither param is set.
   */
  FetchOptions buildFetchOptions(long fromInclNo, long toExclNo) {
    FetchOptions opts = null;
    Integer offset = null;
    if (fromInclNo != 0 && rangeValueIsSet(fromInclNo)) {
      // datastore api expects an int because we cap you at 1000 anyway.
      offset = (int) Math.min(Integer.MAX_VALUE, fromInclNo);
      opts = withOffset(offset);
    }
    if (rangeValueIsSet(toExclNo)) {
      // datastore api expects an int because we cap you at 1000 anyway.
      int intExclNo = (int) Math.min(Integer.MAX_VALUE, toExclNo);
      if (opts == null) {
        // When fromInclNo isn't specified, intExclNo (the index of the last
        // result to return) and limit are the same.
        opts = withLimit(intExclNo);
      } else {
        // When we have values for both fromInclNo and toExclNo
        // we can't take toExclNo as the limit for the query because
        // toExclNo is the index of the last result, not the max
        // results to return.  In this scenario the limit is the
        // index of the last result minus the offset.  For example, if
        // fromInclNo is 10 and toExclNo is 25, the limit for the query
        // is 15 because we want 15 results starting after the first 10.

        // We know that offset won't be null because opts is not null.
        opts.limit(intExclNo - offset);
      }
    }
    Cursor cursor = getCursor();
    // If we have a cursor, add it to the fetch options
    if (cursor != null) {
      if (opts == null) {
        opts = withCursor(cursor);
      } else {
        opts.cursor(cursor);
      }
    }
    return opts;
  }
View Full Code Here

TOP

Related Classes of com.google.appengine.api.datastore.FetchOptions

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.