mongo 2.4.3 → 2.5.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +3 -2
  3. data.tar.gz.sig +0 -0
  4. data/lib/mongo.rb +3 -2
  5. data/lib/mongo/auth/cr.rb +6 -4
  6. data/lib/mongo/auth/cr/conversation.rb +33 -17
  7. data/lib/mongo/auth/ldap.rb +4 -2
  8. data/lib/mongo/auth/ldap/conversation.rb +19 -9
  9. data/lib/mongo/auth/scram.rb +7 -4
  10. data/lib/mongo/auth/scram/conversation.rb +62 -24
  11. data/lib/mongo/auth/user.rb +10 -0
  12. data/lib/mongo/auth/user/view.rb +44 -22
  13. data/lib/mongo/auth/x509.rb +4 -2
  14. data/lib/mongo/auth/x509/conversation.rb +19 -9
  15. data/lib/mongo/bulk_write.rb +33 -27
  16. data/lib/mongo/bulk_write/combineable.rb +5 -0
  17. data/lib/mongo/bulk_write/transformable.rb +2 -0
  18. data/lib/mongo/bulk_write/validatable.rb +4 -0
  19. data/lib/mongo/client.rb +123 -12
  20. data/lib/mongo/cluster.rb +52 -11
  21. data/lib/mongo/cluster/app_metadata.rb +8 -2
  22. data/lib/mongo/cluster/cursor_reaper.rb +0 -1
  23. data/lib/mongo/cluster/topology.rb +1 -1
  24. data/lib/mongo/collection.rb +114 -27
  25. data/lib/mongo/collection/view.rb +8 -2
  26. data/lib/mongo/collection/view/aggregation.rb +11 -7
  27. data/lib/mongo/collection/view/builder/aggregation.rb +5 -1
  28. data/lib/mongo/collection/view/builder/find_command.rb +5 -3
  29. data/lib/mongo/collection/view/builder/map_reduce.rb +11 -3
  30. data/lib/mongo/collection/view/builder/op_query.rb +1 -1
  31. data/lib/mongo/collection/view/change_stream.rb +160 -0
  32. data/lib/mongo/collection/view/change_stream/retryable.rb +57 -0
  33. data/lib/mongo/collection/view/iterable.rb +11 -10
  34. data/lib/mongo/collection/view/map_reduce.rb +22 -18
  35. data/lib/mongo/collection/view/readable.rb +51 -37
  36. data/lib/mongo/collection/view/writable.rb +72 -40
  37. data/lib/mongo/cursor.rb +25 -4
  38. data/lib/mongo/cursor/builder/get_more_command.rb +4 -2
  39. data/lib/mongo/database.rb +22 -11
  40. data/lib/mongo/database/view.rb +16 -12
  41. data/lib/mongo/error.rb +5 -0
  42. data/lib/mongo/error/invalid_session.rb +36 -0
  43. data/lib/mongo/error/missing_resume_token.rb +39 -0
  44. data/lib/mongo/error/operation_failure.rb +17 -0
  45. data/lib/mongo/error/parser.rb +3 -2
  46. data/lib/mongo/error/unknown_payload_type.rb +41 -0
  47. data/lib/mongo/error/unsupported_array_filters.rb +51 -0
  48. data/lib/mongo/error/unsupported_message_type.rb +23 -0
  49. data/lib/mongo/grid/fs_bucket.rb +5 -4
  50. data/lib/mongo/grid/stream/read.rb +3 -2
  51. data/lib/mongo/grid/stream/write.rb +2 -2
  52. data/lib/mongo/index/view.rb +35 -25
  53. data/lib/mongo/monitoring/event/secure.rb +14 -0
  54. data/lib/mongo/operation.rb +16 -0
  55. data/lib/mongo/operation/commands.rb +1 -0
  56. data/lib/mongo/operation/commands/aggregate.rb +9 -5
  57. data/lib/mongo/operation/commands/aggregate/result.rb +1 -1
  58. data/lib/mongo/operation/commands/collections_info.rb +6 -6
  59. data/lib/mongo/operation/commands/command.rb +2 -1
  60. data/lib/mongo/operation/commands/create.rb +6 -2
  61. data/lib/mongo/operation/commands/drop.rb +6 -2
  62. data/lib/mongo/operation/commands/drop_database.rb +6 -2
  63. data/lib/mongo/operation/commands/explain.rb +27 -0
  64. data/lib/mongo/operation/commands/explain/result.rb +52 -0
  65. data/lib/mongo/operation/commands/indexes.rb +1 -1
  66. data/lib/mongo/operation/commands/list_collections.rb +1 -1
  67. data/lib/mongo/operation/commands/list_collections/result.rb +1 -1
  68. data/lib/mongo/operation/commands/list_indexes.rb +1 -1
  69. data/lib/mongo/operation/commands/list_indexes/result.rb +1 -1
  70. data/lib/mongo/operation/commands/map_reduce.rb +8 -4
  71. data/lib/mongo/operation/commands/map_reduce/result.rb +13 -1
  72. data/lib/mongo/operation/commands/user_query.rb +1 -1
  73. data/lib/mongo/operation/commands/users_info.rb +6 -2
  74. data/lib/mongo/operation/executable.rb +4 -1
  75. data/lib/mongo/operation/read_preference.rb +10 -5
  76. data/lib/mongo/operation/result.rb +26 -2
  77. data/lib/mongo/operation/specifiable.rb +13 -1
  78. data/lib/mongo/operation/uses_command_op_msg.rb +47 -0
  79. data/lib/mongo/operation/write/bulk/bulkable.rb +4 -1
  80. data/lib/mongo/operation/write/bulk/insert/result.rb +4 -4
  81. data/lib/mongo/operation/write/command/create_index.rb +6 -1
  82. data/lib/mongo/operation/write/command/delete.rb +28 -4
  83. data/lib/mongo/operation/write/command/drop_index.rb +6 -1
  84. data/lib/mongo/operation/write/command/insert.rb +22 -18
  85. data/lib/mongo/operation/write/command/update.rb +24 -9
  86. data/lib/mongo/operation/write/command/writable.rb +14 -1
  87. data/lib/mongo/operation/write/insert.rb +4 -1
  88. data/lib/mongo/operation/write/insert/result.rb +2 -2
  89. data/lib/mongo/operation/write/update.rb +7 -1
  90. data/lib/mongo/operation/write/write_command_enabled.rb +20 -3
  91. data/lib/mongo/protocol.rb +3 -0
  92. data/lib/mongo/protocol/bit_vector.rb +2 -2
  93. data/lib/mongo/protocol/compressed.rb +135 -0
  94. data/lib/mongo/protocol/delete.rb +8 -6
  95. data/lib/mongo/protocol/get_more.rb +8 -6
  96. data/lib/mongo/protocol/insert.rb +8 -6
  97. data/lib/mongo/protocol/kill_cursors.rb +8 -6
  98. data/lib/mongo/protocol/message.rb +31 -3
  99. data/lib/mongo/protocol/msg.rb +172 -0
  100. data/lib/mongo/protocol/query.rb +26 -6
  101. data/lib/mongo/protocol/registry.rb +76 -0
  102. data/lib/mongo/protocol/reply.rb +10 -5
  103. data/lib/mongo/protocol/serializers.rb +224 -0
  104. data/lib/mongo/protocol/update.rb +8 -6
  105. data/lib/mongo/retryable.rb +4 -2
  106. data/lib/mongo/server.rb +6 -3
  107. data/lib/mongo/server/connectable.rb +1 -1
  108. data/lib/mongo/server/connection.rb +30 -8
  109. data/lib/mongo/server/description.rb +25 -1
  110. data/lib/mongo/server/description/features.rb +4 -1
  111. data/lib/mongo/server/monitor.rb +5 -0
  112. data/lib/mongo/server/monitor/connection.rb +50 -2
  113. data/lib/mongo/server_selector/nearest.rb +10 -4
  114. data/lib/mongo/server_selector/primary.rb +20 -0
  115. data/lib/mongo/server_selector/primary_preferred.rb +10 -4
  116. data/lib/mongo/server_selector/secondary.rb +10 -4
  117. data/lib/mongo/server_selector/secondary_preferred.rb +24 -4
  118. data/lib/mongo/session.rb +180 -0
  119. data/lib/mongo/session/server_session.rb +73 -0
  120. data/lib/mongo/session/session_pool.rb +161 -0
  121. data/lib/mongo/uri.rb +11 -0
  122. data/lib/mongo/version.rb +1 -1
  123. data/mongo.gemspec +2 -1
  124. data/spec/mongo/auth/cr_spec.rb +12 -0
  125. data/spec/mongo/auth/ldap_spec.rb +2 -0
  126. data/spec/mongo/auth/scram/conversation_spec.rb +6 -6
  127. data/spec/mongo/auth/scram_spec.rb +25 -1
  128. data/spec/mongo/auth/user/view_spec.rb +268 -76
  129. data/spec/mongo/auth/x509_spec.rb +2 -0
  130. data/spec/mongo/bulk_write_spec.rb +435 -5
  131. data/spec/mongo/client_spec.rb +356 -39
  132. data/spec/mongo/cluster/app_metadata_spec.rb +2 -2
  133. data/spec/mongo/cluster_spec.rb +176 -0
  134. data/spec/mongo/collection/view/aggregation_spec.rb +33 -12
  135. data/spec/mongo/collection/view/builder/find_command_spec.rb +46 -6
  136. data/spec/mongo/collection/view/change_stream_spec.rb +814 -0
  137. data/spec/mongo/collection/view/map_reduce_spec.rb +94 -17
  138. data/spec/mongo/collection/view/readable_spec.rb +3 -12
  139. data/spec/mongo/collection_spec.rb +1048 -42
  140. data/spec/mongo/cursor/builder/get_more_command_spec.rb +19 -0
  141. data/spec/mongo/cursor_spec.rb +2 -2
  142. data/spec/mongo/database_spec.rb +50 -1
  143. data/spec/mongo/grid/fs_bucket_spec.rb +225 -137
  144. data/spec/mongo/grid/stream/read_spec.rb +2 -2
  145. data/spec/mongo/index/view_spec.rb +146 -8
  146. data/spec/mongo/monitoring/event/secure_spec.rb +42 -0
  147. data/spec/mongo/operation/read/query_spec.rb +2 -1
  148. data/spec/mongo/operation/specifiable_spec.rb +2 -2
  149. data/spec/mongo/operation/write/command/delete_spec.rb +96 -13
  150. data/spec/mongo/operation/write/command/insert_spec.rb +111 -12
  151. data/spec/mongo/operation/write/command/update_spec.rb +93 -10
  152. data/spec/mongo/operation/write/delete_spec.rb +1 -1
  153. data/spec/mongo/operation/write/insert_spec.rb +1 -1
  154. data/spec/mongo/operation/write/update_spec.rb +1 -1
  155. data/spec/mongo/protocol/compressed_spec.rb +66 -0
  156. data/spec/mongo/protocol/delete_spec.rb +14 -0
  157. data/spec/mongo/protocol/get_more_spec.rb +14 -0
  158. data/spec/mongo/protocol/insert_spec.rb +14 -0
  159. data/spec/mongo/protocol/kill_cursors_spec.rb +14 -0
  160. data/spec/mongo/protocol/msg_spec.rb +499 -0
  161. data/spec/mongo/protocol/query_spec.rb +45 -0
  162. data/spec/mongo/protocol/registry_spec.rb +31 -0
  163. data/spec/mongo/protocol/reply_spec.rb +14 -0
  164. data/spec/mongo/protocol/update_spec.rb +14 -0
  165. data/spec/mongo/retryable_spec.rb +6 -2
  166. data/spec/mongo/sdam_spec.rb +4 -0
  167. data/spec/mongo/server/connection_spec.rb +4 -2
  168. data/spec/mongo/server/description_spec.rb +28 -1
  169. data/spec/mongo/session/server_session_spec.rb +16 -0
  170. data/spec/mongo/session/session_pool_spec.rb +194 -0
  171. data/spec/mongo/uri_spec.rb +31 -2
  172. data/spec/spec_helper.rb +104 -0
  173. data/spec/support/authorization.rb +6 -1
  174. data/spec/support/crud.rb +3 -1
  175. data/spec/support/crud/write.rb +6 -1
  176. data/spec/support/crud_tests/write/findOneAndUpdate-arrayFilters.yml +69 -0
  177. data/spec/support/crud_tests/write/updateMany-arrayFilters.yml +63 -0
  178. data/spec/support/crud_tests/write/updateOne-arrayFilters.yml +109 -0
  179. data/spec/support/sdam/rs/discover_arbiters.yml +1 -1
  180. data/spec/support/sdam/rs/discover_passives.yml +2 -2
  181. data/spec/support/sdam/rs/discover_primary.yml +1 -1
  182. data/spec/support/sdam/rs/discover_secondary.yml +1 -1
  183. data/spec/support/sdam/rs/discovery.yml +4 -4
  184. data/spec/support/sdam/rs/equal_electionids.yml +1 -0
  185. data/spec/support/sdam/rs/ghost_discovered.yml +1 -1
  186. data/spec/support/sdam/rs/hosts_differ_from_seeds.yml +1 -1
  187. data/spec/support/sdam/rs/ls_timeout.yml +88 -0
  188. data/spec/support/sdam/rs/member_reconfig.yml +2 -2
  189. data/spec/support/sdam/rs/member_standalone.yml +2 -2
  190. data/spec/support/sdam/rs/new_primary.yml +2 -2
  191. data/spec/support/sdam/rs/new_primary_new_electionid.yml +3 -0
  192. data/spec/support/sdam/rs/new_primary_new_setversion.yml +3 -0
  193. data/spec/support/sdam/rs/new_primary_wrong_set_name.yml +2 -2
  194. data/spec/support/sdam/rs/non_rs_member.yml +1 -1
  195. data/spec/support/sdam/rs/normalize_case.yml +1 -1
  196. data/spec/support/sdam/rs/null_election_id.yml +4 -0
  197. data/spec/support/sdam/rs/primary_becomes_standalone.yml +2 -2
  198. data/spec/support/sdam/rs/primary_changes_set_name.yml +2 -2
  199. data/spec/support/sdam/rs/primary_disconnect.yml +2 -2
  200. data/spec/support/sdam/rs/primary_disconnect_electionid.yml +5 -0
  201. data/spec/support/sdam/rs/primary_disconnect_setversion.yml +5 -0
  202. data/spec/support/sdam/rs/primary_hint_from_secondary_with_mismatched_me.yml +58 -0
  203. data/spec/support/sdam/rs/primary_reports_new_member.yml +4 -4
  204. data/spec/support/sdam/rs/primary_to_no_primary_mismatched_me.yml +2 -2
  205. data/spec/support/sdam/rs/primary_wrong_set_name.yml +1 -1
  206. data/spec/support/sdam/rs/response_from_removed.yml +2 -2
  207. data/spec/support/sdam/rs/rsother_discovered.yml +1 -1
  208. data/spec/support/sdam/rs/sec_not_auth.yml +1 -1
  209. data/spec/support/sdam/rs/secondary_wrong_set_name.yml +1 -1
  210. data/spec/support/sdam/rs/secondary_wrong_set_name_with_primary.yml +2 -2
  211. data/spec/support/sdam/rs/setversion_without_electionid.yml +2 -0
  212. data/spec/support/sdam/rs/stepdown_change_set_name.yml +2 -2
  213. data/spec/support/sdam/rs/unexpected_mongos.yml +1 -1
  214. data/spec/support/sdam/rs/use_setversion_without_electionid.yml +3 -0
  215. data/spec/support/sdam/rs/wrong_set_name.yml +1 -1
  216. data/spec/support/sdam/sharded/ls_timeout_mongos.yml +97 -0
  217. data/spec/support/sdam/sharded/mongos_disconnect.yml +3 -3
  218. data/spec/support/sdam/sharded/multiple_mongoses.yml +1 -1
  219. data/spec/support/sdam/sharded/non_mongos_removed.yml +1 -1
  220. data/spec/support/sdam/sharded/normalize_uri_case.yml +1 -1
  221. data/spec/support/sdam/single/direct_connection_external_ip.yml +1 -1
  222. data/spec/support/sdam/single/direct_connection_mongos.yml +1 -1
  223. data/spec/support/sdam/single/direct_connection_rsarbiter.yml +1 -1
  224. data/spec/support/sdam/single/direct_connection_rsprimary.yml +1 -1
  225. data/spec/support/sdam/single/direct_connection_rssecondary.yml +1 -1
  226. data/spec/support/sdam/single/direct_connection_slave.yml +1 -1
  227. data/spec/support/sdam/single/direct_connection_standalone.yml +1 -1
  228. data/spec/support/sdam/single/ls_timeout_standalone.yml +35 -0
  229. data/spec/support/sdam/single/not_ok_response.yml +1 -1
  230. data/spec/support/sdam/single/standalone_removed.yml +1 -1
  231. data/spec/support/sdam/single/unavailable_seed.yml +1 -1
  232. data/spec/support/server_discovery_and_monitoring.rb +4 -0
  233. data/spec/support/shared/session.rb +236 -0
  234. metadata +53 -15
  235. metadata.gz.sig +0 -0
@@ -63,16 +63,16 @@ module Mongo
63
63
  # @example Return the event payload.
64
64
  # message.payload
65
65
  #
66
- # @return [ Hash ] The event payload.
66
+ # @return [ BSON::Document ] The event payload.
67
67
  #
68
68
  # @since 2.1.0
69
69
  def payload
70
- {
70
+ BSON::Document.new(
71
71
  command_name: 'insert',
72
72
  database_name: @database,
73
73
  command: upconverter.command,
74
74
  request_id: request_id
75
- }
75
+ )
76
76
  end
77
77
 
78
78
  protected
@@ -87,9 +87,9 @@ module Mongo
87
87
 
88
88
  # The operation code required to specify an Insert message.
89
89
  # @return [Fixnum] the operation code.
90
- def op_code
91
- 2002
92
- end
90
+ #
91
+ # @since 2.5.0
92
+ OP_CODE = 2002
93
93
 
94
94
  # Available flags for an Insert message.
95
95
  FLAGS = [:continue_on_error]
@@ -169,6 +169,8 @@ module Mongo
169
169
  document
170
170
  end
171
171
  end
172
+
173
+ Registry.register(OP_CODE, self)
172
174
  end
173
175
  end
174
176
  end
@@ -44,16 +44,16 @@ module Mongo
44
44
  # @example Return the event payload.
45
45
  # message.payload
46
46
  #
47
- # @return [ Hash ] The event payload.
47
+ # @return [ BSON::Document ] The event payload.
48
48
  #
49
49
  # @since 2.1.0
50
50
  def payload
51
- {
51
+ BSON::Document.new(
52
52
  command_name: 'killCursors',
53
53
  database_name: @database,
54
54
  command: upconverter.command,
55
55
  request_id: request_id
56
- }
56
+ )
57
57
  end
58
58
 
59
59
  protected
@@ -64,9 +64,9 @@ module Mongo
64
64
 
65
65
  # The operation code required to specify +KillCursors+ message.
66
66
  # @return [Fixnum] the operation code.
67
- def op_code
68
- 2007
69
- end
67
+ #
68
+ # @since 2.5.0
69
+ OP_CODE = 2007
70
70
 
71
71
  # Field representing Zero encoded as an Int32.
72
72
  field :zero, Zero
@@ -130,6 +130,8 @@ module Mongo
130
130
  document
131
131
  end
132
132
  end
133
+
134
+ Registry.register(OP_CODE, self)
133
135
  end
134
136
  end
135
137
  end
@@ -94,6 +94,27 @@ module Mongo
94
94
  false
95
95
  end
96
96
 
97
+ # Compress a message.
98
+ #
99
+ # @param [ String, Symbol ] compressor The compressor to use.
100
+ # @param [ Integer ] zlib_compression_level The zlib compression level to use.
101
+ #
102
+ # @return [ self ] Always returns self. Other message types should override this method.
103
+ #
104
+ # @since 2.5.0
105
+ def compress!(compressor, zlib_compression_level = nil)
106
+ self
107
+ end
108
+
109
+ # Inflate a message.
110
+ #
111
+ # @return [ self ] Always returns self. Other message types should override this method.
112
+ #
113
+ # @since 2.5.0
114
+ def inflate!
115
+ self
116
+ end
117
+
97
118
  # Serializes message into bytes that can be sent on the wire
98
119
  #
99
120
  # @param buffer [String] buffer where the message should be inserted
@@ -128,17 +149,17 @@ module Mongo
128
149
  raise Error::UnexpectedResponse.new(expected_response_to, response_to)
129
150
  end
130
151
 
152
+ message = Registry.get(_op_code).allocate
131
153
  buffer = BSON::ByteBuffer.new(io.read(length - 16))
132
- message = allocate
133
154
 
134
- fields.each do |field|
155
+ message.send(:fields).each do |field|
135
156
  if field[:multi]
136
157
  deserialize_array(message, buffer, field)
137
158
  else
138
159
  deserialize_field(message, buffer, field)
139
160
  end
140
161
  end
141
- message
162
+ message.inflate!
142
163
  end
143
164
 
144
165
  # Tests for equality between two wire protocol messages
@@ -174,6 +195,13 @@ module Mongo
174
195
  end
175
196
  end
176
197
 
198
+ # Default number returned value for protocol messages.
199
+ #
200
+ # @return [ 0 ] This method must be overridden, otherwise, always returns 0.
201
+ #
202
+ # @since 2.5.0
203
+ def number_returned; 0; end
204
+
177
205
  private
178
206
 
179
207
  @@request_id = 0
@@ -0,0 +1,172 @@
1
+ # Copyright (C) 2017 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ module Protocol
17
+
18
+ # MongoDB Wire protocol Msg message (OP_MSG), a bi-directional wire protocol opcode.
19
+ #
20
+ # OP_MSG is only available in MongoDB 3.6 (maxWireVersion >= 6) and later.
21
+ #
22
+ # @api private
23
+ #
24
+ # @since 2.5.0
25
+ class Msg < Message
26
+ include Monitoring::Event::Secure
27
+
28
+ # The identifier for the database name to execute the command on.
29
+ #
30
+ # @since 2.5.0
31
+ DATABASE_IDENTIFIER = '$db'.freeze
32
+
33
+ # Creates a new OP_MSG protocol message
34
+ #
35
+ # @example Create a OP_MSG wire protocol message
36
+ # Msg.new([:more_to_come], {}, { ismaster: 1 },
37
+ # { type: 1, payload: { identifier: 'documents', sequence: [..] } })
38
+ #
39
+ # @param [ Array<Symbol> ] flags The flag bits. Current supported values are
40
+ # :more_to_come and :checksum_present.
41
+ # @param [ Hash ] options The options. There are currently no supported options, this is a
42
+ # place-holder for the future.
43
+ # @param [ BSON::Document, Hash ] global_args The global arguments, becomes a section of payload type 0.
44
+ # @param [ BSON::Document, Hash ] sections Zero or more sections, in the format
45
+ # { type: 1, payload: { identifier: <String>, sequence: <Array<BSON::Document, Hash>> } } or
46
+ # { type: 0, payload: <BSON::Document, Hash> }
47
+ #
48
+ # @option options [ true, false ] validating_keys Whether keys should be validated.
49
+ #
50
+ # @api private
51
+ #
52
+ # @since 2.5.0
53
+ def initialize(flags, options, global_args, *sections)
54
+ @flags = flags || [ :none ]
55
+ @options = options
56
+ @global_args = global_args
57
+ @sections = [ { type: 0, payload: global_args } ] + sections
58
+ @request_id = nil
59
+ super
60
+ end
61
+
62
+ # Whether the message expects a reply from the database.
63
+ #
64
+ # @example Does the message require a reply?
65
+ # message.replyable?
66
+ #
67
+ # @return [ true, false ] If the message expects a reply.
68
+ #
69
+ # @since 2.5.0
70
+ def replyable?
71
+ @replyable ||= !flags.include?(:more_to_come)
72
+ end
73
+
74
+ # Return the event payload for monitoring.
75
+ #
76
+ # @example Return the event payload.
77
+ # message.payload
78
+ #
79
+ # @return [ BSON::Document ] The event payload.
80
+ #
81
+ # @since 2.5.0
82
+ def payload
83
+ BSON::Document.new(
84
+ command_name: command.keys.first,
85
+ database_name: global_args[DATABASE_IDENTIFIER],
86
+ command: command,
87
+ request_id: request_id,
88
+ reply: sections[0]
89
+ )
90
+ end
91
+
92
+ # Serializes message into bytes that can be sent on the wire.
93
+ #
94
+ # @param [ BSON::ByteBuffer ] buffer where the message should be inserted.
95
+ # @param [ Integer ] max_bson_size The maximum bson object size.
96
+ #
97
+ # @return [ BSON::ByteBuffer ] buffer containing the serialized message.
98
+ #
99
+ # @since 2.5.0
100
+ def serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil)
101
+ super
102
+ add_check_sum(buffer)
103
+ buffer
104
+ end
105
+
106
+ # Compress this message.
107
+ #
108
+ # @param [ String, Symbol ] compressor The compressor to use.
109
+ # @param [ Integer ] zlib_compression_level The zlib compression level to use.
110
+ #
111
+ # @return [ Compressed, self ] A Protocol::Compressed message or self, depending on whether
112
+ # this message can be compressed.
113
+ #
114
+ # @since 2.5.0
115
+ def compress!(compressor, zlib_compression_level = nil)
116
+ if compressor && compression_allowed?(command.keys.first)
117
+ Compressed.new(self, compressor, zlib_compression_level)
118
+ else
119
+ self
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ def command
126
+ @command ||= global_args.dup.tap do |cmd|
127
+ cmd.delete(DATABASE_IDENTIFIER)
128
+ sections.each do |section|
129
+ if section[:type] == 1
130
+ identifier = section[:payload][:identifier]
131
+ cmd[identifier] ||= []
132
+ cmd[identifier] += section[:payload][:sequence]
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ def add_check_sum(buffer)
139
+ if flags.include?(:checksum_present)
140
+ #buffer.put_int32(checksum)
141
+ end
142
+ end
143
+
144
+ def global_args
145
+ @global_args ||= (sections[0] || {})
146
+ end
147
+
148
+ # The operation code required to specify a OP_MSG message.
149
+ # @return [ Fixnum ] the operation code.
150
+ #
151
+ # @since 2.5.0
152
+ OP_CODE = 2013
153
+
154
+ # Available flags for a OP_MSG message.
155
+ FLAGS = Array.new(16).tap { |arr|
156
+ arr[0] = :checksum_present
157
+ arr[1] = :more_to_come
158
+ }
159
+
160
+ # @!attribute
161
+ # @return [Array<Symbol>] The flags for this message.
162
+ field :flags, BitVector.new(FLAGS)
163
+
164
+ # @!attribute
165
+ # @return [Hash] The sections of payload type 1 or 0.
166
+ field :sections, Sections
167
+ alias :documents :sections
168
+
169
+ Registry.register(OP_CODE, self)
170
+ end
171
+ end
172
+ end
@@ -29,6 +29,7 @@ module Mongo
29
29
  #
30
30
  # @api semipublic
31
31
  class Query < Message
32
+ include Monitoring::Event::Secure
32
33
 
33
34
  # Creates a new Query message
34
35
  #
@@ -75,16 +76,16 @@ module Mongo
75
76
  # @example Return the event payload.
76
77
  # message.payload
77
78
  #
78
- # @return [ Hash ] The event payload.
79
+ # @return [ BSON::Document ] The event payload.
79
80
  #
80
81
  # @since 2.1.0
81
82
  def payload
82
- {
83
+ BSON::Document.new(
83
84
  command_name: upconverter.command_name,
84
85
  database_name: @database,
85
86
  command: upconverter.command,
86
87
  request_id: request_id
87
- }
88
+ )
88
89
  end
89
90
 
90
91
  # Query messages require replies from the database.
@@ -99,6 +100,23 @@ module Mongo
99
100
  true
100
101
  end
101
102
 
103
+ # Compress this message.
104
+ #
105
+ # @param [ String, Symbol ] compressor The compressor to use.
106
+ # @param [ Integer ] zlib_compression_level The zlib compression level to use.
107
+ #
108
+ # @return [ Compressed, self ] A Protocol::Compressed message or self, depending on whether
109
+ # this message can be compressed.
110
+ #
111
+ # @since 2.5.0
112
+ def compress!(compressor, zlib_compression_level = nil)
113
+ if compressor && compression_allowed?(selector.keys.first)
114
+ Compressed.new(self, compressor, zlib_compression_level)
115
+ else
116
+ self
117
+ end
118
+ end
119
+
102
120
  protected
103
121
 
104
122
  attr_reader :upconverter
@@ -107,9 +125,9 @@ module Mongo
107
125
 
108
126
  # The operation code required to specify a Query message.
109
127
  # @return [Fixnum] the operation code.
110
- def op_code
111
- 2004
112
- end
128
+ #
129
+ # @since 2.5.0
130
+ OP_CODE = 2004
113
131
 
114
132
  def determine_limit
115
133
  [ @options[:limit] || @options[:batch_size], @options[:batch_size] || @options[:limit] ].min || 0
@@ -292,6 +310,8 @@ module Mongo
292
310
  document
293
311
  end
294
312
  end
313
+
314
+ Registry.register(OP_CODE, self)
295
315
  end
296
316
  end
297
317
  end
@@ -0,0 +1,76 @@
1
+ # Copyright (C) 2009-2017 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+
17
+ module Protocol
18
+
19
+ # Provides a registry for looking up a message class based on op code.
20
+ #
21
+ # @since 2.5.0
22
+ module Registry
23
+ extend self
24
+
25
+ # A Mapping of all the op codes to their corresponding Ruby classes.
26
+ #
27
+ # @since 2.5.0
28
+ MAPPINGS = {}
29
+
30
+ # Get the class for the given op code and raise an error if it's not found.
31
+ #
32
+ # @example Get the type for the op code.
33
+ # Mongo::Protocol::Registry.get(1)
34
+ #
35
+ # @return [ Class ] The corresponding Ruby class for the message type.
36
+ #
37
+ # @since 2.5.0
38
+ def get(op_code, message = nil)
39
+ if type = MAPPINGS[op_code]
40
+ type
41
+ else
42
+ handle_unsupported_op_code!(op_code)
43
+ end
44
+ end
45
+
46
+ # Register the Ruby type for the corresponding op code.
47
+ #
48
+ # @example Register the op code.
49
+ # Mongo::Protocol::Registry.register(1, Reply)
50
+ #
51
+ # @param [ Fixnum ] op_code The op code.
52
+ # @param [ Class ] type The class the op code maps to.
53
+ #
54
+ # @return [ Class ] The class.
55
+ #
56
+ # @since 2.5.0
57
+ def register(op_code, type)
58
+ MAPPINGS.store(op_code, type)
59
+ define_type_reader(type)
60
+ end
61
+
62
+ private
63
+
64
+ def define_type_reader(type)
65
+ type.module_eval <<-MOD
66
+ def op_code; OP_CODE; end
67
+ MOD
68
+ end
69
+
70
+ def handle_unsupported_op_code!(op_code)
71
+ message = "Detected unknown message type with op code: #{op_code}."
72
+ raise Error::UnsupportedMessageType.new(message)
73
+ end
74
+ end
75
+ end
76
+ end