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
ConstructorsConstructorDescriptionDatastoreTemplate(Supplier<? extends com.google.cloud.datastore.DatastoreReaderWriter> datastore, DatastoreEntityConverter datastoreEntityConverter, DatastoreMappingContext datastoreMappingContext, ObjectToKeyFactory objectToKeyFactory) -
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplyQueryOptions(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.longCount all occurrences of entities of the given domain type.com.google.cloud.datastore.KeyCreate aKeyfrom entity class and id value.com.google.cloud.datastore.KeyCreate aKeyfrom kind name and id.<T> voiddelete(T entity) Delete an entity from Cloud Datastore.longDelete all entities of a given domain type.<T> voidDeletes multiple entities from Cloud Datastore.<T> voiddeleteAllById(Iterable<?> ids, Class<T> entityClass) Delete multiple IDs from Cloud Datastore.<T> voiddeleteById(Object id, Class<T> entityClass) Delete an entity from Cloud Datastore.<T> booleanexistsById(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> TGet 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 theDatastoreEntityConverterused by this template.com.google.cloud.datastore.KeyCreate aKeyfrom id property of an entity object.<T> Tinsert(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.PathElementkeyToPathElement(com.google.cloud.datastore.Key key) <A> AperformTransaction(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> Tsave(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.voidsetApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) <V> voidSave 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:DatastoreOperationsGet theDatastoreEntityConverterused by this template.- Specified by:
getDatastoreEntityConverterin interfaceDatastoreOperations- Returns:
- the converter.
-
setApplicationEventPublisher
public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) - Specified by:
setApplicationEventPublisherin interfaceorg.springframework.context.ApplicationEventPublisherAware
-
findById
Description copied from interface:DatastoreOperationsGet an entity based on a id.- Specified by:
findByIdin interfaceDatastoreOperations- Type Parameters:
T- the class type of the entity.- Parameters:
id- the id of the entity. If this is actually aKeythen 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:DatastoreOperationsSaves 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:
savein 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:DatastoreOperationsSaves multiple instances of objects to Cloud Datastore. Behaves as update or insert. Ancestors can be added only to entries with Key ids.- Specified by:
saveAllin 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:DatastoreOperationsInserts 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:
insertin 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:DatastoreOperationsSaves 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:
insertAllin 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:DatastoreOperationsDelete an entity from Cloud Datastore. Deleting IDs that do not exist in Cloud Datastore will result in no operation.- Specified by:
deleteByIdin interfaceDatastoreOperations- Type Parameters:
T- ths entity type- Parameters:
id- the ID of the entity to delete. If this is actually aKeythen 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:DatastoreOperationsDelete multiple IDs from Cloud Datastore. Deleting IDs that do not exist in Cloud Datastore will result in no operation.- Specified by:
deleteAllByIdin interfaceDatastoreOperations- Type Parameters:
T- ths entity type- Parameters:
ids- the IDs to delete. If any of these is actually aKeythen 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:DatastoreOperationsDelete an entity from Cloud Datastore. Deleting entities that don't exist in Cloud Datastore will result in no operation.- Specified by:
deletein interfaceDatastoreOperations- Type Parameters:
T- the entity type- Parameters:
entity- the entity to delete.
-
deleteAll
Description copied from interface:DatastoreOperationsDeletes multiple entities from Cloud Datastore. Deleting entities that don't exist in Cloud Datastore will result in no operation.- Specified by:
deleteAllin interfaceDatastoreOperations- Type Parameters:
T- the entity type.- Parameters:
entities- the entities to delete.
-
deleteAll
Description copied from interface:DatastoreOperationsDelete all entities of a given domain type.- Specified by:
deleteAllin interfaceDatastoreOperations- Parameters:
entityClass- the domain type to delete from Cloud Datastore.- Returns:
- the number of entities that were deleted.
-
count
Description copied from interface:DatastoreOperationsCount all occurrences of entities of the given domain type.- Specified by:
countin interfaceDatastoreOperations- Parameters:
entityClass- the domain type to count.- Returns:
- the number of entities of the given type.
-
findAllById
Description copied from interface:DatastoreOperationsFind all the entities of the given IDs. If an ID is actually aKeythen 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:
findAllByIdin 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:DatastoreOperationsFinds objects by using a Cloud Datastore query.- Specified by:
queryin 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:DatastoreOperationsFinds 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:
queryKeysSlicein 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:DatastoreOperationsFinds 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:
queryEntitiesSlicein 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:DatastoreOperationsFinds objects by using a Cloud Datastore query. If the query is a key-query, then keys are returned.- Specified by:
queryKeysOrEntitiesin 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:DatastoreOperationsRuns given query and applies given function to each entity in the result.- Specified by:
queryin interfaceDatastoreOperations- Type Parameters:
A- the row type of the query. This type can be eitherKeyor 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:DatastoreOperationsRuns given query and applies given function to each entity in the result.- Specified by:
queryIterablein interfaceDatastoreOperations- Type Parameters:
A- the row type of the query. This type can be eitherKeyor 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:DatastoreOperationsFinds Cloud Datastore Keys by using a Cloud Datastore query.- Specified by:
queryKeysin interfaceDatastoreOperations- Parameters:
query- the query to execute that retrieves only Keys.- Returns:
- the list of keys found.
-
findAll
Description copied from interface:DatastoreOperationsGet all the entities of the given domain type.- Specified by:
findAllin 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:DatastoreOperationsRun query by example.- Specified by:
queryByExamplein 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:DatastoreOperationsRun key query by example.- Specified by:
keyQueryByExamplein 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:DatastoreOperationsGet all the entities of the given domain type applying limit, offset and sort.- Specified by:
findAllin 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:DatastoreOperationsCheck if the given ID belongs to an entity in Cloud Datastore. If this is actually aKeythen 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:
existsByIdin 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:DatastoreOperationsPerforms multiple read and write operations in a single transaction.- Specified by:
performTransactionin interfaceDatastoreOperations- Type Parameters:
A- the final return type of the operations.- Parameters:
operations- the function that usesDatastoreOperationsto perform operations in a transaction.- Returns:
- the final result of the transaction.
-
findByIdAsMap
Description copied from interface:DatastoreOperationsGet a Datastore entity based on a id and convert it to a map.- Specified by:
findByIdAsMapin 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,
nullotherwise
-
writeMap
Description copied from interface:DatastoreOperationsSave a map as a Datastore entity, using map keys as field names.- Specified by:
writeMapin 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:DatastoreOperationsCreate aKeyfrom kind name and id.- Specified by:
createKeyin 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:DatastoreOperationsCreate aKeyfrom entity class and id value.- Specified by:
createKeyin 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:DatastoreOperationsCreate aKeyfrom id property of an entity object.- Specified by:
getKeyin interfaceDatastoreOperations- Parameters:
entity- the Cloud Datastore entity object- Returns:
- key for the given entity
-