com.googlecode.objectify.impl
Class QueryImpl<T>

java.lang.Object
  extended by com.googlecode.objectify.impl.QueryImpl<T>
All Implemented Interfaces:
com.google.appengine.api.datastore.QueryResultIterable<T>, Query<T>, java.lang.Cloneable, java.lang.Iterable<T>
Direct Known Subclasses:
SessionCachingQueryImpl

public class QueryImpl<T>
extends java.lang.Object
implements Query<T>, java.lang.Cloneable

Implementation of Query.

Author:
Jeff Schnitzer

Nested Class Summary
protected  class QueryImpl.ToKeyIterable
          Iterable that translates from datastore Entity to Keys
protected  class QueryImpl.ToKeyIterator
          Iterator that translates from datastore Entity to Keys
protected  class QueryImpl.ToObjectIterable
          Iterable that translates from datastore Entity to POJO
protected  class QueryImpl.ToObjectIterator
          Iterator that translates from datastore Entity to typed Objects
 
Constructor Summary
QueryImpl(ObjectifyFactory fact, Objectify objectify)
           
QueryImpl(ObjectifyFactory fact, Objectify objectify, java.lang.Class<T> clazz)
           
 
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()
           
protected  com.google.appengine.api.datastore.Query cloneRawQuery(com.google.appengine.api.datastore.Query orig)
          Make a new Query object that is exactly like the old.
 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.
protected  com.google.appengine.api.datastore.Query getActual()
           
 Key<T> getKey()
          Get the key of the first entity in the result set.
 com.google.appengine.api.datastore.QueryResultIterator<T> iterator()
           
 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.
protected  com.google.appengine.api.datastore.Query.FilterOperator translate(java.lang.String operator)
          Converts the textual operator (">", "<=", etc) into a FilterOperator.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QueryImpl

public QueryImpl(ObjectifyFactory fact,
                 Objectify objectify)

QueryImpl

public QueryImpl(ObjectifyFactory fact,
                 Objectify objectify,
                 java.lang.Class<T> clazz)
Method Detail

getActual

protected com.google.appengine.api.datastore.Query getActual()
Returns:
the underlying datastore query object

filter

public Query<T> filter(java.lang.String condition,
                       java.lang.Object value)
Description copied from interface: Query

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.

Specified by:
filter in interface Query<T>

translate

protected com.google.appengine.api.datastore.Query.FilterOperator translate(java.lang.String operator)
Converts the textual operator (">", "<=", etc) into a FilterOperator. Forgiving about the syntax; != and <> are NOT_EQUAL, = and == are EQUAL.


order

public Query<T> order(java.lang.String condition)
Description copied from interface: Query

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.

Specified by:
order in interface Query<T>

ancestor

public Query<T> ancestor(java.lang.Object keyOrEntity)
Description copied from interface: Query
Restricts result set only to objects which have the given ancestor somewhere in the chain. Doesn't need to be the immediate parent.

Specified by:
ancestor in interface Query<T>
Parameters:
keyOrEntity - can be a Key, a Key, or an Objectify entity object.

limit

public Query<T> limit(int value)
Description copied from interface: Query
Limit the fetched result set to a certain number of values.

Specified by:
limit in interface Query<T>
Parameters:
value - must be >= 0. A value of 0 indicates no limit.

offset

public Query<T> offset(int value)
Description copied from interface: Query
Starts the query results at a particular zero-based offset.

Specified by:
offset in interface Query<T>
Parameters:
value - must be >= 0

startCursor

public Query<T> startCursor(com.google.appengine.api.datastore.Cursor value)
Description copied from interface: Query
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.

Specified by:
startCursor in interface Query<T>

endCursor

public Query<T> endCursor(com.google.appengine.api.datastore.Cursor value)
Description copied from interface: Query
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.

Specified by:
endCursor in interface Query<T>

chunkSize

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

Specified by:
chunkSize in interface Query<T>
Parameters:
value - must be > 0

prefetchSize

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

Specified by:
prefetchSize in interface Query<T>
Parameters:
value - must be >= 0

toString

public java.lang.String toString()
Description copied from interface: Query

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 Query<T>
Overrides:
toString in class java.lang.Object

iterator

public com.google.appengine.api.datastore.QueryResultIterator<T> iterator()
Specified by:
iterator in interface com.google.appengine.api.datastore.QueryResultIterable<T>
Specified by:
iterator in interface java.lang.Iterable<T>

get

public T get()
Description copied from interface: Query
Gets the first entity in the result set. Obeys the offset value.

Specified by:
get in interface Query<T>
Returns:
the only instance in the result, or null if the result set is empty.

getKey

public Key<T> getKey()
Description copied from interface: Query
Get the key of the first entity in the result set. Obeys the offset value.

Specified by:
getKey in interface Query<T>
Returns:
the key of the first instance in the result, or null if the result set is empty.

count

public int count()
Description copied from interface: Query

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.

Specified by:
count in interface Query<T>

fetch

public com.google.appengine.api.datastore.QueryResultIterable<T> fetch()
Description copied from interface: Query
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.

Specified by:
fetch in interface Query<T>

fetchKeys

public com.google.appengine.api.datastore.QueryResultIterable<Key<T>> fetchKeys()
Description copied from interface: Query
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.

Specified by:
fetchKeys in interface Query<T>

fetchParentKeys

public <V> java.util.Set<Key<V>> fetchParentKeys()
Description copied from interface: Query
Execute a keys-only query and then extract parent keys, returning them as a Set.

Specified by:
fetchParentKeys in interface Query<T>

fetchParents

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

Specified by:
fetchParents in interface Query<T>

list

public java.util.List<T> list()
Description copied from interface: Query

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.

Specified by:
list in interface Query<T>

listKeys

public java.util.List<Key<T>> listKeys()
Description copied from interface: Query

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.

Specified by:
listKeys in interface Query<T>

clone

public Query<T> clone()
Specified by:
clone in interface Query<T>
Overrides:
clone in class java.lang.Object
Returns:
a clone of this query object at its current state. You can then modify the clone without modifying the original query.

cloneRawQuery

protected com.google.appengine.api.datastore.Query cloneRawQuery(com.google.appengine.api.datastore.Query orig)
Make a new Query object that is exactly like the old. Too bad Query isn't Cloneable.



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