Skip to content

Commit 12ac983

Browse files
committed
Merge pull request #550 from tseaver/514-rip_out_connection_cruft
#514: rip out `Connection` cruft
2 parents 64aaed4 + f10620e commit 12ac983

File tree

11 files changed

+225
-690
lines changed

11 files changed

+225
-690
lines changed

gcloud/datastore/api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"""
2020

2121
from gcloud.datastore import _implicit_environ
22-
from gcloud.datastore.batch import _BATCHES
2322
from gcloud.datastore.batch import Batch
23+
from gcloud.datastore.transaction import Transaction
2424
from gcloud.datastore import helpers
2525

2626

@@ -113,10 +113,13 @@ def get(keys, missing=None, deferred=None, connection=None):
113113
connection = _require_connection(connection)
114114
dataset_id = _get_dataset_id_from_keys(keys)
115115

116+
transaction = Transaction.current()
117+
116118
entity_pbs = connection.lookup(
117119
dataset_id=dataset_id,
118120
key_pbs=[k.to_protobuf() for k in keys],
119121
missing=missing, deferred=deferred,
122+
transaction_id=transaction and transaction.id,
120123
)
121124

122125
if missing is not None:
@@ -150,7 +153,7 @@ def put(entities, connection=None):
150153

151154
connection = connection or _implicit_environ.CONNECTION
152155

153-
current = _BATCHES.top
156+
current = Batch.current()
154157
in_batch = current is not None
155158
if not in_batch:
156159
keys = [entity.key for entity in entities]
@@ -177,7 +180,7 @@ def delete(keys, connection=None):
177180
connection = connection or _implicit_environ.CONNECTION
178181

179182
# We allow partial keys to attempt a delete, the backend will fail.
180-
current = _BATCHES.top
183+
current = Batch.current()
181184
in_batch = current is not None
182185
if not in_batch:
183186
dataset_id = _get_dataset_id_from_keys(keys)

gcloud/datastore/batch.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ def __init__(self, dataset_id=None, connection=None):
130130
self._mutation = datastore_pb.Mutation()
131131
self._auto_id_entities = []
132132

133+
@staticmethod
134+
def current():
135+
"""Return the topmost batch / transaction, or None."""
136+
return _BATCHES.top
137+
133138
@property
134139
def dataset_id(self):
135140
"""Getter for dataset ID in which the batch will run.
@@ -308,6 +313,11 @@ def _assign_entity_to_mutation(mutation_pb, entity, auto_id_entities):
308313
insert.key.CopyFrom(key_pb)
309314

310315
for name, value in entity.items():
316+
317+
value_is_list = isinstance(value, list)
318+
if value_is_list and len(value) == 0:
319+
continue
320+
311321
prop = insert.property.add()
312322
# Set the name of the property.
313323
prop.name = name
@@ -316,7 +326,7 @@ def _assign_entity_to_mutation(mutation_pb, entity, auto_id_entities):
316326
helpers._set_protobuf_value(prop.value, value)
317327

318328
if name in entity.exclude_from_indexes:
319-
if not isinstance(value, list):
329+
if not value_is_list:
320330
prop.value.indexed = False
321331

322332
for sub_value in prop.value.list_value:

0 commit comments

Comments
 (0)