mongo 2.0.0.beta → 2.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +14 -1
  5. data/lib/mongo.rb +2 -1
  6. data/lib/mongo/auth.rb +2 -2
  7. data/lib/mongo/bulk_write.rb +52 -0
  8. data/lib/mongo/bulk_write/bulk_writable.rb +191 -0
  9. data/lib/mongo/bulk_write/deletable.rb +60 -0
  10. data/lib/mongo/bulk_write/insertable.rb +52 -0
  11. data/lib/mongo/bulk_write/ordered_bulk_write.rb +48 -0
  12. data/lib/mongo/bulk_write/replacable.rb +57 -0
  13. data/lib/mongo/bulk_write/unordered_bulk_write.rb +46 -0
  14. data/lib/mongo/bulk_write/updatable.rb +68 -0
  15. data/lib/mongo/client.rb +15 -2
  16. data/lib/mongo/cluster/topology/replica_set.rb +1 -1
  17. data/lib/mongo/cluster/topology/sharded.rb +1 -1
  18. data/lib/mongo/cluster/topology/standalone.rb +1 -1
  19. data/lib/mongo/cluster/topology/unknown.rb +1 -1
  20. data/lib/mongo/collection.rb +1 -1
  21. data/lib/mongo/collection/view/aggregation.rb +1 -1
  22. data/lib/mongo/collection/view/readable.rb +1 -1
  23. data/lib/mongo/collection/view/writable.rb +5 -5
  24. data/lib/mongo/cursor.rb +0 -2
  25. data/lib/mongo/error.rb +4 -3
  26. data/lib/mongo/error/{bulk_write_failure.rb → bulk_write_error.rb} +2 -2
  27. data/lib/mongo/error/invalid_bulk_operation.rb +2 -2
  28. data/lib/mongo/error/invalid_bulk_operation_type.rb +36 -0
  29. data/lib/mongo/error/{empty_batch.rb → multi_index_drop.rb} +5 -10
  30. data/lib/mongo/grid/file/metadata.rb +1 -8
  31. data/lib/mongo/grid/fs.rb +5 -5
  32. data/lib/mongo/index/view.rb +90 -34
  33. data/lib/mongo/operation.rb +0 -1
  34. data/lib/mongo/operation/aggregate.rb +2 -2
  35. data/lib/mongo/operation/aggregate/result.rb +4 -0
  36. data/lib/mongo/operation/bulk_delete/result.rb +2 -127
  37. data/lib/mongo/operation/bulk_insert/result.rb +2 -132
  38. data/lib/mongo/operation/bulk_update/result.rb +2 -142
  39. data/lib/mongo/operation/command.rb +1 -1
  40. data/lib/mongo/operation/executable.rb +3 -3
  41. data/lib/mongo/operation/list_collections/result.rb +0 -2
  42. data/lib/mongo/operation/map_reduce.rb +1 -1
  43. data/lib/mongo/operation/read/collections_info.rb +1 -1
  44. data/lib/mongo/operation/read/get_more.rb +1 -1
  45. data/lib/mongo/operation/read/indexes.rb +1 -1
  46. data/lib/mongo/operation/read/list_collections.rb +1 -1
  47. data/lib/mongo/operation/read/list_indexes.rb +1 -1
  48. data/lib/mongo/operation/read/query.rb +1 -1
  49. data/lib/mongo/operation/specifiable.rb +18 -1
  50. data/lib/mongo/operation/write.rb +2 -0
  51. data/lib/mongo/operation/write/bulk_delete.rb +5 -19
  52. data/lib/mongo/operation/write/bulk_insert.rb +9 -40
  53. data/lib/mongo/operation/write/bulk_mergable.rb +67 -0
  54. data/lib/mongo/operation/write/bulk_update.rb +5 -19
  55. data/lib/mongo/operation/write/command.rb +1 -1
  56. data/lib/mongo/operation/write/command/{ensure_index.rb → create_index.rb} +4 -9
  57. data/lib/mongo/operation/write/command/delete.rb +4 -4
  58. data/lib/mongo/operation/write/command/drop_index.rb +1 -1
  59. data/lib/mongo/operation/write/command/insert.rb +4 -4
  60. data/lib/mongo/operation/write/command/update.rb +4 -4
  61. data/lib/mongo/operation/write/create_index.rb +12 -7
  62. data/lib/mongo/operation/write/create_user.rb +1 -1
  63. data/lib/mongo/operation/write/delete.rb +1 -1
  64. data/lib/mongo/operation/write/drop_index.rb +1 -1
  65. data/lib/mongo/operation/write/insert.rb +1 -1
  66. data/lib/mongo/operation/write/legacy_bulk_mergable.rb +83 -0
  67. data/lib/mongo/operation/write/remove_user.rb +1 -1
  68. data/lib/mongo/operation/write/update.rb +1 -1
  69. data/lib/mongo/{bulk.rb → options.rb} +2 -2
  70. data/lib/mongo/options/mapper.rb +78 -0
  71. data/lib/mongo/server.rb +2 -2
  72. data/lib/mongo/server/connection_pool/queue.rb +1 -1
  73. data/lib/mongo/server/description.rb +1 -1
  74. data/lib/mongo/server/description/inspector.rb +1 -1
  75. data/lib/mongo/server_selector.rb +1 -1
  76. data/lib/mongo/socket/unix.rb +2 -4
  77. data/lib/mongo/version.rb +1 -1
  78. data/lib/mongo/write_concern/acknowledged.rb +15 -1
  79. data/lib/mongo/write_concern/normalizable.rb +0 -22
  80. data/lib/mongo/write_concern/unacknowledged.rb +12 -0
  81. data/spec/mongo/bulk/bulk_write_spec.rb +178 -91
  82. data/spec/mongo/collection/view/aggregation_spec.rb +14 -1
  83. data/spec/mongo/collection/view/explainable_spec.rb +1 -1
  84. data/spec/mongo/collection/view/map_reduce_spec.rb +2 -2
  85. data/spec/mongo/collection/view/readable_spec.rb +2 -2
  86. data/spec/mongo/collection/view/writable_spec.rb +7 -7
  87. data/spec/mongo/collection/view_spec.rb +14 -5
  88. data/spec/mongo/collection_spec.rb +5 -5
  89. data/spec/mongo/cursor_spec.rb +3 -3
  90. data/spec/mongo/database_spec.rb +4 -8
  91. data/spec/mongo/grid/fs_spec.rb +8 -8
  92. data/spec/mongo/index/view_spec.rb +139 -35
  93. data/spec/mongo/operation/aggregate_spec.rb +14 -22
  94. data/spec/mongo/operation/command_spec.rb +8 -16
  95. data/spec/mongo/operation/map_reduce_spec.rb +1 -1
  96. data/spec/mongo/operation/read/indexes_spec.rb +2 -2
  97. data/spec/mongo/operation/write/bulk_delete_spec.rb +1 -239
  98. data/spec/mongo/operation/write/bulk_insert_spec.rb +3 -234
  99. data/spec/mongo/operation/write/bulk_update_spec.rb +1 -289
  100. data/spec/mongo/operation/write/command/delete_spec.rb +0 -13
  101. data/spec/mongo/operation/write/command/insert_spec.rb +0 -14
  102. data/spec/mongo/operation/write/command/update_spec.rb +0 -14
  103. data/spec/mongo/operation/write/{ensure_index_spec.rb → create_index_spec.rb} +8 -26
  104. data/spec/mongo/operation/write/delete_spec.rb +1 -1
  105. data/spec/mongo/operation/write/drop_index_spec.rb +1 -1
  106. data/spec/mongo/operation/write/insert_spec.rb +3 -3
  107. data/spec/mongo/operation/write/update_spec.rb +1 -1
  108. data/spec/mongo/server_selection_spec.rb +2 -2
  109. data/spec/support/authorization.rb +4 -2
  110. data/spec/support/server_selection.rb +25 -8
  111. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/Nearest.yml +4 -10
  112. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/Nearest_non_matching.yml +6 -12
  113. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/Primary.yml +4 -6
  114. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/PrimaryPreferred.yml +4 -10
  115. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/PrimaryPreferred_non_matching.yml +6 -12
  116. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/Secondary.yml +4 -10
  117. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/SecondaryPreferred.yml +4 -10
  118. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/SecondaryPreferred_non_matching.yml +6 -12
  119. data/spec/support/server_selection/selection/ReplicaSetNoPrimary/read/Secondary_non_matching.yml +6 -12
  120. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Nearest.yml +9 -17
  121. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Nearest_non_matching.yml +9 -17
  122. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Primary.yml +6 -10
  123. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/PrimaryPreferred.yml +11 -21
  124. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/PrimaryPreferred_non_matching.yml +11 -18
  125. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Secondary.yml +31 -0
  126. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/SecondaryPreferred.yml +31 -0
  127. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/SecondaryPreferred_non_matching.yml +29 -0
  128. data/spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Secondary_non_matching.yml +26 -0
  129. data/spec/support/server_selection/selection/Sharded/read/SecondaryPreferred.yml +4 -10
  130. data/spec/support/server_selection/selection/Single/read/SecondaryPreferred.yml +2 -6
  131. data/spec/support/server_selection/selection/Unknown/read/SecondaryPreferred.yml +0 -2
  132. data/spec/support/shared/bulk_write.rb +109 -72
  133. metadata +29 -10
  134. metadata.gz.sig +0 -0
  135. data/lib/mongo/bulk/bulk_write.rb +0 -307
  136. data/lib/mongo/operation/batchable.rb +0 -103
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta
4
+ version: 2.0.0.rc
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Brock
@@ -32,7 +32,7 @@ cert_chain:
32
32
  o2UXDbWtz5PqoFd8EgNJAn3+BG1pwC9S9pVFG3WPucfAx/bE8iq/vvchHei5Y/Vo
33
33
  aAz5f/hY4zFeYWvGDBHYEXE1rTN2hhMSyJscPcFbmz0=
34
34
  -----END CERTIFICATE-----
35
- date: 2015-02-24 00:00:00.000000000 Z
35
+ date: 2015-03-17 00:00:00.000000000 Z
36
36
  dependencies:
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bson
@@ -77,8 +77,14 @@ files:
77
77
  - lib/mongo/auth/user/view.rb
78
78
  - lib/mongo/auth/x509.rb
79
79
  - lib/mongo/auth/x509/conversation.rb
80
- - lib/mongo/bulk.rb
81
- - lib/mongo/bulk/bulk_write.rb
80
+ - lib/mongo/bulk_write.rb
81
+ - lib/mongo/bulk_write/bulk_writable.rb
82
+ - lib/mongo/bulk_write/deletable.rb
83
+ - lib/mongo/bulk_write/insertable.rb
84
+ - lib/mongo/bulk_write/ordered_bulk_write.rb
85
+ - lib/mongo/bulk_write/replacable.rb
86
+ - lib/mongo/bulk_write/unordered_bulk_write.rb
87
+ - lib/mongo/bulk_write/updatable.rb
82
88
  - lib/mongo/client.rb
83
89
  - lib/mongo/cluster.rb
84
90
  - lib/mongo/cluster/topology.rb
@@ -99,9 +105,9 @@ files:
99
105
  - lib/mongo/database.rb
100
106
  - lib/mongo/database/view.rb
101
107
  - lib/mongo/error.rb
102
- - lib/mongo/error/bulk_write_failure.rb
103
- - lib/mongo/error/empty_batch.rb
108
+ - lib/mongo/error/bulk_write_error.rb
104
109
  - lib/mongo/error/invalid_bulk_operation.rb
110
+ - lib/mongo/error/invalid_bulk_operation_type.rb
105
111
  - lib/mongo/error/invalid_collection_name.rb
106
112
  - lib/mongo/error/invalid_database_name.rb
107
113
  - lib/mongo/error/invalid_document.rb
@@ -112,6 +118,7 @@ files:
112
118
  - lib/mongo/error/invalid_update_document.rb
113
119
  - lib/mongo/error/max_bson_size.rb
114
120
  - lib/mongo/error/max_message_size.rb
121
+ - lib/mongo/error/multi_index_drop.rb
115
122
  - lib/mongo/error/need_primary_server.rb
116
123
  - lib/mongo/error/operation_failure.rb
117
124
  - lib/mongo/error/parser.rb
@@ -137,7 +144,6 @@ files:
137
144
  - lib/mongo/operation.rb
138
145
  - lib/mongo/operation/aggregate.rb
139
146
  - lib/mongo/operation/aggregate/result.rb
140
- - lib/mongo/operation/batchable.rb
141
147
  - lib/mongo/operation/bulk_delete/result.rb
142
148
  - lib/mongo/operation/bulk_insert/result.rb
143
149
  - lib/mongo/operation/bulk_update/result.rb
@@ -162,12 +168,13 @@ files:
162
168
  - lib/mongo/operation/write.rb
163
169
  - lib/mongo/operation/write/bulk_delete.rb
164
170
  - lib/mongo/operation/write/bulk_insert.rb
171
+ - lib/mongo/operation/write/bulk_mergable.rb
165
172
  - lib/mongo/operation/write/bulk_update.rb
166
173
  - lib/mongo/operation/write/command.rb
174
+ - lib/mongo/operation/write/command/create_index.rb
167
175
  - lib/mongo/operation/write/command/create_user.rb
168
176
  - lib/mongo/operation/write/command/delete.rb
169
177
  - lib/mongo/operation/write/command/drop_index.rb
170
- - lib/mongo/operation/write/command/ensure_index.rb
171
178
  - lib/mongo/operation/write/command/insert.rb
172
179
  - lib/mongo/operation/write/command/remove_user.rb
173
180
  - lib/mongo/operation/write/command/update.rb
@@ -177,8 +184,11 @@ files:
177
184
  - lib/mongo/operation/write/delete.rb
178
185
  - lib/mongo/operation/write/drop_index.rb
179
186
  - lib/mongo/operation/write/insert.rb
187
+ - lib/mongo/operation/write/legacy_bulk_mergable.rb
180
188
  - lib/mongo/operation/write/remove_user.rb
181
189
  - lib/mongo/operation/write/update.rb
190
+ - lib/mongo/options.rb
191
+ - lib/mongo/options/mapper.rb
182
192
  - lib/mongo/protocol.rb
183
193
  - lib/mongo/protocol/bit_vector.rb
184
194
  - lib/mongo/protocol/delete.rb
@@ -278,10 +288,10 @@ files:
278
288
  - spec/mongo/operation/write/command/delete_spec.rb
279
289
  - spec/mongo/operation/write/command/insert_spec.rb
280
290
  - spec/mongo/operation/write/command/update_spec.rb
291
+ - spec/mongo/operation/write/create_index_spec.rb
281
292
  - spec/mongo/operation/write/create_user_spec.rb
282
293
  - spec/mongo/operation/write/delete_spec.rb
283
294
  - spec/mongo/operation/write/drop_index_spec.rb
284
- - spec/mongo/operation/write/ensure_index_spec.rb
285
295
  - spec/mongo/operation/write/insert_spec.rb
286
296
  - spec/mongo/operation/write/remove_user_spec.rb
287
297
  - spec/mongo/operation/write/response_spec.rb
@@ -383,6 +393,10 @@ files:
383
393
  - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Primary.yml
384
394
  - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/PrimaryPreferred.yml
385
395
  - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/PrimaryPreferred_non_matching.yml
396
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Secondary.yml
397
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/SecondaryPreferred.yml
398
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/SecondaryPreferred_non_matching.yml
399
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Secondary_non_matching.yml
386
400
  - spec/support/server_selection/selection/Sharded/read/SecondaryPreferred.yml
387
401
  - spec/support/server_selection/selection/Single/read/SecondaryPreferred.yml
388
402
  - spec/support/server_selection/selection/Unknown/read/SecondaryPreferred.yml
@@ -475,10 +489,10 @@ test_files:
475
489
  - spec/mongo/operation/write/command/delete_spec.rb
476
490
  - spec/mongo/operation/write/command/insert_spec.rb
477
491
  - spec/mongo/operation/write/command/update_spec.rb
492
+ - spec/mongo/operation/write/create_index_spec.rb
478
493
  - spec/mongo/operation/write/create_user_spec.rb
479
494
  - spec/mongo/operation/write/delete_spec.rb
480
495
  - spec/mongo/operation/write/drop_index_spec.rb
481
- - spec/mongo/operation/write/ensure_index_spec.rb
482
496
  - spec/mongo/operation/write/insert_spec.rb
483
497
  - spec/mongo/operation/write/remove_user_spec.rb
484
498
  - spec/mongo/operation/write/response_spec.rb
@@ -579,6 +593,10 @@ test_files:
579
593
  - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Primary.yml
580
594
  - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/PrimaryPreferred.yml
581
595
  - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/PrimaryPreferred_non_matching.yml
596
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Secondary.yml
597
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/Secondary_non_matching.yml
598
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/SecondaryPreferred.yml
599
+ - spec/support/server_selection/selection/ReplicaSetWithPrimary/read/SecondaryPreferred_non_matching.yml
582
600
  - spec/support/server_selection/selection/Sharded/read/SecondaryPreferred.yml
583
601
  - spec/support/server_selection/selection/Single/read/SecondaryPreferred.yml
584
602
  - spec/support/server_selection/selection/Unknown/read/SecondaryPreferred.yml
@@ -591,3 +609,4 @@ test_files:
591
609
  - spec/support/shared/server_selector.rb
592
610
  - spec/support/shared/socket.rb
593
611
  - spec/support/travis.rb
612
+ has_rdoc: yard
metadata.gz.sig CHANGED
Binary file
@@ -1,307 +0,0 @@
1
- # Copyright (C) 2014-2015 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
- # This semi-public class handles the logic for executing a batch of
18
- # operations.
19
- #
20
- # @since 2.0.0
21
- class BulkWrite
22
-
23
- # @return [ Mongo::Collection ] The collection on which this bulk write
24
- # operation will be executed.
25
- attr_reader :collection
26
-
27
- # @return [ Array<Hash> ] operations The list of operations.
28
- attr_reader :operations
29
-
30
- # @return [ Hash ] options The options.
31
- attr_reader :options
32
-
33
- # @return [ Acknowledged, Unacknowledged ] write_concern The write concern.
34
- attr_reader :write_concern
35
-
36
- # Initialize the BulkWrite object.
37
- #
38
- # @example Create an ordered bulk write object.
39
- # BulkWrite.new([
40
- # { insert_one: { x: 1 }},
41
- # { update_one: [{ x: 1 }, { '$set' => { x: 2 }}]}
42
- # ],
43
- # { ordered: true },
44
- # collection
45
- # )
46
- #
47
- # @example Create an unordered bulk write object.
48
- # BulkWrite.new([
49
- # { insert_one: { x: 1 }},
50
- # { update_one: [{ x: 1 }, { '$set' => { x: 2 }}]}
51
- # ],
52
- # { ordered: false },
53
- # collection
54
- # )
55
- #
56
- # @param [ Array<Hash> ] operations The operations to execute.
57
- # @param [ Hash ] options The options for executing the operations.
58
- # @param [ Collection ] collection The collection on which the
59
- # operations will be executed.
60
- #
61
- # @option options [ String ] :ordered Whether the operations should
62
- # be executed in order.
63
- # @option options [ Hash ] :write_concern The write concern to use when
64
- # executing the operations.
65
- #
66
- # @since 2.0.0
67
- def initialize(operations, options, collection)
68
- @operations = operations
69
- @options = options
70
- @collection = collection
71
- if options[:write_concern]
72
- @write_concern = WriteConcern.get(options[:write_concern])
73
- else
74
- @write_concern = collection.write_concern
75
- end
76
- end
77
-
78
- # Is the bulk write operation ordered?
79
- #
80
- # @example Is the bulk write operation ordered?
81
- # bulk_write.ordered?
82
- #
83
- # @return [ true, false ] If the bulk write is ordered.
84
- #
85
- # @since 2.0.0
86
- def ordered?
87
- @ordered ||= !!options[:ordered]
88
- end
89
-
90
- # Execute the bulk write.
91
- #
92
- # @example execute the bulk write operations.
93
- # bulk.execute
94
- #
95
- # @return [ Hash ] The result of doing the bulk write.
96
- def execute
97
- raise Error::EmptyBatch.new if operations.empty?
98
-
99
- @index = -1
100
- @ops = []
101
-
102
- operations.each do |operation|
103
- operation.each do |name, document|
104
- if respond_to?(name, true)
105
- send(name, document)
106
- else
107
- raise Error::InvalidBulkOperation.new(name)
108
- end
109
- end
110
- end
111
-
112
- ops = merge_ops
113
-
114
- replies = []
115
- until ops.empty?
116
- op = ops.shift
117
-
118
- until op.valid_batch_size?(collection.next_primary.context.max_write_batch_size)
119
- ops = op.batch(2) + ops
120
- op = ops.shift
121
- end
122
-
123
- begin
124
- replies << op.execute(collection.next_primary.context)
125
- # @todo: No test for max message size.
126
- rescue Error::MaxBSONSize, Error::MaxMessageSize => ex
127
- raise ex unless op.batchable?
128
- ops = op.batch(2) + ops
129
- end
130
- return make_response!(replies) if stop_executing?(replies.last)
131
- end
132
- make_response!(replies) if op.write_concern.get_last_error
133
- end
134
-
135
- private
136
-
137
- def increment_index
138
- @index += 1
139
- end
140
-
141
- def valid_doc?(doc)
142
- doc.respond_to?(:keys)
143
- end
144
-
145
- def update_doc?(doc)
146
- !doc.empty? &&
147
- doc.respond_to?(:keys) &&
148
- doc.keys.first.to_s =~ /^\$/
149
- end
150
-
151
- def replacement_doc?(doc)
152
- doc.respond_to?(:keys) && doc.keys.all?{|key| key !~ /^\$/}
153
- end
154
-
155
- def insert_one(doc)
156
- raise Error::InvalidDocument.new unless valid_doc?(doc)
157
- spec = { documents: [ doc ],
158
- db_name: db_name,
159
- coll_name: collection.name,
160
- ordered: ordered?,
161
- write_concern: write_concern }
162
-
163
- push_op(Mongo::Operation::Write::BulkInsert, spec)
164
- end
165
-
166
- def delete_one(selector)
167
- raise Error::InvalidDocument.new unless valid_doc?(selector)
168
- spec = { deletes: [{ q: selector, limit: 1 }],
169
- db_name: db_name,
170
- coll_name: collection.name,
171
- ordered: ordered?,
172
- write_concern: write_concern }
173
-
174
- push_op(Mongo::Operation::Write::BulkDelete, spec)
175
- end
176
-
177
- def delete_many(selector)
178
- raise Error::InvalidDocument.new unless valid_doc?(selector)
179
- spec = { deletes: [{ q: selector, limit: 0 }],
180
- db_name: db_name,
181
- coll_name: collection.name,
182
- ordered: ordered?,
183
- write_concern: write_concern }
184
-
185
- push_op(Mongo::Operation::Write::BulkDelete, spec)
186
- end
187
-
188
- def replace_one(docs)
189
- selector = docs[0]
190
- replacement = docs[1]
191
- upsert = (docs[2] || {})[:upsert]
192
- raise ArgumentError unless selector && replacement
193
- raise Error::InvalidReplacementDocument.new unless replacement_doc?(replacement)
194
- upsert = !!upsert
195
- spec = { updates: [{ q: selector,
196
- u: replacement,
197
- multi: false,
198
- upsert: upsert }],
199
- db_name: db_name,
200
- coll_name: collection.name,
201
- ordered: ordered?,
202
- write_concern: write_concern }
203
-
204
- push_op(Mongo::Operation::Write::BulkUpdate, spec)
205
- end
206
-
207
- def update_one(docs)
208
- upsert = (docs[2] || {})[:upsert]
209
- update_one_or_many(docs[0], docs[1], upsert, false)
210
- end
211
-
212
- def update_many(docs)
213
- upsert = (docs[2] || {})[:upsert]
214
- update_one_or_many(docs[0], docs[1], upsert, true)
215
- end
216
-
217
- def update_one_or_many(selector, update, upsert, multi)
218
- raise ArgumentError unless selector && update
219
- raise Error::InvalidUpdateDocument.new unless update_doc?(update)
220
- upsert = !!upsert
221
- spec = { updates: [{ q: selector,
222
- u: update,
223
- multi: multi,
224
- upsert: upsert }],
225
- db_name: db_name,
226
- coll_name: collection.name,
227
- ordered: ordered?,
228
- write_concern: write_concern }
229
-
230
- push_op(Mongo::Operation::Write::BulkUpdate, spec)
231
- end
232
-
233
- def push_op(op_class, spec)
234
- spec.merge!(indexes: [ increment_index ])
235
- @ops << op_class.send(:new, spec)
236
- end
237
-
238
- def merge_ops
239
- if ordered?
240
- merge_consecutive_ops(@ops)
241
- else
242
- merge_ops_by_type(@ops)
243
- end
244
- end
245
-
246
- def merge_consecutive_ops(operations)
247
- operations.inject([]) do |merged_ops, op|
248
- previous_op = merged_ops.last
249
- if previous_op.class == op.class
250
- merged_ops.tap do |m|
251
- m[m.size - 1] = previous_op.merge!(op)
252
- end
253
- else
254
- merged_ops << op
255
- end
256
- end
257
- end
258
-
259
- def merge_ops_by_type(operations)
260
- ops_by_type = operations.inject({}) do |merged_ops, op|
261
- if merged_ops[op.class]
262
- merged_ops.tap do |m|
263
- m[op.class] << op
264
- end
265
- else
266
- merged_ops.tap do |m|
267
- m[op.class] = [ op ]
268
- end
269
- end
270
- end
271
-
272
- ops_by_type.keys.inject([]) do |merged_ops, type|
273
- merged_ops << merge_consecutive_ops( ops_by_type[ type ] )
274
- end.flatten
275
- end
276
-
277
- def stop_executing?(result)
278
- result && ordered? && !result.successful?
279
- end
280
-
281
- def make_response!(results)
282
- response = results.reduce({}) do |response, result|
283
- write_errors = result.aggregate_write_errors
284
- write_concern_errors = result.aggregate_write_concern_errors
285
- response.tap do |r|
286
- r['nInserted'] = ( r['nInserted'] || 0 ) + result.n_inserted if result.respond_to?(:n_inserted)
287
- r['nMatched'] = ( r['nMatched'] || 0 ) + result.n_matched if result.respond_to?(:n_matched)
288
- r['nModified'] = ( r['nModified'] || 0 ) + result.n_modified if result.respond_to?(:n_modified) && result.n_modified
289
- r['nUpserted'] = ( r['nUpserted'] || 0 ) + result.n_upserted if result.respond_to?(:n_upserted)
290
- r['nRemoved'] = ( r['nRemoved'] || 0 ) + result.n_removed if result.respond_to?(:n_removed)
291
- r['writeErrors'] = ( r['writeErrors'] || [] ) + write_errors if write_errors
292
- r['writeConcernErrors'] = ( r['writeConcernErrors'] || [] ) + write_concern_errors if write_concern_errors
293
- end
294
- end
295
-
296
- if response['writeErrors'] || response['writeConcernErrors']
297
- response.merge!('errmsg' => 'batch item errors occurred')
298
- raise Error::BulkWriteFailure.new(response)
299
- end
300
- response
301
- end
302
-
303
- def db_name
304
- collection.database.name
305
- end
306
- end
307
- end
@@ -1,103 +0,0 @@
1
- # Copyright (C) 2014-2015 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 Operation
18
-
19
- # This module contains common functionality for splitting an operation
20
- # into the specified number of children operations.
21
- # An operation including this module must provide a method called
22
- # #batch_key. It specifies the key of the spec array element to split.
23
- #
24
- # @since 2.0.0
25
- module Batchable
26
-
27
- # Slices this operation into the specified number of children operations.
28
- #
29
- # @params [ Integer ] n_batches The number of children operations to split
30
- # this one into.
31
- #
32
- # @return [ Array ] An array of children operations.
33
- #
34
- # @since 2.0.0
35
- def batch(n_batches)
36
- items = spec[batch_key]
37
- raise Exception, "Cannot batch" unless items.size >= n_batches
38
-
39
- items_per_batch = items.size / n_batches
40
- batches = items.each_slice(items_per_batch).to_a
41
- index_batches = indexes.each_slice(items_per_batch).to_a
42
-
43
- # #each_slice makes groups containing exactly items_per_batch number of items.
44
- # You could therefore end up with more groups than n_batches, so put the
45
- # remaining items in the last group.
46
- if batches.size > n_batches
47
- batches[n_batches - 1] << batches.pop(batches.size - n_batches)
48
- batches[-1].flatten!
49
- end
50
-
51
- if index_batches.size > n_batches
52
- index_batches[n_batches - 1] << index_batches.pop(index_batches.size - n_batches)
53
- index_batches[-1].flatten!
54
- end
55
-
56
- batches.each_with_index.inject([]) do |children, (batch, i)|
57
- spec_copy = spec.dup
58
- spec_copy[:indexes] = index_batches[i]
59
- spec_copy[batch_key] = batch
60
- children << self.class.new(spec_copy)
61
- end
62
- end
63
-
64
- # Merge another operation with this one.
65
- # Requires that the collection and database of the two ops are the same.
66
- #
67
- # @params[ Mongo::Operation ] The other operation.
68
- #
69
- # @return [ self ] This operation merged with the other one.
70
- #
71
- # @since 2.0.0
72
- def merge!(other)
73
- # @todo: use specific exception
74
- raise Exception, "Cannot merge" unless self.class == other.class &&
75
- coll_name == other.coll_name &&
76
- db_name == other.db_name
77
- @spec[batch_key] += other.spec[batch_key]
78
- @spec[:indexes] = indexes + other.indexes
79
- self
80
- end
81
-
82
- # Determine if the batch size exceeds a given maximum.
83
- #
84
- # @params[ Integer ] The max batch size.
85
- #
86
- # @return [ true, false ] If this operation exceeds the given batch size.
87
- #
88
- # @since 2.0.0
89
- def valid_batch_size?(max)
90
- spec[batch_key].size < max
91
- end
92
-
93
- # Whether this operation can be split into batches.
94
- #
95
- # @return [ true, false ] If this operation can be split into batches.
96
- #
97
- # @since 2.0.0
98
- def batchable?
99
- spec[batch_key].size > 1
100
- end
101
- end
102
- end
103
- end