15
15
16
16
import os
17
17
18
- from google .cloud .proto .datastore .v1 import datastore_pb2 as _datastore_pb2
19
-
20
18
from 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
+
23
22
from 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
27
23
28
- from google .cloud .datastore ._http import HTTPDatastoreAPI
29
24
from google .cloud .datastore import helpers
25
+ from google .cloud .datastore ._http import HTTPDatastoreAPI
30
26
from google .cloud .datastore .batch import Batch
31
27
from google .cloud .datastore .entity import Entity
32
28
from google .cloud .datastore .key import Key
33
29
from google .cloud .datastore .query import Query
34
30
from 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
+
35
36
try :
36
37
from google .cloud .datastore ._gax import make_datastore_api
37
38
_HAVE_GRPC = True
@@ -74,7 +75,7 @@ def _determine_default_project(project=None):
74
75
project = _get_gcd_project ()
75
76
76
77
if project is None :
77
- project = _base_default_project (project = project )
78
+ project = _default_project (project = project )
78
79
79
80
return project
80
81
@@ -131,7 +132,7 @@ def _extended_lookup(datastore_api, project, key_pbs,
131
132
results = []
132
133
133
134
loop_num = 0
134
- read_options = _get_read_options (eventual , transaction_id )
135
+ read_options = helpers . get_read_options (eventual , transaction_id )
135
136
while loop_num < _MAX_LOOPS : # loop against possible deferred.
136
137
loop_num += 1
137
138
lookup_response = datastore_api .lookup (
@@ -276,7 +277,8 @@ def current_transaction(self):
276
277
if isinstance (transaction , Transaction ):
277
278
return transaction
278
279
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 ):
280
282
"""Retrieve an entity from a single key (if it exists).
281
283
282
284
.. note::
@@ -302,15 +304,27 @@ def get(self, key, missing=None, deferred=None, transaction=None):
302
304
:param transaction: (Optional) Transaction to use for read consistency.
303
305
If not passed, uses current transaction, if set.
304
306
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
+
305
313
:rtype: :class:`google.cloud.datastore.entity.Entity` or ``NoneType``
306
314
:returns: The requested entity if it exists.
315
+
316
+ :raises: :class:`ValueError` if eventual is True and in a transaction.
307
317
"""
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 )
310
323
if entities :
311
324
return entities [0 ]
312
325
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 ):
314
328
"""Retrieve entities, along with their attributes.
315
329
316
330
: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):
331
345
:param transaction: (Optional) Transaction to use for read consistency.
332
346
If not passed, uses current transaction, if set.
333
347
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
+
334
354
:rtype: list of :class:`google.cloud.datastore.entity.Entity`
335
355
:returns: The requested entities.
336
356
:raises: :class:`ValueError` if one or more of ``keys`` has a project
337
357
which does not match our project.
358
+ :raises: :class:`ValueError` if eventual is True and in a transaction.
338
359
"""
339
360
if not keys :
340
361
return []
@@ -350,7 +371,8 @@ def get_multi(self, keys, missing=None, deferred=None, transaction=None):
350
371
entity_pbs = _extended_lookup (
351
372
datastore_api = self ._datastore_api ,
352
373
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 ,
354
376
missing = missing ,
355
377
deferred = deferred ,
356
378
transaction_id = transaction and transaction .id ,
@@ -578,34 +600,3 @@ def do_something(entity):
578
600
if 'namespace' not in kwargs :
579
601
kwargs ['namespace' ] = self .namespace
580
602
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