Class Batch (1.15.5)

Batch(client)

An abstraction representing a collected group of updates / deletes.

Used to build up a bulk mutation.

For example, the following snippet of code will put the two save operations and the delete operation into the same mutation, and send them to the server in a single API request::

from google.cloud import datastore client = datastore.Client() batch = client.batch() batch.begin() batch.put(entity1) batch.put(entity2) batch.delete(key3) batch.commit()

You can also use a batch as a context manager, in which case commit will be called automatically if its block exits without raising an exception::

with batch: ... batch.put(entity1) ... batch.put(entity2) ... batch.delete(key3)

By default, no updates will be sent if the block exits with an error::

with batch: ... do_some_work(batch) ... raise Exception() # rolls back

Parameter

NameDescription
client Client

The client used to connect to datastore.

Properties

mutations

Getter for the changes accumulated by this batch.

Every batch is committed with a single commit request containing all the work to be done as mutations. Inside a batch, calling put with an entity, or delete with a key, builds up the request by adding a new mutation. This getter returns the protobuf that has been built-up so far.

Returns
TypeDescription
iterableThe list of .datastore_pb2.Mutation protobufs to be sent in the commit request.

namespace

Getter for namespace in which the batch will run.

Returns
TypeDescription
strThe namespace in which the batch will run.

project

Getter for project in which the batch will run.

Returns
TypeDescription
strThe project in which the batch will run.

Methods

begin

begin()

Begins a batch.

This method is called automatically when entering a with statement, however it can be called explicitly if you don't want to use a context manager.

Overridden by xref_Transaction.

Exceptions
TypeDescription
`ValueErrorif the batch has already begun.

commit

commit(retry=None, timeout=None)

Commits the batch.

This is called automatically upon exiting a with statement, however it can be called explicitly if you don't want to use a context manager.

Parameters
NameDescription
retry google.api_core.retry.Retry

A retry object used to retry requests. If None is specified, requests will be retried using a default configuration.

timeout float

Time, in seconds, to wait for the request to complete. Note that if retry is specified, the timeout applies to each individual attempt.

Exceptions
TypeDescription
`exceptions.ValueErrorif the batch is not in progress.

current

current()

Return the topmost batch / transaction, or None.

delete

delete(key)

Remember a key to be deleted during commit.

Parameter
NameDescription
key Key

the key to be deleted.

Exceptions
TypeDescription
`exceptions.ValueErrorif the batch is not in progress, if key is not complete, or if the key's project does not match ours.

put

put(entity)

Remember an entity's state to be saved during commit.

When an entity has a partial key, calling commit sends it as an insert mutation and the key is completed. On return, the key for the entity passed in is updated to match the key ID assigned by the server.

Parameter
NameDescription
entity Entity

the entity to be saved.

Exceptions
TypeDescription
`exceptions.ValueErrorif the batch is not in progress, if entity has no key assigned, or if the key's project does not match ours.

rollback

rollback()

Rolls back the current batch.

Marks the batch as aborted (can't be used again).

Overridden by xref_Transaction.

Exceptions
TypeDescription
`exceptions.ValueErrorif the batch is not in progress.