sensu-plugins-mongodb-mrtrotl 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1 -0
- data/LICENSE +22 -0
- data/README.md +27 -0
- data/bin/check-mongodb-metric.rb +144 -0
- data/bin/check-mongodb-query-count.rb +267 -0
- data/bin/check-mongodb.py +1644 -0
- data/bin/check-mongodb.rb +5 -0
- data/bin/metrics-mongodb-replication.rb +254 -0
- data/bin/metrics-mongodb.rb +133 -0
- data/lib/bson/__init__.py +1347 -0
- data/lib/bson/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/_helpers.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/binary.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/code.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/codec_options.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/dbref.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/decimal128.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/int64.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/json_util.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/max_key.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/min_key.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/objectid.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/raw_bson.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/regex.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/son.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/timestamp.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/tz_util.cpython-310.pyc +0 -0
- data/lib/bson/_cbson.cpython-310-x86_64-linux-gnu.so +0 -0
- data/lib/bson/_helpers.py +41 -0
- data/lib/bson/binary.py +364 -0
- data/lib/bson/code.py +101 -0
- data/lib/bson/codec_options.py +414 -0
- data/lib/bson/codec_options.pyi +100 -0
- data/lib/bson/dbref.py +133 -0
- data/lib/bson/decimal128.py +314 -0
- data/lib/bson/errors.py +35 -0
- data/lib/bson/int64.py +39 -0
- data/lib/bson/json_util.py +874 -0
- data/lib/bson/max_key.py +55 -0
- data/lib/bson/min_key.py +55 -0
- data/lib/bson/objectid.py +286 -0
- data/lib/bson/py.typed +2 -0
- data/lib/bson/raw_bson.py +175 -0
- data/lib/bson/regex.py +135 -0
- data/lib/bson/son.py +208 -0
- data/lib/bson/timestamp.py +124 -0
- data/lib/bson/tz_util.py +52 -0
- data/lib/gridfs/__init__.py +1015 -0
- data/lib/gridfs/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/gridfs/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/gridfs/__pycache__/grid_file.cpython-310.pyc +0 -0
- data/lib/gridfs/errors.py +33 -0
- data/lib/gridfs/grid_file.py +907 -0
- data/lib/gridfs/py.typed +2 -0
- data/lib/pymongo/__init__.py +185 -0
- data/lib/pymongo/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/_csot.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/aggregation.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/auth.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/auth_aws.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/bulk.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/change_stream.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/client_options.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/client_session.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/collation.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/collection.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/command_cursor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/common.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/compression_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/cursor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/daemon.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/database.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/driver_info.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/encryption.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/encryption_options.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/event_loggers.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/hello.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/helpers.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/max_staleness_selectors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/message.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/mongo_client.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/monitor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/monitoring.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/network.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ocsp_cache.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ocsp_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/operations.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/periodic_executor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/pool.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/pyopenssl_context.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/read_concern.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/read_preferences.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/response.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/results.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/saslprep.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_api.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_description.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_selectors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_type.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/settings.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/socket_checker.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/srv_resolver.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ssl_context.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ssl_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/topology.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/topology_description.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/typings.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/uri_parser.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/write_concern.cpython-310.pyc +0 -0
- data/lib/pymongo/_cmessage.cpython-310-x86_64-linux-gnu.so +0 -0
- data/lib/pymongo/_csot.py +118 -0
- data/lib/pymongo/aggregation.py +229 -0
- data/lib/pymongo/auth.py +549 -0
- data/lib/pymongo/auth_aws.py +94 -0
- data/lib/pymongo/bulk.py +513 -0
- data/lib/pymongo/change_stream.py +457 -0
- data/lib/pymongo/client_options.py +302 -0
- data/lib/pymongo/client_session.py +1112 -0
- data/lib/pymongo/collation.py +224 -0
- data/lib/pymongo/collection.py +3204 -0
- data/lib/pymongo/command_cursor.py +353 -0
- data/lib/pymongo/common.py +984 -0
- data/lib/pymongo/compression_support.py +149 -0
- data/lib/pymongo/cursor.py +1345 -0
- data/lib/pymongo/daemon.py +141 -0
- data/lib/pymongo/database.py +1202 -0
- data/lib/pymongo/driver_info.py +42 -0
- data/lib/pymongo/encryption.py +884 -0
- data/lib/pymongo/encryption_options.py +221 -0
- data/lib/pymongo/errors.py +365 -0
- data/lib/pymongo/event_loggers.py +221 -0
- data/lib/pymongo/hello.py +219 -0
- data/lib/pymongo/helpers.py +259 -0
- data/lib/pymongo/max_staleness_selectors.py +114 -0
- data/lib/pymongo/message.py +1440 -0
- data/lib/pymongo/mongo_client.py +2144 -0
- data/lib/pymongo/monitor.py +440 -0
- data/lib/pymongo/monitoring.py +1801 -0
- data/lib/pymongo/network.py +311 -0
- data/lib/pymongo/ocsp_cache.py +87 -0
- data/lib/pymongo/ocsp_support.py +372 -0
- data/lib/pymongo/operations.py +507 -0
- data/lib/pymongo/periodic_executor.py +183 -0
- data/lib/pymongo/pool.py +1660 -0
- data/lib/pymongo/py.typed +2 -0
- data/lib/pymongo/pyopenssl_context.py +383 -0
- data/lib/pymongo/read_concern.py +75 -0
- data/lib/pymongo/read_preferences.py +609 -0
- data/lib/pymongo/response.py +109 -0
- data/lib/pymongo/results.py +217 -0
- data/lib/pymongo/saslprep.py +113 -0
- data/lib/pymongo/server.py +247 -0
- data/lib/pymongo/server_api.py +170 -0
- data/lib/pymongo/server_description.py +285 -0
- data/lib/pymongo/server_selectors.py +153 -0
- data/lib/pymongo/server_type.py +32 -0
- data/lib/pymongo/settings.py +159 -0
- data/lib/pymongo/socket_checker.py +104 -0
- data/lib/pymongo/srv_resolver.py +126 -0
- data/lib/pymongo/ssl_context.py +39 -0
- data/lib/pymongo/ssl_support.py +99 -0
- data/lib/pymongo/topology.py +890 -0
- data/lib/pymongo/topology_description.py +639 -0
- data/lib/pymongo/typings.py +39 -0
- data/lib/pymongo/uri_parser.py +624 -0
- data/lib/pymongo/write_concern.py +129 -0
- data/lib/pymongo-4.2.0.dist-info/INSTALLER +1 -0
- data/lib/pymongo-4.2.0.dist-info/LICENSE +201 -0
- data/lib/pymongo-4.2.0.dist-info/METADATA +250 -0
- data/lib/pymongo-4.2.0.dist-info/RECORD +167 -0
- data/lib/pymongo-4.2.0.dist-info/REQUESTED +0 -0
- data/lib/pymongo-4.2.0.dist-info/WHEEL +6 -0
- data/lib/pymongo-4.2.0.dist-info/top_level.txt +3 -0
- data/lib/sensu-plugins-mongodb/metrics.rb +391 -0
- data/lib/sensu-plugins-mongodb/version.rb +9 -0
- data/lib/sensu-plugins-mongodb.rb +1 -0
- metadata +407 -0
data/lib/bson/max_key.py
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Copyright 2010-present 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
|
+
"""Representation for the MongoDB internal MaxKey type.
|
16
|
+
"""
|
17
|
+
from typing import Any
|
18
|
+
|
19
|
+
|
20
|
+
class MaxKey(object):
|
21
|
+
"""MongoDB internal MaxKey type."""
|
22
|
+
|
23
|
+
__slots__ = ()
|
24
|
+
|
25
|
+
_type_marker = 127
|
26
|
+
|
27
|
+
def __getstate__(self) -> Any:
|
28
|
+
return {}
|
29
|
+
|
30
|
+
def __setstate__(self, state: Any) -> None:
|
31
|
+
pass
|
32
|
+
|
33
|
+
def __eq__(self, other: Any) -> bool:
|
34
|
+
return isinstance(other, MaxKey)
|
35
|
+
|
36
|
+
def __hash__(self) -> int:
|
37
|
+
return hash(self._type_marker)
|
38
|
+
|
39
|
+
def __ne__(self, other: Any) -> bool:
|
40
|
+
return not self == other
|
41
|
+
|
42
|
+
def __le__(self, other: Any) -> bool:
|
43
|
+
return isinstance(other, MaxKey)
|
44
|
+
|
45
|
+
def __lt__(self, dummy: Any) -> bool:
|
46
|
+
return False
|
47
|
+
|
48
|
+
def __ge__(self, dummy: Any) -> bool:
|
49
|
+
return True
|
50
|
+
|
51
|
+
def __gt__(self, other: Any) -> bool:
|
52
|
+
return not isinstance(other, MaxKey)
|
53
|
+
|
54
|
+
def __repr__(self):
|
55
|
+
return "MaxKey()"
|
data/lib/bson/min_key.py
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Copyright 2010-present 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
|
+
"""Representation for the MongoDB internal MinKey type.
|
16
|
+
"""
|
17
|
+
from typing import Any
|
18
|
+
|
19
|
+
|
20
|
+
class MinKey(object):
|
21
|
+
"""MongoDB internal MinKey type."""
|
22
|
+
|
23
|
+
__slots__ = ()
|
24
|
+
|
25
|
+
_type_marker = 255
|
26
|
+
|
27
|
+
def __getstate__(self) -> Any:
|
28
|
+
return {}
|
29
|
+
|
30
|
+
def __setstate__(self, state: Any) -> None:
|
31
|
+
pass
|
32
|
+
|
33
|
+
def __eq__(self, other: Any) -> bool:
|
34
|
+
return isinstance(other, MinKey)
|
35
|
+
|
36
|
+
def __hash__(self) -> int:
|
37
|
+
return hash(self._type_marker)
|
38
|
+
|
39
|
+
def __ne__(self, other: Any) -> bool:
|
40
|
+
return not self == other
|
41
|
+
|
42
|
+
def __le__(self, dummy: Any) -> bool:
|
43
|
+
return True
|
44
|
+
|
45
|
+
def __lt__(self, other: Any) -> bool:
|
46
|
+
return not isinstance(other, MinKey)
|
47
|
+
|
48
|
+
def __ge__(self, other: Any) -> bool:
|
49
|
+
return isinstance(other, MinKey)
|
50
|
+
|
51
|
+
def __gt__(self, dummy: Any) -> bool:
|
52
|
+
return False
|
53
|
+
|
54
|
+
def __repr__(self):
|
55
|
+
return "MinKey()"
|
@@ -0,0 +1,286 @@
|
|
1
|
+
# Copyright 2009-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
|
+
"""Tools for working with MongoDB ObjectIds.
|
16
|
+
"""
|
17
|
+
|
18
|
+
import binascii
|
19
|
+
import calendar
|
20
|
+
import datetime
|
21
|
+
import os
|
22
|
+
import struct
|
23
|
+
import threading
|
24
|
+
import time
|
25
|
+
from random import SystemRandom
|
26
|
+
from typing import Any, NoReturn, Optional, Type, Union
|
27
|
+
|
28
|
+
from bson.errors import InvalidId
|
29
|
+
from bson.tz_util import utc
|
30
|
+
|
31
|
+
_MAX_COUNTER_VALUE = 0xFFFFFF
|
32
|
+
|
33
|
+
|
34
|
+
def _raise_invalid_id(oid: str) -> NoReturn:
|
35
|
+
raise InvalidId(
|
36
|
+
"%r is not a valid ObjectId, it must be a 12-byte input"
|
37
|
+
" or a 24-character hex string" % oid
|
38
|
+
)
|
39
|
+
|
40
|
+
|
41
|
+
def _random_bytes() -> bytes:
|
42
|
+
"""Get the 5-byte random field of an ObjectId."""
|
43
|
+
return os.urandom(5)
|
44
|
+
|
45
|
+
|
46
|
+
class ObjectId(object):
|
47
|
+
"""A MongoDB ObjectId."""
|
48
|
+
|
49
|
+
_pid = os.getpid()
|
50
|
+
|
51
|
+
_inc = SystemRandom().randint(0, _MAX_COUNTER_VALUE)
|
52
|
+
_inc_lock = threading.Lock()
|
53
|
+
|
54
|
+
__random = _random_bytes()
|
55
|
+
|
56
|
+
__slots__ = ("__id",)
|
57
|
+
|
58
|
+
_type_marker = 7
|
59
|
+
|
60
|
+
def __init__(self, oid: Optional[Union[str, "ObjectId", bytes]] = None) -> None:
|
61
|
+
"""Initialize a new ObjectId.
|
62
|
+
|
63
|
+
An ObjectId is a 12-byte unique identifier consisting of:
|
64
|
+
|
65
|
+
- a 4-byte value representing the seconds since the Unix epoch,
|
66
|
+
- a 5-byte random value,
|
67
|
+
- a 3-byte counter, starting with a random value.
|
68
|
+
|
69
|
+
By default, ``ObjectId()`` creates a new unique identifier. The
|
70
|
+
optional parameter `oid` can be an :class:`ObjectId`, or any 12
|
71
|
+
:class:`bytes`.
|
72
|
+
|
73
|
+
For example, the 12 bytes b'foo-bar-quux' do not follow the ObjectId
|
74
|
+
specification but they are acceptable input::
|
75
|
+
|
76
|
+
>>> ObjectId(b'foo-bar-quux')
|
77
|
+
ObjectId('666f6f2d6261722d71757578')
|
78
|
+
|
79
|
+
`oid` can also be a :class:`str` of 24 hex digits::
|
80
|
+
|
81
|
+
>>> ObjectId('0123456789ab0123456789ab')
|
82
|
+
ObjectId('0123456789ab0123456789ab')
|
83
|
+
|
84
|
+
Raises :class:`~bson.errors.InvalidId` if `oid` is not 12 bytes nor
|
85
|
+
24 hex digits, or :class:`TypeError` if `oid` is not an accepted type.
|
86
|
+
|
87
|
+
:Parameters:
|
88
|
+
- `oid` (optional): a valid ObjectId.
|
89
|
+
|
90
|
+
.. seealso:: The MongoDB documentation on `ObjectIds <http://dochub.mongodb.org/core/objectids>`_.
|
91
|
+
|
92
|
+
.. versionchanged:: 3.8
|
93
|
+
:class:`~bson.objectid.ObjectId` now implements the `ObjectID
|
94
|
+
specification version 0.2
|
95
|
+
<https://github.com/mongodb/specifications/blob/master/source/
|
96
|
+
objectid.rst>`_.
|
97
|
+
"""
|
98
|
+
if oid is None:
|
99
|
+
self.__generate()
|
100
|
+
elif isinstance(oid, bytes) and len(oid) == 12:
|
101
|
+
self.__id = oid
|
102
|
+
else:
|
103
|
+
self.__validate(oid)
|
104
|
+
|
105
|
+
@classmethod
|
106
|
+
def from_datetime(cls: Type["ObjectId"], generation_time: datetime.datetime) -> "ObjectId":
|
107
|
+
"""Create a dummy ObjectId instance with a specific generation time.
|
108
|
+
|
109
|
+
This method is useful for doing range queries on a field
|
110
|
+
containing :class:`ObjectId` instances.
|
111
|
+
|
112
|
+
.. warning::
|
113
|
+
It is not safe to insert a document containing an ObjectId
|
114
|
+
generated using this method. This method deliberately
|
115
|
+
eliminates the uniqueness guarantee that ObjectIds
|
116
|
+
generally provide. ObjectIds generated with this method
|
117
|
+
should be used exclusively in queries.
|
118
|
+
|
119
|
+
`generation_time` will be converted to UTC. Naive datetime
|
120
|
+
instances will be treated as though they already contain UTC.
|
121
|
+
|
122
|
+
An example using this helper to get documents where ``"_id"``
|
123
|
+
was generated before January 1, 2010 would be:
|
124
|
+
|
125
|
+
>>> gen_time = datetime.datetime(2010, 1, 1)
|
126
|
+
>>> dummy_id = ObjectId.from_datetime(gen_time)
|
127
|
+
>>> result = collection.find({"_id": {"$lt": dummy_id}})
|
128
|
+
|
129
|
+
:Parameters:
|
130
|
+
- `generation_time`: :class:`~datetime.datetime` to be used
|
131
|
+
as the generation time for the resulting ObjectId.
|
132
|
+
"""
|
133
|
+
offset = generation_time.utcoffset()
|
134
|
+
if offset is not None:
|
135
|
+
generation_time = generation_time - offset
|
136
|
+
timestamp = calendar.timegm(generation_time.timetuple())
|
137
|
+
oid = struct.pack(">I", int(timestamp)) + b"\x00\x00\x00\x00\x00\x00\x00\x00"
|
138
|
+
return cls(oid)
|
139
|
+
|
140
|
+
@classmethod
|
141
|
+
def is_valid(cls: Type["ObjectId"], oid: Any) -> bool:
|
142
|
+
"""Checks if a `oid` string is valid or not.
|
143
|
+
|
144
|
+
:Parameters:
|
145
|
+
- `oid`: the object id to validate
|
146
|
+
|
147
|
+
.. versionadded:: 2.3
|
148
|
+
"""
|
149
|
+
if not oid:
|
150
|
+
return False
|
151
|
+
|
152
|
+
try:
|
153
|
+
ObjectId(oid)
|
154
|
+
return True
|
155
|
+
except (InvalidId, TypeError):
|
156
|
+
return False
|
157
|
+
|
158
|
+
@classmethod
|
159
|
+
def _random(cls) -> bytes:
|
160
|
+
"""Generate a 5-byte random number once per process."""
|
161
|
+
pid = os.getpid()
|
162
|
+
if pid != cls._pid:
|
163
|
+
cls._pid = pid
|
164
|
+
cls.__random = _random_bytes()
|
165
|
+
return cls.__random
|
166
|
+
|
167
|
+
def __generate(self) -> None:
|
168
|
+
"""Generate a new value for this ObjectId."""
|
169
|
+
|
170
|
+
# 4 bytes current time
|
171
|
+
oid = struct.pack(">I", int(time.time()))
|
172
|
+
|
173
|
+
# 5 bytes random
|
174
|
+
oid += ObjectId._random()
|
175
|
+
|
176
|
+
# 3 bytes inc
|
177
|
+
with ObjectId._inc_lock:
|
178
|
+
oid += struct.pack(">I", ObjectId._inc)[1:4]
|
179
|
+
ObjectId._inc = (ObjectId._inc + 1) % (_MAX_COUNTER_VALUE + 1)
|
180
|
+
|
181
|
+
self.__id = oid
|
182
|
+
|
183
|
+
def __validate(self, oid: Any) -> None:
|
184
|
+
"""Validate and use the given id for this ObjectId.
|
185
|
+
|
186
|
+
Raises TypeError if id is not an instance of
|
187
|
+
(:class:`basestring` (:class:`str` or :class:`bytes`
|
188
|
+
in python 3), ObjectId) and InvalidId if it is not a
|
189
|
+
valid ObjectId.
|
190
|
+
|
191
|
+
:Parameters:
|
192
|
+
- `oid`: a valid ObjectId
|
193
|
+
"""
|
194
|
+
if isinstance(oid, ObjectId):
|
195
|
+
self.__id = oid.binary
|
196
|
+
elif isinstance(oid, str):
|
197
|
+
if len(oid) == 24:
|
198
|
+
try:
|
199
|
+
self.__id = bytes.fromhex(oid)
|
200
|
+
except (TypeError, ValueError):
|
201
|
+
_raise_invalid_id(oid)
|
202
|
+
else:
|
203
|
+
_raise_invalid_id(oid)
|
204
|
+
else:
|
205
|
+
raise TypeError(
|
206
|
+
"id must be an instance of (bytes, str, ObjectId), not %s" % (type(oid),)
|
207
|
+
)
|
208
|
+
|
209
|
+
@property
|
210
|
+
def binary(self) -> bytes:
|
211
|
+
"""12-byte binary representation of this ObjectId."""
|
212
|
+
return self.__id
|
213
|
+
|
214
|
+
@property
|
215
|
+
def generation_time(self) -> datetime.datetime:
|
216
|
+
"""A :class:`datetime.datetime` instance representing the time of
|
217
|
+
generation for this :class:`ObjectId`.
|
218
|
+
|
219
|
+
The :class:`datetime.datetime` is timezone aware, and
|
220
|
+
represents the generation time in UTC. It is precise to the
|
221
|
+
second.
|
222
|
+
"""
|
223
|
+
timestamp = struct.unpack(">I", self.__id[0:4])[0]
|
224
|
+
return datetime.datetime.fromtimestamp(timestamp, utc)
|
225
|
+
|
226
|
+
def __getstate__(self) -> bytes:
|
227
|
+
"""return value of object for pickling.
|
228
|
+
needed explicitly because __slots__() defined.
|
229
|
+
"""
|
230
|
+
return self.__id
|
231
|
+
|
232
|
+
def __setstate__(self, value: Any) -> None:
|
233
|
+
"""explicit state set from pickling"""
|
234
|
+
# Provide backwards compatability with OIDs
|
235
|
+
# pickled with pymongo-1.9 or older.
|
236
|
+
if isinstance(value, dict):
|
237
|
+
oid = value["_ObjectId__id"]
|
238
|
+
else:
|
239
|
+
oid = value
|
240
|
+
# ObjectIds pickled in python 2.x used `str` for __id.
|
241
|
+
# In python 3.x this has to be converted to `bytes`
|
242
|
+
# by encoding latin-1.
|
243
|
+
if isinstance(oid, str):
|
244
|
+
self.__id = oid.encode("latin-1")
|
245
|
+
else:
|
246
|
+
self.__id = oid
|
247
|
+
|
248
|
+
def __str__(self) -> str:
|
249
|
+
return binascii.hexlify(self.__id).decode()
|
250
|
+
|
251
|
+
def __repr__(self):
|
252
|
+
return "ObjectId('%s')" % (str(self),)
|
253
|
+
|
254
|
+
def __eq__(self, other: Any) -> bool:
|
255
|
+
if isinstance(other, ObjectId):
|
256
|
+
return self.__id == other.binary
|
257
|
+
return NotImplemented
|
258
|
+
|
259
|
+
def __ne__(self, other: Any) -> bool:
|
260
|
+
if isinstance(other, ObjectId):
|
261
|
+
return self.__id != other.binary
|
262
|
+
return NotImplemented
|
263
|
+
|
264
|
+
def __lt__(self, other: Any) -> bool:
|
265
|
+
if isinstance(other, ObjectId):
|
266
|
+
return self.__id < other.binary
|
267
|
+
return NotImplemented
|
268
|
+
|
269
|
+
def __le__(self, other: Any) -> bool:
|
270
|
+
if isinstance(other, ObjectId):
|
271
|
+
return self.__id <= other.binary
|
272
|
+
return NotImplemented
|
273
|
+
|
274
|
+
def __gt__(self, other: Any) -> bool:
|
275
|
+
if isinstance(other, ObjectId):
|
276
|
+
return self.__id > other.binary
|
277
|
+
return NotImplemented
|
278
|
+
|
279
|
+
def __ge__(self, other: Any) -> bool:
|
280
|
+
if isinstance(other, ObjectId):
|
281
|
+
return self.__id >= other.binary
|
282
|
+
return NotImplemented
|
283
|
+
|
284
|
+
def __hash__(self) -> int:
|
285
|
+
"""Get a hash value for this :class:`ObjectId`."""
|
286
|
+
return hash(self.__id)
|
data/lib/bson/py.typed
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
# Copyright 2015-present 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
|
+
"""Tools for representing raw BSON documents.
|
16
|
+
|
17
|
+
Inserting and Retrieving RawBSONDocuments
|
18
|
+
=========================================
|
19
|
+
|
20
|
+
Example: Moving a document between different databases/collections
|
21
|
+
|
22
|
+
.. doctest::
|
23
|
+
|
24
|
+
>>> import bson
|
25
|
+
>>> from pymongo import MongoClient
|
26
|
+
>>> from bson.raw_bson import RawBSONDocument
|
27
|
+
>>> client = MongoClient(document_class=RawBSONDocument)
|
28
|
+
>>> client.drop_database('db')
|
29
|
+
>>> client.drop_database('replica_db')
|
30
|
+
>>> db = client.db
|
31
|
+
>>> result = db.test.insert_many([{'_id': 1, 'a': 1},
|
32
|
+
... {'_id': 2, 'b': 1},
|
33
|
+
... {'_id': 3, 'c': 1},
|
34
|
+
... {'_id': 4, 'd': 1}])
|
35
|
+
>>> replica_db = client.replica_db
|
36
|
+
>>> for doc in db.test.find():
|
37
|
+
... print(f"raw document: {doc.raw}")
|
38
|
+
... print(f"decoded document: {bson.decode(doc.raw)}")
|
39
|
+
... result = replica_db.test.insert_one(doc)
|
40
|
+
raw document: b'...'
|
41
|
+
decoded document: {'_id': 1, 'a': 1}
|
42
|
+
raw document: b'...'
|
43
|
+
decoded document: {'_id': 2, 'b': 1}
|
44
|
+
raw document: b'...'
|
45
|
+
decoded document: {'_id': 3, 'c': 1}
|
46
|
+
raw document: b'...'
|
47
|
+
decoded document: {'_id': 4, 'd': 1}
|
48
|
+
|
49
|
+
For use cases like moving documents across different databases or writing binary
|
50
|
+
blobs to disk, using raw BSON documents provides better speed and avoids the
|
51
|
+
overhead of decoding or encoding BSON.
|
52
|
+
"""
|
53
|
+
|
54
|
+
from typing import Any, ItemsView, Iterator, Mapping, Optional
|
55
|
+
|
56
|
+
from bson import _get_object_size, _raw_to_dict
|
57
|
+
from bson.codec_options import _RAW_BSON_DOCUMENT_MARKER
|
58
|
+
from bson.codec_options import DEFAULT_CODEC_OPTIONS as DEFAULT
|
59
|
+
from bson.codec_options import CodecOptions
|
60
|
+
from bson.son import SON
|
61
|
+
|
62
|
+
|
63
|
+
class RawBSONDocument(Mapping[str, Any]):
|
64
|
+
"""Representation for a MongoDB document that provides access to the raw
|
65
|
+
BSON bytes that compose it.
|
66
|
+
|
67
|
+
Only when a field is accessed or modified within the document does
|
68
|
+
RawBSONDocument decode its bytes.
|
69
|
+
"""
|
70
|
+
|
71
|
+
__slots__ = ("__raw", "__inflated_doc", "__codec_options")
|
72
|
+
_type_marker = _RAW_BSON_DOCUMENT_MARKER
|
73
|
+
|
74
|
+
def __init__(self, bson_bytes: bytes, codec_options: Optional[CodecOptions] = None) -> None:
|
75
|
+
"""Create a new :class:`RawBSONDocument`
|
76
|
+
|
77
|
+
:class:`RawBSONDocument` is a representation of a BSON document that
|
78
|
+
provides access to the underlying raw BSON bytes. Only when a field is
|
79
|
+
accessed or modified within the document does RawBSONDocument decode
|
80
|
+
its bytes.
|
81
|
+
|
82
|
+
:class:`RawBSONDocument` implements the ``Mapping`` abstract base
|
83
|
+
class from the standard library so it can be used like a read-only
|
84
|
+
``dict``::
|
85
|
+
|
86
|
+
>>> from bson import encode
|
87
|
+
>>> raw_doc = RawBSONDocument(encode({'_id': 'my_doc'}))
|
88
|
+
>>> raw_doc.raw
|
89
|
+
b'...'
|
90
|
+
>>> raw_doc['_id']
|
91
|
+
'my_doc'
|
92
|
+
|
93
|
+
:Parameters:
|
94
|
+
- `bson_bytes`: the BSON bytes that compose this document
|
95
|
+
- `codec_options` (optional): An instance of
|
96
|
+
:class:`~bson.codec_options.CodecOptions` whose ``document_class``
|
97
|
+
must be :class:`RawBSONDocument`. The default is
|
98
|
+
:attr:`DEFAULT_RAW_BSON_OPTIONS`.
|
99
|
+
|
100
|
+
.. versionchanged:: 3.8
|
101
|
+
:class:`RawBSONDocument` now validates that the ``bson_bytes``
|
102
|
+
passed in represent a single bson document.
|
103
|
+
|
104
|
+
.. versionchanged:: 3.5
|
105
|
+
If a :class:`~bson.codec_options.CodecOptions` is passed in, its
|
106
|
+
`document_class` must be :class:`RawBSONDocument`.
|
107
|
+
"""
|
108
|
+
self.__raw = bson_bytes
|
109
|
+
self.__inflated_doc: Optional[Mapping[str, Any]] = None
|
110
|
+
# Can't default codec_options to DEFAULT_RAW_BSON_OPTIONS in signature,
|
111
|
+
# it refers to this class RawBSONDocument.
|
112
|
+
if codec_options is None:
|
113
|
+
codec_options = DEFAULT_RAW_BSON_OPTIONS
|
114
|
+
elif codec_options.document_class is not RawBSONDocument:
|
115
|
+
raise TypeError(
|
116
|
+
"RawBSONDocument cannot use CodecOptions with document "
|
117
|
+
"class %s" % (codec_options.document_class,)
|
118
|
+
)
|
119
|
+
self.__codec_options = codec_options
|
120
|
+
# Validate the bson object size.
|
121
|
+
_get_object_size(bson_bytes, 0, len(bson_bytes))
|
122
|
+
|
123
|
+
@property
|
124
|
+
def raw(self) -> bytes:
|
125
|
+
"""The raw BSON bytes composing this document."""
|
126
|
+
return self.__raw
|
127
|
+
|
128
|
+
def items(self) -> ItemsView[str, Any]:
|
129
|
+
"""Lazily decode and iterate elements in this document."""
|
130
|
+
return self.__inflated.items()
|
131
|
+
|
132
|
+
@property
|
133
|
+
def __inflated(self) -> Mapping[str, Any]:
|
134
|
+
if self.__inflated_doc is None:
|
135
|
+
# We already validated the object's size when this document was
|
136
|
+
# created, so no need to do that again.
|
137
|
+
# Use SON to preserve ordering of elements.
|
138
|
+
self.__inflated_doc = _inflate_bson(self.__raw, self.__codec_options)
|
139
|
+
return self.__inflated_doc
|
140
|
+
|
141
|
+
def __getitem__(self, item: str) -> Any:
|
142
|
+
return self.__inflated[item]
|
143
|
+
|
144
|
+
def __iter__(self) -> Iterator[str]:
|
145
|
+
return iter(self.__inflated)
|
146
|
+
|
147
|
+
def __len__(self) -> int:
|
148
|
+
return len(self.__inflated)
|
149
|
+
|
150
|
+
def __eq__(self, other: Any) -> bool:
|
151
|
+
if isinstance(other, RawBSONDocument):
|
152
|
+
return self.__raw == other.raw
|
153
|
+
return NotImplemented
|
154
|
+
|
155
|
+
def __repr__(self):
|
156
|
+
return "RawBSONDocument(%r, codec_options=%r)" % (self.raw, self.__codec_options)
|
157
|
+
|
158
|
+
|
159
|
+
def _inflate_bson(bson_bytes: bytes, codec_options: CodecOptions) -> Mapping[Any, Any]:
|
160
|
+
"""Inflates the top level fields of a BSON document.
|
161
|
+
|
162
|
+
:Parameters:
|
163
|
+
- `bson_bytes`: the BSON bytes that compose this document
|
164
|
+
- `codec_options`: An instance of
|
165
|
+
:class:`~bson.codec_options.CodecOptions` whose ``document_class``
|
166
|
+
must be :class:`RawBSONDocument`.
|
167
|
+
"""
|
168
|
+
# Use SON to preserve ordering of elements.
|
169
|
+
return _raw_to_dict(bson_bytes, 4, len(bson_bytes) - 1, codec_options, SON())
|
170
|
+
|
171
|
+
|
172
|
+
DEFAULT_RAW_BSON_OPTIONS: CodecOptions = DEFAULT.with_options(document_class=RawBSONDocument)
|
173
|
+
"""The default :class:`~bson.codec_options.CodecOptions` for
|
174
|
+
:class:`RawBSONDocument`.
|
175
|
+
"""
|