Skip to content

Commit

Permalink
Merge pull request #550 from tseaver/514-rip_out_connection_cruft
Browse files Browse the repository at this point in the history
#514: rip out `Connection` cruft
  • Loading branch information
tseaver committed Jan 15, 2015
2 parents 64aaed4 + f10620e commit 12ac983
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 690 deletions.
9 changes: 6 additions & 3 deletions gcloud/datastore/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"""

from gcloud.datastore import _implicit_environ
from gcloud.datastore.batch import _BATCHES
from gcloud.datastore.batch import Batch
from gcloud.datastore.transaction import Transaction
from gcloud.datastore import helpers


Expand Down Expand Up @@ -113,10 +113,13 @@ def get(keys, missing=None, deferred=None, connection=None):
connection = _require_connection(connection)
dataset_id = _get_dataset_id_from_keys(keys)

transaction = Transaction.current()

entity_pbs = connection.lookup(
dataset_id=dataset_id,
key_pbs=[k.to_protobuf() for k in keys],
missing=missing, deferred=deferred,
transaction_id=transaction and transaction.id,
)

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

connection = connection or _implicit_environ.CONNECTION

current = _BATCHES.top
current = Batch.current()
in_batch = current is not None
if not in_batch:
keys = [entity.key for entity in entities]
Expand All @@ -177,7 +180,7 @@ def delete(keys, connection=None):
connection = connection or _implicit_environ.CONNECTION

# We allow partial keys to attempt a delete, the backend will fail.
current = _BATCHES.top
current = Batch.current()
in_batch = current is not None
if not in_batch:
dataset_id = _get_dataset_id_from_keys(keys)
Expand Down
12 changes: 11 additions & 1 deletion gcloud/datastore/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def __init__(self, dataset_id=None, connection=None):
self._mutation = datastore_pb.Mutation()
self._auto_id_entities = []

@staticmethod
def current():
"""Return the topmost batch / transaction, or None."""
return _BATCHES.top

@property
def dataset_id(self):
"""Getter for dataset ID in which the batch will run.
Expand Down Expand Up @@ -308,6 +313,11 @@ def _assign_entity_to_mutation(mutation_pb, entity, auto_id_entities):
insert.key.CopyFrom(key_pb)

for name, value in entity.items():

value_is_list = isinstance(value, list)
if value_is_list and len(value) == 0:
continue

prop = insert.property.add()
# Set the name of the property.
prop.name = name
Expand All @@ -316,7 +326,7 @@ def _assign_entity_to_mutation(mutation_pb, entity, auto_id_entities):
helpers._set_protobuf_value(prop.value, value)

if name in entity.exclude_from_indexes:
if not isinstance(value, list):
if not value_is_list:
prop.value.indexed = False

for sub_value in prop.value.list_value:
Expand Down
Loading

0 comments on commit 12ac983

Please sign in to comment.