com.googlecode.objectify
Interface Query<T>

All Superinterfaces:
java.lang.Iterable<T>, com.google.appengine.api.datastore.QueryResultIterable<T>
All Known Implementing Classes:
QueryImpl, QueryWrapper, SessionCachingQueryImpl

public interface Query<T>
extends com.google.appengine.api.datastore.QueryResultIterable<T>

This is similar to the datastore Query object, but better understands real class objects - it allows you to filter and sort by the key field normally.

The methods of this class follow the GAE/Python Query class rather than the GAE/Java Query class because the Python version is much more convenient to use. The Java version seems to have been designed for machines, not humans. You will appreciate the improvement.

Construct this class by calling Objectify.query()

Note that this class is Iterable; to get results, call iterator().

To obtain a Cursor call Query.iterator().getCursor(). This cursor can be resumed with Query.cursor().

Author:
Jeff Schnitzer

Method Summary
 Query<T> ancestor(java.lang.Object keyOrEntity)
          Restricts result set only to objects which have the given ancestor somewhere in the chain.
 Query<T> chunkSize(int value)
          Sets the internal chunking strategy within the low-level API.
 Query<T> clone()
           
 int count()
          Count the total number of values in the result.
 Query<T> endCursor(com.google.appengine.api.datastore.Cursor value)
          Ends query results at the specified Cursor.
 com.google.appengine.api.datastore.QueryResultIterable<T> fetch()
          Starts an asynchronous query.
 com.google.appengine.api.datastore.QueryResultIterable<Key<T>> fetchKeys()
          Prepares an Iterable that will obtain the keys of the results.
<V> java.util.Set<Key<V>>
fetchParentKeys()
          Execute a keys-only query and then extract parent keys, returning them as a Set.
<V> java.util.Map<Key<V>,V>
fetchParents()
          Gets the parent keys and then fetches the actual entities.
 Query<T> filter(java.lang.String condition, java.lang.Object value)
          Create a filter based on the specified condition and value, using the same syntax as the GAE/Python query class.
 T get()
          Gets the first entity in the result set.
 Key<T> getKey()
          Get the key of the first entity in the result set.
 Query<T> limit(int value)
          Limit the fetched result set to a certain number of values.
 java.util.List<T> list()
          Execute the query and get the results as a List.
 java.util.List<Key<T>> listKeys()
          Execute a keys-only query and get the results as a List.
 Query<T> offset(int value)
          Starts the query results at a particular zero-based offset.
 Query<T> order(java.lang.String condition)
          Sorts based on a property.
 Query<T> prefetchSize(int value)
          Sets the number of results retreived on the first call to the datastore.
 Query<T> startCursor(com.google.appengine.api.datastore.Cursor value)
          Starts query results at the specified Cursor.
 java.lang.String toString()
          Generates a string that consistently and uniquely specifies this query.
 
Methods inherited from interface com.google.appengine.api.datastore.QueryResultIterable
iterator
 

Method Detail

filter

Query<T> filter(java.lang.String condition,
                java.lang.Object value)

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

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

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

In addition to filtering on indexed properties, you can filter on @Id properties if this query is restricted to a Class and the entity has no @Parent. If you are having trouble working around this limitation, please consult the objectify-appengine google group.

You can not filter on @Parent properties. Use the ancestor() method instead.


order

Query<T> order(java.lang.String condition)

Sorts based on a property. Examples:

You can sort on id properties if this query is restricted to a Class. Note that this is only important for descending sorting; default iteration is key-ascending.

You can not sort on @Parent properties.


ancestor

Query<T> ancestor(java.lang.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.

Parameters:
keyOrEntity - can be a Key, a Key, or an Objectify entity object.

limit

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

Parameters:
value - must be >= 0. A value of 0 indicates no limit.

offset

Query<T> offset(int value)
Starts the query results at a particular zero-based offset.

Parameters:
value - must be >= 0

startCursor

Query<T> startCursor(com.google.appengine.api.datastore.Cursor value)
Starts query results at the specified Cursor. You can obtain a Cursor from a QueryResultIterator by calling the getCursor() method. Note that limit() and offset() are NOT encoded within a cursor; they operate on the results of the query after a cursor is established.


endCursor

Query<T> endCursor(com.google.appengine.api.datastore.Cursor value)
Ends query results at the specified Cursor. You can obtain a Cursor from a QueryResultIterator by calling the getCursor() method. Note that limit() and offset() are NOT encoded within a cursor; they operate on the results of the query after a cursor is established.


chunkSize

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

Parameters:
value - must be > 0

prefetchSize

Query<T> prefetchSize(int value)
Sets the number of results retreived on the first call to the datastore. Affects performance only; the result set will be the same.

Parameters:
value - must be >= 0

toString

java.lang.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.

Overrides:
toString in class java.lang.Object

get

T get()
Gets the first entity in the result set. Obeys the offset value.

Returns:
the only instance in the result, or null if the result set is empty.

getKey

Key<T> getKey()
Get the key of the first entity in the result set. Obeys the offset value.

Returns:
the key of the first instance in the result, or null if the result set is empty.

fetch

com.google.appengine.api.datastore.QueryResultIterable<T> fetch()
Starts an asynchronous query. While the Query itself is iterable, the datastore does not begin executing your query until iterator() or fetch() is called. If you do not need to run multiple queries in parallel, this method is unnecessary; just iterate over the Query object itself.


fetchKeys

com.google.appengine.api.datastore.QueryResultIterable<Key<T>> fetchKeys()
Prepares an Iterable that will obtain the keys of the results. This is more efficient than fetching the actual results. Note that every time iterator() is called on the Iterable, a fresh query is executed; calling this method does not cause a datastore operation.


fetchParentKeys

<V> java.util.Set<Key<V>> fetchParentKeys()
Execute a keys-only query and then extract parent keys, returning them as a Set.

Throws:
java.lang.IllegalStateException - if any member of the query result does not have a parent.

fetchParents

<V> java.util.Map<Key<V>,V> fetchParents()
Gets the parent keys and then fetches the actual entities. This is the same as calling ofy.get(query.fetchParentKeys()).

Throws:
java.lang.IllegalStateException - if any member of the query result does not have a parent.

count

int count()

Count the total number of values in the result. limit and offset are obeyed.

This is somewhat faster than fetching, but the time still grows with the number of results. The datastore actually walks through the result set and counts for you.


list

java.util.List<T> list()

Execute the query and get the results as a List. The list will be equivalent to a simple ArrayList; you can iterate through it multiple times without incurring additional datastore cost.

Note that you must be careful about limit()ing the size of the list returned; you can easily exceed the practical memory limits of Appengine by querying for a very large dataset.


listKeys

java.util.List<Key<T>> listKeys()

Execute a keys-only query and get the results as a List. This is more efficient than fetching the actual results.

The size and scope considerations of list() apply; don't fetch more data than you can fit in a simple ArrayList.


clone

Query<T> clone()
Returns:
a clone of this query object at its current state. You can then modify the clone without modifying the original query.


Copyright © 2011 Jeff Schnitzer and a gang of pirates. All Rights Reserved. Build version: 3.1