Interface DatastoreService (2.0.0)

public interface DatastoreService extends BaseDatastoreService

The DatastoreService provides synchronous access to a schema-less data storage system. The fundamental unit of data in this system is the Entity, which has an immutable identity (represented by a Key) and zero or more mutable properties. Entity objects can be created, updated, deleted, retrieved by identifier, and queried via a combination of properties.

The DatastoreService can be used transactionally and supports the notion of a "current" transaction. A current transaction is established by calling #beginTransaction(). The transaction returned by this method ceases to be current when an attempt is made to commit or rollback or when another call is made to #beginTransaction(). A transaction can only be current within the Thread that created it.

The various overloads of put, get, and delete all support transactions. Users of this class have the choice of explicitly passing a (potentially null) Transaction to these methods or relying on the behavior governed by the ImplicitTransactionManagementPolicy. If a user explicitly provides a Transaction it is up to the user to call Transaction#commit() or Transaction#rollback() at the proper time. If a user relies on implicit transaction management and the installed policy creates a transaction, that transaction will be committed (in the case of a success) or rolled back (in the case of a failure) before the operation returns to the user. The methods that manage transactions according to ImplicitTransactionManagementPolicy are: #delete(Key...), #delete(Iterable), #get(Key), #get(Iterable), #put(Entity), and #put(Iterable).

The overload of prepare that takes a Transaction parameter behaves the same as the overloads of put, get, and delete that take a Transaction parameter. However, the overload of prepare that does not take a Transaction parameter, unlike put, get, and delete, does not use an existing Transaction if one is already running and does not consult the ImplicitTransactionManagementPolicy if one is not already running.

Methods

allocateIdRange(KeyRange range)

public abstract DatastoreService.KeyRangeState allocateIdRange(KeyRange range)

This method allocates a user-specified contiguous range of unique IDs.

Once these IDs have been allocated they may be provided manually to newly created entities.

Since the datastore's automatic ID allocator will never assign a key to a new entity that will cause an existing entity to be overwritten, entities written to the given KeyRange will never be overwritten. However, writing entities with manually assigned keys in this range may overwrite existing entities (or new entities written by a separate request) depending on the KeyRangeState returned.

This method should only be used if you have an existing numeric id range that you want to reserve, e.g. bulk loading entities that already have IDs. If you don't care about which IDs you receive, use #allocateIds instead.

Parameter
Name Description
range KeyRange

The key range to allocate.

Returns
Type Description
DatastoreService.KeyRangeState

The state of the id range allocated.

allocateIds(Key parent, String kind, long num)

public abstract KeyRange allocateIds(Key parent, String kind, long num)

IDs are allocated within a namespace defined by a parent key and a kind. This method allocates a contiguous range of unique IDs of size num within the namespace defined by the given parent key and the given kind.

IDs allocated in this manner may be provided manually to newly created entities. They will not be used by the datastore's automatic ID allocator for entities with the same kind and parent.

Parameters
Name Description
parent Key

The key for which the child entity IDs should be allocated. Can be null.

kind String

The kind for which the child entity IDs should be allocated.

num long

The number of IDs to allocate.

Returns
Type Description
KeyRange

A range of IDs of size num that are guaranteed to be unique.

allocateIds(String kind, long num)

public abstract KeyRange allocateIds(String kind, long num)

IDs are allocated within a namespace defined by a parent key and a kind. This method allocates a contiguous range of unique IDs of size num within the namespace defined by a null parent key (root entities) and the given kind.

IDs allocated in this manner may be provided manually to newly created entities. They will not be used by the datastore's automatic ID allocator for root entities of the same kind.

Parameters
Name Description
kind String

The kind for which the root entity IDs should be allocated.

num long

The number of IDs to allocate.

Returns
Type Description
KeyRange

A KeyRange of size num.

beginTransaction()

public abstract Transaction beginTransaction()

Equivalent to beginTransaction(TransactionOptions.Builder.withDefaults()). See Also: #beginTransaction(TransactionOptions)

Returns
Type Description
Transaction

the Transaction that was started.

beginTransaction(TransactionOptions options)

public abstract Transaction beginTransaction(TransactionOptions options)

Begins a transaction against the datastore. Callers are responsible for explicitly calling Transaction#commit() or Transaction#rollback() when they no longer need the Transaction.

The Transaction returned by this call will be considered the current transaction and will be returned by subsequent, same-thread calls to #getCurrentTransaction() and #getCurrentTransaction(Transaction) until one of the following happens: 1) #beginTransaction() is invoked from the same thread. In this case #getCurrentTransaction() and #getCurrentTransaction(Transaction) will return the result of the more recent call to #beginTransaction(). 2) Transaction#commit() is invoked on the Transaction returned by this method. Whether or not the commit returns successfully, the Transaction will no longer be the current transaction. 3) Transaction#rollback() ()} is invoked on the Transaction returned by this method. Whether or not the rollback returns successfully, the Transaction will no longer be the current transaction. See Also: TransactionOptions, #getCurrentTransaction()

Parameter
Name Description
options TransactionOptions

The options for the new transaction.

Returns
Type Description
Transaction

the Transaction that was started.

delete(Key[] keys)

public abstract void delete(Key[] keys)

Deletes the Entity entities specified by keys.

If there is a current transaction, this operation will execute within that transaction. In this case it is up to the caller to commit or rollback. If there is no current transaction, the behavior of this method with respect to transactions will be determined by the ImplicitTransactionManagementPolicy available on the DatastoreServiceConfig.

Parameter
Name Description
keys Key[]

delete(Transaction txn, Key[] keys)

public abstract void delete(Transaction txn, Key[] keys)

Exhibits the same behavior as #delete(Key...), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.

Parameters
Name Description
txn Transaction
keys Key[]

delete(Transaction txn, Iterable<Key> keys)

public abstract void delete(Transaction txn, Iterable<Key> keys)

Exhibits the same behavior as #delete(Iterable), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.

Parameters
Name Description
txn Transaction
keys Iterable<Key>

delete(Iterable<Key> keys)

public abstract void delete(Iterable<Key> keys)

Equivalent to #delete(Key...).

Parameter
Name Description
keys Iterable<Key>

get(Key key)

public abstract Entity get(Key key)

Retrieves the Entity with the specified Key.

If there is a current transaction, this operation will execute within that transaction. In this case it is up to the caller to commit or rollback. If there is no current transaction, the behavior of this method with respect to transactions will be determined by the ImplicitTransactionManagementPolicy available on the DatastoreServiceConfig.

Parameter
Name Description
key Key
Returns
Type Description
Entity
Exceptions
Type Description
EntityNotFoundException

If the specified entity could not be found.

get(Transaction txn, Key key)

public abstract Entity get(Transaction txn, Key key)

Exhibits the same behavior as #get(Key), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.

Parameters
Name Description
txn Transaction
key Key
Returns
Type Description
Entity
Exceptions
Type Description
EntityNotFoundException

If txn is not null and not active.

get(Transaction txn, Iterable<Key> keys)

public abstract Map<Key,Entity> get(Transaction txn, Iterable<Key> keys)

Exhibits the same behavior as #get(Iterable), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.

Parameters
Name Description
txn Transaction
keys Iterable<Key>
Returns
Type Description
Map<Key,Entity>

get(Iterable<Key> keys)

public abstract Map<Key,Entity> get(Iterable<Key> keys)

Retrieves the set of Entities matching keys. The result Map will only contain Keys for which Entities could be found.

If there is a current transaction, this operation will execute within that transaction. In this case it is up to the caller to commit or rollback. If there is no current transaction, the behavior of this method with respect to transactions will be determined by the ImplicitTransactionManagementPolicy available on the DatastoreServiceConfig.

Parameter
Name Description
keys Iterable<Key>
Returns
Type Description
Map<Key,Entity>

getDatastoreAttributes()

public abstract DatastoreAttributes getDatastoreAttributes()

Retrieves the current datastore's attributes.

Returns
Type Description
DatastoreAttributes

The attributes of the datastore used to fulfill requests.

getIndexes()

public abstract Map<Index,Index.IndexState> getIndexes()

Returns the application indexes and their states.

Returns
Type Description
Map<Index,IndexState>

put(Entity entity)

public abstract Key put(Entity entity)

If the specified Entity does not yet exist in the data store, create it and assign its Key. If the specified Entity already exists in the data store, save the new version.

The Key is returned, and is also returned by future calls to entity.getKey().

If there is a current transaction, this operation will execute within that transaction. In this case it is up to the caller to commit or rollback. If there is no current transaction, the behavior of this method with respect to transactions will be determined by the ImplicitTransactionManagementPolicy available on the DatastoreServiceConfig.

Parameter
Name Description
entity Entity
Returns
Type Description
Key

put(Transaction txn, Entity entity)

public abstract Key put(Transaction txn, Entity entity)

Exhibits the same behavior as #put(Entity), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.

Parameters
Name Description
txn Transaction
entity Entity
Returns
Type Description
Key

put(Transaction txn, Iterable<Entity> entities)

public abstract List<Key> put(Transaction txn, Iterable<Entity> entities)

Exhibits the same behavior as #put(Iterable), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.

Parameters
Name Description
txn Transaction
entities Iterable<Entity>
Returns
Type Description
List<Key>

The Keys that were assigned to the entities that were put. If the Iterable that was provided as an argument has a stable iteration order the Keys in the List we return are in that same order. If the Iterable that was provided as an argument does not have a stable iteration order the order of the Keys in the List we return is undefined.

put(Iterable<Entity> entities)

public abstract List<Key> put(Iterable<Entity> entities)

Performs a batch put of all entities.

If there is a current transaction, this operation will execute within that transaction. In this case it is up to the caller to commit or rollback. If there is no current transaction, the behavior of this method with respect to transactions will be determined by the ImplicitTransactionManagementPolicy available on the DatastoreServiceConfig.

Parameter
Name Description
entities Iterable<Entity>
Returns
Type Description
List<Key>

The Keys that were assigned to the entities that were put. If the Iterable that was provided as an argument has a stable iteration order the Keys in the List we return are in that same order. If the Iterable that was provided as an argument does not have a stable iteration order the order of the Keys in the List we return is undefined.