@@ -105,6 +105,12 @@ def __str__(self):
105
105
106
106
def __repr__ (self ):
107
107
return str (self )
108
+
109
+ def _read_preference (self , allow_secondary ):
110
+ """ Return the mongo read preference given an 'allow_secondary' argument
111
+ """
112
+ allow_secondary = self ._allow_secondary if allow_secondary is None else allow_secondary
113
+ return ReadPreference .NEAREST if allow_secondary else ReadPreference .PRIMARY
108
114
109
115
@mongo_retry
110
116
def list_symbols (self , all_symbols = False , snapshot = None , regex = None , ** kwargs ):
@@ -166,7 +172,7 @@ def list_symbols(self, all_symbols=False, snapshot=None, regex=None, **kwargs):
166
172
return sorted ([x ['symbol' ] for x in results ])
167
173
168
174
@mongo_retry
169
- def has_symbol (self , symbol , as_of = None ):
175
+ def has_symbol (self , symbol , as_of = None , allow_secondary = None ):
170
176
"""
171
177
Return True if the 'symbol' exists in this library AND the symbol
172
178
isn't deleted in the specified as_of.
@@ -177,9 +183,19 @@ def has_symbol(self, symbol, as_of=None):
177
183
----------
178
184
symbol : `str`
179
185
symbol name for the item
186
+ as_of : `str` or int or `datetime.datetime`
187
+ Return the data as it was as_of the point in time.
188
+ `int` : specific version number
189
+ `str` : snapshot name which contains the version
190
+ `datetime.datetime` : the version of the data that existed as_of the requested point in time
191
+ allow_secondary : `bool` or `None`
192
+ Override the default behavior for allowing reads from secondary members of a cluster:
193
+ `None` : use the settings from the top-level `Arctic` object used to query this version store.
194
+ `True` : allow reads from secondary members
195
+ `False` : only allow reads from primary members
180
196
"""
181
197
try :
182
- self ._read_metadata (symbol , as_of = as_of )
198
+ self ._read_metadata (symbol , as_of = as_of , read_preference = self . _read_preference ( allow_secondary ) )
183
199
return True
184
200
except NoDataFoundException :
185
201
return False
@@ -287,14 +303,18 @@ def read(self, symbol, as_of=None, from_version=None, allow_secondary=None, **kw
287
303
`int` : specific version number
288
304
`str` : snapshot name which contains the version
289
305
`datetime.datetime` : the version of the data that existed as_of the requested point in time
306
+ allow_secondary : `bool` or `None`
307
+ Override the default behavior for allowing reads from secondary members of a cluster:
308
+ `None` : use the settings from the top-level `Arctic` object used to query this version store.
309
+ `True` : allow reads from secondary members
310
+ `False` : only allow reads from primary members
290
311
291
312
Returns
292
313
-------
293
314
VersionedItem namedtuple which contains a .data and .metadata element
294
315
"""
295
- allow_secondary = self ._allow_secondary if allow_secondary is None else allow_secondary
296
316
try :
297
- read_preference = ReadPreference . NEAREST if allow_secondary else ReadPreference . PRIMARY
317
+ read_preference = self . _read_preference ( allow_secondary )
298
318
_version = self ._read_metadata (symbol , as_of = as_of , read_preference = read_preference )
299
319
return self ._do_read (symbol , _version , from_version , read_preference = read_preference , ** kwargs )
300
320
except (OperationFailure , AutoReconnect ) as e :
@@ -347,7 +367,7 @@ def _do_read(self, symbol, version, from_version=None, **kwargs):
347
367
_do_read_retry = mongo_retry (_do_read )
348
368
349
369
@mongo_retry
350
- def read_metadata (self , symbol , as_of = None ):
370
+ def read_metadata (self , symbol , as_of = None , allow_secondary = None ):
351
371
"""
352
372
Return the metadata saved for a symbol. This method is fast as it doesn't
353
373
actually load the data.
@@ -361,8 +381,13 @@ def read_metadata(self, symbol, as_of=None):
361
381
`int` : specific version number
362
382
`str` : snapshot name which contains the version
363
383
`datetime.datetime` : the version of the data that existed as_of the requested point in time
384
+ allow_secondary : `bool` or `None`
385
+ Override the default behavior for allowing reads from secondary members of a cluster:
386
+ `None` : use the settings from the top-level `Arctic` object used to query this version store.
387
+ `True` : allow reads from secondary members
388
+ `False` : only allow reads from primary members
364
389
"""
365
- _version = self ._read_metadata (symbol , as_of = as_of )
390
+ _version = self ._read_metadata (symbol , as_of = as_of , read_preference = self . _read_preference ( allow_secondary ) )
366
391
return VersionedItem (symbol = symbol , library = self ._arctic_lib .get_name (), version = _version ['version' ],
367
392
metadata = _version .pop ('metadata' , None ), data = None )
368
393
@@ -642,7 +667,7 @@ def delete(self, symbol):
642
667
'metadata.deleted' : {'$ne' : True }})
643
668
if not snapped_version :
644
669
self ._delete_version (symbol , sentinel .version )
645
- assert not self .has_symbol (symbol )
670
+ assert not self .has_symbol (symbol , allow_secondary = False )
646
671
647
672
def _write_audit (self , user , message , changed_version ):
648
673
"""
0 commit comments