public class DatastoreTemplate extends Object implements DatastoreOperations
DatastoreOperations.| Constructor and Description |
|---|
DatastoreTemplate(com.google.cloud.datastore.DatastoreReaderWriter datastore,
DatastoreEntityConverter datastoreEntityConverter,
DatastoreMappingContext datastoreMappingContext,
ObjectToKeyFactory objectToKeyFactory) |
| Modifier and Type | Method and Description |
|---|---|
static void |
applyQueryOptions(com.google.cloud.datastore.StructuredQuery.Builder builder,
DatastoreQueryOptions queryOptions,
DatastorePersistentEntity<?> persistentEntity) |
<T> List<T> |
convertEntitiesForRead(Iterator<? extends com.google.cloud.datastore.BaseEntity> entities,
Class<T> entityClass)
Convert Datastore entities to objects of a specified type.
|
long |
count(Class<?> entityClass)
Count all occurrences of entities of the given domain type.
|
com.google.cloud.datastore.Key |
createKey(String kind,
Object id)
Create a
Key from kind name and id. |
<T> void |
delete(T entity)
Delete an entity from Cloud Datastore.
|
long |
deleteAll(Class<?> entityClass)
Delete all entities of a given domain type.
|
<T> void |
deleteAll(Iterable<T> entities)
Deletes multiple entities from Cloud Datastore.
|
<T> void |
deleteAllById(Iterable<?> ids,
Class<T> entityClass)
Delete multiple IDs from Cloud Datastore.
|
<T> void |
deleteById(Object id,
Class<T> entityClass)
Delete an entity from Cloud Datastore.
|
<T> boolean |
existsById(Object id,
Class<T> entityClass)
Check if the given ID belongs to an entity in Cloud Datastore.
|
<T> Collection<T> |
findAll(Class<T> entityClass)
Get all the entities of the given domain type.
|
<T> Collection<T> |
findAll(Class<T> entityClass,
DatastoreQueryOptions queryOptions)
Get all the entities of the given domain type applying limit, offset and sort.
|
<T> Collection<T> |
findAllById(Iterable<?> ids,
Class<T> entityClass)
Find all the entities of the given IDs.
|
<T> T |
findById(Object id,
Class<T> entityClass)
Get an entity based on a id.
|
<T> Map<String,T> |
findByIdAsMap(com.google.cloud.datastore.Key key,
Class<T> valueType)
Get a Datastore entity based on a id and convert it to a map.
|
DatastoreEntityConverter |
getDatastoreEntityConverter()
Get the
DatastoreEntityConverter used by this template. |
static com.google.cloud.datastore.PathElement |
keyToPathElement(com.google.cloud.datastore.Key key) |
<A> A |
performTransaction(Function<DatastoreOperations,A> operations)
Performs multiple read and write operations in a single transaction.
|
<T> Iterable<T> |
query(com.google.cloud.datastore.Query<? extends com.google.cloud.datastore.BaseEntity> query,
Class<T> entityClass)
Finds objects by using a Cloud Datastore query.
|
<A,T> List<T> |
query(com.google.cloud.datastore.Query<A> query,
Function<A,T> entityFunc)
Runs given query and applies given function to each entity in the result.
|
Iterable<com.google.cloud.datastore.Key> |
queryKeys(com.google.cloud.datastore.Query<com.google.cloud.datastore.Key> query)
Finds Cloud Datastore Keys by using a Cloud Datastore query.
|
<T> Iterable<?> |
queryKeysOrEntities(com.google.cloud.datastore.Query query,
Class<T> entityClass)
Finds objects by using a Cloud Datastore query.
|
<T> T |
save(T instance,
com.google.cloud.datastore.Key... ancestors)
Saves an instance of an object to Cloud Datastore.
|
<T> Iterable<T> |
saveAll(Iterable<T> entities,
com.google.cloud.datastore.Key... ancestors)
Saves multiple instances of objects to Cloud Datastore.
|
<V> void |
writeMap(com.google.cloud.datastore.Key datastoreKey,
Map<String,V> map)
Save a map as a Datastore entity, using map keys as field names.
|
public DatastoreTemplate(com.google.cloud.datastore.DatastoreReaderWriter datastore,
DatastoreEntityConverter datastoreEntityConverter,
DatastoreMappingContext datastoreMappingContext,
ObjectToKeyFactory objectToKeyFactory)
public DatastoreEntityConverter getDatastoreEntityConverter()
DatastoreEntityConverter used by this template.public <T> T findById(Object id, Class<T> entityClass)
DatastoreOperationsfindById in interface DatastoreOperationsT - the class type of the entity.id - the id of the entity. If this is actually a
Key then it will be used. Otherwise it will be
attempted to be converted to an integer or string value and it will be assumed to
be a root key value with the Kind determined by the entityClass. param.entityClass - the type of the entity to get.public <T> T save(T instance,
com.google.cloud.datastore.Key... ancestors)
DatastoreOperationssave in interface DatastoreOperationsT - the type of the object to saveinstance - the instance to save.ancestors - ancestors that should be added to the entrypublic <T> Iterable<T> saveAll(Iterable<T> entities, com.google.cloud.datastore.Key... ancestors)
DatastoreOperationssaveAll in interface DatastoreOperationsT - the type of entities to saveentities - the objects to save.ancestors - ancestors that should be added to each entrypublic <T> void deleteById(Object id, Class<T> entityClass)
DatastoreOperationsdeleteById in interface DatastoreOperationsT - ths entity typeid - the ID of the entity to delete. If this is actually a
Key then it will be used. Otherwise it will be
attempted to be converted to an integer or string value and it will be assumed to
be a root key value with the Kind determined by the entityClass.entityClass - the type of thepublic <T> void deleteAllById(Iterable<?> ids, Class<T> entityClass)
DatastoreOperationsdeleteAllById in interface DatastoreOperationsT - ths entity typeids - the IDs to delete. If any of these is actually a
Key then it will be used. Otherwise it will be
attempted to be converted to an integer or string value and it will be assumed to
be a root key value with the Kind determined by the entityClass.entityClass - the type of thepublic <T> void delete(T entity)
DatastoreOperationsdelete in interface DatastoreOperationsT - the entity typeentity - the entity to delete.public <T> void deleteAll(Iterable<T> entities)
DatastoreOperationsdeleteAll in interface DatastoreOperationsT - the entity type.entities - the entities to delete.public long deleteAll(Class<?> entityClass)
DatastoreOperationsdeleteAll in interface DatastoreOperationsentityClass - the domain type to delete from Cloud Datastore.public long count(Class<?> entityClass)
DatastoreOperationscount in interface DatastoreOperationsentityClass - the domain type to count.public <T> Collection<T> findAllById(Iterable<?> ids, Class<T> entityClass)
DatastoreOperationsKey then it will be used. Otherwise it will be
attempted to be converted to an integer or string value and it will be assumed to
be a root key value with the Kind determined by the entityClass.findAllById in interface DatastoreOperationsT - the type parameter of the domain type.ids - the IDs to search.entityClass - the domain type of the objects.public <T> Iterable<T> query(com.google.cloud.datastore.Query<? extends com.google.cloud.datastore.BaseEntity> query, Class<T> entityClass)
DatastoreOperationsquery in interface DatastoreOperationsT - the type of object to retrieve.query - the query to execute.entityClass - the type of object to retrieve.public <T> Iterable<?> queryKeysOrEntities(com.google.cloud.datastore.Query query, Class<T> entityClass)
T - the type of object to retrieve.query - the query to execute.entityClass - the type of object to retrieve.public <A,T> List<T> query(com.google.cloud.datastore.Query<A> query, Function<A,T> entityFunc)
DatastoreOperationsquery in interface DatastoreOperationsA - the row type of the query. This type can be either Key or a
Cloud Datastore entity.T - the type to map each entity or key to.query - the query to run.entityFunc - the function to apply to each found entity or key.public Iterable<com.google.cloud.datastore.Key> queryKeys(com.google.cloud.datastore.Query<com.google.cloud.datastore.Key> query)
DatastoreOperationsqueryKeys in interface DatastoreOperationsquery - the query to execute that retrieves only Keys.public <T> Collection<T> findAll(Class<T> entityClass)
DatastoreOperationsfindAll in interface DatastoreOperationsT - the type param of the domain type.entityClass - the domain type to get.public <T> Collection<T> findAll(Class<T> entityClass, DatastoreQueryOptions queryOptions)
DatastoreOperationsfindAll in interface DatastoreOperationsT - the type param of the domain type.entityClass - the domain type to get.queryOptions - query optionspublic static void applyQueryOptions(com.google.cloud.datastore.StructuredQuery.Builder builder,
DatastoreQueryOptions queryOptions,
DatastorePersistentEntity<?> persistentEntity)
public <T> boolean existsById(Object id, Class<T> entityClass)
DatastoreOperationsKey then it will be used. Otherwise it will be
attempted to be converted to an integer or string value and it will be assumed to
be a root key value with the Kind determined by the entityClass.existsById in interface DatastoreOperationsT - the type param of the domain type.id - the ID to search for.entityClass - the domain type of the entities to search for.public <A> A performTransaction(Function<DatastoreOperations,A> operations)
DatastoreOperationsperformTransaction in interface DatastoreOperationsA - the final return type of the operations.operations - the function that uses DatastoreOperations
to perform operations in a transaction.public <T> Map<String,T> findByIdAsMap(com.google.cloud.datastore.Key key, Class<T> valueType)
DatastoreOperationsfindByIdAsMap in interface DatastoreOperationsT - the value type of the mapkey - the key of the entityvalueType - type values should be converted tonull otherwisepublic <V> void writeMap(com.google.cloud.datastore.Key datastoreKey,
Map<String,V> map)
DatastoreOperationswriteMap in interface DatastoreOperationsV - the value type of the map to writedatastoreKey - the key for the entitymap - a mappublic com.google.cloud.datastore.Key createKey(String kind, Object id)
DatastoreOperationsKey from kind name and id.createKey in interface DatastoreOperationskind - the Cloud Datastore kind nameid - object to be used as id; if it is a Long, the value is used, otherwise it is
converted to Stringpublic static com.google.cloud.datastore.PathElement keyToPathElement(com.google.cloud.datastore.Key key)
public <T> List<T> convertEntitiesForRead(Iterator<? extends com.google.cloud.datastore.BaseEntity> entities, Class<T> entityClass)
T - the type the entities should be converted to.entities - the Datastore entitiesentityClass - the type the entities should be converted to.Copyright © 2019 Pivotal Software, Inc.. All rights reserved.