Class: Mongo::Protocol::Message Abstract

Inherits:
Object
  • Object
show all
Includes:
Id, Serializers
Defined in:
lib/mongo/protocol/message.rb

Overview

This class is abstract.

A base class providing functionality required by all messages in the MongoDB wire protocol. It provides a minimal DSL for defining typed fields to enable serialization and deserialization over the wire.

Examples:

class WireProtocolMessage < Message

  private

  def op_code
    1234
  end

  FLAGS = [:first_bit, :bit_two]

  # payload
  field :flags, BitVector.new(FLAGS)
  field :namespace, CString
  field :document, Document
  field :documents, Document, true
end

Direct Known Subclasses

Compressed, Delete, GetMore, Insert, KillCursors, Msg, Query, Reply, Update

Constant Summary collapse

BATCH_SIZE =

The batch size constant.

Since:

  • 2.2.0

'batchSize'.freeze
COLLECTION =

The collection constant.

Since:

  • 2.2.0

'collection'.freeze
LIMIT =

The limit constant.

Since:

  • 2.2.0

'limit'.freeze
ORDERED =

The ordered constant.

Since:

  • 2.2.0

'ordered'.freeze
Q =

The q constant.

Since:

  • 2.2.0

'q'.freeze
MAX_MESSAGE_SIZE =

Default max message size of 48MB.

Since:

  • 2.2.1

50331648.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Id

included

Constructor Details

#initialize(*args) ⇒ Message

:nodoc:



76
77
78
# File 'lib/mongo/protocol/message.rb', line 76

def initialize(*args) # :nodoc:
  set_request_id
end

Instance Attribute Details

#request_idFixnum (readonly)

Returns the request id for the message



83
84
85
# File 'lib/mongo/protocol/message.rb', line 83

def request_id
  @request_id
end

Class Method Details

.deserialize(io, max_message_size = MAX_MESSAGE_SIZE, expected_response_to = nil, options = {}) ⇒ Message

Deserializes messages from an IO stream.

This method returns decompressed messages (i.e. if the message on the wire was OP_COMPRESSED, this method would typically return the OP_MSG message that is the result of decompression).

Options Hash (options):

  • :deserialize_as_bson (Boolean)

    Whether to deserialize this message using BSON types instead of native Ruby types wherever possible.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mongo/protocol/message.rb', line 204

def self.deserialize(io, max_message_size = MAX_MESSAGE_SIZE, expected_response_to = nil, options = {})
  length, _request_id, response_to, _op_code = deserialize_header(BSON::ByteBuffer.new(io.read(16)))

  # Protection from potential DOS man-in-the-middle attacks. See
  # DRIVERS-276.
  if length > (max_message_size || MAX_MESSAGE_SIZE)
    raise Error::MaxMessageSize.new(max_message_size)
  end

  # Protection against returning the response to a previous request. See
  # RUBY-1117
  if expected_response_to && response_to != expected_response_to
    raise Error::UnexpectedResponse.new(expected_response_to, response_to)
  end

  message = Registry.get(_op_code).allocate
  buffer = BSON::ByteBuffer.new(io.read(length - 16))

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

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Tests for equality between two wire protocol messages by comparing class and field values.



240
241
242
243
244
245
246
247
# File 'lib/mongo/protocol/message.rb', line 240

def ==(other)
  return false if self.class != other.class
  fields.all? do |field|
    name = field[:name]
    instance_variable_get(name) ==
      other.instance_variable_get(name)
  end
end

#hashFixnum

Creates a hash from the values of the fields of a message.



253
254
255
# File 'lib/mongo/protocol/message.rb', line 253

def hash
  fields.map { |field| instance_variable_get(field[:name]) }.hash
end

#maybe_compress(compressor, zlib_compression_level = nil) ⇒ 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 the message, if supported by the wire protocol used and if the command being sent permits compression. Otherwise returns self.

Since:

  • 2.5.0



109
110
111
# File 'lib/mongo/protocol/message.rb', line 109

def maybe_compress(compressor, zlib_compression_level = nil)
  self
end

#maybe_decrypt(client) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/mongo/protocol/message.rb', line 143

def maybe_decrypt(client)
  # TODO determine if we should be decrypting data coming from pre-4.2
  # servers, potentially using legacy wire protocols. If so we need
  # to implement decryption for those wire protocols as our current
  # encryption/decryption code is OP_MSG-specific.
  self
end

#maybe_encrypt(server, client) ⇒ Object



151
152
153
154
# File 'lib/mongo/protocol/message.rb', line 151

def maybe_encrypt(server, client)
  # Do nothing if the Message subclass has not implemented this method
  self
end

#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.

Inflate a message if it is compressed.

Since:

  • 2.5.0



139
140
141
# File 'lib/mongo/protocol/message.rb', line 139

def maybe_inflate
  self
end

#number_returned0

Default number returned value for protocol messages.

Since:

  • 2.5.0



271
# File 'lib/mongo/protocol/message.rb', line 271

def number_returned; 0; end

#replyable?false

The default for messages is not to require a reply after sending a message to the server.

Examples:

Does the message require a reply?

message.replyable?

Since:

  • 2.0.0



94
95
96
# File 'lib/mongo/protocol/message.rb', line 94

def replyable?
  false
end

#serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil) ⇒ String Also known as: to_s

Serializes message into bytes that can be sent on the wire



180
181
182
183
184
185
# File 'lib/mongo/protocol/message.rb', line 180

def serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil)
  start = buffer.length
  serialize_header(buffer)
  serialize_fields(buffer, max_bson_size)
  buffer.replace_int32(start, buffer.length - start)
end

#set_request_idFixnum

Generates a request id for a message



262
263
264
# File 'lib/mongo/protocol/message.rb', line 262

def set_request_id
  @request_id = self.class.next_id
end