Class: Mongo::Protocol::Compressed
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Message
|
|
|
Instance Chain:
|
|
| Inherits: |
Mongo::Protocol::Message
|
| Defined in: | lib/mongo/protocol/compressed.rb |
Overview
MongoDB Wire protocol Compressed message.
This is a bi-directional message that compresses another opcode. See github.com/mongodb/specifications/blob/master/source/compression/OP_COMPRESSED.md
Constant Summary
-
COMPRESSOR_ID_MAP =
# File 'lib/mongo/protocol/compressed.rb', line 62
The compressor identifier to byte map.
{ SNAPPY => SNAPPY_BYTE, ZSTD => ZSTD_BYTE, ZLIB => ZLIB_BYTE }.freeze -
NOOP =
# File 'lib/mongo/protocol/compressed.rb', line 32
The noop compressor identifier.
'noop'.freeze
-
NOOP_BYTE =
# File 'lib/mongo/protocol/compressed.rb', line 35
The byte signaling that the message has not been compressed (test mode).
0.chr.force_encoding(BSON::BINARY).freeze
-
OP_CODE =
# File 'lib/mongo/protocol/compressed.rb', line 130
The operation code for a
Compressedmessage.2012 -
SNAPPY =
# File 'lib/mongo/protocol/compressed.rb', line 38
The snappy compressor identifier.
'snappy'.freeze
-
SNAPPY_BYTE =
# File 'lib/mongo/protocol/compressed.rb', line 41
The byte signaling that the message has been compressed with snappy.
1.chr.force_encoding(BSON::BINARY).freeze
-
ZLIB =
# File 'lib/mongo/protocol/compressed.rb', line 51
The Zlib compressor identifier.
'zlib'.freeze
-
ZLIB_BYTE =
# File 'lib/mongo/protocol/compressed.rb', line 46
The byte signaling that the message has been compressed with Zlib.
2.chr.force_encoding(BSON::BINARY).freeze
-
ZSTD =
# File 'lib/mongo/protocol/compressed.rb', line 54
The zstd compressor identifier.
'zstd'.freeze
-
ZSTD_BYTE =
# File 'lib/mongo/protocol/compressed.rb', line 57
The byte signaling that the message has been compressed with zstd.
3.chr.force_encoding(BSON::BINARY).freeze
Serializers - Included
HEADER_PACK, INT32_PACK, INT64_PACK, NULL, ZERO
Message - Inherited
Class Method Summary
-
.new(message, compressor, zlib_compression_level = nil) ⇒ Compressed
constructor
Creates a new OP_COMPRESSED message.
Message - Inherited
| .deserialize | Deserializes messages from an IO stream. |
| .new | :nodoc: |
| .deserialize_array | Deserializes an array of fields in a message. |
| .deserialize_field | Deserializes a single field in a message. |
| .deserialize_header | Deserializes the header of the message. |
| .field | A method for declaring a message field. |
| .fields | A class method for getting the fields for a message class. |
Instance Attribute Summary
-
#replyable? ⇒ true, false
readonly
Whether the message expects a reply from the database.
- #compressed_message ⇒ String rw private
- #compressor_id ⇒ String rw private
-
#original_op_code
rw
private
Field representing the original message’s op code as an Int32.
- #uncompressed_size ⇒ Fixnum rw private
Message - Inherited
| #replyable? | The default for messages is not to require a reply after sending a message to the server. |
| #request_id | Returns the request id for the message. |
Instance Method Summary
-
#maybe_inflate ⇒ Protocol::Message
Internal use only
Internal use only
Inflates an OP_COMRESSED message and returns the original message.
- #compress(buffer) private
- #decompress(compressed_message) private
- #serialize_fields(buffer, max_bson_size) private
Message - Inherited
| #== | Tests for equality between two wire protocol messages by comparing class and field values. |
| #eql? | Alias for Message#==. |
| #hash | Creates a hash from the values of the fields of a message. |
| #maybe_add_server_api, | |
| #maybe_compress | Compress the message, if supported by the wire protocol used and if the command being sent permits compression. |
| #maybe_decrypt | Possibly decrypt this message with libmongocrypt. |
| #maybe_encrypt | Possibly encrypt this message with libmongocrypt. |
| #maybe_inflate | Inflate a message if it is compressed. |
| #number_returned | Default number returned value for protocol messages. |
| #serialize | Serializes message into bytes that can be sent on the wire. |
| #set_request_id | Generates a request id for a message. |
| #to_s | Alias for Message#serialize. |
| #compress_if_possible | Compress the message, if the command being sent permits compression. |
| #fields | A method for getting the fields for a message class. |
| #merge_sections, | |
| #serialize_fields | Serializes message fields into a buffer. |
| #serialize_header | Serializes the header of the message consisting of 4 32bit integers. |
Constructor Details
.new(message, compressor, zlib_compression_level = nil) ⇒ Compressed
Creates a new OP_COMPRESSED message.
# File 'lib/mongo/protocol/compressed.rb', line 79
def initialize(, compressor, zlib_compression_level = nil) @original_message = @original_op_code = .op_code @uncompressed_size = 0 @compressor_id = COMPRESSOR_ID_MAP[compressor] @compressed_message = '' @zlib_compression_level = zlib_compression_level if zlib_compression_level && zlib_compression_level != -1 @request_id = .request_id end
Instance Attribute Details
#compressed_message ⇒ String (rw, private)
# File 'lib/mongo/protocol/compressed.rb', line 146
field :, Bytes
#compressor_id ⇒ String (rw, private)
# File 'lib/mongo/protocol/compressed.rb', line 142
field :compressor_id, Byte
#original_op_code (rw, private)
Field representing the original message’s op code as an Int32.
# File 'lib/mongo/protocol/compressed.rb', line 134
field :original_op_code, Int32
#replyable? ⇒ true, false (readonly)
Whether the message expects a reply from the database.
# File 'lib/mongo/protocol/compressed.rb', line 120
def replyable? @original_message.replyable? end
#uncompressed_size ⇒ Fixnum (rw, private)
# File 'lib/mongo/protocol/compressed.rb', line 138
field :uncompressed_size, Int32
Instance Method Details
#compress(buffer) (private)
# File 'lib/mongo/protocol/compressed.rb', line 156
def compress(buffer) if @compressor_id == NOOP_BYTE buffer.to_s.force_encoding(BSON::BINARY) elsif @compressor_id == ZLIB_BYTE Zlib::Deflate.deflate(buffer.to_s, @zlib_compression_level).force_encoding(BSON::BINARY) elsif @compressor_id == SNAPPY_BYTE Snappy.deflate(buffer.to_s).force_encoding(BSON::BINARY) elsif @compressor_id == ZSTD_BYTE # DRIVERS-600 will allow this to be configurable in the future Zstd.compress(buffer.to_s).force_encoding(BSON::BINARY) end end
#decompress(compressed_message) (private)
# File 'lib/mongo/protocol/compressed.rb', line 169
def decompress() if @compressor_id == NOOP_BYTE BSON::ByteBuffer.new() elsif @compressor_id == ZLIB_BYTE BSON::ByteBuffer.new(Zlib::Inflate.inflate()) elsif @compressor_id == SNAPPY_BYTE BSON::ByteBuffer.new(Snappy.inflate()) elsif @compressor_id == ZSTD_BYTE BSON::ByteBuffer.new(Zstd.decompress()) end end
#maybe_inflate ⇒ Protocol::Message
Inflates an OP_COMRESSED message and returns the original message.
# File 'lib/mongo/protocol/compressed.rb', line 95
def maybe_inflate = Registry.get(@original_op_code).allocate buf = decompress(@compressed_message) .send(:fields).each do |field| if field[:multi] Message.deserialize_array(, buf, field) else Message.deserialize_field(, buf, field) end end if .is_a?(Msg) .fix_after_deserialization end end