/** Create a PreparedQuery that fetches keys only, relevant to our current state. */
  private PreparedQuery prepareKeysOnly() {
    // Can't modify the query, we might need to use it again
    com.google.appengine.api.datastore.Query cloned = this.cloneRawQuery(this.actual);
    cloned.setKeysOnly();

    return this.ofy.getDatastore().prepare(this.ofy.getTxn(), cloned);
  }
예제 #2
0
  /**
   * Returns the Account key associated with the specified authorization key.
   *
   * @param pm reference to the persistence manager
   * @param authorizationKey authorization key to return the account key for
   * @return the Account key associated with the specified authorization key; or <code>null</code>
   *     if the authorization key is invalid
   */
  public static Key getAccountKeyByAuthKey(
      final PersistenceManager pm, final String authorizationKey) {
    final String memcacheKey = CACHE_KEY_AUTH_KEY_ACCOUNT_KEY_PREFIX + authorizationKey;
    final String accountKeyString = (String) memcacheService.get(memcacheKey);
    if (accountKeyString != null) return KeyFactory.stringToKey(accountKeyString);

    final Query q = new Query(Account.class.getSimpleName());
    q.setFilter(new FilterPredicate("authorizationKey", FilterOperator.EQUAL, authorizationKey));
    q.setKeysOnly();
    final List<Entity> entityList =
        DatastoreServiceFactory.getDatastoreService()
            .prepare(q)
            .asList(FetchOptions.Builder.withDefaults());
    if (entityList.isEmpty()) return null;

    final Key accountKey = entityList.get(0).getKey();
    try {
      memcacheService.put(memcacheKey, KeyFactory.keyToString(accountKey));
    } catch (final MemcacheServiceException mse) {
      LOGGER.log(Level.WARNING, "Failed to put key to memcache: " + memcacheKey, mse);
      // Ignore memcache errors, do not prevent serving user request
    }

    return accountKey;
  }
예제 #3
0
 public List<Entry<String, Entity>> scan(
     String start, String end, int max, SortDirection direction, boolean keysOnly) {
   Preconditions.checkNotNull(start);
   Preconditions.checkNotNull(end);
   Preconditions.checkArgument(max > -1);
   if (max == 0) {
     return Lists.newArrayList();
   }
   Query query = new Query(kind);
   query.addFilter(
       "__key__", FilterOperator.GREATER_THAN_OR_EQUAL, KeyFactory.createKey(kind, escape(start)));
   query.addFilter("__key__", FilterOperator.LESS_THAN, KeyFactory.createKey(kind, escape(end)));
   query.addSort("__key__", direction);
   if (keysOnly) {
     query.setKeysOnly();
   }
   PreparedQuery preparedQuery = service.prepare(query);
   List<Entry<String, Entity>> result = Lists.newArrayList();
   for (Entity entity : preparedQuery.asIterable(FetchOptions.Builder.withLimit(max))) {
     if (keysOnly) {
       result.add(Maps.immutableEntry(unescape(entity.getKey().getName()), (Entity) null));
     } else {
       result.add(Maps.immutableEntry(unescape(entity.getKey().getName()), entity));
     }
   }
   return result;
 }
예제 #4
0
 @Override
 public Iterator<String> getAllEntityKeys(Class<?> entityClass, String lastKey) {
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   com.google.appengine.api.datastore.Query q =
       new com.google.appengine.api.datastore.Query(entityClass.getName());
   q.setKeysOnly();
   if (lastKey != null) {
     q.addFilter("name", FilterOperator.GREATER_THAN, lastKey);
   }
   PreparedQuery pq = datastore.prepare(q);
   FetchOptions fetchOptions = FetchOptions.Builder.withOffset(0);
   return new ExtractingKeyIterable(pq.asIterator(fetchOptions));
 }
  /**
   * Counts the rows of the specified table
   *
   * @param searchIn the kind of entity (table) to search in
   * @param parent search for all entities with this parent
   * @param filters all properties to search for
   * @return number of rows
   */
  public static int count(String searchIn, Key parent, FilterWrapper... filters) {
    Query query = new Query(searchIn);
    query.setKeysOnly();

    if (parent != null) {
      query.setAncestor(parent);
    }

    setFilterProperties(query, filters);

    PreparedQuery preparedQuery = mDatastore.prepare(query);
    return preparedQuery.countEntities(FetchOptions.Builder.withDefaults());
  }
  /** Make a new Query object that is exactly like the old. Too bad Query isn't Cloneable. */
  protected com.google.appengine.api.datastore.Query cloneRawQuery(
      com.google.appengine.api.datastore.Query orig) {
    com.google.appengine.api.datastore.Query copy =
        new com.google.appengine.api.datastore.Query(orig.getKind(), orig.getAncestor());

    for (FilterPredicate filter : orig.getFilterPredicates())
      copy.addFilter(filter.getPropertyName(), filter.getOperator(), filter.getValue());

    for (SortPredicate sort : orig.getSortPredicates())
      copy.addSort(sort.getPropertyName(), sort.getDirection());

    // This should be impossible but who knows what might happen in the future
    if (orig.isKeysOnly()) copy.setKeysOnly();

    return copy;
  }
  /**
   * Searches for an existing entity
   *
   * @param searchIn what kind of entity to search in
   * @param parent the parent of the entity to find, set to null to skip
   * @param onlyKeys will only retrieve keys for the found entity
   * @param filters property name and values to search for
   * @return found entity, null if none or more than 1 was found
   */
  private static Entity getSingleEntity(
      String searchIn, Key parent, boolean onlyKeys, FilterWrapper... filters) {
    Query query = new Query(searchIn);

    if (onlyKeys) {
      query.setKeysOnly();
    }

    setFilterProperties(query, filters);

    if (parent != null) {
      query.setAncestor(parent);
    }

    try {
      return mDatastore.prepare(query).asSingleEntity();
    } catch (TooManyResultsException e) {
      // Does nothing
    }
    return null;
  }
  /**
   * Get all entities (with only keys) with the specified parent and properties
   *
   * @param searchIn what kind of entity (table) to search in
   * @param parent search for all entities with this parent
   * @param filters all properties to search for
   * @return an array list of all found entities with the specified parent
   */
  public static List<Key> getKeys(String searchIn, Key parent, FilterWrapper... filters) {
    Query query = new Query(searchIn);

    query.setKeysOnly();

    // Parent
    if (parent != null) {
      query.setAncestor(parent);
    }

    // Search by properties
    setFilterProperties(query, filters);

    PreparedQuery preparedQuery = mDatastore.prepare(query);

    ArrayList<Key> keys = new ArrayList<>();
    for (Entity entity : preparedQuery.asIterable()) {
      keys.add(entity.getKey());
    }

    return keys;
  }