Skip to content

Commit 359fbd8

Browse files
author
Mike Dirolf
committed
support for MaxKey and MinKey PYTHON-75
1 parent 0a6c5bb commit 359fbd8

5 files changed

Lines changed: 92 additions & 0 deletions

File tree

pymongo/_cbsonmodule.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ static PyObject* DBRef = NULL;
4040
static PyObject* RECompile = NULL;
4141
static PyObject* UUID = NULL;
4242
static PyObject* Timestamp = NULL;
43+
static PyObject* MinKey = NULL;
44+
static PyObject* MaxKey = NULL;
4345
static PyTypeObject* REType = NULL;
4446

4547
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
@@ -272,6 +274,8 @@ static int _reload_python_objects(void) {
272274
_reload_object(&ObjectId, "pymongo.objectid", "ObjectId") ||
273275
_reload_object(&DBRef, "pymongo.dbref", "DBRef") ||
274276
_reload_object(&Timestamp, "pymongo.timestamp", "Timestamp") ||
277+
_reload_object(&MinKey, "pymongo.min_key", "MinKey") ||
278+
_reload_object(&MaxKey, "pymongo.max_key", "MaxKey") ||
275279
_reload_object(&RECompile, "re", "compile")) {
276280
return 1;
277281
}
@@ -666,6 +670,12 @@ static int write_element_to_buffer(bson_buffer* buffer, int type_byte, PyObject*
666670
}
667671
*(buffer->buffer + type_byte) = 0x0B;
668672
return 1;
673+
} else if (PyObject_IsInstance(value, MinKey)) {
674+
*(buffer->buffer + type_byte) = 0xFF;
675+
return 1;
676+
} else if (PyObject_IsInstance(value, MaxKey)) {
677+
*(buffer->buffer + type_byte) = 0x7F;
678+
return 1;
669679
} else if (first_attempt) {
670680
/* Try reloading the modules and having one more go at it. */
671681
if (WARN(PyExc_RuntimeWarning, "couldn't encode - reloading python "
@@ -1518,6 +1528,16 @@ static PyObject* get_value(const char* buffer, int* position, int type,
15181528
*position += 8;
15191529
break;
15201530
}
1531+
case -1:
1532+
{
1533+
value = PyObject_CallFunctionObjArgs(MinKey, NULL);
1534+
break;
1535+
}
1536+
case 127:
1537+
{
1538+
value = PyObject_CallFunctionObjArgs(MaxKey, NULL);
1539+
break;
1540+
}
15211541
default:
15221542
{
15231543
PyObject* InvalidDocument = _error("InvalidDocument");

pymongo/bson.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
InvalidDocument,
2929
InvalidName,
3030
InvalidStringData)
31+
from pymongo.max_key import MaxKey
32+
from pymongo.min_key import MinKey
3133
from pymongo.objectid import ObjectId
3234
from pymongo.son import SON
3335
from timestamp import Timestamp
@@ -206,6 +208,8 @@ def _get_long(data, as_class):
206208
"\x10": _get_int, # number_int
207209
"\x11": _get_timestamp,
208210
"\x12": _get_long,
211+
"\xFF": lambda x, y: (MinKey(), x),
212+
"\x7F": lambda x, y: (MaxKey(), x)
209213
}
210214

211215

@@ -329,6 +333,10 @@ def _element_to_bson(key, value, check_keys):
329333
_make_c_string(flags)
330334
if isinstance(value, DBRef):
331335
return _element_to_bson(key, value.as_doc(), False)
336+
if isinstance(value, MinKey):
337+
return "\xFF" + name
338+
if isinstance(value, MaxKey):
339+
return "\x7F" + name
332340

333341
raise InvalidDocument("cannot convert value of type %s to bson" %
334342
type(value))

pymongo/max_key.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2010 10gen, 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+
"""Representation for the MongoDB internal MaxKey type.
16+
17+
.. versionadded:: 1.6+
18+
"""
19+
20+
class MaxKey(object):
21+
"""MongoDB internal MaxKey type.
22+
"""
23+
24+
def __eq__(self, other):
25+
if isinstance(other, MaxKey):
26+
return True
27+
return NotImplemented
28+
29+
def __repr__(self):
30+
return "MaxKey()"

pymongo/min_key.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2010 10gen, 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+
"""Representation for the MongoDB internal MinKey type.
16+
17+
.. versionadded:: 1.6+
18+
"""
19+
20+
class MinKey(object):
21+
"""MongoDB internal MinKey type.
22+
"""
23+
24+
def __eq__(self, other):
25+
if isinstance(other, MinKey):
26+
return True
27+
return NotImplemented
28+
29+
def __repr__(self):
30+
return "MinKey()"

test/test_bson.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
from pymongo.timestamp import Timestamp
3939
from pymongo.bson import BSON, is_valid, _to_dicts
4040
from pymongo.errors import InvalidDocument, InvalidStringData
41+
from pymongo.max_key import MaxKey
42+
from pymongo.min_key import MinKey
4143
import qcheck
4244

4345
class SomeZone(datetime.tzinfo):
@@ -171,6 +173,8 @@ def helper(dict):
171173
helper({"ref": DBRef("coll", 5)})
172174
helper({"ref": DBRef("coll", 5, "foo")})
173175
helper({"ref": Timestamp(1,2)})
176+
helper({"foo": MinKey()})
177+
helper({"foo": MaxKey()})
174178

175179
def from_then_to_dict(dict):
176180
return dict == (BSON.from_dict(dict)).to_dict()

0 commit comments

Comments
 (0)