Interface DatastoreService
-
- All Superinterfaces:
- BaseDatastoreService
public interface DatastoreService extends BaseDatastoreService
TheDatastoreServiceprovides synchronous access to a schema-less data storage system. The fundamental unit of data in this system is theEntity, which has an immutable identity (represented by aKey) and zero or more mutable properties.Entityobjects can be created, updated, deleted, retrieved by identifier, and queried via a combination of properties.The
DatastoreServicecan be used transactionally and supports the notion of a "current" transaction. A current transaction is established by callingbeginTransaction(). 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 tobeginTransaction(). 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)Transactionto these methods or relying on the behavior governed by theImplicitTransactionManagementPolicy. If a user explicitly provides aTransactionit is up to the user to callTransaction.commit()orTransaction.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 toImplicitTransactionManagementPolicyare:delete(Key...),delete(Iterable),get(Key),get(Iterable),put(Entity), andput(Iterable).The overload of prepare that takes a
Transactionparameter behaves the same as the overloads of put, get, and delete that take aTransactionparameter. However, the overload of prepare that does not take aTransactionparameter, unlike put, get, and delete, does not use an existingTransactionif one is already running and does not consult theImplicitTransactionManagementPolicyif one is not already running.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface and Description static classDatastoreService.KeyRangeStateIndicates the state of aKeyRange.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method and Description DatastoreService.KeyRangeStateallocateIdRange(KeyRange range)This method allocates a user-specified contiguous range of unique IDs.KeyRangeallocateIds(Key parent, java.lang.String kind, long num)IDs are allocated within a namespace defined by a parent key and a kind.KeyRangeallocateIds(java.lang.String kind, long num)IDs are allocated within a namespace defined by a parent key and a kind.TransactionbeginTransaction()Equivalent tobeginTransaction(TransactionOptions.Builder.withDefaults()).TransactionbeginTransaction(TransactionOptions options)Begins a transaction against the datastore.voiddelete(java.lang.Iterable<Key> keys)Equivalent todelete(Key...).voiddelete(Key... keys)Deletes theEntity entitiesspecified bykeys.voiddelete(Transaction txn, java.lang.Iterable<Key> keys)Exhibits the same behavior asdelete(Iterable), but executes within the provided transaction.voiddelete(Transaction txn, Key... keys)Exhibits the same behavior asdelete(Key...), but executes within the provided transaction.java.util.Map<Key,Entity>get(java.lang.Iterable<Key> keys)Retrieves the set ofEntitiesmatchingkeys.Entityget(Key key)Retrieves theEntitywith the specifiedKey.java.util.Map<Key,Entity>get(Transaction txn, java.lang.Iterable<Key> keys)Exhibits the same behavior asget(Iterable), but executes within the provided transaction.Entityget(Transaction txn, Key key)Exhibits the same behavior asget(Key), but executes within the provided transaction.DatastoreAttributesgetDatastoreAttributes()Retrieves the current datastore's attributes.java.util.Map<Index,Index.IndexState>getIndexes()Returns the application indexes and their states.Keyput(Entity entity)If the specifiedEntitydoes not yet exist in the data store, create it and assign itsKey.java.util.List<Key>put(java.lang.Iterable<Entity> entities)Performs a batchputof allentities.Keyput(Transaction txn, Entity entity)Exhibits the same behavior asput(Entity), but executes within the provided transaction.java.util.List<Key>put(Transaction txn, java.lang.Iterable<Entity> entities)Exhibits the same behavior asput(Iterable), but executes within the provided transaction.-
Methods inherited from interface com.google.appengine.api.datastore.BaseDatastoreService
getActiveTransactions, getCurrentTransaction, getCurrentTransaction, prepare, prepare
-
-
-
-
Method Detail
-
get
Entity get(Key key) throws EntityNotFoundException
Retrieves theEntitywith the specifiedKey.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
ImplicitTransactionManagementPolicyavailable on theDatastoreServiceConfig.- Throws:
EntityNotFoundException- If the specified entity could not be found.java.lang.IllegalArgumentException- If the specified key is invalid.DatastoreFailureException- If any other datastore error occurs.
-
get
Entity get(Transaction txn, Key key) throws EntityNotFoundException
Exhibits the same behavior asget(Key), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.- Throws:
java.lang.IllegalStateException- Iftxnis not null and not active.EntityNotFoundException
-
get
java.util.Map<Key,Entity> get(java.lang.Iterable<Key> keys)
Retrieves the set ofEntitiesmatchingkeys. The resultMapwill only containKeysfor whichEntitiescould 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
ImplicitTransactionManagementPolicyavailable on theDatastoreServiceConfig.- Throws:
java.lang.IllegalArgumentException- If anyKeyin keys is invalid.DatastoreFailureException- If any other datastore error occurs.
-
get
java.util.Map<Key,Entity> get(Transaction txn, java.lang.Iterable<Key> keys)
Exhibits the same behavior asget(Iterable), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.- Throws:
java.lang.IllegalStateException- Iftxnis not null and not active.
-
put
Key put(Entity entity)
If the specifiedEntitydoes not yet exist in the data store, create it and assign itsKey. If the specifiedEntityalready exists in the data store, save the new version.The
Keyis returned, and is also returned by future calls toentity.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
ImplicitTransactionManagementPolicyavailable on theDatastoreServiceConfig.- Throws:
java.lang.IllegalArgumentException- If the specified entity was incomplete.java.util.ConcurrentModificationException- If the entity group to which the entity belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
put
Key put(Transaction txn, Entity entity)
Exhibits the same behavior asput(Entity), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.- Throws:
java.lang.IllegalStateException- Iftxnis not null and not active.java.util.ConcurrentModificationException- If the entity group to which the entity belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
put
java.util.List<Key> put(java.lang.Iterable<Entity> entities)
Performs a batchputof allentities.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
ImplicitTransactionManagementPolicyavailable on theDatastoreServiceConfig.- Returns:
- The
Keys that were assigned to the entities that were put. If theIterablethat was provided as an argument has a stable iteration order theKeys in theListwe return are in that same order. If theIterablethat was provided as an argument does not have a stable iteration order the order of theKeys in theListwe return is undefined. - Throws:
java.lang.IllegalArgumentException- If any entity is incomplete.java.util.ConcurrentModificationException- If an entity group to which any provided entity belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
put
java.util.List<Key> put(Transaction txn, java.lang.Iterable<Entity> entities)
Exhibits the same behavior asput(Iterable), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.- Returns:
- The
Keys that were assigned to the entities that were put. If theIterablethat was provided as an argument has a stable iteration order theKeys in theListwe return are in that same order. If theIterablethat was provided as an argument does not have a stable iteration order the order of theKeys in theListwe return is undefined. - Throws:
java.lang.IllegalStateException- Iftxnis not null and not active.java.util.ConcurrentModificationException- If an entity group to which any provided entity belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
delete
void delete(Key... keys)
Deletes theEntity entitiesspecified bykeys.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
ImplicitTransactionManagementPolicyavailable on theDatastoreServiceConfig.- Throws:
java.lang.IllegalArgumentException- If the specified key was invalid.java.util.ConcurrentModificationException- If an entity group to which any provided key belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
delete
void delete(Transaction txn, Key... keys)
Exhibits the same behavior asdelete(Key...), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.- Throws:
java.lang.IllegalStateException- Iftxnis not null and not active.java.util.ConcurrentModificationException- If an entity group to which any provided key belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
delete
void delete(java.lang.Iterable<Key> keys)
Equivalent todelete(Key...).- Throws:
java.util.ConcurrentModificationException- If an entity group to which any provided key belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
delete
void delete(Transaction txn, java.lang.Iterable<Key> keys)
Exhibits the same behavior asdelete(Iterable), but executes within the provided transaction. It is up to the caller to commit or rollback. Transaction can be null.- Throws:
java.lang.IllegalStateException- Iftxnis not null and not active.java.util.ConcurrentModificationException- If an entity group to which any provided key belongs was modified concurrently.DatastoreFailureException- If any other datastore error occurs.
-
beginTransaction
Transaction beginTransaction()
Equivalent tobeginTransaction(TransactionOptions.Builder.withDefaults()).- Returns:
- the
Transactionthat was started. - Throws:
DatastoreFailureException- If a datastore error occurs.- See Also:
beginTransaction(TransactionOptions)
-
beginTransaction
Transaction beginTransaction(TransactionOptions options)
Begins a transaction against the datastore. Callers are responsible for explicitly callingTransaction.commit()orTransaction.rollback()when they no longer need theTransaction.The
Transactionreturned by this call will be considered the current transaction and will be returned by subsequent, same-thread calls toBaseDatastoreService.getCurrentTransaction()andBaseDatastoreService.getCurrentTransaction(Transaction)until one of the following happens: 1)beginTransaction()is invoked from the same thread. In this caseBaseDatastoreService.getCurrentTransaction()andBaseDatastoreService.getCurrentTransaction(Transaction)will return the result of the more recent call tobeginTransaction(). 2)Transaction.commit()is invoked on theTransactionreturned by this method. Whether or not the commit returns successfully, theTransactionwill no longer be the current transaction. 3)Transaction.rollback()()} is invoked on theTransactionreturned by this method. Whether or not the rollback returns successfully, theTransactionwill no longer be the current transaction.- Parameters:
options- The options for the new transaction.- Returns:
- the
Transactionthat was started. - Throws:
DatastoreFailureException- If a datastore error occurs.- See Also:
BaseDatastoreService.getCurrentTransaction(),TransactionOptions
-
allocateIds
KeyRange allocateIds(java.lang.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 sizenumwithin 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:
kind- The kind for which the root entity IDs should be allocated.num- The number of IDs to allocate.- Returns:
- A
KeyRangeof sizenum. - Throws:
java.lang.IllegalArgumentException- Ifnumis less than 1 or ifnumis greater than 1 billion.DatastoreFailureException- If a datastore error occurs.
-
allocateIds
KeyRange allocateIds(Key parent, java.lang.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 sizenumwithin 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:
parent- The key for which the child entity IDs should be allocated. Can be null.kind- The kind for which the child entity IDs should be allocated.num- The number of IDs to allocate.- Returns:
- A range of IDs of size
numthat are guaranteed to be unique. - Throws:
java.lang.IllegalArgumentException- Ifparentis not a complete key, ifnumis less than 1, or ifnumis greater than 1 billion.DatastoreFailureException- If a datastore error occurs.
-
allocateIdRange
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
KeyRangewill 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 theDatastoreService.KeyRangeStatereturned.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(java.lang.String, long)instead.- Parameters:
range- The key range to allocate.- Returns:
- The state of the id range allocated.
- Throws:
DatastoreFailureException- If a datastore error occurs.
-
getDatastoreAttributes
DatastoreAttributes getDatastoreAttributes()
Retrieves the current datastore's attributes.- Returns:
- The attributes of the datastore used to fulfill requests.
- Throws:
DatastoreFailureException- If a datastore error occurs.
-
getIndexes
java.util.Map<Index,Index.IndexState> getIndexes()
Returns the application indexes and their states.
-
-