Skip to content

Commit ba45461

Browse files
author
Chris Rossi
authored
NDB: Stub out Context class. (#7379)
1 parent 32bb97d commit ba45461

4 files changed

Lines changed: 392 additions & 25 deletions

File tree

ndb/src/google/cloud/ndb/context.py

Lines changed: 229 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Context for currently running tasks and transactions."""
1616

17+
from google.cloud.ndb import exceptions
18+
1719

1820
__all__ = ["AutoBatcher", "Context", "ContextOptions", "TransactionOptions"]
1921

@@ -22,13 +24,237 @@ class AutoBatcher:
2224
__slots__ = ()
2325

2426
def __init__(self, *args, **kwargs):
25-
raise NotImplementedError
27+
raise exceptions.NoLongerImplementedError()
2628

2729

2830
class Context:
29-
__slots__ = ()
31+
def clear_cache(self):
32+
"""Clears the in-memory cache.
3033
31-
def __init__(self, *args, **kwargs):
34+
This does not affect memcache.
35+
"""
36+
raise NotImplementedError
37+
38+
def flush(self):
39+
"""Force any pending batch operations to go ahead and run."""
40+
raise NotImplementedError
41+
42+
def get_cache_policy(self):
43+
"""Return the current context cache policy function.
44+
45+
Returns:
46+
Callable: A function that accepts a
47+
:class:`~google.cloud.ndb.key.Key` instance as a single
48+
positional argument and returns a ``bool`` indicating if it
49+
should be cached. May be :data:`None`.
50+
"""
51+
raise NotImplementedError
52+
53+
def get_datastore_policy(self):
54+
"""Return the current context datastore policy function.
55+
56+
Returns:
57+
Callable: A function that accepts a
58+
:class:`~google.cloud.ndb.key.Key` instance as a single
59+
positional argument and returns a ``bool`` indicating if it
60+
should use the datastore. May be :data:`None`.
61+
"""
62+
raise NotImplementedError
63+
64+
def get_memcache_policy(self):
65+
"""Return the current memcache policy function.
66+
67+
Returns:
68+
Callable: A function that accepts a
69+
:class:`~google.cloud.ndb.key.Key` instance as a single
70+
positional argument and returns a ``bool`` indicating if it
71+
should be cached. May be :data:`None`.
72+
"""
73+
raise NotImplementedError
74+
75+
def get_memcache_timeout_policy(self):
76+
"""Return the current policy function memcache timeout (expiration).
77+
78+
Returns:
79+
Callable: A function that accepts a
80+
:class:`~google.cloud.ndb.key.Key` instance as a single
81+
positional argument and returns an ``int`` indicating the
82+
timeout, in seconds, for the key. :data:`0` implies the default
83+
timeout. May be :data:`None`.
84+
"""
85+
raise NotImplementedError
86+
87+
def set_cache_policy(self, policy):
88+
"""Set the context cache policy function.
89+
90+
Args:
91+
policy (Callable): A function that accepts a
92+
:class:`~google.cloud.ndb.key.Key` instance as a single
93+
positional argument and returns a ``bool`` indicating if it
94+
should be cached. May be :data:`None`.
95+
"""
96+
raise NotImplementedError
97+
98+
def set_datastore_policy(self, policy):
99+
"""Set the context datastore policy function.
100+
101+
Args:
102+
policy (Callable): A function that accepts a
103+
:class:`~google.cloud.ndb.key.Key` instance as a single
104+
positional argument and returns a ``bool`` indicating if it
105+
should use the datastore. May be :data:`None`.
106+
"""
107+
raise NotImplementedError
108+
109+
def set_memcache_policy(self, policy):
110+
"""Set the memcache policy function.
111+
112+
Args:
113+
policy (Callable): A function that accepts a
114+
:class:`~google.cloud.ndb.key.Key` instance as a single
115+
positional argument and returns a ``bool`` indicating if it
116+
should be cached. May be :data:`None`.
117+
"""
118+
raise NotImplementedError
119+
120+
def set_memcache_timeout_policy(self, policy):
121+
"""Set the policy function for memcache timeout (expiration).
122+
123+
Args:
124+
policy (Callable): A function that accepts a
125+
:class:`~google.cloud.ndb.key.Key` instance as a single
126+
positional argument and returns an ``int`` indicating the
127+
timeout, in seconds, for the key. :data:`0` implies the default
128+
timout. May be :data:`None`.
129+
"""
130+
raise NotImplementedError
131+
132+
def call_on_commit(self, callback):
133+
"""Call a callback upon successful commit of a transaction.
134+
135+
If not in a transaction, the callback is called immediately.
136+
137+
In a transaction, multiple callbacks may be registered and will be
138+
called once the transaction commits, in the order in which they
139+
were registered. If the transaction fails, the callbacks will not
140+
be called.
141+
142+
If the callback raises an exception, it bubbles up normally. This
143+
means: If the callback is called immediately, any exception it
144+
raises will bubble up immediately. If the call is postponed until
145+
commit, remaining callbacks will be skipped and the exception will
146+
bubble up through the transaction() call. (However, the
147+
transaction is already committed at that point.)
148+
149+
Args:
150+
callback (Callable): The callback function.
151+
"""
152+
raise NotImplementedError
153+
154+
def in_transaction(self):
155+
"""Get whether a transaction is currently active.
156+
157+
Returns:
158+
bool: :data:`True` if currently in a transaction, otherwise
159+
:data:`False`.
160+
"""
161+
raise NotImplementedError
162+
163+
@staticmethod
164+
def default_cache_policy(key):
165+
"""Default cache policy.
166+
167+
This defers to :meth:`~google.cloud.ndb.model.Model._use_cache`.
168+
169+
Args:
170+
key (google.cloud.ndb.model.key.Key): The key.
171+
172+
Returns:
173+
Union[bool, NoneType]: Whether to cache the key.
174+
"""
175+
raise NotImplementedError
176+
177+
@staticmethod
178+
def default_datastore_policy(key):
179+
"""Default cache policy.
180+
181+
This defers to :meth:`~google.cloud.ndb.model.Model._use_datastore`.
182+
183+
Args:
184+
key (google.cloud.ndb.model.key.Key): The key.
185+
186+
Returns:
187+
Union[bool, NoneType]: Whether to use datastore.
188+
"""
189+
raise NotImplementedError
190+
191+
@staticmethod
192+
def default_memcache_policy(key):
193+
"""Default memcache policy.
194+
195+
This defers to :meth:`~google.cloud.ndb.model.Model._use_memcache`.
196+
197+
Args:
198+
key (google.cloud.ndb.model.key.Key): The key.
199+
200+
Returns:
201+
Union[bool, NoneType]: Whether to cache the key.
202+
"""
203+
raise NotImplementedError
204+
205+
@staticmethod
206+
def default_memcache_timeout_policy(key):
207+
"""Default memcache timeout policy.
208+
209+
This defers to :meth:`~google.cloud.ndb.model.Model._memcache_timeout`.
210+
211+
Args:
212+
key (google.cloud.ndb.model.key.Key): The key.
213+
214+
Returns:
215+
Union[int, NoneType]: Memcache timeout to use.
216+
"""
217+
raise NotImplementedError
218+
219+
def memcache_add(self, *args, **kwargs):
220+
"""Direct pass-through to memcache client."""
221+
raise NotImplementedError
222+
223+
def memcache_cas(self, *args, **kwargs):
224+
"""Direct pass-through to memcache client."""
225+
226+
raise NotImplementedError
227+
228+
def memcache_decr(self, *args, **kwargs):
229+
"""Direct pass-through to memcache client."""
230+
raise NotImplementedError
231+
232+
def memcache_delete(self, *args, **kwargs):
233+
"""Direct pass-through to memcache client."""
234+
raise NotImplementedError
235+
236+
def memcache_get(self, *args, **kwargs):
237+
"""Direct pass-through to memcache client."""
238+
raise NotImplementedError
239+
240+
def memcache_gets(self, *args, **kwargs):
241+
"""Direct pass-through to memcache client."""
242+
raise NotImplementedError
243+
244+
def memcache_incr(self, *args, **kwargs):
245+
"""Direct pass-through to memcache client."""
246+
raise NotImplementedError
247+
248+
def memcache_replace(self, *args, **kwargs):
249+
"""Direct pass-through to memcache client."""
250+
raise NotImplementedError
251+
252+
def memcache_set(self, *args, **kwargs):
253+
"""Direct pass-through to memcache client."""
254+
raise NotImplementedError
255+
256+
def urlfetch(self, *args, **kwargs):
257+
"""Fetch a resource using HTTP."""
32258
raise NotImplementedError
33259

34260

ndb/src/google/cloud/ndb/exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,20 @@ def __init__(self, filter):
8686
self.filter = filter
8787
message = "invalid filter: {}.".format(self.filter).encode("utf-8")
8888
super(BadFilterError, self).__init__(message)
89+
90+
91+
class NoLongerImplementedError(NotImplementedError):
92+
"""Indicates a legacy function that is intentionally left unimplemented.
93+
94+
In the vast majority of cases, this should only be raised by classes,
95+
functions, or methods that were only been used internally in legacy NDB and
96+
are no longer necessary because of refactoring. Legacy NDB did a poor job
97+
of distinguishing between internal and public API. Where we have determined
98+
that something is probably not a part of the public API, we've removed it
99+
in order to keep the supported API as clean as possible. It's possible that
100+
in some cases we've guessed wrong. Get in touch with the NDB development
101+
team if you think this is the case.
102+
"""
103+
104+
def __init__(self):
105+
super(NoLongerImplementedError, self).__init__("No longer implemented")

ndb/src/google/cloud/ndb/model.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110

111111
_MEANING_PREDEFINED_ENTITY_USER = 20
112112
_MAX_STRING_LENGTH = 1500
113-
_NO_LONGER_IMPLEMENTED = "No longer used"
114113
Key = key_module.Key
115114
BlobKey = _datastore_types.BlobKey
116115
GeoPt = helpers.GeoPoint
@@ -293,7 +292,7 @@ class ModelAdapter:
293292
__slots__ = ()
294293

295294
def __new__(self, *args, **kwargs):
296-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
295+
raise exceptions.NoLongerImplementedError()
297296

298297

299298
def _entity_from_protobuf(protobuf):
@@ -1539,7 +1538,7 @@ def _deserialize(self, entity, p, unused_depth=1):
15391538
Raises:
15401539
NotImplementedError: Always. This method is deprecated.
15411540
"""
1542-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
1541+
raise exceptions.NoLongerImplementedError()
15431542

15441543
def _prepare_for_put(self, entity):
15451544
"""Allow this property to define a pre-put hook.
@@ -1768,7 +1767,7 @@ def _db_get_value(self, v, unused_p):
17681767
Raises:
17691768
NotImplementedError: Always. This method is deprecated.
17701769
"""
1771-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
1770+
raise exceptions.NoLongerImplementedError()
17721771

17731772

17741773
class IntegerProperty(Property):
@@ -1817,7 +1816,7 @@ def _db_get_value(self, v, unused_p):
18171816
Raises:
18181817
NotImplementedError: Always. This method is deprecated.
18191818
"""
1820-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
1819+
raise exceptions.NoLongerImplementedError()
18211820

18221821

18231822
class FloatProperty(Property):
@@ -1867,7 +1866,7 @@ def _db_get_value(self, v, unused_p):
18671866
Raises:
18681867
NotImplementedError: Always. This method is deprecated.
18691868
"""
1870-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
1869+
raise exceptions.NoLongerImplementedError()
18711870

18721871

18731872
class _CompressedValue:
@@ -2070,7 +2069,7 @@ def _db_get_value(self, v, unused_p):
20702069
Raises:
20712070
NotImplementedError: Always. This method is deprecated.
20722071
"""
2073-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
2072+
raise exceptions.NoLongerImplementedError()
20742073

20752074

20762075
class TextProperty(BlobProperty):
@@ -2279,7 +2278,7 @@ def _db_get_value(self, v, unused_p):
22792278
Raises:
22802279
NotImplementedError: Always. This method is deprecated.
22812280
"""
2282-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
2281+
raise exceptions.NoLongerImplementedError()
22832282

22842283

22852284
class PickleProperty(BlobProperty):
@@ -2777,7 +2776,7 @@ def _db_get_value(self, v, unused_p):
27772776
Raises:
27782777
NotImplementedError: Always. This method is deprecated.
27792778
"""
2780-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
2779+
raise exceptions.NoLongerImplementedError()
27812780

27822781

27832782
class KeyProperty(Property):
@@ -3012,7 +3011,7 @@ def _db_get_value(self, v, unused_p):
30123011
Raises:
30133012
NotImplementedError: Always. This method is deprecated.
30143013
"""
3015-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
3014+
raise exceptions.NoLongerImplementedError()
30163015

30173016

30183017
class BlobKeyProperty(Property):
@@ -3052,7 +3051,7 @@ def _db_get_value(self, v, unused_p):
30523051
Raises:
30533052
NotImplementedError: Always. This method is deprecated.
30543053
"""
3055-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
3054+
raise exceptions.NoLongerImplementedError()
30563055

30573056

30583057
class DateTimeProperty(Property):
@@ -3204,7 +3203,7 @@ def _db_get_value(self, v, unused_p):
32043203
Raises:
32053204
NotImplementedError: Always. This method is deprecated.
32063205
"""
3207-
raise NotImplementedError(_NO_LONGER_IMPLEMENTED)
3206+
raise exceptions.NoLongerImplementedError()
32083207

32093208

32103209
class DateProperty(DateTimeProperty):

0 commit comments

Comments
 (0)