public interface Datastore extends Service<DatastoreOptions>, DatastoreReaderWriter
Modifier and Type | Interface and Description |
---|---|
static interface |
Datastore.TransactionCallable<T>
A callback for running with a transactional
DatastoreReaderWriter . |
Modifier and Type | Method and Description |
---|---|
List<Entity> |
add(FullEntity<?>... entities)
Datastore add operation: inserts the provided entities.
|
Entity |
add(FullEntity<?> entity)
Datastore add operation: inserts the provided entity.
|
List<Key> |
allocateId(IncompleteKey... keys)
Returns a list of keys using the allocated ids ordered by the input.
|
Key |
allocateId(IncompleteKey key)
Allocate a unique id for the given key.
|
void |
delete(Key... keys)
A datastore delete operation.
|
List<Entity> |
fetch(Iterable<Key> keys,
ReadOption... options)
Returns a list with a value for each given key (ordered by input).
|
Iterator<Entity> |
get(Iterable<Key> keys,
ReadOption... options)
|
Entity |
get(Key key,
ReadOption... options)
|
Batch |
newBatch()
Returns a new Batch for processing multiple write operations in one request.
|
KeyFactory |
newKeyFactory()
Returns a new KeyFactory for this service
|
Transaction |
newTransaction()
Returns a new Datastore transaction.
|
Transaction |
newTransaction(com.google.datastore.v1.TransactionOptions options)
Returns a new Datastore transaction.
|
List<Entity> |
put(FullEntity<?>... entities)
A Datastore put (a.k.a upsert) operation: creates an entity if it does not exist, updates it
otherwise.
|
Entity |
put(FullEntity<?> entity)
A Datastore put (a.k.a upsert) operation: inserts an entity if it does not exist, updates it
otherwise.
|
<T> QueryResults<T> |
run(Query<T> query,
ReadOption... options)
Submits a
Query and returns its result. |
<T> T |
runInTransaction(Datastore.TransactionCallable<T> callable)
Invokes the callback's
Datastore.TransactionCallable.run(com.google.cloud.datastore.DatastoreReaderWriter) method with a DatastoreReaderWriter that is associated with a new transaction. |
<T> T |
runInTransaction(Datastore.TransactionCallable<T> callable,
com.google.datastore.v1.TransactionOptions options)
Invokes the callback's
Datastore.TransactionCallable.run(com.google.cloud.datastore.DatastoreReaderWriter) method with a DatastoreReaderWriter that is associated with a new transaction. |
void |
update(Entity... entities)
A Datastore update operation.
|
getOptions
fetch, get, get, run
Transaction newTransaction(com.google.datastore.v1.TransactionOptions options)
options
- a transaction option indicating the mode of the transaction (read-only or
read-write)DatastoreException
- upon failureTransaction newTransaction()
DatastoreException
- upon failure<T> T runInTransaction(Datastore.TransactionCallable<T> callable)
Datastore.TransactionCallable.run(com.google.cloud.datastore.DatastoreReaderWriter)
method with a DatastoreReaderWriter
that is associated with a new transaction. The transaction will be
committed upon successful invocation. Any thrown exception will cause the transaction to
rollback and will be propagated as a DatastoreException
with the original exception as
its root cause.
Example of running in a transaction.
String callableResult = "my_callable_result";
TransactionCallable<String> callable = new TransactionCallable<String>() {
public String run(DatastoreReaderWriter readerWriter) {
// use readerWriter to run in transaction
return callableResult;
}
};
String result = datastore.runInTransaction(callable);
callable
- the callback to call with a newly created transactional readerWriterDatastoreException
- upon failure<T> T runInTransaction(Datastore.TransactionCallable<T> callable, com.google.datastore.v1.TransactionOptions options)
Datastore.TransactionCallable.run(com.google.cloud.datastore.DatastoreReaderWriter)
method with a DatastoreReaderWriter
that is associated with a new transaction. The transaction will be
committed upon successful invocation. Any thrown exception will cause the transaction to
rollback and will be propagated as a DatastoreException
with the original exception as
its root cause. If TransactionOptions
is set to read-write mode, previous transaction
Id in the options will be automatically populated each time a transaction is retried.
Example of running in a transaction.
String callableResult = "my_callable_result";
TransactionCallable<String> callable = new TransactionCallable<String>() {
public String run(DatastoreReaderWriter readerWriter) {
// use readerWriter to run in transaction
return callableResult;
}
};
TransactionOptions options = TransactionOptions.newBuilder()
.setReadWrite(TransactionOptions.ReadWrite
.getDefaultInstance())
.build();
String result = datastore.runInTransaction(callable, options);
callable
- the callback to call with a newly created transactional readerWriteroptions
- the Transaction options indicating whether the transaction mode is Read-only or
Read-WriteDatastoreException
- upon failureBatch newBatch()
Example of starting a new batch.
String keyName1 = "my_key_name_1";
String keyName2 = "my_key_name_2";
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
Batch batch = datastore.newBatch();
Entity entity1 = Entity.newBuilder(key1).set("name", "John").build();
Entity entity2 = Entity.newBuilder(key2).set("title", "title").build();
batch.add(entity1);
batch.add(entity2);
batch.submit();
Key allocateId(IncompleteKey key)
Example of allocating an id.
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
IncompleteKey incompleteKey = keyFactory.newKey();
// let cloud datastore automatically assign an id
Key key = datastore.allocateId(incompleteKey);
DatastoreException
- upon failureList<Key> allocateId(IncompleteKey... keys)
Example of allocating multiple ids in a single batch.
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
IncompleteKey incompleteKey1 = keyFactory.newKey();
IncompleteKey incompleteKey2 = keyFactory.newKey();
// let cloud datastore automatically assign the ids
List<Key> keys = datastore.allocateId(incompleteKey1, incompleteKey2);
DatastoreException
- upon failureallocateId(IncompleteKey)
Entity add(FullEntity<?> entity)
If an entity for entity.getKey()
does not exist, entity
is inserted.
Otherwise, a DatastoreException
is thrown with BaseServiceException.getReason()
equal to "ALREADY_EXISTS"
.
Example of adding a single entity.
String keyName = "my_key_name";
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity.Builder entityBuilder = Entity.newBuilder(key);
entityBuilder.set("propertyName", "value");
Entity entity = entityBuilder.build();
try {
datastore.add(entity);
} catch (DatastoreException ex) {
if ("ALREADY_EXISTS".equals(ex.getReason())) {
// entity.getKey() already exists
}
}
add
in interface DatastoreWriter
entity
- the entity to addEntity
with the same properties and a key that is either newly allocated or
the same one if key is already completeDatastoreException
- upon failure or if an entity for entity.getKey()
already
existsList<Entity> add(FullEntity<?>... entities)
If none of entities' keys exist, all entities are inserted. If any of entities' keys already
exists the method throws a DatastoreException
with BaseServiceException.getReason()
equal to "ALREADY_EXISTS"
. All entities in entities
whose key did not exist are inserted. To achieve a transactional behavior, use Transaction
.
Example of adding multiple entities.
String keyName1 = "my_key_name1";
String keyName2 = "my_key_name2";
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
entityBuilder1.set("propertyName", "value1");
Entity entity1 = entityBuilder1.build();
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
entityBuilder2.set("propertyName", "value2");
Entity entity2 = entityBuilder2.build();
try {
datastore.add(entity1, entity2);
} catch (DatastoreException ex) {
if ("ALREADY_EXISTS".equals(ex.getReason())) {
// at least one of entity1.getKey() and entity2.getKey() already exists
}
}
add
in interface DatastoreWriter
Entity
ordered by input with the same properties and a key that is
either newly allocated or the same one if was already completeDatastoreException
- upon failure or if any of entities' keys already existsDatastoreWriter.add(FullEntity)
void update(Entity... entities)
Example of updating multiple entities.
String keyName1 = "my_key_name_1";
String keyName2 = "my_key_name_2";
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
entityBuilder1.set("propertyName", "updatedValue1");
Entity entity1 = entityBuilder1.build();
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
entityBuilder2.set("propertyName", "updatedValue2");
Entity entity2 = entityBuilder2.build();
datastore.update(entity1, entity2);
update
in interface DatastoreWriter
DatastoreException
- upon failureEntity put(FullEntity<?> entity)
Example of putting a single entity.
String keyName = "my_key_name";
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity.Builder entityBuilder = Entity.newBuilder(key);
entityBuilder.set("propertyName", "value");
Entity entity = entityBuilder.build();
datastore.put(entity);
put
in interface DatastoreWriter
entity
- the entity to putEntity
with the same properties and a key that is either newly allocated or
the same one if key is already completeDatastoreException
- upon failureList<Entity> put(FullEntity<?>... entities)
Example of putting multiple entities.
String keyName1 = "my_key_name1";
String keyName2 = "my_key_name2";
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
entityBuilder1.set("propertyName", "value1");
Entity entity1 = entityBuilder1.build();
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
entityBuilder2.set("propertyName", "value2");
Entity entity2 = entityBuilder2.build();
datastore.put(entity1, entity2);
put
in interface DatastoreWriter
Entity
, ordered by input. Returned keys are
either newly allocated or the same one if was already complete.DatastoreException
- upon failurevoid delete(Key... keys)
Example of deleting multiple entities.
String keyName1 = "my_key_name1";
String keyName2 = "my_key_name2";
Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
datastore.delete(key1, key2);
delete
in interface DatastoreWriter
DatastoreException
- upon failureKeyFactory newKeyFactory()
Example of creating a KeyFactory
.
KeyFactory keyFactory = datastore.newKeyFactory();
Entity get(Key key, ReadOption... options)
Entity
for the given Key
or null
if it doesn't exist. ReadOption
s can be specified if desired.
Example of getting an entity.
String keyName = "my_key_name";
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity entity = datastore.get(key);
// Do something with the entity
DatastoreException
- upon failureIterator<Entity> get(Iterable<Key> keys, ReadOption... options)
Entity
for each given Key
that exists in the Datastore. The order of
the result is unspecified. Results are loaded lazily, so it is possible to get a DatastoreException
from the returned Iterator
's hasNext
or
next
methods. ReadOption
s can be specified if desired.
Example of getting multiple entity objects.
String firstKeyName = "my_first_key_name";
String secondKeyName = "my_second_key_name";
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
Iterator<Entity> entitiesIterator = datastore.get(Lists.newArrayList(firstKey, secondKey));
List<Entity> entities = Lists.newArrayList();
while (entitiesIterator.hasNext()) {
Entity entity = entitiesIterator.next();
// do something with the entity
entities.add(entity);
}
DatastoreException
- upon failureDatastoreReader.get(Key)
List<Entity> fetch(Iterable<Key> keys, ReadOption... options)
null
values are
returned for nonexistent keys. When possible prefer using DatastoreReader.get(Key...)
to avoid eagerly
loading the results. ReadOption
s can be specified if desired.
Example of fetching a list of Entity objects.
String firstKeyName = "my_first_key_name";
String secondKeyName = "my_second_key_name";
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
List<Entity> entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey));
for (Entity entity : entities) {
// do something with the entity
}
<T> QueryResults<T> run(Query<T> query, ReadOption... options)
Query
and returns its result. ReadOption
s can be specified if
desired.
Example of running a query to find all entities of one kind.
String kind = "my_kind";
StructuredQuery<Entity> query = Query.newEntityQueryBuilder()
.setKind(kind)
.build();
QueryResults<Entity> results = datastore.run(query);
List<Entity> entities = Lists.newArrayList();
while (results.hasNext()) {
Entity result = results.next();
// do something with result
entities.add(result);
}
Example of running a query to find all entities with a matching property value.
String kind = "my_kind";
String property = "my_property";
String value = "my_value";
StructuredQuery<Entity> query = Query.newEntityQueryBuilder()
.setKind(kind)
.setFilter(PropertyFilter.eq(property, value))
.build();
QueryResults<Entity> results = datastore.run(query);
List<Entity> entities = Lists.newArrayList();
while (results.hasNext()) {
Entity result = results.next();
// do something with result
entities.add(result);
}
DatastoreException
- upon failureCopyright © 2019 Google LLC. All rights reserved.