Package com.google.appengine.api.datastore

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


        encodedCursor = (String) cache.get(key);
      }

      if (encodedCursor != null) {
        // Got a cursor, use it
        Cursor cursor = Cursor.fromWebSafeString(encodedCursor);
        query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        query.setFirstResult(0);
      } else if (firstResult + maxResults < 1000) {
        // Results may be retrieved directly using "OFFSET"
        query.setHint(JPACursorHelper.CURSOR_HINT, null);
        query.setFirstResult(firstResult);
      } else {
        // Skip results
        int pos = (firstResult / 1000) * 1000;
        Cursor trialCursor = null;
        while (pos > 0) {
          String trialKey = createKey(employeeId, department, startsWith,
              orderBy, pos);
          String trialEncodedCursor = (String) cache.get(trialKey);
          if (trialEncodedCursor != null) {
            trialCursor = Cursor.fromWebSafeString(trialEncodedCursor);
            break;
          }
          pos -= 1000;
        }

        // If trialCursor is null, we'll start searching from result 0
        query.setHint(JPACursorHelper.CURSOR_HINT, trialCursor);
        while (firstResult > pos) {
          int min = Math.min(firstResult - pos, 1000);
         
          // If we need to skip more than 1000 records, ensure the
          // breaks occur at multiples of 1000 in order to increase the
          // chances of reusing cursors from the memcache
          if (pos + min < firstResult) {
            int mod = (pos + min) % 1000;
            min -= mod;
          }
         
          query.setMaxResults(min);
          List<ReportBack> results = query.getResultList();
          int count = results.size();
          if (count == 0) {
            break;
          }
          pos += count;
         
          // Save the cursor for later
          Cursor cursor = JPACursorHelper.getCursor(results);
          if (cache != null) {
            String key = createKey(employeeId, department, startsWith, orderBy,
                pos);
            cache.put(key, cursor.toWebSafeString());
          }
         
          query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }
      }
     
      query.setMaxResults(maxResults);

      List<ReportBack> reportList = query.getResultList();
      // force it to materialize
      reportList.size();
     
      Cursor cursor = JPACursorHelper.getCursor(reportList);
      if (cache != null) {
        int pos = firstResult + reportList.size();
        String key = createKey(employeeId, department, startsWith, orderBy, pos);
        cache.put(key, cursor.toWebSafeString());
      }
     
      return reportList;
    } finally {
      em.close();
View Full Code Here


        encodedCursor = (String) cache.get(key);
      }

      if (encodedCursor != null) {
        // Got a cursor, use it
        Cursor cursor = Cursor.fromWebSafeString(encodedCursor);
        query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        query.setFirstResult(0);
      } else if (firstResult + maxResults < 1000) {
        // Results may be retrieved directly using "OFFSET"
        query.setHint(JPACursorHelper.CURSOR_HINT, null);
        query.setFirstResult(firstResult);
      } else {
        // Skip results
        int pos = (firstResult / 1000) * 1000;
        Cursor trialCursor = null;
        while (pos > 0) {
          String trialKey = createKey(employeeId, department, startsWith,
              orderBy, pos);
          String trialEncodedCursor = (String) cache.get(trialKey);
          if (trialEncodedCursor != null) {
            trialCursor = Cursor.fromWebSafeString(trialEncodedCursor);
            break;
          }
          pos -= 1000;
        }

        // If trialCursor is null, we'll start searching from result 0
        query.setHint(JPACursorHelper.CURSOR_HINT, trialCursor);
        while (firstResult > pos) {
          int min = Math.min(firstResult - pos, 1000);

          // If we need to skip more than 1000 records, ensure the
          // breaks occur at multiples of 1000 in order to increase the
          // chances of reusing cursors from the memcache
          if (pos + min < firstResult) {
            int mod = (pos + min) % 1000;
            min -= mod;
          }

          query.setMaxResults(min);
          List<Report> results = query.getResultList();
          int count = results.size();
          if (count == 0) {
            break;
          }
          pos += count;

          // Save the cursor for later
          Cursor cursor = JPACursorHelper.getCursor(results);
          if (cache != null) {
            String key = createKey(employeeId, department, startsWith, orderBy,
                pos);
            cache.put(key, cursor.toWebSafeString());
          }

          query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }
      }

      query.setMaxResults(maxResults);

      List<Report> reportList = query.getResultList();
      // force it to materialize
      reportList.size();

      Cursor cursor = JPACursorHelper.getCursor(reportList);
      if (cache != null) {
        int pos = firstResult + reportList.size();
        String key = createKey(employeeId, department, startsWith, orderBy,
            pos);
        cache.put(key, cursor.toWebSafeString());
      }

      return reportList;
    } finally {
      em.close();
View Full Code Here

            ret.put(Pagination.PAGINATION, pagination);
            pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCnt);

            QueryResultList<Entity> queryResultList;
            if (1 != currentPageNum) {
                final Cursor startCursor = getStartCursor(currentPageNum, pageSize, preparedQuery);

                queryResultList = preparedQuery.asQueryResultList(withStartCursor(
                        startCursor).limit(pageSize).chunkSize(QUERY_CHUNK_SIZE));
            } else { // The first page
                queryResultList = preparedQuery.asQueryResultList(withLimit(pageSize).chunkSize(QUERY_CHUNK_SIZE));
View Full Code Here

     * @param preparedQuery the specified prepared query
     * @return the start cursor
     */
    private Cursor getStartCursor(final int currentPageNum, final int pageSize, final PreparedQuery preparedQuery) {
        int i = currentPageNum - 1;
        Cursor ret = null;
        for (; i > 0; i--) {
            final String cacheKey = CACHE_KEY_PREFIX + getName() + REPOSITORY_CACHE_QUERY_CURSOR + '(' + i + ')';
            ret = (Cursor) CACHE.get(cacheKey);
            if (null != ret) {
                LOGGER.log(Level.FINEST, "Found a query cursor[{0}] in repository cache[name={1}]",
View Full Code Here

    Query q = null;
    try {
      q = pm.newQuery(FeedInfo.class);
      q.setOrdering("dateRequested desc");
      if (cursorString != null) {
        Cursor cursor = Cursor.fromWebSafeString(cursorString);
        Map<String, Object> extensionMap = new HashMap<String, Object>();
        extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
        q.setExtensions(extensionMap);
      }

      q.setRange(0, batch);
      @SuppressWarnings("unchecked")
      List<FeedInfo> results = (List<FeedInfo>) q.execute();
      logger.fine("performed query");
      if (results.iterator().hasNext()) {

        batchcount++;
        Cursor cursor = JDOCursorHelper.getCursor(results);
        cursorString = cursor.toWebSafeString();
        // for efficiency, add next job to queue before processing this one
        Queue queue = QueueFactory.getQueue("frfeedupdates");
        queue.add(url("/feedupdate").param("cursor", cursorString).param("num",
          ""+batch).param("max", ""+max).param("bc", ""+batchcount));
         
View Full Code Here

      }
      else {
        q = pm.newQuery(StreamItem.class);
      }
      if (cursorString != null) {
        Cursor cursor = Cursor.fromWebSafeString(cursorString);
        Map<String, Object> extensionMap = new HashMap<String, Object>();
        extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
        q.setExtensions(extensionMap);
      }
      q.setRange(0, range);
      if (prior == null) {
        results = (List<StreamItem>) q.execute();
      }
      else {
        results = (List<StreamItem>) q.execute(prior);
      }
      if (results.iterator().hasNext()) {
        for (StreamItem streamItem : results) {
          pm.deletePersistent(streamItem);
        }
        Cursor cursor = JDOCursorHelper.getCursor(results);
        cursorString = cursor.toWebSafeString();
      }
      else {
        // no results
        cursorString = null;
      }
View Full Code Here

    PersistenceManager pm = PMF.get().getPersistenceManager();

      try {
        q = pm.newQuery(FriendDetails.class);
        if (cursorString != null) {
          Cursor cursor = Cursor.fromWebSafeString(cursorString);
          Map<String, Object> extensionMap = new HashMap<String, Object>();
          extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
          q.setExtensions(extensionMap);
        }
        q.setRange(0, range);
        @SuppressWarnings("unchecked")
        List<FriendDetails> results = (List<FriendDetails>) q.execute();
        if (results.size() > 0) {
          for (FriendDetails friendd : results) {
            // initialize the new field if necessary
            Set<String> urls = friendd.getUrls();
            if (urls == null || urls.isEmpty()) {
              if (urls == null) {
                friendd.setUrls(new HashSet<String>());
              }
              JDOHelper.makeDirty(friendd, "urls");
            }
          }
          Cursor cursor = JDOCursorHelper.getCursor(results);
          cursorString = cursor.toWebSafeString();
        }
        else {
          // no results
          cursorString = null;
        }
View Full Code Here

  }

  private Object fulfillEntityQuery(
      PreparedQuery preparedQuery, FetchOptions opts, Function<Entity, Object> resultTransformer,
      DatastoreService ds, ManagedConnection mconn) {
    Cursor endCursor = null;
    Iterable<Entity> entityIterable;
    if (opts != null) {
      if (opts.getLimit() != null) {
        QueryResultList<Entity> entities = preparedQuery.asQueryResultList(opts);
        endCursor = entities.getCursor();
View Full Code Here

        // 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 {
View Full Code Here

TOP

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

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.