Interface Query<T>

  • All Superinterfaces:
    Iterable<T>, QueryExecute<T>, QueryResultIterable<T>, SimpleQuery<T>
    All Known Subinterfaces:
    LoadType<T>
    All Known Implementing Classes:
    QueryImpl

    public interface Query<T>
    extends SimpleQuery<T>
    The basic options for a typed Query. In addition to adding a few methods that are only available for typed queries, this interface overrides the QueryCommon methods to return the full Query.
    Author:
    Jeff Schnitzer
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      Query<T> ancestor​(Object keyOrEntity)
      Restricts result set only to objects which have the given ancestor somewhere in the chain.
      Query<T> chunk​(int value)
      Sets the internal chunking and prefetching strategy within the low-level API.
      Query<T> chunkAll()
      Sets the internal chunking and prefetching strategy within the low-level API to attempt to get all results at once.
      Query<T> distinct​(boolean value)
      Determines whether this is a SELECT DISTINCT query.
      Query<T> endAt​(com.google.cloud.datastore.Cursor value)
      Ends query results at the specified Cursor.
      Query<T> filter​(com.google.cloud.datastore.StructuredQuery.Filter filter)
      Create a filter based on the raw low-level Filter.
      Query<T> filter​(String condition, Object value)
      Create a filter based on the specified condition and value, using the same syntax as the GAE/Python query class.
      Query<T> filterKey​(Object value)
      An alias for filterKey("=", value)
      Query<T> filterKey​(String condition, Object value)
      Create a filter on the key of an entity.
      Query<T> hybrid​(boolean force)
      This method forces Objectify to (or not to) hybridize the query into a keys-only fetch plus batch get.
      Query<T> limit​(int value)
      Limit the fetched result set to a certain number of values.
      Query<T> offset​(int value)
      Starts the query results at a particular zero-based offset.
      Query<T> order​(String condition)
      Sorts based on a property.
      Query<T> orderKey​(boolean descending)
      Shorthand for order("__key__") or order("-__key__")
      Query<T> project​(String... fields)
      Converts this query into a projection query.
      Query<T> startAt​(com.google.cloud.datastore.Cursor value)
      Starts query results at the specified Cursor.
    • Method Detail

      • filter

        Query<T> filter​(String condition,
                        Object value)

        Create a filter based on the specified condition and value, using the same syntax as the GAE/Python query class. Examples:

        • filter("age >=", age)
        • filter("age =", age)
        • filter("age", age) (if no operator, = is assumed)
        • filter("age !=", age)

        The space between the property name and the operator is required. Filtering a condition of "age>=" will perform an equality test on an entity property exactly named "age>=". You can't create properties like this with Objectify, but you can with the Low-Level API.

        Multiple calls to filter() will produce an AND (intersection) query.

        == is an alias of =, <> is an alias of !=.

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

        You can not filter on @Id or @Parent properties. Use filterKey() or ancestor() instead.

      • filter

        Query<T> filter​(com.google.cloud.datastore.StructuredQuery.Filter filter)

        Create a filter based on the raw low-level Filter. This is a very low-level operation; the values in the Filter are not translated in any way. For example, this only understands native datastore Key objects and not Objectify Key<?> objects.

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

        You can not filter on @Id or @Parent properties. Use filterKey() or ancestor() instead.

      • filterKey

        Query<T> filterKey​(String condition,
                           Object value)
        Description copied from interface: SimpleQuery

        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 Key, 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.

        Specified by:
        filterKey in interface SimpleQuery<T>
        Returns:
        a new immutable query object that applies the filter
      • filterKey

        Query<T> filterKey​(Object value)
        Description copied from interface: SimpleQuery
        An alias for filterKey("=", value)

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

        Specified by:
        filterKey in interface SimpleQuery<T>
        Returns:
        a new immutable query object that applies the filter
      • order

        Query<T> order​(String condition)

        Sorts based on a property. Examples:

        • order("age")
        • order("-age") (descending sort)

        You can not sort on @Id or @Parent properties. Sort by __key__ or -__key__ instead.

      • orderKey

        Query<T> orderKey​(boolean descending)
        Shorthand for order("__key__") or order("-__key__")
        Specified by:
        orderKey in interface SimpleQuery<T>
        Parameters:
        descending - if true, specifies a descending (aka reverse) sort
        Returns:
        a new immutable query object that applies the sort order
      • ancestor

        Query<T> ancestor​(Object keyOrEntity)
        Description copied from interface: SimpleQuery
        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.

        Specified by:
        ancestor in interface SimpleQuery<T>
        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

        Query<T> limit​(int value)
        Description copied from interface: SimpleQuery
        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.

        Specified by:
        limit in interface SimpleQuery<T>
        Parameters:
        value - must be >= 0. A value of 0 indicates no limit.
        Returns:
        a new immutable query object that applies the limit
      • offset

        Query<T> offset​(int value)
        Description copied from interface: SimpleQuery
        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.

        Specified by:
        offset in interface SimpleQuery<T>
        Parameters:
        value - must be >= 0
        Returns:
        a new immutable query object that applies the offset
      • startAt

        Query<T> startAt​(com.google.cloud.datastore.Cursor value)
        Description copied from interface: SimpleQuery
        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.
        Specified by:
        startAt in interface SimpleQuery<T>
        Returns:
        a new immutable query object that applies the cursor
      • endAt

        Query<T> endAt​(com.google.cloud.datastore.Cursor value)
        Description copied from interface: SimpleQuery
        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.
        Specified by:
        endAt in interface SimpleQuery<T>
        Returns:
        a new immutable query object that applies the cursor
      • chunk

        Query<T> chunk​(int value)
        Description copied from interface: SimpleQuery
        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.

        Specified by:
        chunk in interface SimpleQuery<T>
        Parameters:
        value - must be >= 0
        Returns:
        a new immutable query object that applies the chunk size
      • chunkAll

        Query<T> chunkAll()
        Description copied from interface: SimpleQuery

        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).

        Specified by:
        chunkAll in interface SimpleQuery<T>
        Returns:
        a new immutable query object that applies the chunk size
      • hybrid

        Query<T> hybrid​(boolean force)
        Description copied from interface: SimpleQuery

        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 (keys-only results are 1/7th the price of a full fetch) 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.
        • Hybrid queries have a slightly different consistency model than normal queries. You may wish to force hybridization off to ensure a weakly consistent query (see below).

        For the most part, hybridization only affects the performance and cost of queries. However, it is possible for hybridization to alter the content of the result set. In the HRD, queries always have EVENTUAL consistency but get operations may have either STRONG or EVENTUAL consistency depending on the read policy (see Objectify.consistency(). The keys-only query results will always be EVENTUAL and may return data that has been deleted, but Objectify will exclude these results when the more-current batch get fails to return data. So the count of the returned data may actually be less than the limit(), even if there are plenty of actual results. hybrid(false) will prevent this behavior.

        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.

        Specified by:
        hybrid in interface SimpleQuery<T>
        Returns:
        a new immutable query object that forces hybridization on or off
      • project

        Query<T> project​(String... fields)
        Description copied from interface: SimpleQuery

        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.

        Specified by:
        project in interface SimpleQuery<T>
        Parameters:
        fields - is one or more field names
        Returns:
        a new immutable query object that projects the specified fields
      • distinct

        Query<T> distinct​(boolean value)
        Description copied from interface: SimpleQuery
        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.

        Specified by:
        distinct in interface SimpleQuery<T>
        Returns:
        a new immutable query object that applies the distinct operator