Class: Mongo::Protocol::Message Abstract
Overview
提供MongoDB传输协议中所有消息所需功能的基类。 它提供了用于定义类型化字段的最小 DSL,以启用通过网络进行序列化和反序列化。
直接已知子类
Compressed 、 GetMore 、 KillCursors 、 Msg 、 Query 、 Reply
常量摘要折叠
- BATCH_SIZE =
批处理大小常量。
'batchSize'- 集合 =
集合常量。
'集合'- LIMIT =
极限常量。
'limit'- ORDERED =
有序常量。
'ordered'- Q =
q 常量。
'q'- MAX_MESSAGE_SIZE =
默认最大消息大小为48 MB。
50_331_648
序列化器中包含的常量
Serializers::HEADER_PACK、Serializers::INT32_PACK、Serializers::INT64_PACK、Serializers::NULL、Serializers::ZERO
实例属性摘要折叠
-
#request_id ⇒ Fixnum
只读
返回消息的请求 ID。
类方法摘要折叠
-
。 deserialize (io, max_message_size = MAX_MESSAGE_SIZE,expected_response_to = nil, options = {}) ⇒ 消息
private
反序列化来自 IO 流的消息。
-
。deserialize_array(message, io, 字段, options = {}) ⇒ 消息
private
反序列化消息中的字段数组。
-
。deserialize_field(message, io, 字段, options = {}) ⇒ 消息
private
反序列化消息中的单个字段。
-
。deserialize_header(io) ⇒ Array<Fixnum>
private
反序列化消息头。
-
。字段(name, type, multi = false) ⇒ NilClass
private
一种声明消息字段的方法。
-
。字段 ⇒ 整数
private
用于获取消息类字段的类方法。
实例方法摘要折叠
-
# == (other) ⇒ true, false (又名:#eql?)
通过比较类和字段值来测试两个传输协议消息之间的相等性。
-
#hash ⇒ Fixnum
根据消息字段的值创建哈希。
-
#initialize (*_args) ⇒ 消息
构造函数
:nodoc:.
-
#也许_add_server_api (server_api) ⇒ 对象
支持服务器API 选项的协议消息子类应重写此方法,以将服务器API文档添加到消息中。
-
#maybe_compress (_compressor, _zlib_compression_level = nil) ⇒ self
private
如果所使用的传输协议支持并且发送的命令允许压缩,则压缩消息。
-
# 也许_decrypt (_context) ⇒ Mongo::协议::Msg
可能使用 libmongocrypt 解密此消息。
-
#也许_加密(_connection, _context) ⇒ Mongo::协议::Msg
可以使用 libmongocrypt 加密此消息。
-
#maybe_inflate ⇒ Protocol::Message
private
如果消息被压缩,则对消息进行扩充。
-
# number_returned ⇒ 0
协议消息的默认返回值。
-
#可回复? ⇒ false
消息默认不要求在向服务器发送消息后进行回复。
-
#序列化(缓冲区= BSON::ByteBuffer.new,max_bson_size = nil,bson_overhead = nil)→ string (也:#to_s)
将消息序列化为可以在线发送的字节。
-
#set_request_id ⇒ Fixnum
生成消息的请求ID。
ID中包含的方法
构造函数详情
#initialize(*_args) ⇒ 消息
:nodoc:
77 78 79 |
# File 'lib/ Mongo/ 协议/message.rb', line 77 def 初始化(*_args) # :nodoc: set_request_id end |
实例属性详细信息
# request_id =" Fixnum " (只读)
返回消息的请求 ID
84 85 86 |
# File 'lib/ Mongo/ 协议/message.rb', line 84 def request_id @request_id end |
类方法详细信息
。 deserialize (io, max_message_size = MAX_MESSAGE_SIZE,expected_response_to = nil, options = {}) ⇒消息
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
反序列化来自 IO 流的消息。
该方法返回解压缩的消息(即,如果传输的消息是 OP_COMPRESSED,则该方法通常会返回作为解压缩结果的 OP_MSG 消息)。
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/ Mongo/ 协议/message.rb', line 235 def self.反序列化(io, = MAX_MESSAGE_SIZE, expired_response_to = nil, = {}) # io 通常是一个 Mongo::Socket实例,它支持 # 超时选项。 为了与可能调用此 # 具有其他类似 IO对象的方法,仅当它们 # 不为空。 = .slice(:timeout, :socket_timeout) 数据段 = if .空? io.读(16) else io.读(16, **) end buf = BSON::ByteBuffer.new(数据段) 长度, _request_id, response_to, _op_code = deserialize_header(buf) # 防止潜在的 DOS 中间人攻击。 请参阅 # DRIVERS- 276 。 提高 错误::MaxMessageSize.new() if 长度 > ( || MAX_MESSAGE_SIZE) # 防止返回对先前请求的响应。 请参阅 # RUBY- 1117 if expired_response_to && response_to != expired_response_to 提高 错误::UnexpectedResponse.new(expired_response_to, response_to) end 数据段 = if .空? io.读(长度 - 16) else io.读(长度 - 16, **) end buf = BSON::ByteBuffer.new(数据段) = 注册表.获取(_op_code).分配 .发送(:fields).每 do |字段| if 字段[:multi] deserialize_array(, buf, 字段, ) else deserialize_field(, buf, 字段, ) end end .fix_after_deserialization if .is_a?(消息) .也许_inflate end |
。deserialize_array (message, io, 字段, options = {}) ⇒ 消息
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
反序列化消息中的字段数组
数组中的项目数必须由先前反序列化的字段来描述,该字段由键下的字段dsl在类中指定 :multi
382 383 384 385 386 387 |
# File 'lib/ Mongo/ 协议/message.rb', line 382 def self.deserialize_array(, io, 字段, = {}) 元素 = [] 数数 = .instance_variable_get(字段[:multi]) 数数.次 { 元素 << 字段[:type].反序列化(io, ) } .instance_variable_set(字段[:name], 元素) end |
。deserialize_field (message, io, 字段, options = {}) ⇒ 消息
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
反序列化消息中的单个字段
401 402 403 404 405 406 |
# File 'lib/ Mongo/ 协议/message.rb', line 401 def self.deserialize_field(, io, 字段, = {}) .instance_variable_set( 字段[:name], 字段[:type].反序列化(io, ) ) end |
。deserialize_header(io) ⇒ 数组<Fixnum>
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
反序列化消息头
336 337 338 |
# File 'lib/ Mongo/ 协议/message.rb', line 336 def self.deserialize_header(io) 标头.反序列化(io) end |
。字段(name, type, multi = false) ⇒ NilClass
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
一种声明消息字段的方法
356 357 358 359 360 361 362 363 364 |
# File 'lib/ Mongo/ 协议/message.rb', line 356 def self.字段(名称, 类型, 多 = false) 字段 << { 名称: :"@#{名称}", 类型: 类型, multi: 多 } attr_reader 名称 end |
。字段 ⇒ 整数
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
用于获取消息类字段的类方法
327 328 329 |
# File 'lib/ Mongo/ 协议/message.rb', line 327 def self.字段 @fields ||= [] end |
实例方法详细信息
# == (other) ⇒ true , false也称为: eql?
通过比较类和字段值来测试两个传输协议消息之间的相等性。
287 288 289 290 291 292 293 294 295 |
# File 'lib/ Mongo/ 协议/message.rb', line 287 def ==(其他) return false if self.class != 其他.class 字段.全部? do |字段| 名称 = 字段[:name] instance_variable_get(名称) == 其他.instance_variable_get(名称) end end |
# hash =" Fixnum "
根据消息字段的值创建哈希。
301 302 303 |
# File 'lib/ Mongo/ 协议/message.rb', line 301 def 哈希 字段.map { |字段| instance_variable_get(字段[:name]) }.哈希 end |
#也许_add_server_api (server_api) ⇒对象
支持服务器API 选项的协议消息子类应重写此方法,以将服务器API文档添加到消息中。
175 176 177 |
# File 'lib/ Mongo/ 协议/message.rb', line 175 def 也许_add_server_api(server_api) 提高 NotImplementedError end |
#maybe_compress (_compressor, _zlib_compression_level = nil) ⇒ self
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
如果所使用的传输协议支持并且发送的命令允许压缩,则压缩消息。 否则返回 self。
110 111 112 |
# File 'lib/ Mongo/ 协议/message.rb', line 110 def 也许_压缩(_compressor, 压缩 = nil) self end |
#maybe_decrypt(_context) ⇒ Mongo::协议::Msg
可能使用 libmongocrypt 解密此消息。
150 151 152 153 154 155 156 |
# File 'lib/ Mongo/ 协议/message.rb', line 150 def Maybe_decrypt(_context) # TODO: 确定我们是否应该解密来自 4.2 之前版本的数据 # 个服务器,可能使用传统传输协议。 如果是这样,我们需要 # 对这些传输协议实现解密,因为我们当前 # 加密/解密代码特定于 OP_MSG。 self end |
#maybe_encrypt(_connection, _context) ⇒ Mongo::协议::Msg
可以使用 libmongocrypt 加密此消息。
166 167 168 169 |
# File 'lib/ Mongo/ 协议/message.rb', line 166 def 也许_加密(_connection, _context) # 如果 Message 子类未实现此方法,则不执行任何操作 self end |
#也许_inflate ⇒ Protocol::Message
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
如果消息被压缩,则对消息进行扩充。
140 141 142 |
# File 'lib/ Mongo/ 协议/message.rb', line 140 def 也许_inflate self end |
# number_returned ⇒ 0
协议消息的默认返回值。
319 320 321 |
# File 'lib/ Mongo/ 协议/message.rb', line 319 def number_returned 0 end |
#可回复? ⇒ false
消息默认不要求在向服务器发送消息后进行回复。
95 96 97 |
# File 'lib/ Mongo/ 协议/message.rb', line 95 def 可回复? false end |
# Serialize (buffer = BSON::ByteBuffer.new, max_bson_size = nil, bson_overhead = nil) ⇒ string也称为: to_s
将消息序列化为可通过网络发送的字节
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/ Mongo/ 协议/message.rb', line 200 def 序列化(缓冲 = BSON::ByteBuffer.new, max_bson_size = nil, bson_overhead = nil) max_size = if max_bson_size && bson_overhead max_bson_size + bson_overhead elsif max_bson_size max_bson_size end 开始 = 缓冲.长度 Serialize_header(缓冲) Serialize_fields(缓冲, max_size) 缓冲.replace_int 32(开始, 缓冲.长度 - 开始) end |
#set_request_id ⇒ Fixnum
生成消息的请求ID
310 311 312 |
# File 'lib/ Mongo/ 协议/message.rb', line 310 def set_request_id @request_id = self.class.next_id end |