Skip to content

Commit 8bd5059

Browse files
committed
Clean up '_assign_entity_to_mutation'.
Addresses @dhermes' comments: #524 (comment) #524 (comment) #524 (comment)
1 parent 990ab5c commit 8bd5059

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

gcloud/datastore/batch.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ def put(self, entity):
210210
if entity.key is None:
211211
raise ValueError("Entity must have a key")
212212

213-
if _assign_entity_to_mutation(self.mutation, entity):
214-
self._auto_id_entities.append(entity)
213+
_assign_entity_to_mutation(
214+
self.mutation, entity, self._auto_id_entities)
215215

216216
def delete(self, key):
217217
"""Remember a key to be deleted durring ``commit``.
@@ -273,15 +273,32 @@ def __exit__(self, exc_type, exc_val, exc_tb):
273273
_BATCHES.pop()
274274

275275

276-
def _assign_entity_to_mutation(mutation_pb, entity):
277-
"""Helper method for ``Batch.put``."""
276+
def _assign_entity_to_mutation(mutation_pb, entity, auto_id_entities):
277+
"""Copy ``entity`` into appropriate slot of ``mutation_pb``.
278+
279+
If ``entity.key`` is incomplete, append ``entity`` to ``auto_id_entities``
280+
for later fixup during ``commit``.
281+
282+
Helper method for ``Batch.put``.
283+
284+
:type mutation_pb: :class:`gcloud.datastore.datastore_v1_pb2.Mutation`
285+
:param mutation_pb; the Mutation protobuf for the batch / transaction.
286+
287+
:type entity: :class:`gcloud.datastore.entity.Entity`
288+
:param entity; the entity being updated within the batch / transaction.
289+
290+
:type auto_id_entities: list of :class:`gcloud.datastore.entity.Entity`
291+
:param auto_id_entities: entiites with partial keys, to be fixed up
292+
during commit.
293+
"""
278294
auto_id = entity.key.is_partial
279295

280296
key_pb = entity.key.to_protobuf()
281297
key_pb = helpers._prepare_key_for_request(key_pb)
282298

283299
if auto_id:
284300
insert = mutation_pb.insert_auto_id.add()
301+
auto_id_entities.append(entity)
285302
else:
286303
insert = mutation_pb.upsert.add()
287304

@@ -301,5 +318,3 @@ def _assign_entity_to_mutation(mutation_pb, entity):
301318

302319
for sub_value in prop.value.list_value:
303320
sub_value.indexed = False
304-
305-
return auto_id

0 commit comments

Comments
 (0)