1414
1515"""Create / interact with gcloud datastore transactions."""
1616
17- from gcloud .datastore import _implicit_environ
18- from gcloud .datastore import datastore_v1_pb2 as datastore_pb
17+ from gcloud .datastore .batch import Batch
1918
2019
21- class Transaction (object ):
20+ class Transaction (Batch ):
2221 """An abstraction representing datastore Transactions.
2322
2423 Transactions can be used to build up a bulk mutuation as well as
@@ -38,6 +37,13 @@ class Transaction(object):
3837 ... entity1.save()
3938 ... entity2.save()
4039
40+ Because it derives from :class:`Batch`, :class`Transaction` also provides
41+ :meth:`put` and :meth:`delete` methods::
42+
43+ >>> with Transaction()
44+ ... transaction.put(entity1)
45+ ... transaction.delete(entity2.key)
46+
4147 By default, the transaction is rolled back if the transaction block
4248 exits with an error::
4349
@@ -123,34 +129,8 @@ class Transaction(object):
123129 """
124130
125131 def __init__ (self , dataset_id = None , connection = None ):
126- self ._connection = connection or _implicit_environ .CONNECTION
127- self ._dataset_id = dataset_id or _implicit_environ .DATASET_ID
128-
129- if self ._connection is None or self ._dataset_id is None :
130- raise ValueError ('A transaction must have a connection and '
131- 'a dataset ID set.' )
132-
132+ super (Transaction , self ).__init__ (dataset_id , connection )
133133 self ._id = None
134- self ._mutation = datastore_pb .Mutation ()
135- self ._auto_id_entities = []
136-
137- @property
138- def dataset_id (self ):
139- """Getter for dataset ID in which the transaction will run.
140-
141- :rtype: string
142- :returns: The dataset ID in which the transaction will run.
143- """
144- return self ._dataset_id
145-
146- @property
147- def connection (self ):
148- """Getter for connection over which the transaction will run.
149-
150- :rtype: :class:`gcloud.datastore.connection.Connection`
151- :returns: The connection over which the transaction will run.
152- """
153- return self ._connection
154134
155135 @property
156136 def id (self ):
@@ -161,37 +141,6 @@ def id(self):
161141 """
162142 return self ._id
163143
164- @property
165- def mutation (self ):
166- """Getter for the current mutation.
167-
168- Every transaction is committed with a single Mutation
169- representing the 'work' to be done as part of the transaction.
170- Inside a transaction, calling ``save()`` on an entity builds up
171- the mutation. This getter returns the Mutation protobuf that
172- has been built-up so far.
173-
174- :rtype: :class:`gcloud.datastore.datastore_v1_pb2.Mutation`
175- :returns: The Mutation protobuf to be sent in the commit request.
176- """
177- return self ._mutation
178-
179- def add_auto_id_entity (self , entity ):
180- """Adds an entity to the list of entities to update with IDs.
181-
182- When an entity has a partial key, calling ``save()`` adds an
183- insert_auto_id entry in the mutation. In order to make sure we
184- update the Entity once the transaction is committed, we need to
185- keep track of which entities to update (and the order is
186- important).
187-
188- When you call ``save()`` on an entity inside a transaction, if
189- the entity has a partial key, it adds itself to the list of
190- entities to be updated once the transaction is committed by
191- calling this method.
192- """
193- self ._auto_id_entities .append (entity )
194-
195144 def begin (self ):
196145 """Begins a transaction.
197146
@@ -230,14 +179,7 @@ def commit(self):
230179 # It's possible that they called commit() already, in which case
231180 # we shouldn't do any committing of our own.
232181 if self .connection .transaction ():
233- result = self .connection .commit (self ._dataset_id ,
234- self .mutation )
235-
236- # For any of the auto-id entities, make sure we update their keys.
237- for i , entity in enumerate (self ._auto_id_entities ):
238- key_pb = result .insert_auto_id_key [i ]
239- new_id = key_pb .path_element [- 1 ].id
240- entity .key = entity .key .completed_key (new_id )
182+ super (Transaction , self ).commit ()
241183
242184 # Tell the connection that the transaction is over.
243185 self .connection .transaction (None )
@@ -246,10 +188,12 @@ def commit(self):
246188 self ._id = None
247189
248190 def __enter__ (self ):
191+ # Don't call super() -- we have different semantics.
249192 self .begin ()
250193 return self
251194
252195 def __exit__ (self , exc_type , exc_val , exc_tb ):
196+ # Don't call super() -- we have different semantics.
253197 if exc_type is None :
254198 self .commit ()
255199 else :
0 commit comments