1515
1616import os
1717
18- from google .cloud .proto .datastore .v1 import datastore_pb2 as _datastore_pb2
19-
2018from google .cloud ._helpers import _LocalStack
21- from google .cloud ._helpers import (
22- _determine_default_project as _base_default_project )
19+ from google .cloud ._helpers import (_determine_default_project as
20+ _default_project )
21+
2322from google .cloud .client import ClientWithProject
24- from google .cloud .environment_vars import DISABLE_GRPC
25- from google .cloud .environment_vars import GCD_DATASET
26- from google .cloud .environment_vars import GCD_HOST
2723
28- from google .cloud .datastore ._http import HTTPDatastoreAPI
2924from google .cloud .datastore import helpers
25+ from google .cloud .datastore ._http import HTTPDatastoreAPI
3026from google .cloud .datastore .batch import Batch
3127from google .cloud .datastore .entity import Entity
3228from google .cloud .datastore .key import Key
3329from google .cloud .datastore .query import Query
3430from google .cloud .datastore .transaction import Transaction
31+
32+ from google .cloud .environment_vars import DISABLE_GRPC
33+ from google .cloud .environment_vars import GCD_DATASET
34+ from google .cloud .environment_vars import GCD_HOST
35+
3536try :
3637 from google .cloud .datastore ._gax import make_datastore_api
3738 _HAVE_GRPC = True
@@ -74,7 +75,7 @@ def _determine_default_project(project=None):
7475 project = _get_gcd_project ()
7576
7677 if project is None :
77- project = _base_default_project (project = project )
78+ project = _default_project (project = project )
7879
7980 return project
8081
@@ -131,7 +132,7 @@ def _extended_lookup(datastore_api, project, key_pbs,
131132 results = []
132133
133134 loop_num = 0
134- read_options = _get_read_options (eventual , transaction_id )
135+ read_options = helpers . get_read_options (eventual , transaction_id )
135136 while loop_num < _MAX_LOOPS : # loop against possible deferred.
136137 loop_num += 1
137138 lookup_response = datastore_api .lookup (
@@ -276,7 +277,8 @@ def current_transaction(self):
276277 if isinstance (transaction , Transaction ):
277278 return transaction
278279
279- def get (self , key , missing = None , deferred = None , transaction = None ):
280+ def get (self , key , missing = None , deferred = None ,
281+ transaction = None , eventual = False ):
280282 """Retrieve an entity from a single key (if it exists).
281283
282284 .. note::
@@ -302,15 +304,27 @@ def get(self, key, missing=None, deferred=None, transaction=None):
302304 :param transaction: (Optional) Transaction to use for read consistency.
303305 If not passed, uses current transaction, if set.
304306
307+ :type eventual: bool
308+ :param eventual: (Optional) Defaults to strongly consistent (False).
309+ Setting True will use eventual consistency,
310+ but cannot be used inside a transaction or
311+ will raise ValueError.
312+
305313 :rtype: :class:`google.cloud.datastore.entity.Entity` or ``NoneType``
306314 :returns: The requested entity if it exists.
315+
316+ :raises: :class:`ValueError` if eventual is True and in a transaction.
307317 """
308- entities = self .get_multi (keys = [key ], missing = missing ,
309- deferred = deferred , transaction = transaction )
318+ entities = self .get_multi (keys = [key ],
319+ missing = missing ,
320+ deferred = deferred ,
321+ transaction = transaction ,
322+ eventual = eventual )
310323 if entities :
311324 return entities [0 ]
312325
313- def get_multi (self , keys , missing = None , deferred = None , transaction = None ):
326+ def get_multi (self , keys , missing = None , deferred = None ,
327+ transaction = None , eventual = False ):
314328 """Retrieve entities, along with their attributes.
315329
316330 :type keys: list of :class:`google.cloud.datastore.key.Key`
@@ -331,10 +345,17 @@ def get_multi(self, keys, missing=None, deferred=None, transaction=None):
331345 :param transaction: (Optional) Transaction to use for read consistency.
332346 If not passed, uses current transaction, if set.
333347
348+ :type eventual: bool
349+ :param eventual: (Optional) Defaults to strongly consistent (False).
350+ Setting True will use eventual consistency,
351+ but cannot be used inside a transaction or
352+ will raise ValueError.
353+
334354 :rtype: list of :class:`google.cloud.datastore.entity.Entity`
335355 :returns: The requested entities.
336356 :raises: :class:`ValueError` if one or more of ``keys`` has a project
337357 which does not match our project.
358+ :raises: :class:`ValueError` if eventual is True and in a transaction.
338359 """
339360 if not keys :
340361 return []
@@ -350,7 +371,8 @@ def get_multi(self, keys, missing=None, deferred=None, transaction=None):
350371 entity_pbs = _extended_lookup (
351372 datastore_api = self ._datastore_api ,
352373 project = self .project ,
353- key_pbs = [k .to_protobuf () for k in keys ],
374+ key_pbs = [key .to_protobuf () for key in keys ],
375+ eventual = eventual ,
354376 missing = missing ,
355377 deferred = deferred ,
356378 transaction_id = transaction and transaction .id ,
@@ -578,34 +600,3 @@ def do_something(entity):
578600 if 'namespace' not in kwargs :
579601 kwargs ['namespace' ] = self .namespace
580602 return Query (self , ** kwargs )
581-
582-
583- def _get_read_options (eventual , transaction_id ):
584- """Validate rules for read options, and assign to the request.
585-
586- Helper method for ``lookup()`` and ``run_query``.
587-
588- :type eventual: bool
589- :param eventual: Flag indicating if ``EVENTUAL`` or ``STRONG``
590- consistency should be used.
591-
592- :type transaction_id: bytes
593- :param transaction_id: A transaction identifier (may be null).
594-
595- :rtype: :class:`.datastore_pb2.ReadOptions`
596- :returns: The read options corresponding to the inputs.
597- :raises: :class:`ValueError` if ``eventual`` is ``True`` and the
598- ``transaction_id`` is not ``None``.
599- """
600- if transaction_id is None :
601- if eventual :
602- return _datastore_pb2 .ReadOptions (
603- read_consistency = _datastore_pb2 .ReadOptions .EVENTUAL )
604- else :
605- return _datastore_pb2 .ReadOptions ()
606- else :
607- if eventual :
608- raise ValueError ('eventual must be False when in a transaction' )
609- else :
610- return _datastore_pb2 .ReadOptions (
611- transaction = transaction_id )
0 commit comments