|
26 | 26 | ConfigurationError, |
27 | 27 | InvalidName, |
28 | 28 | OperationFailure) |
29 | | -from pymongo import read_preferences as rp |
| 29 | +from pymongo.read_preferences import (ReadPreference, |
| 30 | + modes, secondary_ok_commands) |
30 | 31 |
|
31 | 32 |
|
32 | 33 | def _check_name(name): |
@@ -276,59 +277,56 @@ def _command(self, command, value=1, |
276 | 277 | """ |
277 | 278 |
|
278 | 279 | if isinstance(command, basestring): |
| 280 | + command_name = command.lower() |
279 | 281 | command = SON([(command, value)]) |
| 282 | + else: |
| 283 | + command_name = command.keys()[0].lower() |
| 284 | + |
| 285 | + orig = mode = kwargs.pop('read_preference', self.read_preference) |
| 286 | + tags = kwargs.pop('tag_sets', self.tag_sets) |
| 287 | + latency = kwargs.pop('secondary_acceptable_latency_ms', |
| 288 | + self.secondary_acceptable_latency_ms) |
| 289 | + as_class = kwargs.pop('as_class', None) |
280 | 290 |
|
281 | | - command_name = command.keys()[0].lower() |
282 | | - must_use_master = kwargs.pop('_use_master', False) |
283 | | - if command_name not in rp.secondary_ok_commands: |
284 | | - must_use_master = True |
| 291 | + if command_name not in secondary_ok_commands: |
| 292 | + mode = ReadPreference.PRIMARY |
285 | 293 |
|
286 | 294 | # Special-case: mapreduce can go to secondaries only if inline |
287 | | - if command_name == 'mapreduce': |
| 295 | + elif command_name == 'mapreduce': |
288 | 296 | out = command.get('out') or kwargs.get('out') |
289 | 297 | if not isinstance(out, dict) or not out.get('inline'): |
290 | | - must_use_master = True |
| 298 | + mode = ReadPreference.PRIMARY |
291 | 299 |
|
292 | 300 | # Special-case: aggregate with $out cannot go to secondaries. |
293 | | - if command_name == 'aggregate': |
| 301 | + elif command_name == 'aggregate': |
294 | 302 | for stage in kwargs.get('pipeline', []): |
295 | 303 | if '$out' in stage: |
296 | | - must_use_master = True |
| 304 | + mode = ReadPreference.PRIMARY |
297 | 305 | break |
298 | 306 |
|
299 | | - extra_opts = { |
300 | | - 'as_class': kwargs.pop('as_class', None), |
301 | | - '_must_use_master': must_use_master, |
302 | | - '_uuid_subtype': uuid_subtype |
303 | | - } |
304 | | - |
305 | | - extra_opts['read_preference'] = kwargs.pop( |
306 | | - 'read_preference', |
307 | | - self.read_preference) |
308 | | - extra_opts['tag_sets'] = kwargs.pop( |
309 | | - 'tag_sets', |
310 | | - self.tag_sets) |
311 | | - extra_opts['secondary_acceptable_latency_ms'] = kwargs.pop( |
312 | | - 'secondary_acceptable_latency_ms', |
313 | | - self.secondary_acceptable_latency_ms) |
314 | | - extra_opts['compile_re'] = compile_re |
315 | | - |
316 | | - fields = kwargs.get('fields') |
| 307 | + # Warn if mode will override read_preference. |
| 308 | + if mode != orig: |
| 309 | + warnings.warn("%s does not support %s read preference " |
| 310 | + "and will be routed to the primary instead." % |
| 311 | + (command_name, modes[orig]), UserWarning) |
| 312 | + tags = [{}] |
| 313 | + latency = None |
| 314 | + |
| 315 | + fields = kwargs.pop('fields', None) |
317 | 316 | if fields is not None and not isinstance(fields, dict): |
318 | | - kwargs['fields'] = helpers._fields_list_to_dict(fields) |
| 317 | + fields = helpers._fields_list_to_dict(fields) |
319 | 318 |
|
320 | 319 | command.update(kwargs) |
321 | 320 |
|
322 | | - # Warn if must_use_master will override read_preference. |
323 | | - if (extra_opts['read_preference'] != rp.ReadPreference.PRIMARY and |
324 | | - extra_opts['_must_use_master']): |
325 | | - warnings.warn("%s does not support %s read preference " |
326 | | - "and will be routed to the primary instead." % |
327 | | - (command_name, |
328 | | - rp.modes[extra_opts['read_preference']]), |
329 | | - UserWarning) |
330 | | - |
331 | | - cursor = self["$cmd"].find(command, **extra_opts).limit(-1) |
| 321 | + cursor = self["$cmd"].find(command, |
| 322 | + fields=fields, |
| 323 | + limit=-1, |
| 324 | + as_class=as_class, |
| 325 | + read_preference=mode, |
| 326 | + tag_sets=tags, |
| 327 | + secondary_acceptable_latency_ms=latency, |
| 328 | + compile_re=compile_re, |
| 329 | + _uuid_subtype=uuid_subtype) |
332 | 330 | for doc in cursor: |
333 | 331 | result = doc |
334 | 332 |
|
@@ -437,7 +435,8 @@ def collection_names(self, include_system_collections=True): |
437 | 435 | - `include_system_collections` (optional): if ``False`` list |
438 | 436 | will not include system collections (e.g ``system.indexes``) |
439 | 437 | """ |
440 | | - results = self["system.namespaces"].find(_must_use_master=True) |
| 438 | + results = self["system.namespaces"].find( |
| 439 | + read_preference=ReadPreference.PRIMARY) |
441 | 440 | names = [r["name"] for r in results] |
442 | 441 | names = [n[len(self.__name) + 1:] for n in names |
443 | 442 | if n.startswith(self.__name + ".") and "$" not in n] |
|
0 commit comments