o Sb@sUdZddlmZmZmZmZmZddlmZm Z ddl m Z ddl m Z ddl mZddlmZGdd d eeefZd ed ed eeeffd dZe jedZeed<dS)aSTools for representing raw BSON documents. Inserting and Retrieving RawBSONDocuments ========================================= Example: Moving a document between different databases/collections .. doctest:: >>> import bson >>> from pymongo import MongoClient >>> from bson.raw_bson import RawBSONDocument >>> client = MongoClient(document_class=RawBSONDocument) >>> client.drop_database('db') >>> client.drop_database('replica_db') >>> db = client.db >>> result = db.test.insert_many([{'_id': 1, 'a': 1}, ... {'_id': 2, 'b': 1}, ... {'_id': 3, 'c': 1}, ... {'_id': 4, 'd': 1}]) >>> replica_db = client.replica_db >>> for doc in db.test.find(): ... print(f"raw document: {doc.raw}") ... print(f"decoded document: {bson.decode(doc.raw)}") ... result = replica_db.test.insert_one(doc) raw document: b'...' decoded document: {'_id': 1, 'a': 1} raw document: b'...' decoded document: {'_id': 2, 'b': 1} raw document: b'...' decoded document: {'_id': 3, 'c': 1} raw document: b'...' decoded document: {'_id': 4, 'd': 1} For use cases like moving documents across different databases or writing binary blobs to disk, using raw BSON documents provides better speed and avoids the overhead of decoding or encoding BSON. )Any ItemsViewIteratorMappingOptional)_get_object_size _raw_to_dict)_RAW_BSON_DOCUMENT_MARKER)DEFAULT_CODEC_OPTIONS) CodecOptions)SONc@seZdZdZdZeZddedee ddfddZ e defd d Z de eeffd d Ze deeeffd dZdedefddZdeefddZdefddZdedefddZddZdS)RawBSONDocumentzRepresentation for a MongoDB document that provides access to the raw BSON bytes that compose it. Only when a field is accessed or modified within the document does RawBSONDocument decode its bytes. )Z__rawZ__inflated_docZ__codec_optionsN bson_bytes codec_optionsreturncCsN||_d|_|dur t}n |jturtd|jf||_t|dt|dS)a9Create a new :class:`RawBSONDocument` :class:`RawBSONDocument` is a representation of a BSON document that provides access to the underlying raw BSON bytes. Only when a field is accessed or modified within the document does RawBSONDocument decode its bytes. :class:`RawBSONDocument` implements the ``Mapping`` abstract base class from the standard library so it can be used like a read-only ``dict``:: >>> from bson import encode >>> raw_doc = RawBSONDocument(encode({'_id': 'my_doc'})) >>> raw_doc.raw b'...' >>> raw_doc['_id'] 'my_doc' :Parameters: - `bson_bytes`: the BSON bytes that compose this document - `codec_options` (optional): An instance of :class:`~bson.codec_options.CodecOptions` whose ``document_class`` must be :class:`RawBSONDocument`. The default is :attr:`DEFAULT_RAW_BSON_OPTIONS`. .. versionchanged:: 3.8 :class:`RawBSONDocument` now validates that the ``bson_bytes`` passed in represent a single bson document. .. versionchanged:: 3.5 If a :class:`~bson.codec_options.CodecOptions` is passed in, its `document_class` must be :class:`RawBSONDocument`. Nz>RawBSONDocument cannot use CodecOptions with document class %sr) _RawBSONDocument__raw_RawBSONDocument__inflated_docDEFAULT_RAW_BSON_OPTIONSdocument_classr TypeError_RawBSONDocument__codec_optionsrlen)selfrrr4/tmp/pip-target-onvjaxws/lib/python/bson/raw_bson.py__init__Js" zRawBSONDocument.__init__cCs|jS)z+The raw BSON bytes composing this document.)rrrrrraw{szRawBSONDocument.rawcCs |jS)z4Lazily decode and iterate elements in this document.)_RawBSONDocument__inflateditemsrrrrrs zRawBSONDocument.itemscCs |jdur t|j|j|_|jSN)r _inflate_bsonrrrrrrZ __inflateds zRawBSONDocument.__inflateditemcCs |j|Sr )r)rr"rrr __getitem__ zRawBSONDocument.__getitem__cC t|jSr )iterrrrrr__iter__r$zRawBSONDocument.__iter__cCr%r )rrrrrr__len__r$zRawBSONDocument.__len__othercCst|tr |j|jkStSr ) isinstancer rrNotImplemented)rr)rrr__eq__s  zRawBSONDocument.__eq__cCsd|j|jfS)Nz%RawBSONDocument(%r, codec_options=%r))rrrrrr__repr__szRawBSONDocument.__repr__r )__name__ __module__ __qualname____doc__ __slots__r Z _type_markerbytesrr rpropertyrrstrrrrrr#rr'intr(boolr,r-rrrrr ?s1 r rrrcCst|dt|d|tS)a%Inflates the top level fields of a BSON document. :Parameters: - `bson_bytes`: the BSON bytes that compose this document - `codec_options`: An instance of :class:`~bson.codec_options.CodecOptions` whose ``document_class`` must be :class:`RawBSONDocument`. )rrr )rrrrrr!s r!)rrN)r1typingrrrrrZbsonrrZbson.codec_optionsr r DEFAULTr Zbson.sonr r5r r3r!Z with_optionsr__annotations__rrrrs'    `