Class DatastoreTemplate
java.lang.Object
com.google.cloud.spring.data.datastore.core.DatastoreTemplate
- All Implemented Interfaces:
DatastoreOperations
,org.springframework.beans.factory.Aware
,org.springframework.context.ApplicationEventPublisherAware
@ImportRuntimeHints(DatastoreCoreRuntimeHints.class)
public class DatastoreTemplate
extends Object
implements DatastoreOperations, org.springframework.context.ApplicationEventPublisherAware
An implementation of
DatastoreOperations
.- Since:
- 1.1
-
Constructor Summary
ConstructorDescriptionDatastoreTemplate
(Supplier<? extends com.google.cloud.datastore.DatastoreReaderWriter> datastore, DatastoreEntityConverter datastoreEntityConverter, DatastoreMappingContext datastoreMappingContext, ObjectToKeyFactory objectToKeyFactory) -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 all occurrences of entities of the given domain type.com.google.cloud.datastore.Key
Create aKey
from entity class and id value.com.google.cloud.datastore.Key
Create aKey
from kind name and id.<T> void
delete
(T entity) Delete an entity from Cloud Datastore.long
Delete all entities of a given domain type.<T> void
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>
Get all the entities of the given domain type.<T> DatastoreResultsCollection<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
Get an entity based on a id.findByIdAsMap
(com.google.cloud.datastore.Key key, Class<T> valueType) Get a Datastore entity based on a id and convert it to a map.Get theDatastoreEntityConverter
used by this template.com.google.cloud.datastore.Key
Create aKey
from id property of an entity object.<T> T
insert
(T instance, com.google.cloud.datastore.Key... ancestors) Inserts an instance of an object to Cloud Datastore.<T> Iterable<T>
Saves multiple instances of objects to Cloud Datastore.<T> Iterable<com.google.cloud.datastore.Key>
keyQueryByExample
(org.springframework.data.domain.Example<T> example, DatastoreQueryOptions queryOptions) Run key query by example.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> DatastoreResultsIterable<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> Runs given query and applies given function to each entity in the result.<T> DatastoreResultsIterable<T>
queryByExample
(org.springframework.data.domain.Example<T> example, DatastoreQueryOptions queryOptions) Run query by example.<T> org.springframework.data.domain.Slice<T>
queryEntitiesSlice
(com.google.cloud.datastore.StructuredQuery query, Class<T> entityClass, org.springframework.data.domain.Pageable pageable) Finds entities by using a Cloud Datastore query.<A,
T> DatastoreResultsIterable<T> queryIterable
(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> DatastoreResultsIterable<?>
queryKeysOrEntities
(com.google.cloud.datastore.Query query, Class<T> entityClass) Finds objects by using a Cloud Datastore query.<T> org.springframework.data.domain.Slice<com.google.cloud.datastore.Key>
queryKeysSlice
(com.google.cloud.datastore.KeyQuery query, Class<T> entityClass, org.springframework.data.domain.Pageable pageable) Finds keys 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>
Saves multiple instances of objects to Cloud Datastore.void
setApplicationEventPublisher
(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) <V> void
Save a map as a Datastore entity, using map keys as field names.
-
Constructor Details
-
DatastoreTemplate
public DatastoreTemplate(Supplier<? extends com.google.cloud.datastore.DatastoreReaderWriter> datastore, DatastoreEntityConverter datastoreEntityConverter, DatastoreMappingContext datastoreMappingContext, ObjectToKeyFactory objectToKeyFactory)
-
-
Method Details
-
getDatastoreEntityConverter
Description copied from interface:DatastoreOperations
Get theDatastoreEntityConverter
used by this template.- Specified by:
getDatastoreEntityConverter
in interfaceDatastoreOperations
- Returns:
- the converter.
-
setApplicationEventPublisher
public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) - Specified by:
setApplicationEventPublisher
in interfaceorg.springframework.context.ApplicationEventPublisherAware
-
findById
Description copied from interface:DatastoreOperations
Get an entity based on a id.- Specified by:
findById
in interfaceDatastoreOperations
- Type Parameters:
T
- the class type of the entity.- Parameters:
id
- the id of the entity. If this is actually aKey
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.- Returns:
- the entity that was found with that id.
-
save
public <T> T save(T instance, com.google.cloud.datastore.Key... ancestors) Description copied from interface:DatastoreOperations
Saves an instance of an object to Cloud Datastore. Behaves as update or insert. Ancestors can be added only to entries with Key ids.- Specified by:
save
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of the object to save- Parameters:
instance
- the instance to save.ancestors
- ancestors that should be added to the entry- Returns:
- the instance that was saved.
-
saveAll
Description copied from interface:DatastoreOperations
Saves multiple instances of objects to Cloud Datastore. Behaves as update or insert. Ancestors can be added only to entries with Key ids.- Specified by:
saveAll
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of entities to save- Parameters:
entities
- the objects to save.ancestors
- ancestors that should be added to each entry- Returns:
- the entities that were saved.
-
insert
public <T> T insert(T instance, com.google.cloud.datastore.Key... ancestors) Description copied from interface:DatastoreOperations
Inserts an instance of an object to Cloud Datastore. Throws a DatastoreException if an entry with same ID already exists. Ancestors can be added only to entries with Key ids.- Specified by:
insert
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of the object to save- Parameters:
instance
- the instance to save.ancestors
- ancestors that should be added to the entry- Returns:
- the instance that was saved.
-
insertAll
Description copied from interface:DatastoreOperations
Saves multiple instances of objects to Cloud Datastore. Throws a DatastoreException if any entry with one of the IDs already exists. Ancestors can be added only to entries with Key ids.- Specified by:
insertAll
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of entities to save- Parameters:
entities
- the objects to save.ancestors
- ancestors that should be added to each entry- Returns:
- the entities that were saved.
-
deleteById
Description copied from interface:DatastoreOperations
Delete an entity from Cloud Datastore. Deleting IDs that do not exist in Cloud Datastore will result in no operation.- Specified by:
deleteById
in interfaceDatastoreOperations
- Type Parameters:
T
- ths entity type- Parameters:
id
- the ID of the entity to delete. If this is actually aKey
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 the
-
deleteAllById
Description copied from interface:DatastoreOperations
Delete multiple IDs from Cloud Datastore. Deleting IDs that do not exist in Cloud Datastore will result in no operation.- Specified by:
deleteAllById
in interfaceDatastoreOperations
- Type Parameters:
T
- ths entity type- Parameters:
ids
- the IDs to delete. If any of these is actually aKey
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 the
-
delete
public <T> void delete(T entity) Description copied from interface:DatastoreOperations
Delete an entity from Cloud Datastore. Deleting entities that don't exist in Cloud Datastore will result in no operation.- Specified by:
delete
in interfaceDatastoreOperations
- Type Parameters:
T
- the entity type- Parameters:
entity
- the entity to delete.
-
deleteAll
Description copied from interface:DatastoreOperations
Deletes multiple entities from Cloud Datastore. Deleting entities that don't exist in Cloud Datastore will result in no operation.- Specified by:
deleteAll
in interfaceDatastoreOperations
- Type Parameters:
T
- the entity type.- Parameters:
entities
- the entities to delete.
-
deleteAll
Description copied from interface:DatastoreOperations
Delete all entities of a given domain type.- Specified by:
deleteAll
in interfaceDatastoreOperations
- Parameters:
entityClass
- the domain type to delete from Cloud Datastore.- Returns:
- the number of entities that were deleted.
-
count
Description copied from interface:DatastoreOperations
Count all occurrences of entities of the given domain type.- Specified by:
count
in interfaceDatastoreOperations
- Parameters:
entityClass
- the domain type to count.- Returns:
- the number of entities of the given type.
-
findAllById
Description copied from interface:DatastoreOperations
Find all the entities of the given IDs. If an ID is actually aKey
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.- Specified by:
findAllById
in interfaceDatastoreOperations
- Type Parameters:
T
- the type parameter of the domain type.- Parameters:
ids
- the IDs to search.entityClass
- the domain type of the objects.- Returns:
- the entities that were found.
-
query
public <T> DatastoreResultsIterable<T> query(com.google.cloud.datastore.Query<? extends com.google.cloud.datastore.BaseEntity> query, Class<T> entityClass) Description copied from interface:DatastoreOperations
Finds objects by using a Cloud Datastore query.- Specified by:
query
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of object to retrieve.- Parameters:
query
- the query to execute.entityClass
- the type of object to retrieve.- Returns:
- an iterable containing found entities
-
queryKeysSlice
public <T> org.springframework.data.domain.Slice<com.google.cloud.datastore.Key> queryKeysSlice(com.google.cloud.datastore.KeyQuery query, Class<T> entityClass, org.springframework.data.domain.Pageable pageable) Description copied from interface:DatastoreOperations
Finds keys by using a Cloud Datastore query. Resulting Slice can be used to get a Pageable for the next page or to determine if the next page exists.- Specified by:
queryKeysSlice
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of object to retrieve.- Parameters:
query
- the query to execute.entityClass
- the type of object to retrieve.pageable
- that indicates page number and page size- Returns:
- a Slice containing found objects
-
queryEntitiesSlice
public <T> org.springframework.data.domain.Slice<T> queryEntitiesSlice(com.google.cloud.datastore.StructuredQuery query, Class<T> entityClass, org.springframework.data.domain.Pageable pageable) Description copied from interface:DatastoreOperations
Finds entities by using a Cloud Datastore query. Resulting Slice can be used to get a Pageable for the next page or to determine if the next page exists.- Specified by:
queryEntitiesSlice
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of object to retrieve.- Parameters:
query
- the query to execute.entityClass
- the type of object to retrieve.pageable
- that indicates page number and page size- Returns:
- a Slice containing found objects
-
queryKeysOrEntities
public <T> DatastoreResultsIterable<?> queryKeysOrEntities(com.google.cloud.datastore.Query query, Class<T> entityClass) Description copied from interface:DatastoreOperations
Finds objects by using a Cloud Datastore query. If the query is a key-query, then keys are returned.- Specified by:
queryKeysOrEntities
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of object to retrieve.- Parameters:
query
- the query to execute.entityClass
- the type of object to retrieve.- Returns:
- an iterable containing found objects and a cursor
-
query
Description copied from interface:DatastoreOperations
Runs given query and applies given function to each entity in the result.- Specified by:
query
in interfaceDatastoreOperations
- Type Parameters:
A
- the row type of the query. This type can be eitherKey
or a Cloud Datastore entity.T
- the type to map each entity or key to.- Parameters:
query
- the query to run.entityFunc
- the function to apply to each found entity or key.- Returns:
- the mapped entities or keys.
-
queryIterable
public <A,T> DatastoreResultsIterable<T> queryIterable(com.google.cloud.datastore.Query<A> query, Function<A, T> entityFunc) Description copied from interface:DatastoreOperations
Runs given query and applies given function to each entity in the result.- Specified by:
queryIterable
in interfaceDatastoreOperations
- Type Parameters:
A
- the row type of the query. This type can be eitherKey
or a Cloud Datastore entity.T
- the type to map each entity or key to.- Parameters:
query
- the query to run.entityFunc
- the function to apply to each found entity or key.- Returns:
- the mapped entities or keys.
-
queryKeys
public Iterable<com.google.cloud.datastore.Key> queryKeys(com.google.cloud.datastore.Query<com.google.cloud.datastore.Key> query) Description copied from interface:DatastoreOperations
Finds Cloud Datastore Keys by using a Cloud Datastore query.- Specified by:
queryKeys
in interfaceDatastoreOperations
- Parameters:
query
- the query to execute that retrieves only Keys.- Returns:
- the list of keys found.
-
findAll
Description copied from interface:DatastoreOperations
Get all the entities of the given domain type.- Specified by:
findAll
in interfaceDatastoreOperations
- Type Parameters:
T
- the type param of the domain type.- Parameters:
entityClass
- the domain type to get.- Returns:
- the entities that were found.
-
queryByExample
public <T> DatastoreResultsIterable<T> queryByExample(org.springframework.data.domain.Example<T> example, DatastoreQueryOptions queryOptions) Description copied from interface:DatastoreOperations
Run query by example.- Specified by:
queryByExample
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of probe and resulted entities- Parameters:
example
- the examplequeryOptions
- the query options- Returns:
- query results, converted to objects of class T
-
keyQueryByExample
public <T> Iterable<com.google.cloud.datastore.Key> keyQueryByExample(org.springframework.data.domain.Example<T> example, DatastoreQueryOptions queryOptions) Description copied from interface:DatastoreOperations
Run key query by example.- Specified by:
keyQueryByExample
in interfaceDatastoreOperations
- Type Parameters:
T
- the type of probe- Parameters:
example
- the examplequeryOptions
- the query options- Returns:
- result keys
-
findAll
public <T> DatastoreResultsCollection<T> findAll(Class<T> entityClass, DatastoreQueryOptions queryOptions) Description copied from interface:DatastoreOperations
Get all the entities of the given domain type applying limit, offset and sort.- Specified by:
findAll
in interfaceDatastoreOperations
- Type Parameters:
T
- the type param of the domain type.- Parameters:
entityClass
- the domain type to get.queryOptions
- query options- Returns:
- the entities that were found.
-
applyQueryOptions
public static void applyQueryOptions(com.google.cloud.datastore.StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) -
existsById
Description copied from interface:DatastoreOperations
Check if the given ID belongs to an entity in Cloud Datastore. If this is actually aKey
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.- Specified by:
existsById
in interfaceDatastoreOperations
- Type Parameters:
T
- the type param of the domain type.- Parameters:
id
- the ID to search for.entityClass
- the domain type of the entities to search for.- Returns:
- true if the given ID refers to an existing entity. False otherwise.
-
performTransaction
Description copied from interface:DatastoreOperations
Performs multiple read and write operations in a single transaction.- Specified by:
performTransaction
in interfaceDatastoreOperations
- Type Parameters:
A
- the final return type of the operations.- Parameters:
operations
- the function that usesDatastoreOperations
to perform operations in a transaction.- Returns:
- the final result of the transaction.
-
findByIdAsMap
Description copied from interface:DatastoreOperations
Get a Datastore entity based on a id and convert it to a map.- Specified by:
findByIdAsMap
in interfaceDatastoreOperations
- Type Parameters:
T
- the value type of the map- Parameters:
key
- the key of the entityvalueType
- type values should be converted to- Returns:
- if an entity for a given key exists, returns the map representation of it,
null
otherwise
-
writeMap
Description copied from interface:DatastoreOperations
Save a map as a Datastore entity, using map keys as field names.- Specified by:
writeMap
in interfaceDatastoreOperations
- Type Parameters:
V
- the value type of the map to write- Parameters:
datastoreKey
- the key for the entitymap
- a map
-
createKey
Description copied from interface:DatastoreOperations
Create aKey
from kind name and id.- Specified by:
createKey
in interfaceDatastoreOperations
- Parameters:
kind
- the Cloud Datastore kind nameid
- object to be used as id; if it is a Long, the value is used, otherwise it isconverted to String- Returns:
- created key
-
createKey
Description copied from interface:DatastoreOperations
Create aKey
from entity class and id value.- Specified by:
createKey
in interfaceDatastoreOperations
- Parameters:
clazz
- the Cloud Datastore entity classid
- object to be used as id; if it is a Long, the value is used, otherwise it is converted to String- Returns:
- created key
-
keyToPathElement
public static com.google.cloud.datastore.PathElement keyToPathElement(com.google.cloud.datastore.Key key) -
convertEntitiesForRead
public <T> List<T> convertEntitiesForRead(Iterator<? extends com.google.cloud.datastore.BaseEntity> entities, Class<T> entityClass) Convert Datastore entities to objects of a specified type.- Type Parameters:
T
- the type the entities should be converted to.- Parameters:
entities
- the Datastore entitiesentityClass
- the type the entities should be converted to.- Returns:
- a list of converted entities
-
getKey
Description copied from interface:DatastoreOperations
Create aKey
from id property of an entity object.- Specified by:
getKey
in interfaceDatastoreOperations
- Parameters:
entity
- the Cloud Datastore entity object- Returns:
- key for the given entity
-