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.
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) ⇒ 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.
-
#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.
Instance Attribute Details
#request_id ⇒ Fixnum (readonly)
Returns the request id for the message
78 79 80 |
# File 'lib/mongo/protocol/message.rb', line 78 def request_id @request_id end |
Class Method Details
.deserialize(io, max_message_size = MAX_MESSAGE_SIZE) ⇒ Message
Deserializes messages from an IO stream
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mongo/protocol/message.rb', line 112 def self.deserialize(io, = MAX_MESSAGE_SIZE) length = deserialize_header(BSON::ByteBuffer.new(io.read(16))).first # Protection from potential DOS man-in-the-middle attacks. See # DRIVERS-276. if length > raise Error::MaxMessageSize.new() end buffer = BSON::ByteBuffer.new(io.read(length - 16)) = allocate fields.each do |field| if field[:multi] deserialize_array(, buffer, field) else deserialize_field(, buffer, field) end end 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.
138 139 140 141 142 143 144 145 |
# File 'lib/mongo/protocol/message.rb', line 138 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.
151 152 153 |
# File 'lib/mongo/protocol/message.rb', line 151 def hash fields.map { |field| instance_variable_get(field[:name]) }.hash end |
#replyable? ⇒ false
The default for messages is not to require a reply after sending a message to the server.
89 90 91 |
# File 'lib/mongo/protocol/message.rb', line 89 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
97 98 99 100 101 102 |
# File 'lib/mongo/protocol/message.rb', line 97 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
160 161 162 163 164 |
# File 'lib/mongo/protocol/message.rb', line 160 def set_request_id @@id_lock.synchronize do @request_id = @@request_id += 1 end end |