Interface DatastoreOperations

All Known Implementing Classes:
DatastoreTemplate

public interface DatastoreOperations
An interface of operations that can be done with Cloud Datastore.
Since:
1.1
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    count(Class<?> entityClass)
    Count all occurrences of entities of the given domain type.
    com.google.cloud.datastore.Key
    createKey(Class clazz, Object id)
    Create a Key from entity class and id value.
    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> Iterable<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> Iterable<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.
    Get the DatastoreEntityConverter used by this template.
    com.google.cloud.datastore.Key
    getKey(Object entity)
    Create a Key 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>
    insertAll(Iterable<T> entities, com.google.cloud.datastore.Key... ancestors)
    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.
    <A> A
    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> Iterable<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.
    <T> Iterable<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.
    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.
    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>
    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 key, Map<String,V> map)
    Save a map as a Datastore entity, using map keys as field names.
  • Method Details

    • findById

      <T> T findById(Object id, Class<T> entityClass)
      Get an entity based on a id.
      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

      <T> T save(T instance, com.google.cloud.datastore.Key... ancestors)
      Saves an instance of an object to Cloud Datastore. Behaves as update or insert. Ancestors can be added only to entries with Key ids.
      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

      <T> Iterable<T> saveAll(Iterable<T> entities, com.google.cloud.datastore.Key... ancestors)
      Saves multiple instances of objects to Cloud Datastore. Behaves as update or insert. Ancestors can be added only to entries with Key ids.
      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

      <T> T insert(T instance, com.google.cloud.datastore.Key... ancestors)
      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.
      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.
      Throws:
      com.google.cloud.datastore.DatastoreException - If the entity already exists
    • insertAll

      <T> Iterable<T> insertAll(Iterable<T> entities, com.google.cloud.datastore.Key... ancestors)
      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.
      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.
      Throws:
      com.google.cloud.datastore.DatastoreException - If any entity already exists
    • deleteById

      <T> void deleteById(Object id, Class<T> entityClass)
      Delete an entity from Cloud Datastore. Deleting IDs that do not exist in Cloud Datastore will result in no operation.
      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

      <T> void deleteAllById(Iterable<?> ids, Class<T> entityClass)
      Delete multiple IDs from Cloud Datastore. Deleting IDs that do not exist in Cloud Datastore will result in no operation.
      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

      <T> void delete(T entity)
      Delete an entity from Cloud Datastore. Deleting entities that don't exist in Cloud Datastore will result in no operation.
      Type Parameters:
      T - the entity type
      Parameters:
      entity - the entity to delete.
    • deleteAll

      <T> void deleteAll(Iterable<T> entities)
      Deletes multiple entities from Cloud Datastore. Deleting entities that don't exist in Cloud Datastore will result in no operation.
      Type Parameters:
      T - the entity type.
      Parameters:
      entities - the entities to delete.
    • deleteAll

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

      long count(Class<?> entityClass)
      Count all occurrences of entities of the given domain type.
      Parameters:
      entityClass - the domain type to count.
      Returns:
      the number of entities of the given type.
    • findAllById

      <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> entityClass)
      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.
      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

      <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.
      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
    • query

      <A, T> Iterable<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.
      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

      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.
      Parameters:
      query - the query to execute that retrieves only Keys.
      Returns:
      the list of keys found.
    • findAll

      <T> Iterable<T> findAll(Class<T> entityClass)
      Get all the entities of the given domain type.
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      entityClass - the domain type to get.
      Returns:
      the entities that were found.
    • findAll

      <T> Collection<T> findAll(Class<T> entityClass, DatastoreQueryOptions queryOptions)
      Get all the entities of the given domain type applying limit, offset and sort.
      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.
    • existsById

      <T> boolean existsById(Object id, Class<T> entityClass)
      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.
      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

      <A> A performTransaction(Function<DatastoreOperations,A> operations)
      Performs multiple read and write operations in a single transaction.
      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

      <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.
      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

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

      com.google.cloud.datastore.Key createKey(String kind, Object id)
      Create a Key from kind name and id.
      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

      com.google.cloud.datastore.Key createKey(Class clazz, Object id)
      Create a Key from entity class and id value.
      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
    • getKey

      com.google.cloud.datastore.Key getKey(Object entity)
      Create a Key from id property of an entity object.
      Parameters:
      entity - the Cloud Datastore entity object
      Returns:
      key for the given entity
    • queryByExample

      <T> Iterable<T> queryByExample(org.springframework.data.domain.Example<T> example, DatastoreQueryOptions queryOptions)
      Run query by example.
      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

      <T> Iterable<com.google.cloud.datastore.Key> keyQueryByExample(org.springframework.data.domain.Example<T> example, DatastoreQueryOptions queryOptions)
      Run key query by example.
      Type Parameters:
      T - the type of probe
      Parameters:
      example - the example
      queryOptions - the query options
      Returns:
      result keys
    • getDatastoreEntityConverter

      DatastoreEntityConverter getDatastoreEntityConverter()
      Get the DatastoreEntityConverter used by this template.
      Returns:
      the converter.
    • queryKeysSlice

      <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. Resulting Slice can be used to get a Pageable for the next page or to determine if the next page exists.
      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

      <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. Resulting Slice can be used to get a Pageable for the next page or to determine if the next page exists.
      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

      <T> DatastoreResultsIterable<?> queryKeysOrEntities(com.google.cloud.datastore.Query query, Class<T> entityClass)
      Finds objects by using a Cloud Datastore query. If the query is a key-query, then keys are returned.
      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
    • queryIterable

      <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.
      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.