Class: Mongo::Protocol::Message Abstract
- Inherits:
-
Object
- Object
- Mongo::Protocol::Message
- Includes:
- 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) ⇒ 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.
-
#compress!(compressor, zlib_compression_level = nil) ⇒ self
Compress a message.
-
#hash ⇒ Fixnum
Creates a hash from the values of the fields of a message.
-
#inflate! ⇒ self
Inflate a message.
-
#initialize(*args) ⇒ Message
constructor
:nodoc:.
-
#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.
Constructor Details
#initialize(*args) ⇒ Message
:nodoc:
75 76 77 |
# File 'lib/mongo/protocol/message.rb', line 75 def initialize(*args) # :nodoc: @request_id = nil end |
Instance Attribute Details
#request_id ⇒ Fixnum (readonly)
Returns the request id for the message
82 83 84 |
# File 'lib/mongo/protocol/message.rb', line 82 def request_id @request_id end |
Class Method Details
.deserialize(io, max_message_size = MAX_MESSAGE_SIZE, expected_response_to = nil) ⇒ Message
Deserializes messages from an IO stream
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mongo/protocol/message.rb', line 137 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 .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.
170 171 172 173 174 175 176 177 |
# File 'lib/mongo/protocol/message.rb', line 170 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 |
#compress!(compressor, zlib_compression_level = nil) ⇒ self
Compress a message.
105 106 107 |
# File 'lib/mongo/protocol/message.rb', line 105 def compress!(compressor, zlib_compression_level = nil) self end |
#hash ⇒ Fixnum
Creates a hash from the values of the fields of a message.
183 184 185 |
# File 'lib/mongo/protocol/message.rb', line 183 def hash fields.map { |field| instance_variable_get(field[:name]) }.hash end |
#inflate! ⇒ self
Inflate a message.
114 115 116 |
# File 'lib/mongo/protocol/message.rb', line 114 def inflate! self end |
#number_returned ⇒ 0
Default number returned value for protocol messages.
203 |
# File 'lib/mongo/protocol/message.rb', line 203 def number_returned; 0; end |
#replyable? ⇒ false
The default for messages is not to require a reply after sending a message to the server.
93 94 95 |
# File 'lib/mongo/protocol/message.rb', line 93 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
122 123 124 125 126 127 |
# File 'lib/mongo/protocol/message.rb', line 122 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
192 193 194 195 196 |
# File 'lib/mongo/protocol/message.rb', line 192 def set_request_id @@id_lock.synchronize do @request_id = @@request_id += 1 end end |