Interface SimpleQuery<T>

All Superinterfaces:
Iterable<T>, QueryExecute<T>, QueryResultIterable<T>
All Known Subinterfaces:
Loader, LoadType<T>, Query<T>
All Known Implementing Classes:
QueryImpl, SimpleQueryImpl

public interface SimpleQuery<T> extends QueryExecute<T>
A restricted set of query operations that apply to both kindless queries and typed queries.
Author:
Jeff Schnitzer invalid input: '<'[email protected]>
  • Method Summary

    Modifier and Type
    Method
    Description
    com.google.cloud.datastore.AggregationResult
    aggregate(com.google.cloud.datastore.aggregation.Aggregation... aggregations)
    Run the specified aggregations given the query setup as currently defined.
    com.google.cloud.datastore.AggregationResult
    aggregate(com.google.cloud.datastore.aggregation.AggregationBuilder<?>... aggregations)
    Run the specified aggregations given the query setup as currently defined.
    ancestor(Object keyOrEntity)
    Restricts result set only to objects which have the given ancestor somewhere in the chain.
    default double
    avg(String property)
    Average the values of the specified property over the specified query.
    chunk(int value)
    Sets the internal chunking and prefetching strategy within the low-level API.
    Sets the internal chunking and prefetching strategy within the low-level API to attempt to get all results at once.
    default int
    Count the total number of values in the result.
    distinct(boolean value)
    Determines whether this is a SELECT DISTINCT query.
    endAt(com.google.cloud.datastore.Cursor value)
    Ends query results at the specified Cursor.
    An alias for filterKey("=", value)
    filterKey(String condition, Object value)
    Create a filter on the key of an entity.
    hybrid(boolean force)
    This method forces Objectify to (or not to) hybridize the query into a keys-only fetch plus batch get.
    Switches to a keys-only query.
    limit(int value)
    Limit the fetched result set to a certain number of values.
    offset(int value)
    Starts the query results at a particular zero-based offset.
    orderKey(boolean descending)
    Orders results by the key.
    project(String... fields)
    Converts this query into a projection query.
    startAt(com.google.cloud.datastore.Cursor value)
    Starts query results at the specified Cursor.
    default double
    sum(String property)
    Sum the values of the specified property over the specified query.
    default long
    sumLong(String property)
    Sum the values of the specified property over the specified query.
    Generates a string that consistently and uniquely specifies this query.

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator

    Methods inherited from interface com.googlecode.objectify.cmd.QueryExecute

    first, iterable, list, stream

    Methods inherited from interface com.googlecode.objectify.cmd.QueryResultIterable

    iterator
  • Method Details

    • filterKey

      SimpleQuery<T> filterKey(String condition, Object value)

      Create a filter on the key of an entity. Examples:

      • filterKey(">=", key) (standard inequalities)
      • filterKey("=", key) (wouldn't you rather do a load-by-key?)
      • filterKey("", key) (if no operator, = is assumed)
      • filterKey("!=", key)
      • filterKey("in", keyList) (wouldn't you rather do a batch load-by-key?)

      The key parameter can be anything key-ish; a Keyinvalid input: '<'?>, a native datastore key, a Ref, a pojo entity, etc.

      See the Google documentation for indexes for an explanation of what you can and cannot filter for.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Returns:
      a new immutable query object that applies the filter
    • filterKey

      SimpleQuery<T> filterKey(Object value)
      An alias for filterKey("=", value)

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Returns:
      a new immutable query object that applies the filter
    • orderKey

      SimpleQuery<T> orderKey(boolean descending)
      Orders results by the key.
      Parameters:
      descending - if true, specifies a descending (aka reverse) sort

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Returns:
      a new immutable query object that applies the sort order
    • ancestor

      SimpleQuery<T> ancestor(Object keyOrEntity)
      Restricts result set only to objects which have the given ancestor somewhere in the chain. Doesn't need to be the immediate parent. The specified ancestor itself will be included in the result set (if it exists).

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Parameters:
      keyOrEntity - can be a Key, a Key, or an Objectify entity object.
      Returns:
      a new immutable query object that applies the ancestor filter
    • limit

      SimpleQuery<T> limit(int value)
      Limit the fetched result set to a certain number of values.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Parameters:
      value - must be >= 0. A value of 0 indicates no limit.
      Returns:
      a new immutable query object that applies the limit
    • offset

      SimpleQuery<T> offset(int value)
      Starts the query results at a particular zero-based offset. This can be extraordinarily expensive because each skipped entity is billed as a "minor datastore operation". If you can, you probably want to use cursors instead.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Parameters:
      value - must be >= 0
      Returns:
      a new immutable query object that applies the offset
    • startAt

      SimpleQuery<T> startAt(com.google.cloud.datastore.Cursor value)
      Starts query results at the specified Cursor. You can obtain a Cursor from a QueryResultIterator by calling the getCursor() method.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Note that limit() and offset() are NOT encoded within a cursor; they operate on the results of the query after a cursor is established.
      Returns:
      a new immutable query object that applies the cursor
    • endAt

      SimpleQuery<T> endAt(com.google.cloud.datastore.Cursor value)
      Ends query results at the specified Cursor. You can obtain a Cursor from a QueryResultIterator by calling the getCursor() method.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Note that limit() and offset() are NOT encoded within a cursor; they operate on the results of the query after a cursor is established.
      Returns:
      a new immutable query object that applies the cursor
    • chunk

      SimpleQuery<T> chunk(int value)
      Sets the internal chunking and prefetching strategy within the low-level API. Affects performance only; the result set will be the same.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Parameters:
      value - must be >= 0
      Returns:
      a new immutable query object that applies the chunk size
    • chunkAll

      SimpleQuery<T> chunkAll()

      Sets the internal chunking and prefetching strategy within the low-level API to attempt to get all results at once. Affects performance only; the result set will be the same.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Same as chunk(Integer.MAX_VALUE).

      Returns:
      a new immutable query object that applies the chunk size
    • project

      SimpleQuery<T> project(String... fields)

      Converts this query into a projection query. Projection queries allow values to be selected directly out of an index rather than loading the whole entity. While this allows data to be fetched quickly and cheaply, it is limited to selecting data that exists in an index.

      Entities returned from projection queries are NOT kept in the session cache. However, @Load annotations are processed normally.

      This method can be called more than once; it will have the same effect as passing all the fields in to a single call.

      Parameters:
      fields - is one or more field names
      Returns:
      a new immutable query object that projects the specified fields
    • distinct

      SimpleQuery<T> distinct(boolean value)
      Determines whether this is a SELECT DISTINCT query.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Returns:
      a new immutable query object that applies the distinct operator
    • hybrid

      SimpleQuery<T> hybrid(boolean force)

      This method forces Objectify to (or not to) hybridize the query into a keys-only fetch plus batch get.

      If Objectify knows you are fetching an entity type that can be cached, it automatically converts queries into a "hybrid" of keys-only query followed by a batch fetch of the keys. This is cheaper, and if the cache hits, significantly faster. However, there are some circumstances in which you may wish to force behavior one way or another:

      • Issuing a kindless query (which Objectify will not auto-hybridize) when you know a significant portion of the result set is cacheable.
      • Some exotic queries cannot be made keys-only, and produce an exception from the Low-Level API when you try to execute the query. Objectify tries to detect these cases but since the underlying implementation may change, you may need to force hybridization off in some cases.

      Note that in hybrid queries, the chunk size defines the batch size.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Returns:
      a new immutable query object that forces hybridization on or off
    • keys

      QueryKeys<T> keys()
      Switches to a keys-only query. Keys-only responses are billed as "minor datastore operations" which are faster and free compared to fetching whole entities.

      All command objects are immutable; this method returns a new object instead of modifying the current command object.

      Returns:
      a new immutable query object that returns keys rather than whole entities
    • aggregate

      com.google.cloud.datastore.AggregationResult aggregate(com.google.cloud.datastore.aggregation.Aggregation... aggregations)

      Run the specified aggregations given the query setup as currently defined. limit and offset are obeyed.

      See Also:
    • aggregate

      com.google.cloud.datastore.AggregationResult aggregate(com.google.cloud.datastore.aggregation.AggregationBuilder<?>... aggregations)

      Run the specified aggregations given the query setup as currently defined. limit and offset are obeyed.

      See Also:
    • count

      default int count()

      Count the total number of values in the result.

      Shorthand for aggregate(Aggregation.count().as("count")).getLong("count").

      This method should return long, but to preserve backwards compatibility it returns int. This may change in the future.

      See Also:
    • sum

      default double sum(String property)

      Sum the values of the specified property over the specified query. Always produces a floating-point value.

      Shorthand for aggregate(Aggregation.sum(property).as("value")).getDouble("value").

      See Also:
    • sumLong

      default long sumLong(String property)

      Sum the values of the specified property over the specified query. Always produces an integer value. If the property values are floating point, the result will be cast to long.

      Shorthand for aggregate(Aggregation.sum(property).as("value")).getLong("value").

      See Also:
    • avg

      default double avg(String property)

      Average the values of the specified property over the specified query.

      Shorthand for aggregate(Aggregation.avg(property).as("value")).getDouble("value").

      See Also:
    • toString

      String toString()

      Generates a string that consistently and uniquely specifies this query. There is no way to convert this string back into a query and there is no guarantee that the string will be consistent across versions of Objectify.

      In particular, this value is useful as a key for a simple memcache query cache.

      Specified by:
      toString in interface QueryExecute<T>
      Overrides:
      toString in class Object