Class: Mongo::Protocol::Compressed

Inherits:
Message
  • Object
show all
Defined in:
build/ruby-driver-master/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.rst

Since:

  • 2.5.0

Constant Summary collapse

NOOP =

The noop compressor identifier.

Since:

  • 2.5.0

'noop'.freeze
NOOP_BYTE =

The byte signaling that the message has not been compressed (test mode).

Since:

  • 2.5.0

0.chr.force_encoding(BSON::BINARY).freeze
SNAPPY =

The snappy compressor identifier.

Since:

  • 2.5.0

'snappy'.freeze
SNAPPY_BYTE =

The byte signaling that the message has been compressed with snappy.

Since:

  • 2.5.0

1.chr.force_encoding(BSON::BINARY).freeze
ZLIB_BYTE =

The byte signaling that the message has been compressed with Zlib.

Since:

  • 2.5.0

2.chr.force_encoding(BSON::BINARY).freeze
ZLIB =

The Zlib compressor identifier.

Since:

  • 2.5.0

'zlib'.freeze
ZSTD =

The zstd compressor identifier.

Since:

  • 2.5.0

'zstd'.freeze
ZSTD_BYTE =

The byte signaling that the message has been compressed with zstd.

Since:

  • 2.5.0

3.chr.force_encoding(BSON::BINARY).freeze
COMPRESSOR_ID_MAP =

The compressor identifier to byte map.

Since:

  • 2.5.0

{
  SNAPPY => SNAPPY_BYTE,
  ZSTD => ZSTD_BYTE,
  ZLIB => ZLIB_BYTE
}.freeze

Constants inherited from Message

Message::BATCH_SIZE, Message::COLLECTION, Message::LIMIT, Message::MAX_MESSAGE_SIZE, Message::ORDERED, Message::Q

Instance Attribute Summary

Attributes inherited from Message

#request_id

Instance Method Summary collapse

Methods inherited from Message

#==, deserialize, #hash, #maybe_add_server_api, #maybe_compress, #maybe_decrypt, #maybe_encrypt, #number_returned, #serialize, #set_request_id

Methods included from Id

included

Constructor Details

#initialize(message, compressor, zlib_compression_level = nil) ⇒ Compressed

Creates a new OP_COMPRESSED message.

Examples:

Create an OP_COMPRESSED message.

Compressed.new(original_message, 'zlib')

Parameters:

  • message (Mongo::Protocol::Message)

    The original message.

  • compressor (String, Symbol)

    The compression algorithm to use.

  • zlib_compression_level (Integer) (defaults to: nil)

    The zlib compression level to use. -1 and nil imply default.

Since:

  • 2.5.0



79
80
81
82
83
84
85
86
87
# File 'build/ruby-driver-master/lib/mongo/protocol/compressed.rb', line 79

def initialize(message, compressor, zlib_compression_level = nil)
  @original_message = message
  @original_op_code = message.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 = message.request_id
end

Instance Method Details

#maybe_inflateProtocol::Message

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inflates an OP_COMRESSED message and returns the original message.

Returns:

Since:

  • 2.5.0



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'build/ruby-driver-master/lib/mongo/protocol/compressed.rb', line 95

def maybe_inflate
  message = Registry.get(@original_op_code).allocate
  buf = decompress(@compressed_message)

  message.send(:fields).each do |field|
    if field[:multi]
      Message.deserialize_array(message, buf, field)
    else
      Message.deserialize_field(message, buf, field)
    end
  end
  if message.is_a?(Msg)
    message.fix_after_deserialization
  end
  message
end

#replyable?true, false

Whether the message expects a reply from the database.

Examples:

Does the message require a reply?

message.replyable?

Returns:

  • (true, false)

    If the message expects a reply.

Since:

  • 2.5.0



120
121
122
# File 'build/ruby-driver-master/lib/mongo/protocol/compressed.rb', line 120

def replyable?
  @original_message.replyable?
end