Class: Mongo::Protocol::Message Abstract
- Inherits:
-
Object
- Object
- Mongo::Protocol::Message
- Includes:
- Id, Serializers
- Defined in:
- lib/mongo/protocol/message.rb
Overview
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.
Direct Known Subclasses
Compressed, Delete, GetMore, Insert, KillCursors, Msg, Query, Reply, Update
Constant Summary collapse
- BATCH_SIZE =
The batch size constant.
'batchSize'.freeze
- COLLECTION =
The collection constant.
'collection'.freeze
- LIMIT =
The limit constant.
'limit'.freeze
- ORDERED =
The ordered constant.
'ordered'.freeze
- Q =
The q constant.
'q'.freeze
- MAX_MESSAGE_SIZE =
Default max message size of 48MB.
50331648.freeze
Instance Attribute Summary collapse
-
#request_id ⇒ Fixnum
readonly
Returns the request id for the message.
Class Method Summary collapse
-
.deserialize(io, max_message_size = MAX_MESSAGE_SIZE, expected_response_to = nil, options = {}) ⇒ Message
Deserializes messages from an IO stream.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Tests for equality between two wire protocol messages by comparing class and field values.
-
#hash ⇒ Fixnum
Creates a hash from the values of the fields of a message.
-
#initialize(*args) ⇒ Message
constructor
:nodoc:.
-
#maybe_compress(compressor, zlib_compression_level = nil) ⇒ self
private
Compress the message, if supported by the wire protocol used and if the command being sent permits compression.
- #maybe_decrypt(client) ⇒ Object
- #maybe_encrypt(server, client) ⇒ Object
-
#maybe_inflate ⇒ Protocol::Message
private
Inflate a message if it is compressed.
-
#number_returned ⇒ 0
Default number returned value for protocol messages.
-
#replyable? ⇒ false
The default for messages is not to require a reply after sending a message to the server.
-
#serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil) ⇒ String
(also: #to_s)
Serializes message into bytes that can be sent on the wire.
-
#set_request_id ⇒ Fixnum
Generates a request id for a message.
Methods included from Id
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_id ⇒ Fixnum (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).
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, expected_response_to = nil, = {}) 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) raise Error::MaxMessageSize.new() 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 = Registry.get(_op_code).allocate buffer = BSON::ByteBuffer.new(io.read(length - 16)) .send(:fields).each do |field| if field[:multi] deserialize_array(, buffer, field, ) else deserialize_field(, buffer, field, ) end end if .is_a?(Msg) .fix_after_deserialization end .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 |
#hash ⇒ Fixnum
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.
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_inflate ⇒ Protocol::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.
139 140 141 |
# File 'lib/mongo/protocol/message.rb', line 139 def maybe_inflate self end |
#number_returned ⇒ 0
Default number returned value for protocol messages.
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.
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_id ⇒ Fixnum
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 |