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 Details

  • Method Details

    • getDatastoreEntityConverter

      public DatastoreEntityConverter getDatastoreEntityConverter()
      Description copied from interface: DatastoreOperations
      Get the DatastoreEntityConverter used by this template.
      Specified by:
      getDatastoreEntityConverter in interface DatastoreOperations
      Returns:
      the converter.
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
      Specified by:
      setApplicationEventPublisher in interface org.springframework.context.ApplicationEventPublisherAware
    • findById

      public <T> T findById(Object id, Class<T> entityClass)
      Description copied from interface: DatastoreOperations
      Get an entity based on a id.
      Specified by:
      findById in interface DatastoreOperations
      Type Parameters:
      T - the class type of the entity.
      Parameters:
      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.
      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 interface DatastoreOperations
      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

      public <T> Iterable<T> saveAll(Iterable<T> entities, com.google.cloud.datastore.Key... ancestors)
      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 interface DatastoreOperations
      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 interface DatastoreOperations
      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

      public <T> Iterable<T> insertAll(Iterable<T> entities, com.google.cloud.datastore.Key... ancestors)
      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 interface DatastoreOperations
      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

      public <T> void deleteById(Object id, Class<T> entityClass)
      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 interface DatastoreOperations
      Type Parameters:
      T - ths entity type
      Parameters:
      id - 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 the
    • deleteAllById

      public <T> void deleteAllById(Iterable<?> ids, Class<T> entityClass)
      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 interface DatastoreOperations
      Type Parameters:
      T - ths entity type
      Parameters:
      ids - 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 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 interface DatastoreOperations
      Type Parameters:
      T - the entity type
      Parameters:
      entity - the entity to delete.
    • deleteAll

      public <T> void deleteAll(Iterable<T> entities)
      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 interface DatastoreOperations
      Type Parameters:
      T - the entity type.
      Parameters:
      entities - the entities to delete.
    • deleteAll

      public long deleteAll(Class<?> entityClass)
      Description copied from interface: DatastoreOperations
      Delete all entities of a given domain type.
      Specified by:
      deleteAll in interface DatastoreOperations
      Parameters:
      entityClass - the domain type to delete from Cloud Datastore.
      Returns:
      the number of entities that were deleted.
    • count

      public long count(Class<?> entityClass)
      Description copied from interface: DatastoreOperations
      Count all occurrences of entities of the given domain type.
      Specified by:
      count in interface DatastoreOperations
      Parameters:
      entityClass - the domain type to count.
      Returns:
      the number of entities of the given type.
    • findAllById

      public <T> Collection<T> findAllById(Iterable<?> ids, Class<T> entityClass)
      Description copied from interface: DatastoreOperations
      Find all the entities of the given IDs. If an ID 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.
      Specified by:
      findAllById in interface DatastoreOperations
      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 interface DatastoreOperations
      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 interface DatastoreOperations
      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 interface DatastoreOperations
      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 interface DatastoreOperations
      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

      public <A, T> List<T> query(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:
      query in interface DatastoreOperations
      Type Parameters:
      A - 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.
      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 interface DatastoreOperations
      Type Parameters:
      A - 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.
      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 interface DatastoreOperations
      Parameters:
      query - the query to execute that retrieves only Keys.
      Returns:
      the list of keys found.
    • findAll

      public <T> Collection<T> findAll(Class<T> entityClass)
      Description copied from interface: DatastoreOperations
      Get all the entities of the given domain type.
      Specified by:
      findAll in interface DatastoreOperations
      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 interface DatastoreOperations
      Type Parameters:
      T - the type of probe and resulted entities
      Parameters:
      example - the example
      queryOptions - 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 interface DatastoreOperations
      Type Parameters:
      T - the type of probe
      Parameters:
      example - the example
      queryOptions - 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 interface DatastoreOperations
      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

      public <T> boolean existsById(Object id, Class<T> entityClass)
      Description copied from interface: DatastoreOperations
      Check if the given ID belongs to an entity in Cloud Datastore. 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.
      Specified by:
      existsById in interface DatastoreOperations
      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

      public <A> A performTransaction(Function<DatastoreOperations,A> operations)
      Description copied from interface: DatastoreOperations
      Performs multiple read and write operations in a single transaction.
      Specified by:
      performTransaction in interface DatastoreOperations
      Type Parameters:
      A - the final return type of the operations.
      Parameters:
      operations - the function that uses DatastoreOperations to perform operations in a transaction.
      Returns:
      the final result of the transaction.
    • findByIdAsMap

      public <T> Map<String,T> findByIdAsMap(com.google.cloud.datastore.Key key, Class<T> valueType)
      Description copied from interface: DatastoreOperations
      Get a Datastore entity based on a id and convert it to a map.
      Specified by:
      findByIdAsMap in interface DatastoreOperations
      Type Parameters:
      T - the value type of the map
      Parameters:
      key - the key of the entity
      valueType - type values should be converted to
      Returns:
      if an entity for a given key exists, returns the map representation of it, null otherwise
    • writeMap

      public <V> void writeMap(com.google.cloud.datastore.Key datastoreKey, Map<String,V> map)
      Description copied from interface: DatastoreOperations
      Save a map as a Datastore entity, using map keys as field names.
      Specified by:
      writeMap in interface DatastoreOperations
      Type Parameters:
      V - the value type of the map to write
      Parameters:
      datastoreKey - the key for the entity
      map - a map
    • createKey

      public com.google.cloud.datastore.Key createKey(String kind, Object id)
      Description copied from interface: DatastoreOperations
      Create a Key from kind name and id.
      Specified by:
      createKey in interface DatastoreOperations
      Parameters:
      kind - the Cloud Datastore kind name
      id - object to be used as id; if it is a Long, the value is used, otherwise it isconverted to String
      Returns:
      created key
    • createKey

      public com.google.cloud.datastore.Key createKey(Class clazz, Object id)
      Description copied from interface: DatastoreOperations
      Create a Key from entity class and id value.
      Specified by:
      createKey in interface DatastoreOperations
      Parameters:
      clazz - the Cloud Datastore entity class
      id - 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 entities
      entityClass - the type the entities should be converted to.
      Returns:
      a list of converted entities
    • getKey

      public com.google.cloud.datastore.Key getKey(Object entity)
      Description copied from interface: DatastoreOperations
      Create a Key from id property of an entity object.
      Specified by:
      getKey in interface DatastoreOperations
      Parameters:
      entity - the Cloud Datastore entity object
      Returns:
      key for the given entity