~ubuntu-branches/ubuntu/wily/pymongo/wily-proposed

« back to all changes in this revision

Viewing changes to pymongo/ismaster.py

  • Committer: Package Import Robot
  • Author(s): Federico Ceratto
  • Date: 2015-04-26 22:43:13 UTC
  • mfrom: (24.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20150426224313-0hga2jphvf0rrmfe
Tags: 3.0.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 MongoDB, Inc.
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
# http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
 
14
 
 
15
"""Parse a response to the 'ismaster' command."""
 
16
 
 
17
import itertools
 
18
 
 
19
from bson.py3compat import imap
 
20
from pymongo import common
 
21
from pymongo.server_type import SERVER_TYPE
 
22
 
 
23
 
 
24
def _get_server_type(doc):
 
25
    """Determine the server type from an ismaster response."""
 
26
    if not doc.get('ok'):
 
27
        return SERVER_TYPE.Unknown
 
28
 
 
29
    if doc.get('isreplicaset'):
 
30
        return SERVER_TYPE.RSGhost
 
31
    elif doc.get('setName'):
 
32
        if doc.get('hidden'):
 
33
            return SERVER_TYPE.RSOther
 
34
        elif doc.get('ismaster'):
 
35
            return SERVER_TYPE.RSPrimary
 
36
        elif doc.get('secondary'):
 
37
            return SERVER_TYPE.RSSecondary
 
38
        elif doc.get('arbiterOnly'):
 
39
            return SERVER_TYPE.RSArbiter
 
40
        else:
 
41
            return SERVER_TYPE.RSOther
 
42
    elif doc.get('msg') == 'isdbgrid':
 
43
        return SERVER_TYPE.Mongos
 
44
    else:
 
45
        return SERVER_TYPE.Standalone
 
46
 
 
47
 
 
48
class IsMaster(object):
 
49
    __slots__ = ('_doc', '_server_type', '_is_writable', '_is_readable')
 
50
 
 
51
    def __init__(self, doc):
 
52
        """Parse an ismaster response from the server."""
 
53
        self._server_type = _get_server_type(doc)
 
54
        self._doc = doc
 
55
        self._is_writable = self._server_type in (
 
56
            SERVER_TYPE.RSPrimary,
 
57
            SERVER_TYPE.Standalone,
 
58
            SERVER_TYPE.Mongos)
 
59
 
 
60
        self._is_readable = (
 
61
            self.server_type == SERVER_TYPE.RSSecondary
 
62
            or self._is_writable)
 
63
 
 
64
    @property
 
65
    def server_type(self):
 
66
        return self._server_type
 
67
 
 
68
    @property
 
69
    def all_hosts(self):
 
70
        """List of hosts, passives, and arbiters known to this server."""
 
71
        return set(imap(common.clean_node, itertools.chain(
 
72
            self._doc.get('hosts', []),
 
73
            self._doc.get('passives', []),
 
74
            self._doc.get('arbiters', []))))
 
75
 
 
76
    @property
 
77
    def tags(self):
 
78
        """Replica set member tags or empty dict."""
 
79
        return self._doc.get('tags', {})
 
80
 
 
81
    @property
 
82
    def primary(self):
 
83
        """This server's opinion about who the primary is, or None."""
 
84
        if self._doc.get('primary'):
 
85
            return common.partition_node(self._doc['primary'])
 
86
        else:
 
87
            return None
 
88
 
 
89
    @property
 
90
    def replica_set_name(self):
 
91
        """Replica set name or None."""
 
92
        return self._doc.get('setName')
 
93
 
 
94
    @property
 
95
    def max_bson_size(self):
 
96
        return self._doc.get('maxBsonObjectSize', common.MAX_BSON_SIZE)
 
97
 
 
98
    @property
 
99
    def max_message_size(self):
 
100
        return self._doc.get('maxMessageSizeBytes', 2 * self.max_bson_size)
 
101
 
 
102
    @property
 
103
    def max_write_batch_size(self):
 
104
        return self._doc.get('maxWriteBatchSize', common.MAX_WRITE_BATCH_SIZE)
 
105
 
 
106
    @property
 
107
    def min_wire_version(self):
 
108
        return self._doc.get('minWireVersion', common.MIN_WIRE_VERSION)
 
109
 
 
110
    @property
 
111
    def max_wire_version(self):
 
112
        return self._doc.get('maxWireVersion', common.MAX_WIRE_VERSION)
 
113
 
 
114
    @property
 
115
    def is_writable(self):
 
116
        return self._is_writable
 
117
 
 
118
    @property
 
119
    def is_readable(self):
 
120
        return self._is_readable