@@ -65,7 +65,7 @@ class Batch(object):
65
65
def __init__ (self , client ):
66
66
self ._client = client
67
67
self ._mutation = datastore_pb .Mutation ()
68
- self ._auto_id_entities = []
68
+ self ._partial_key_entities = []
69
69
70
70
def current (self ):
71
71
"""Return the topmost batch / transaction, or None."""
@@ -114,30 +114,6 @@ def mutation(self):
114
114
"""
115
115
return self ._mutation
116
116
117
- def add_auto_id_entity (self , entity ):
118
- """Adds an entity to the list of entities to update with IDs.
119
-
120
- When an entity has a partial key, calling ``save()`` adds an
121
- insert_auto_id entry in the mutation. In order to make sure we
122
- update the Entity once the transaction is committed, we need to
123
- keep track of which entities to update (and the order is
124
- important).
125
-
126
- When you call ``save()`` on an entity inside a transaction, if
127
- the entity has a partial key, it adds itself to the list of
128
- entities to be updated once the transaction is committed by
129
- calling this method.
130
-
131
- :type entity: :class:`gcloud.datastore.entity.Entity`
132
- :param entity: The entity to be updated with a completed key.
133
-
134
- :raises: ValueError if the entity's key is alread completed.
135
- """
136
- if not entity .key .is_partial :
137
- raise ValueError ("Entity has a completed key" )
138
-
139
- self ._auto_id_entities .append (entity )
140
-
141
117
def put (self , entity ):
142
118
"""Remember an entity's state to be saved during ``commit``.
143
119
@@ -152,6 +128,11 @@ def put(self, entity):
152
128
Python3) map to 'string_value' in the datastore; values which are
153
129
"bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.
154
130
131
+ When an entity has a partial key, calling :meth:`commit`` sends it as
132
+ an ``insert_auto_id`` mutation and the key is completed. On return, the
133
+ key for the ``entity`` passed in as updated to match the key ID
134
+ assigned by the server.
135
+
155
136
:type entity: :class:`gcloud.datastore.entity.Entity`
156
137
:param entity: the entity to be saved.
157
138
@@ -164,8 +145,13 @@ def put(self, entity):
164
145
if not _dataset_ids_equal (self .dataset_id , entity .key .dataset_id ):
165
146
raise ValueError ("Key must be from same dataset as batch" )
166
147
167
- _assign_entity_to_mutation (
168
- self .mutation , entity , self ._auto_id_entities )
148
+ if entity .key .is_partial :
149
+ entity_pb = self .mutation .insert_auto_id .add ()
150
+ self ._partial_key_entities .append (entity )
151
+ else :
152
+ entity_pb = self .mutation .upsert .add ()
153
+
154
+ _assign_entity_to_pb (entity_pb , entity )
169
155
170
156
def delete (self , key ):
171
157
"""Remember a key to be deleted durring ``commit``.
@@ -204,9 +190,9 @@ def commit(self):
204
190
# If the back-end returns without error, we are guaranteed that
205
191
# the response's 'insert_auto_id_key' will match (length and order)
206
192
# the request's 'insert_auto_id` entities, which are derived from
207
- # our '_auto_id_entities ' (no partial success).
193
+ # our '_partial_key_entities ' (no partial success).
208
194
for new_key_pb , entity in zip (response .insert_auto_id_key ,
209
- self ._auto_id_entities ):
195
+ self ._partial_key_entities ):
210
196
new_id = new_key_pb .path_element [- 1 ].id
211
197
entity .key = entity .key .completed_key (new_id )
212
198
@@ -232,47 +218,28 @@ def __exit__(self, exc_type, exc_val, exc_tb):
232
218
self ._client ._pop_batch ()
233
219
234
220
235
- def _assign_entity_to_mutation (mutation_pb , entity , auto_id_entities ):
236
- """Copy ``entity`` into appropriate slot of ``mutation_pb``.
237
-
238
- If ``entity.key`` is incomplete, append ``entity`` to ``auto_id_entities``
239
- for later fixup during ``commit``.
221
+ def _assign_entity_to_pb (entity_pb , entity ):
222
+ """Copy ``entity`` into ``entity_pb``.
240
223
241
224
Helper method for ``Batch.put``.
242
225
243
- :type mutation_pb : :class:`gcloud.datastore._datastore_v1_pb2.Mutation `
244
- :param mutation_pb : The Mutation protobuf for the batch / transaction .
226
+ :type entity_pb : :class:`gcloud.datastore._datastore_v1_pb2.Entity `
227
+ :param entity_pb : The entity owned by a mutation .
245
228
246
229
:type entity: :class:`gcloud.datastore.entity.Entity`
247
230
:param entity: The entity being updated within the batch / transaction.
248
-
249
- :type auto_id_entities: list of :class:`gcloud.datastore.entity.Entity`
250
- :param auto_id_entities: Entities with partial keys, to be fixed up
251
- during commit.
252
231
"""
253
- auto_id = entity .key .is_partial
254
-
255
232
key_pb = entity .key .to_protobuf ()
256
233
key_pb = helpers ._prepare_key_for_request (key_pb )
257
-
258
- if auto_id :
259
- insert = mutation_pb .insert_auto_id .add ()
260
- auto_id_entities .append (entity )
261
- else :
262
- # We use ``upsert`` for entities with completed keys, rather than
263
- # ``insert`` or ``update``, in order not to create race conditions
264
- # based on prior existence / removal of the entity.
265
- insert = mutation_pb .upsert .add ()
266
-
267
- insert .key .CopyFrom (key_pb )
234
+ entity_pb .key .CopyFrom (key_pb )
268
235
269
236
for name , value in entity .items ():
270
237
271
238
value_is_list = isinstance (value , list )
272
239
if value_is_list and len (value ) == 0 :
273
240
continue
274
241
275
- prop = insert .property .add ()
242
+ prop = entity_pb .property .add ()
276
243
# Set the name of the property.
277
244
prop .name = name
278
245
0 commit comments