Skip to content

Commit 8b09538

Browse files
committed
Merge pull request #1296 from dhermes/remove-datastore-add-auto-id
Removing datastore Batch.add_auto_id_entity.
2 parents 46e9800 + 3ec13ca commit 8b09538

File tree

3 files changed

+31
-86
lines changed

3 files changed

+31
-86
lines changed

gcloud/datastore/batch.py

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Batch(object):
6565
def __init__(self, client):
6666
self._client = client
6767
self._mutation = datastore_pb.Mutation()
68-
self._auto_id_entities = []
68+
self._partial_key_entities = []
6969

7070
def current(self):
7171
"""Return the topmost batch / transaction, or None."""
@@ -114,30 +114,6 @@ def mutation(self):
114114
"""
115115
return self._mutation
116116

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-
141117
def put(self, entity):
142118
"""Remember an entity's state to be saved during ``commit``.
143119
@@ -152,6 +128,11 @@ def put(self, entity):
152128
Python3) map to 'string_value' in the datastore; values which are
153129
"bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.
154130
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+
155136
:type entity: :class:`gcloud.datastore.entity.Entity`
156137
:param entity: the entity to be saved.
157138
@@ -164,8 +145,13 @@ def put(self, entity):
164145
if not _dataset_ids_equal(self.dataset_id, entity.key.dataset_id):
165146
raise ValueError("Key must be from same dataset as batch")
166147

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)
169155

170156
def delete(self, key):
171157
"""Remember a key to be deleted durring ``commit``.
@@ -204,9 +190,9 @@ def commit(self):
204190
# If the back-end returns without error, we are guaranteed that
205191
# the response's 'insert_auto_id_key' will match (length and order)
206192
# 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).
208194
for new_key_pb, entity in zip(response.insert_auto_id_key,
209-
self._auto_id_entities):
195+
self._partial_key_entities):
210196
new_id = new_key_pb.path_element[-1].id
211197
entity.key = entity.key.completed_key(new_id)
212198

@@ -232,47 +218,28 @@ def __exit__(self, exc_type, exc_val, exc_tb):
232218
self._client._pop_batch()
233219

234220

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``.
240223
241224
Helper method for ``Batch.put``.
242225
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.
245228
246229
:type entity: :class:`gcloud.datastore.entity.Entity`
247230
: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.
252231
"""
253-
auto_id = entity.key.is_partial
254-
255232
key_pb = entity.key.to_protobuf()
256233
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)
268235

269236
for name, value in entity.items():
270237

271238
value_is_list = isinstance(value, list)
272239
if value_is_list and len(value) == 0:
273240
continue
274241

275-
prop = insert.property.add()
242+
prop = entity_pb.property.add()
276243
# Set the name of the property.
277244
prop.name = name
278245

gcloud/datastore/test_batch.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_ctor(self):
3838
self.assertEqual(batch.namespace, _NAMESPACE)
3939
self.assertTrue(batch._id is None)
4040
self.assertTrue(isinstance(batch.mutation, Mutation))
41-
self.assertEqual(batch._auto_id_entities, [])
41+
self.assertEqual(batch._partial_key_entities, [])
4242

4343
def test_current(self):
4444
_DATASET = 'DATASET'
@@ -59,29 +59,6 @@ def test_current(self):
5959
self.assertTrue(batch1.current() is None)
6060
self.assertTrue(batch2.current() is None)
6161

62-
def test_add_auto_id_entity_w_partial_key(self):
63-
_DATASET = 'DATASET'
64-
connection = _Connection()
65-
client = _Client(_DATASET, connection)
66-
batch = self._makeOne(client)
67-
entity = _Entity()
68-
key = entity.key = _Key(_DATASET)
69-
key._id = None
70-
71-
batch.add_auto_id_entity(entity)
72-
73-
self.assertEqual(batch._auto_id_entities, [entity])
74-
75-
def test_add_auto_id_entity_w_completed_key(self):
76-
_DATASET = 'DATASET'
77-
connection = _Connection()
78-
client = _Client(_DATASET, connection)
79-
batch = self._makeOne(client)
80-
entity = _Entity()
81-
entity.key = _Key(_DATASET)
82-
83-
self.assertRaises(ValueError, batch.add_auto_id_entity, entity)
84-
8562
def test_put_entity_wo_key(self):
8663
_DATASET = 'DATASET'
8764
connection = _Connection()
@@ -119,7 +96,7 @@ def test_put_entity_w_partial_key(self):
11996
self.assertEqual(len(upserts), 0)
12097
deletes = list(batch.mutation.delete)
12198
self.assertEqual(len(deletes), 0)
122-
self.assertEqual(batch._auto_id_entities, [entity])
99+
self.assertEqual(batch._partial_key_entities, [entity])
123100

124101
def test_put_entity_w_completed_key(self):
125102
_DATASET = 'DATASET'
@@ -257,7 +234,7 @@ def test_commit(self):
257234
self.assertEqual(connection._committed,
258235
[(_DATASET, batch.mutation, None)])
259236

260-
def test_commit_w_auto_id_entities(self):
237+
def test_commit_w_partial_key_entities(self):
261238
_DATASET = 'DATASET'
262239
_NEW_ID = 1234
263240
connection = _Connection(_NEW_ID)
@@ -266,7 +243,7 @@ def test_commit_w_auto_id_entities(self):
266243
entity = _Entity({})
267244
key = entity.key = _Key(_DATASET)
268245
key._id = None
269-
batch._auto_id_entities.append(entity)
246+
batch._partial_key_entities.append(entity)
270247

271248
batch.commit()
272249

gcloud/datastore/test_transaction.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_ctor_defaults(self):
3636
self.assertEqual(xact.id, None)
3737
self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
3838
self.assertTrue(isinstance(xact.mutation, Mutation))
39-
self.assertEqual(len(xact._auto_id_entities), 0)
39+
self.assertEqual(len(xact._partial_key_entities), 0)
4040

4141
def test_current(self):
4242
from gcloud.datastore.test_client import _NoCommitBatch
@@ -97,7 +97,7 @@ def test_rollback(self):
9797
self.assertEqual(xact.id, None)
9898
self.assertEqual(connection._rolled_back, (_DATASET, 234))
9999

100-
def test_commit_no_auto_ids(self):
100+
def test_commit_no_partial_keys(self):
101101
_DATASET = 'DATASET'
102102
connection = _Connection(234)
103103
client = _Client(_DATASET, connection)
@@ -108,7 +108,7 @@ def test_commit_no_auto_ids(self):
108108
self.assertEqual(connection._committed, (_DATASET, mutation, 234))
109109
self.assertEqual(xact.id, None)
110110

111-
def test_commit_w_auto_ids(self):
111+
def test_commit_w_partial_keys(self):
112112
_DATASET = 'DATASET'
113113
_KIND = 'KIND'
114114
_ID = 123
@@ -118,7 +118,7 @@ def test_commit_w_auto_ids(self):
118118
client = _Client(_DATASET, connection)
119119
xact = self._makeOne(client)
120120
entity = _Entity()
121-
xact.add_auto_id_entity(entity)
121+
xact.put(entity)
122122
xact._mutation = mutation = object()
123123
xact.begin()
124124
xact.commit()
@@ -197,9 +197,10 @@ def __init__(self, *new_keys):
197197
self.insert_auto_id_key = new_keys
198198

199199

200-
class _Entity(object):
200+
class _Entity(dict):
201201

202202
def __init__(self):
203+
super(_Entity, self).__init__()
203204
from gcloud.datastore.key import Key
204205
self.key = Key('KIND', dataset_id='DATASET')
205206

0 commit comments

Comments
 (0)