Skip to content

Commit ce7ce38

Browse files
committed
Basic support for min and maxWireVersion PYTHON-587
1 parent b627509 commit ce7ce38

File tree

9 files changed

+83
-0
lines changed

9 files changed

+83
-0
lines changed

doc/api/pymongo/connection.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
.. autoattribute:: tz_aware
2828
.. autoattribute:: max_bson_size
2929
.. autoattribute:: max_message_size
30+
.. autoattribute:: min_wire_version
31+
.. autoattribute:: max_wire_version
3032
.. autoattribute:: read_preference
3133
.. autoattribute:: tag_sets
3234
.. autoattribute:: secondary_acceptable_latency_ms

doc/api/pymongo/mongo_client.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
.. autoattribute:: tz_aware
2828
.. autoattribute:: max_bson_size
2929
.. autoattribute:: max_message_size
30+
.. autoattribute:: min_wire_version
31+
.. autoattribute:: max_wire_version
3032
.. autoattribute:: read_preference
3133
.. autoattribute:: tag_sets
3234
.. autoattribute:: secondary_acceptable_latency_ms

doc/api/pymongo/mongo_replica_set_client.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
.. autoattribute:: tz_aware
2828
.. autoattribute:: max_bson_size
2929
.. autoattribute:: max_message_size
30+
.. autoattribute:: min_wire_version
31+
.. autoattribute:: max_wire_version
3032
.. autoattribute:: auto_start_request
3133
.. autoattribute:: read_preference
3234
.. autoattribute:: tag_sets

doc/api/pymongo/replica_set_connection.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
.. autoattribute:: tz_aware
2828
.. autoattribute:: max_bson_size
2929
.. autoattribute:: max_message_size
30+
.. autoattribute:: min_wire_version
31+
.. autoattribute:: max_wire_version
3032
.. autoattribute:: auto_start_request
3133
.. autoattribute:: read_preference
3234
.. autoattribute:: tag_sets

pymongo/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
# Defaults until we connect to a server and get updated limits.
4040
MAX_BSON_SIZE = 16 * (1024 ** 2)
4141
MAX_MESSAGE_SIZE = 2 * MAX_BSON_SIZE
42+
MIN_WIRE_VERSION = 0
43+
MAX_WIRE_VERSION = 0
4244

4345

4446
def raise_config_error(key, dummy):

pymongo/mongo_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class MongoClient(common.BaseObject):
8585

8686
__max_bson_size = common.MAX_BSON_SIZE
8787
__max_message_size = common.MAX_MESSAGE_SIZE
88+
__min_wire_version = common.MIN_WIRE_VERSION
89+
# TODO: write commands with _connect=False
90+
__max_wire_version = common.MAX_WIRE_VERSION
8891

8992
def __init__(self, host=None, port=None, max_pool_size=100,
9093
document_class=dict, tz_aware=False, _connect=True,
@@ -595,6 +598,26 @@ def max_message_size(self):
595598
"""
596599
return self.__max_message_size
597600

601+
@property
602+
def min_wire_version(self):
603+
"""The minWireVersion reported by the server.
604+
605+
Returns ``0`` when connected to server versions prior to MongoDB 2.6.
606+
607+
.. versionadded:: 2.7
608+
"""
609+
return self.__min_wire_version
610+
611+
@property
612+
def max_wire_version(self):
613+
"""The maxWireVersion reported by the server.
614+
615+
Returns ``0`` when connected to server versions prior to MongoDB 2.6.
616+
617+
.. versionadded:: 2.7
618+
"""
619+
return self.__max_wire_version
620+
598621
def __simple_command(self, sock_info, dbname, spec):
599622
"""Send a command to the server.
600623
"""
@@ -639,6 +662,10 @@ def __try_node(self, node):
639662
self.__max_bson_size = response["maxBsonObjectSize"]
640663
if "maxMessageSizeBytes" in response:
641664
self.__max_message_size = response["maxMessageSizeBytes"]
665+
if "minWireVersion" in response:
666+
self.__min_wire_version = response["minWireVersion"]
667+
if "maxWireVersion" in response:
668+
self.__max_wire_version = response["maxWireVersion"]
642669

643670
# Replica Set?
644671
if not self.__direct:

pymongo/mongo_replica_set_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ def __init__(self, host, connection_pool, ismaster_response, ping_time, up):
422422
'maxBsonObjectSize', common.MAX_BSON_SIZE)
423423
self.max_message_size = ismaster_response.get(
424424
'maxMessageSizeBytes', 2 * self.max_bson_size)
425+
self.min_wire_version = ismaster_response.get('minWireVersion',
426+
common.MIN_WIRE_VERSION)
427+
self.max_wire_version = ismaster_response.get('maxWireVersion',
428+
common.MAX_WIRE_VERSION)
425429

426430
def clone_with(self, ismaster_response, ping_time_sample):
427431
"""Get a clone updated with ismaster response and a single ping time.
@@ -1017,6 +1021,32 @@ def max_message_size(self):
10171021
return rs_state.primary_member.max_message_size
10181022
return common.MAX_MESSAGE_SIZE
10191023

1024+
@property
1025+
def min_wire_version(self):
1026+
"""The minWireVersion reported by the server.
1027+
1028+
Returns ``0`` when connected to server versions prior to MongoDB 2.6.
1029+
1030+
.. versionadded:: 2.7
1031+
"""
1032+
rs_state = self.__rs_state
1033+
if rs_state.primary_member:
1034+
return rs_state.primary_member.min_wire_version
1035+
return common.MIN_WIRE_VERSION
1036+
1037+
@property
1038+
def max_wire_version(self):
1039+
"""The maxWireVersion reported by the server.
1040+
1041+
Returns ``0`` when connected to server versions prior to MongoDB 2.6.
1042+
1043+
.. versionadded:: 2.7
1044+
"""
1045+
rs_state = self.__rs_state
1046+
if rs_state.primary_member:
1047+
return rs_state.primary_member.max_wire_version
1048+
return common.MAX_WIRE_VERSION
1049+
10201050
@property
10211051
def auto_start_request(self):
10221052
"""Is auto_start_request enabled?

test/test_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,21 @@ def test_init_disconnected(self):
9797
self.assertEqual(dict, c.get_document_class())
9898
self.assertIsInstance(c.tz_aware, bool)
9999
self.assertIsInstance(c.max_bson_size, int)
100+
self.assertIsInstance(c.min_wire_version, int)
101+
self.assertIsInstance(c.max_wire_version, int)
100102
self.assertEqual(None, c.host)
101103
self.assertEqual(None, c.port)
102104

103105
c.pymongo_test.test.find_one() # Auto-connect.
104106
self.assertEqual(host, c.host)
105107
self.assertEqual(port, c.port)
106108

109+
if version.at_least(c, (2, 5, 4, -1)):
110+
self.assertTrue(c.max_wire_version > 0)
111+
else:
112+
self.assertEqual(c.max_wire_version, 0)
113+
self.assertTrue(c.min_wire_version >= 0)
114+
107115
bad_host = "somedomainthatdoesntexist.org"
108116
c = MongoClient(bad_host, port, connectTimeoutMS=1, _connect=False)
109117
self.assertRaises(ConnectionFailure, c.pymongo_test.test.find_one)

test/test_replica_set_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def test_init_disconnected(self):
129129
self.assertIsInstance(c.auto_start_request, bool)
130130
self.assertIsInstance(c.tz_aware, bool)
131131
self.assertIsInstance(c.max_bson_size, int)
132+
self.assertIsInstance(c.min_wire_version, int)
133+
self.assertIsInstance(c.max_wire_version, int)
132134
self.assertIsInstance(c.seeds, set)
133135
self.assertIsInstance(c.hosts, frozenset)
134136
self.assertIsInstance(c.arbiters, frozenset)
@@ -140,6 +142,12 @@ def test_init_disconnected(self):
140142
self.assertTrue(c.primary)
141143
self.assertTrue(c.secondaries)
142144

145+
if version.at_least(c, (2, 5, 4, -1)):
146+
self.assertTrue(c.max_wire_version > 0)
147+
else:
148+
self.assertEqual(c.max_wire_version, 0)
149+
self.assertTrue(c.min_wire_version >= 0)
150+
143151
c = self._get_client(_connect=False)
144152
c.pymongo_test.test.update({}, {}) # Auto-connect for write.
145153
self.assertTrue(c.primary)

0 commit comments

Comments
 (0)