Class: Mongo::Protocol::Msg Private

Inherits:
Message
  • Object
show all
Includes:
Monitoring::Event::Secure
Defined in:
lib/mongo/protocol/msg.rb

Overview

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

MongoDB Wire protocol Msg message (OP_MSG), a bi-directional wire protocol opcode.

OP_MSG is only available in MongoDB 3.6 (maxWireVersion >= 6) and later.

Since:

  • 2.5.0

Constant Summary collapse

DATABASE_IDENTIFIER =

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

The identifier for the database name to execute the command on.

Since:

  • 2.5.0

'$db'.freeze

Constants included from Monitoring::Event::Secure

Monitoring::Event::Secure::REDACTED_COMMANDS

Constants inherited from Message

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

Instance Attribute Summary

Attributes inherited from Message

#request_id

Instance Method Summary collapse

Methods included from Monitoring::Event::Secure

#compression_allowed?, #redacted

Methods inherited from Message

#==, deserialize, #hash, #inflate!, #number_returned, #set_request_id

Methods included from Id

included

Constructor Details

#initialize(flags, options, global_args, *sections) ⇒ Msg

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.

Creates a new OP_MSG protocol message

are :more_to_come and :checksum_present.

Examples:

Create a OP_MSG wire protocol message

Msg.new([:more_to_come], {}, { ismaster: 1 },
        { type: 1, payload: { identifier: 'documents', sequence: [..] } })

Parameters:

  • flags (Array<Symbol>)

    The flag bits. Current supported values

  • options (Hash)

    The options. There are currently no supported options, this is a placeholder for the future.

  • global_args (BSON::Document, Hash)

    The global arguments, becomes a section of payload type 0.

  • sections (BSON::Document, Hash)

    Zero or more sections, in the format { type: 1, payload: { identifier: <String>, sequence: <Array<BSON::Document, Hash>> } } or { type: 0, payload: <BSON::Document, Hash> }

Options Hash (options):

  • validating_keys (true, false)

    Whether keys should be validated.

Since:

  • 2.5.0



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

def initialize(flags, options, global_args, *sections)
  @flags = flags || []
  @options = options
  @global_args = global_args
  @sections = [ { type: 0, payload: global_args } ] + sections
  @request_id = nil
  super
end

Instance Method Details

#compress!(compressor, zlib_compression_level = nil) ⇒ Compressed, self

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.

Compress this message.

Parameters:

  • compressor (String, Symbol)

    The compressor to use.

  • zlib_compression_level (Integer) (defaults to: nil)

    The zlib compression level to use.

Returns:

  • (Compressed, self)

    A Protocol::Compressed message or self, depending on whether this message can be compressed.

Since:

  • 2.5.0



117
118
119
120
121
122
123
# File 'lib/mongo/protocol/msg.rb', line 117

def compress!(compressor, zlib_compression_level = nil)
  if compressor && compression_allowed?(command.keys.first)
    Compressed.new(self, compressor, zlib_compression_level)
  else
    self
  end
end

#payloadBSON::Document

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.

Return the event payload for monitoring.

Examples:

Return the event payload.

message.payload

Returns:

  • (BSON::Document)

    The event payload.

Since:

  • 2.5.0



84
85
86
87
88
89
90
91
92
# File 'lib/mongo/protocol/msg.rb', line 84

def payload
  BSON::Document.new(
    command_name: command.keys.first.to_s,
    database_name: global_args[DATABASE_IDENTIFIER],
    command: command,
    request_id: request_id,
    reply: sections[0]
  )
end

#replyable?true, false

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.

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



72
73
74
# File 'lib/mongo/protocol/msg.rb', line 72

def replyable?
  @replyable ||= !flags.include?(:more_to_come)
end

#serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil) ⇒ BSON::ByteBuffer

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.

Serializes message into bytes that can be sent on the wire.

Parameters:

  • buffer (BSON::ByteBuffer) (defaults to: BSON::ByteBuffer.new)

    where the message should be inserted.

  • max_bson_size (Integer) (defaults to: nil)

    The maximum bson object size.

Returns:

  • (BSON::ByteBuffer)

    buffer containing the serialized message.

Since:

  • 2.5.0



102
103
104
105
106
# File 'lib/mongo/protocol/msg.rb', line 102

def serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil)
  super
  add_check_sum(buffer)
  buffer
end