public final class KeyQuery<T> extends java.lang.Object
Instances of this class are created by KeyManager, so it has no public constructors.
Once a query is defined, it may be executed multiple times.
Modifier and Type | Method and Description |
---|---|
int |
cardinality()
Returns the number of instances of the given key.
|
void |
defineQuery(KeyFieldValueList keyFields)
Defines the fields that form a key query.
|
void |
defineQuery(KeyFieldValueRangeList keyFields)
Defines the fields that form a range query.
|
T |
getMaximumResult(LockMode objectLock)
Returns the maximum value for the given ordered key.
|
T |
getMinimumResult(LockMode objectLock)
Returns the minimum value for the given ordered key.
|
T |
getOrCreateSingleResult(LockMode objectLock,
KeyFieldValueList additionalFields)
Returns the instance for the given unique key.
|
java.lang.Iterable<T> |
getResults(KeyOrderedBy orderBy,
LockMode objectLock)
Returns the results for a query using an ordered key.
|
java.lang.Iterable<T> |
getResults(LockMode objectLock)
Returns the results of a query.
|
T |
getSingleResult(LockMode objectLock)
Returns the instance matching the given unique key.
|
static void |
resetCache()
Resets internal caches.
|
void |
setQueryScope(QueryScope queryScope)
Set the scope of query.
|
java.lang.String |
toString()
Creates a human readable query string.
|
public java.lang.Iterable<T> getResults(LockMode objectLock) throws KeyIncompleteError
objectLock
- LockMode to apply to each instance returned.KeyIncompleteError
- All the fields for the key have not been defined.
Executes the previously defined query and returns the resulting set. For ordered keys, the default orderBy of ASCENDING is used.
public java.lang.Iterable<T> getResults(KeyOrderedBy orderBy, LockMode objectLock) throws KeyIncompleteError
orderBy
- The order or the results.objectLock
- The lock applied to each instance.KeyIncompleteError
- All the fields for the key have not been defined.KeyIllegalKeyTypeError
- The key is not ordered.
Executes the previously defined query and returns the resulting set.
public T getSingleResult(LockMode objectLock) throws KeyIncompleteError, KeyIllegalKeyTypeError, KeyMalformedQueryError
objectLock
- The object lock applied if an instance is found.KeyIncompleteError
- The fields for the key have not been defined.KeyIllegalKeyTypeError
- The key is not unique.KeyMalformedQueryError
- The query has range operators which may result multiple instances.
Executes the previously defined query and returns an instance.
public T getMinimumResult(LockMode objectLock) throws KeyMalformedQueryError, KeyIllegalKeyTypeError
objectLock
- The object lock applied if an instance is found.KeyIllegalKeyTypeError
- The key is not ordered.KeyMalformedQueryError
- An illegal operator was used when defining the query.
Executes the previously defined query (if any) and returns an instance containing the minimum value. If no query was defined, the instance containing the minimum key is returned. If a query was defined, the instance containing the minimum key value that matches the given fields is returned.
public T getMaximumResult(LockMode objectLock) throws KeyMalformedQueryError, KeyIllegalKeyTypeError
objectLock
- The object lock applied if an instance is found.KeyIllegalKeyTypeError
- The key is not ordered.KeyMalformedQueryError
- An illegal operator was used when defining the query.
Executes the previously defined query (if any) and returns an instance containing the maximum value. If no query was defined, the instance containing the maximum key is returned. If a query was defined, the instance containing the maximum key value that matches the given fields is returned.
public T getOrCreateSingleResult(LockMode objectLock, KeyFieldValueList additionalFields) throws KeyIncompleteError, KeyIllegalKeyTypeError, KeyIllegalFieldError, KeyMalformedQueryError
objectLock
- The object lock applied if a pre-existing instance
is found.additionalFields
- Additional field values to use if an
instance in created. These values are ignored if
the instance already exists. null can be specified
if no additional field values are needed.KeyIncompleteError
- The fields for the key have not been defined.KeyIllegalKeyTypeError
- The key is not unique.KeyIllegalFieldError
- A field in the additionalFields list is not valid.KeyMalformedQueryError
- The query has range operators which may result multiple instances.
Executes the previously defined query and returns an instance. This method will atomically insure that an instance of the type will be created when searching for a instance by key. The additionalFields list passed in allows the programmer to populate secondary key fields if the instance is created. This should be used instead of doing:
KeyedObject ko = kq.getSingleResult(lockMode); if (ko == null) { ko = new KeyedObject(keys); }
The above new can throw an ObjectNotUniqueError exception if another transaction creates the instance between the getSingleResult() and the new call.
public int cardinality() throws KeyIncompleteError
KeyIncompleteError
- All the fields for the key have not been defined.
Executes the previously defined query and returns the number of instances that would result.
No transaction locks are take when this method executes. This means the number returned by cardinality() may change even if called multiple times within a single transaction.
public void defineQuery(KeyFieldValueList keyFields) throws KeyIncompleteError, KeyIllegalFieldError, KeyMalformedQueryError
keyFields
- The fields to use.KeyIncompleteError
- All the fields for the key have not been defined.KeyIllegalFieldError
- A given field is not part of the key.KeyMalformedQueryError
- The resulting key lookup cannot perform a range query
on an ordered key, or duplicate fields exist
for a non-ordered key.
When executed, this defines the values used for a key lookup. Any previous query is overwritten.
public void defineQuery(KeyFieldValueRangeList keyFields) throws KeyIllegalFieldError, KeyMalformedQueryError
keyFields
- The fields to use.KeyIllegalFieldError
- A given field is not part of the key.KeyMalformedQueryError
- The resulting key lookup cannot perform a range query
on an ordered key, duplicate fields are defined
for a non-ordered key, or a range query was attempted
on a non-ordered key.
When executed, this defines the values used for a range query on an ordered key. Any previous query is overwritten.
public final void setQueryScope(QueryScope queryScope) throws java.lang.IllegalArgumentException, ResourceUnavailableException
This method defines the scope of any subsequent queries that are executed. It can be called multiple times with different queryScope values.
queryScope
- Scope of Query.java.lang.IllegalArgumentException
- The queryScope failed audit due to a bad node list.ResourceUnavailableException
- Access to distributed services failed.QueryScope
public java.lang.String toString() throws KeyIncompleteError
toString
in class java.lang.Object
KeyIncompleteError
- All the fields for the key have not been defined.
This method returns a string containing a "select" or "for" statement. The string also contains any optimization warnings for the defined query. This information can be used to help diagnose queries.
public static void resetCache()
Used internally when replacing type descriptors. Applications should not call this.