Class: Mongo::Protocol::Compressed

Inherits:
Message
  • Object
show all
Defined in:
lib/mongo/protocol/compressed.rb

Overview

MongoDB Wire protocol Compressed message.

This is a bi-directional message that compresses another opcode.

Since:

  • 2.5.0

Constant Summary collapse

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
COMPRESSOR_ID_MAP =

The compressor identifier to byte map.

Since:

  • 2.5.0

{ 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

#==, #compress!, deserialize, #hash, #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



55
56
57
58
59
60
61
62
63
# File 'lib/mongo/protocol/compressed.rb', line 55

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

#inflate!Protocol::Message

Inflate an OP_COMRESSED message and return the original message.

Examples:

Inflate a compressed message.

message.inflate!

Returns:

Since:

  • 2.5.0



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mongo/protocol/compressed.rb', line 73

def inflate!
  message = Registry.get(@original_op_code).allocate
  uncompressed_message = Zlib::Inflate.inflate(@compressed_message)

  buf = BSON::ByteBuffer.new(uncompressed_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
  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



97
98
99
# File 'lib/mongo/protocol/compressed.rb', line 97

def replyable?
  @original_message.replyable?
end