Class Query<T>
- java.lang.Object
-
- io.objectbox.query.Query<T>
-
- Type Parameters:
T
- Entity class for which results are returned.
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class Query<T> extends java.lang.Object implements java.io.Closeable
A repeatable Query returning the latest matching objects.Use
find()
or related methods to fetch the latest results from theBoxStore
.Use
property(Property)
to only return values or an aggregate of a single Property.Make sure to
close()
this query once done with it to reclaim resources immediately.See the Queries documentation for details.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
close()
Closes this query and frees used resources.Query<T>
copy()
Creates a copy of this for use in another thread.long
count()
Returns the count of Objects matching the query.java.lang.String
describe()
For logging and testing, returns a string describing this query like "Query for entity Example with 4 conditions with properties prop1, prop2".java.lang.String
describeParameters()
For logging and testing, returns a string describing the conditions of this query like "(prop1 == A AND prop2 is null)".protected void
finalize()
Explicitly callclose()
instead to avoid expensive finalization.java.util.List<T>
find()
Finds objects matching the query.java.util.List<T>
find(long offset, long limit)
Likefind()
, but can skip and limit results.T
findFirst()
Finds the first object matching this query.long
findFirstId()
LikefindFirst()
, but returns just the ID of the object.long[]
findIds()
Likefind()
, but returns just the IDs of the objects.long[]
findIds(long offset, long limit)
LikefindIds()
, but can skip and limit results.java.util.List<IdWithScore>
findIdsWithScores()
Finds IDs of objects matching the query associated to their query score (e.g.java.util.List<IdWithScore>
findIdsWithScores(long offset, long limit)
LikefindIdsWithScores()
, but can skip and limit results.LazyList<T>
findLazy()
LazyList<T>
findLazyCached()
T
findUnique()
Finds the only object matching this query.long
findUniqueId()
LikefindUnique()
, but returns just the ID of the object.java.util.List<ObjectWithScore<T>>
findWithScores()
Finds objects matching the query associated to their query score (e.g.java.util.List<ObjectWithScore<T>>
findWithScores(long offset, long limit)
LikefindWithScores()
, but can skip and limit results.void
forEach(QueryConsumer<T> consumer)
Emits query results one by one to the given consumer (synchronously).PropertyQuery
property(Property<T> property)
Creates aPropertyQuery
for the given property.void
publish()
Publishes the current data to all subscribed @DataObserver
s.long
remove()
Removes (deletes) all Objects matching the queryQuery<T>
setParameter(Property<?> property, boolean value)
Sets a parameter previously given to theQueryBuilder
to a new value.Query<T>
setParameter(Property<?> property, byte[] value)
Sets a parameter previously given to theQueryBuilder
to new values.Query<T>
setParameter(Property<?> property, double value)
Sets a parameter previously given to theQueryBuilder
to a new value.Query<T>
setParameter(Property<?> property, float[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.Query<T>
setParameter(Property<?> property, int[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.Query<T>
setParameter(Property<?> property, long value)
Sets a parameter previously given to theQueryBuilder
to a new value.Query<T>
setParameter(Property<?> property, long[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.Query<T>
setParameter(Property<?> property, java.lang.String value)
Sets a parameter previously given to theQueryBuilder
to a new value.Query<T>
setParameter(Property<?> property, java.lang.String[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.Query<T>
setParameter(Property<?> property, java.util.Date value)
Sets a parameter previously given to theQueryBuilder
to a new value.Query<T>
setParameter(java.lang.String alias, boolean value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, byte[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, double value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, float[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, int[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, long value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, long[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, java.lang.String value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, java.lang.String[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameter(java.lang.String alias, java.util.Date value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.Query<T>
setParameters(Property<?> property, double value1, double value2)
Sets a parameter previously given to theQueryBuilder
to new values.Query<T>
setParameters(Property<?> property, int[] values)
Deprecated.UsesetParameter(Property, int[])
instead.Query<T>
setParameters(Property<?> property, long[] values)
Deprecated.UsesetParameter(Property, long[])
instead.Query<T>
setParameters(Property<?> property, long value1, long value2)
Sets a parameter previously given to theQueryBuilder
to new values.Query<T>
setParameters(Property<?> property, java.lang.String[] values)
Deprecated.UsesetParameter(Property, String[])
instead.Query<T>
setParameters(Property<?> property, java.lang.String key, java.lang.String value)
Sets a parameter previously given to theQueryBuilder
to new values.Query<T>
setParameters(java.lang.String alias, double value1, double value2)
Changes the parameters of the query condition with the matchingalias
to the new values.Query<T>
setParameters(java.lang.String alias, int[] values)
Deprecated.UsesetParameter(String, int[])
instead.Query<T>
setParameters(java.lang.String alias, long[] values)
Deprecated.UsesetParameter(String, long[])
instead.Query<T>
setParameters(java.lang.String alias, long value1, long value2)
Changes the parameters of the query condition with the matchingalias
to the new values.Query<T>
setParameters(java.lang.String alias, java.lang.String[] values)
Deprecated.UsesetParameter(String, String[])
instead.Query<T>
setParameters(java.lang.String alias, java.lang.String key, java.lang.String value)
Changes the parameters of the query condition with the matchingalias
to the new values.SubscriptionBuilder<java.util.List<T>>
subscribe()
ADataObserver
can be subscribed to data changes using the returned builder.SubscriptionBuilder<java.util.List<T>>
subscribe(DataSubscriptionList dataSubscriptionList)
Convenience forsubscribe()
with a subsequent call toSubscriptionBuilder.dataSubscriptionList(DataSubscriptionList)
.
-
-
-
Method Detail
-
finalize
protected void finalize() throws java.lang.Throwable
Explicitly callclose()
instead to avoid expensive finalization.- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
-
close
public void close()
Closes this query and frees used resources.If possible, call this always once done with this. Otherwise, will be called once this is finalized (e.g. garbage collected).
Calling any other methods of this afterwards will throw an
IllegalStateException
.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
findFirst
@Nullable public T findFirst()
Finds the first object matching this query.Note: if no
QueryBuilder.order(io.objectbox.Property<T>)
conditions are present, which object is the first one might be arbitrary (sometimes the one with the lowest ID, but never guaranteed to be).- Returns:
- The first object if there are matches.
null
if no object matches.
-
findUnique
@Nullable public T findUnique()
Finds the only object matching this query.- Returns:
- The object if a single object matches.
null
if no object matches. ThrowsNonUniqueResultException
if there are multiple objects matching the query.
-
find
@Nonnull public java.util.List<T> find()
Finds objects matching the query.Note: if no
QueryBuilder.order(io.objectbox.Property<T>)
conditions are present, the order is arbitrary (sometimes ordered by ID, but never guaranteed to).- Returns:
- A list of matching objects. An empty list if no object matches.
-
find
@Nonnull public java.util.List<T> find(long offset, long limit)
Likefind()
, but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
- Parameters:
offset
- If greater than 0, skips this many results.limit
- If greater than 0, returns at most this many results.
-
findFirstId
public long findFirstId()
LikefindFirst()
, but returns just the ID of the object.This is more efficient as no object is created.
Ignores any
query filter
.- Returns:
- The ID of the first matching object.
0
if no object matches.
-
findUniqueId
public long findUniqueId()
LikefindUnique()
, but returns just the ID of the object.This is more efficient as no object is created.
Ignores any
query filter
.- Returns:
- The ID of the object, if a single object matches.
0
if no object matches. ThrowsNonUniqueResultException
if there are multiple objects matching the query.
-
findIds
@Nonnull public long[] findIds()
Likefind()
, but returns just the IDs of the objects.IDs can later be used to
Box.get(long)
objects.This is very efficient as no objects are created.
Note: a filter set with
QueryBuilder.filter(QueryFilter)
will be silently ignored!- Returns:
- An array of IDs of matching objects. An empty array if no objects match.
-
findIds
@Nonnull public long[] findIds(long offset, long limit)
LikefindIds()
, but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
Note: a filter set with
QueryBuilder.filter(QueryFilter)
will be silently ignored!- Parameters:
offset
- If greater than 0, skips this many results.limit
- If greater than 0, returns at most this many results.
-
findIdsWithScores
@Nonnull public java.util.List<IdWithScore> findIdsWithScores(long offset, long limit)
LikefindIdsWithScores()
, but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
- Parameters:
offset
- If greater than 0, skips this many results.limit
- If greater than 0, returns at most this many results.
-
findIdsWithScores
@Nonnull public java.util.List<IdWithScore> findIdsWithScores()
Finds IDs of objects matching the query associated to their query score (e.g. distance in NN search).This only works on objects with a property with an
HnswIndex
.- Returns:
- A list of
IdWithScore
that wraps IDs of matching objects and their score, sorted by score in ascending order.
-
findWithScores
@Nonnull public java.util.List<ObjectWithScore<T>> findWithScores(long offset, long limit)
LikefindWithScores()
, but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
- Parameters:
offset
- If greater than 0, skips this many results.limit
- If greater than 0, returns at most this many results.
-
findWithScores
@Nonnull public java.util.List<ObjectWithScore<T>> findWithScores()
Finds objects matching the query associated to their query score (e.g. distance in NN search).This only works on objects with a property with an
HnswIndex
.- Returns:
- A list of
ObjectWithScore
that wraps matching objects and their score, sorted by score in ascending order.
-
property
public PropertyQuery property(Property<T> property)
Creates aPropertyQuery
for the given property.A
PropertyQuery
uses the same conditions as this Query object, but returns only the value(s) of a single property (not an entity objects).- Parameters:
property
- the property for which to return values
-
forEach
public void forEach(QueryConsumer<T> consumer)
Emits query results one by one to the given consumer (synchronously). Once this method returns, the consumer will have received all result object). It "streams" each object from the database to the consumer, which is very memory efficient. Because this is run in a read transaction, the consumer gets a consistent view on the data. LikefindLazy()
, this method can be used for a high amount of data.Note: because the consumer is called within a read transaction it may not write to the database.
-
count
public long count()
Returns the count of Objects matching the query.
-
setParameter
public Query<T> setParameter(Property<?> property, java.lang.String value)
Sets a parameter previously given to theQueryBuilder
to a new value.
-
setParameter
public Query<T> setParameter(java.lang.String alias, java.lang.String value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The new value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, long value)
Sets a parameter previously given to theQueryBuilder
to a new value.
-
setParameter
public Query<T> setParameter(java.lang.String alias, long value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The new value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, double value)
Sets a parameter previously given to theQueryBuilder
to a new value.
-
setParameter
public Query<T> setParameter(java.lang.String alias, double value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The new value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, java.util.Date value)
Sets a parameter previously given to theQueryBuilder
to a new value.- Throws:
java.lang.NullPointerException
- if given date is null
-
setParameter
public Query<T> setParameter(java.lang.String alias, java.util.Date value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The new value to use for the query condition.- Throws:
java.lang.NullPointerException
- if given date is null
-
setParameter
public Query<T> setParameter(Property<?> property, boolean value)
Sets a parameter previously given to theQueryBuilder
to a new value.
-
setParameter
public Query<T> setParameter(java.lang.String alias, boolean value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The new value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, int[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.- Parameters:
property
- Property reference from generated entity underscore class, likeExample_.example
.value
- The newint[]
value to use for the query condition.
-
setParameter
public Query<T> setParameter(java.lang.String alias, int[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The newint[]
value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, long[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.- Parameters:
property
- Property reference from generated entity underscore class, likeExample_.example
.value
- The newlong[]
value to use for the query condition.
-
setParameter
public Query<T> setParameter(java.lang.String alias, long[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The newlong[]
value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, float[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.- Parameters:
property
- Property reference from generated entity underscore class, likeExample_.example
.value
- The newfloat[]
value to use for the query condition.
-
setParameter
public Query<T> setParameter(java.lang.String alias, float[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The newfloat[]
value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, java.lang.String[] value)
Changes the parameter of the query condition forproperty
to a newvalue
.- Parameters:
property
- Property reference from generated entity underscore class, likeExample_.example
.value
- The newString[]
value to use for the query condition.
-
setParameter
public Query<T> setParameter(java.lang.String alias, java.lang.String[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The newString[]
value to use for the query condition.
-
setParameters
public Query<T> setParameters(Property<?> property, long value1, long value2)
Sets a parameter previously given to theQueryBuilder
to new values.
-
setParameters
public Query<T> setParameters(java.lang.String alias, long value1, long value2)
Changes the parameters of the query condition with the matchingalias
to the new values.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value1
- The first value to use for the query condition.value2
- The second value to use for the query condition.
-
setParameters
@Deprecated public Query<T> setParameters(Property<?> property, int[] values)
Deprecated.UsesetParameter(Property, int[])
instead.Sets a parameter previously given to theQueryBuilder
to new values.
-
setParameters
@Deprecated public Query<T> setParameters(java.lang.String alias, int[] values)
Deprecated.UsesetParameter(String, int[])
instead.Sets a parameter previously given to theQueryBuilder
to new values.- Parameters:
alias
- as defined usingQueryBuilder.parameterAlias(String)
.
-
setParameters
@Deprecated public Query<T> setParameters(Property<?> property, long[] values)
Deprecated.UsesetParameter(Property, long[])
instead.Sets a parameter previously given to theQueryBuilder
to new values.
-
setParameters
@Deprecated public Query<T> setParameters(java.lang.String alias, long[] values)
Deprecated.UsesetParameter(String, long[])
instead.Sets a parameter previously given to theQueryBuilder
to new values.- Parameters:
alias
- as defined usingQueryBuilder.parameterAlias(String)
.
-
setParameters
public Query<T> setParameters(Property<?> property, double value1, double value2)
Sets a parameter previously given to theQueryBuilder
to new values.
-
setParameters
public Query<T> setParameters(java.lang.String alias, double value1, double value2)
Changes the parameters of the query condition with the matchingalias
to the new values.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value1
- The first value to use for the query condition.value2
- The second value to use for the query condition.
-
setParameters
@Deprecated public Query<T> setParameters(Property<?> property, java.lang.String[] values)
Deprecated.UsesetParameter(Property, String[])
instead.Sets a parameter previously given to theQueryBuilder
to new values.
-
setParameters
@Deprecated public Query<T> setParameters(java.lang.String alias, java.lang.String[] values)
Deprecated.UsesetParameter(String, String[])
instead.Sets a parameter previously given to theQueryBuilder
to new values.- Parameters:
alias
- as defined usingQueryBuilder.parameterAlias(String)
.
-
setParameters
public Query<T> setParameters(Property<?> property, java.lang.String key, java.lang.String value)
Sets a parameter previously given to theQueryBuilder
to new values.
-
setParameters
public Query<T> setParameters(java.lang.String alias, java.lang.String key, java.lang.String value)
Changes the parameters of the query condition with the matchingalias
to the new values.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.key
- The first value to use for the query condition.value
- The second value to use for the query condition.
-
setParameter
public Query<T> setParameter(Property<?> property, byte[] value)
Sets a parameter previously given to theQueryBuilder
to new values.
-
setParameter
public Query<T> setParameter(java.lang.String alias, byte[] value)
Changes the parameter of the query condition with the matchingalias
to a newvalue
.- Parameters:
alias
- as defined usingPropertyQueryCondition.alias(String)
.value
- The new value to use for the query condition.
-
remove
public long remove()
Removes (deletes) all Objects matching the query- Returns:
- count of removed Objects
-
subscribe
public SubscriptionBuilder<java.util.List<T>> subscribe()
ADataObserver
can be subscribed to data changes using the returned builder. The observer is supplied viaSubscriptionBuilder.observer(DataObserver)
and will be notified once the query results have (potentially) changed.With subscribing, the observer will immediately get current query results. The query is run for the subscribing observer.
Threading notes: Query observers are notified from a thread pooled. Observers may be notified in parallel. The notification order is the same as the subscription order, although this may not always be guaranteed in the future.
Stale observers: you must hold on to the Query or
DataSubscription
objects to keep yourDataObserver
s active. If this Query is not referenced anymore (along with itsDataSubscription
s, which hold a reference to the Query internally), it may be GCed and observers may become stale (won't receive anymore data).
-
subscribe
public SubscriptionBuilder<java.util.List<T>> subscribe(DataSubscriptionList dataSubscriptionList)
Convenience forsubscribe()
with a subsequent call toSubscriptionBuilder.dataSubscriptionList(DataSubscriptionList)
.- Parameters:
dataSubscriptionList
- the resultingDataSubscription
will be added to it
-
publish
public void publish()
Publishes the current data to all subscribed @DataObserver
s. This is useful triggering observers when new parameters have been set. Note, that setParameter methods will NOT be propagated to observers.
-
describe
public java.lang.String describe()
For logging and testing, returns a string describing this query like "Query for entity Example with 4 conditions with properties prop1, prop2".Note: the format of the returned string may change without notice.
-
describeParameters
public java.lang.String describeParameters()
For logging and testing, returns a string describing the conditions of this query like "(prop1 == A AND prop2 is null)".Note: the format of the returned string may change without notice.
-
-