mongo 2.0.0.beta → 2.0.0.rc

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 (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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fd709834ca22c23121ccd052a08277d95dee73f
4
- data.tar.gz: 87ff2b2bde354cf9160909c7d2ea18c6dbf8534e
3
+ metadata.gz: 05111deea6c32629a6dea5c78f42502e4667f42e
4
+ data.tar.gz: c83465c51c98355b68843b3e9cdf5f9c89621a8e
5
5
  SHA512:
6
- metadata.gz: b385d02b0e3cf9b60b65051f2c5c417767a722c49f2a4aa90551cd040b64b3871b2ee023a6bc5d3e20855a23d5a162d4ede62028269573231c1e965e67ccf8d2
7
- data.tar.gz: e3408e4cacf4956b4b06f8a1541eb6e34414469f773892592733f028a7a39f9238d51ad842ca637a01d5cd71a7a798aaf20c70153636a391b3deb902984c1299
6
+ metadata.gz: ef54dd2b1941e7b27aae4887e1c8661d929a0c8efe06b00fc3a4ec56541f75023111efbbe7e6dd0934acf48e2147b883d4c68d6756c62c305b374b512ba1ff4a
7
+ data.tar.gz: 38079d819c3d24a50f7f8b4346196b37936ae04074175a06c0aff4dbeee1564544eb10d91cbc97453326b7e0e41461cc79b3239faa836a9ef94c8a3f8ab9e70a
Binary file
data.tar.gz.sig CHANGED
Binary file
data/Rakefile CHANGED
@@ -29,8 +29,21 @@ namespace :spec do
29
29
  end
30
30
 
31
31
  task :release => :spec do
32
- system "git tag -a v#{Mongo::VERSION} -m 'Tagging release: #{Mongo::VERSION}'"
32
+ system "git tag -a #{Mongo::VERSION} -m 'Tagging release: #{Mongo::VERSION}'"
33
33
  system "git push --tags"
34
+ system "gem build mongo.gemspec"
34
35
  system "gem push mongo-#{Mongo::VERSION}.gem"
35
36
  system "rm mongo-#{Mongo::VERSION}.gem"
36
37
  end
38
+
39
+ desc "Generate all documentation"
40
+ task :docs => 'docs:yard'
41
+
42
+ namespace :docs do
43
+ desc "Generate yard documention"
44
+ task :yard do
45
+ out = File.join('docs', Mongo::VERSION)
46
+ FileUtils.rm_rf(out)
47
+ system "yardoc -o #{out} --title mongo-#{Mongo::VERSION}"
48
+ end
49
+ end
@@ -21,7 +21,7 @@ require 'mongo/error'
21
21
  require 'mongo/event'
22
22
  require 'mongo/address'
23
23
  require 'mongo/auth'
24
- require 'mongo/bulk'
24
+ require 'mongo/bulk_write'
25
25
  require 'mongo/client'
26
26
  require 'mongo/cluster'
27
27
  require 'mongo/collection'
@@ -30,6 +30,7 @@ require 'mongo/database'
30
30
  require 'mongo/grid'
31
31
  require 'mongo/index'
32
32
  require 'mongo/operation'
33
+ require 'mongo/options'
33
34
  require 'mongo/protocol'
34
35
  require 'mongo/server'
35
36
  require 'mongo/server_selector'
@@ -56,9 +56,9 @@ module Mongo
56
56
  # Get the authorization strategy for the provided auth mechanism.
57
57
  #
58
58
  # @example Get the strategy.
59
- # Auth.get(:mongodb_cr)
59
+ # Auth.get(user)
60
60
  #
61
- # @param [ Symbol ] mechanism The authorization mechanism.
61
+ # @param [ Auth::User ] user The user object.
62
62
  #
63
63
  # @return [ CR, X509, LDAP, Kerberos ] The auth strategy.
64
64
  #
@@ -0,0 +1,52 @@
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
+ require 'mongo/bulk_write/bulk_writable'
16
+ require 'mongo/bulk_write/ordered_bulk_write'
17
+ require 'mongo/bulk_write/unordered_bulk_write'
18
+
19
+ module Mongo
20
+ module BulkWrite
21
+ extend self
22
+
23
+ # Get a bulk write object either of type ordered or unordered.
24
+ #
25
+ # @example Get a bulk write object.
26
+ # Mongo::BulkWrite.get(collection, operations, ordered: true)
27
+ #
28
+ # @param [ Collection ] collection The collection on which the operations
29
+ # will be executed.
30
+ #
31
+ # @param [ Array<Hash> ] operations The operations to execute.
32
+ #
33
+ # @param [ Hash ] options The options for the bulk write object.
34
+ #
35
+ # @option options [ true, false ] :ordered Whether the operations
36
+ # should be executed in order.
37
+ # @option options [ Hash ] :write_concern The write concern options.
38
+ # Can be :w => Integer, :fsync => Boolean, :j => Boolean.
39
+ #
40
+ # @return [ OrderedBulkWrite, UnorderedBulkWrite ] The appropriate bulk
41
+ # write object.
42
+ #
43
+ # @since 2.0.0
44
+ def get(collection, operations, options)
45
+ if options.fetch(:ordered, true)
46
+ OrderedBulkWrite.new(collection, operations, options)
47
+ else
48
+ UnorderedBulkWrite.new(collection, operations, options)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,191 @@
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
+ require 'mongo/bulk_write/insertable'
16
+ require 'mongo/bulk_write/deletable'
17
+ require 'mongo/bulk_write/updatable'
18
+ require 'mongo/bulk_write/replacable'
19
+
20
+ module Mongo
21
+ module BulkWrite
22
+
23
+ # Defines shared behaviour between ordered and unordered bulk operations.
24
+ #
25
+ # @since 2.0.0
26
+ module BulkWritable
27
+ include Insertable
28
+ include Deletable
29
+ include Updatable
30
+ include Replacable
31
+ extend Forwardable
32
+
33
+ # Delegate various methods to the collection.
34
+ def_delegators :@collection, :database, :cluster, :next_primary
35
+
36
+ # The fields contained in the result document returned from executing the
37
+ # operations.
38
+ #
39
+ # @since 2.0.0.
40
+ RESULT_FIELDS = [ :n_inserted,
41
+ :n_removed,
42
+ :n_modified,
43
+ :n_upserted,
44
+ :n_matched ]
45
+
46
+
47
+ # Initialize a bulk write object.
48
+ #
49
+ # @example Initialize a bulk write object.
50
+ # Mongo::BulkWrite::OrderedBulkWrite.new(collection, operations, options)
51
+ # Mongo::BulkWrite::UnorderedBulkWrite.new(collection, operations, options)
52
+ #
53
+ # @param [ Mongo::Collection ] collection The collection the operations will
54
+ # be executed on.
55
+ # @param [ Array<Hash> ] operations The operations to be executed.
56
+ # @param [ Hash ] options The options.
57
+ #
58
+ # @option options [ Hash ] :write_concern The write concern to use for this
59
+ # bulk write.
60
+ #
61
+ # @since 2.0.0
62
+ def initialize(collection, operations, options)
63
+ @collection = collection
64
+ @operations = operations
65
+ @options = options
66
+ end
67
+
68
+ # Execute the bulk operations.
69
+ #
70
+ # @example Execute the operations.
71
+ # bulk.execute
72
+ #
73
+ # @return [ Hash ] The results from the bulk write.
74
+ #
75
+ # @since 2.0.0
76
+ def execute
77
+ server = next_primary
78
+ validate_operations!
79
+ merged_ops.each do |op|
80
+ validate_type!(op.keys.first)
81
+ execute_op(op, server)
82
+ end
83
+ finalize
84
+ end
85
+
86
+ private
87
+
88
+ def write_concern
89
+ @write_concern ||= WriteConcern.get(@options[:write_concern]) ||
90
+ @collection.write_concern
91
+ end
92
+
93
+ def validate_operations!
94
+ unless @operations && @operations.size > 0
95
+ raise ArgumentError.new('Operations cannot be empty')
96
+ end
97
+ end
98
+
99
+ def validate_type!(type)
100
+ unless respond_to?(type, true)
101
+ raise Error::InvalidBulkOperationType.new(type)
102
+ end
103
+ end
104
+
105
+ def max_write_batches(op, server)
106
+ type = op.keys.first
107
+ ops = []
108
+ while op[type].size > server.max_write_batch_size
109
+ ops << { type => op[type].shift(server.max_write_batch_size),
110
+ :indexes => op[:indexes].shift(server.max_write_batch_size) }
111
+ end
112
+ ops << op
113
+ end
114
+
115
+ def split(op, type)
116
+ n = op[type].size/2
117
+ [ { type => op[type].shift(n),
118
+ :indexes => op[:indexes].shift(n) },
119
+ { type => op[type],
120
+ :indexes => op[:indexes] }
121
+ ]
122
+ end
123
+
124
+ def execute_op(operation, server)
125
+ ops = max_write_batches(operation, server)
126
+
127
+ until ops.empty?
128
+ op = ops.shift
129
+ type = op.keys.first
130
+ begin
131
+ process(send(type, op, server), op[:indexes])
132
+ rescue Error::MaxBSONSize, Error::MaxMessageSize => ex
133
+ raise ex if op[type].size < 2
134
+ ops = split(op, type) + ops
135
+ end
136
+ end
137
+ end
138
+
139
+ def merge_consecutive_ops(ops)
140
+ ops.each_with_index.inject([]) do |merged, (op, i)|
141
+ type = op.keys.first
142
+ op[:indexes] ||= [ i ]
143
+ previous = merged.last
144
+ if previous && previous.keys.first == type
145
+ merged[-1].merge!(type => previous[type] << op[type],
146
+ :indexes => previous[:indexes] + op[:indexes])
147
+ merged
148
+ else
149
+ merged << { type => [ op[type] ].flatten,
150
+ :indexes => op[:indexes] }
151
+ end
152
+ end
153
+ end
154
+
155
+ def merge_ops_by_type
156
+ indexes = {}
157
+ ops_hash = @operations.each_with_index.inject({}) do |merged, (op, i)|
158
+ type = op.keys.first
159
+ merged.merge!(op) { |type, v1, v2| ([v1] << v2).flatten }
160
+ indexes[type] = (indexes[type] || []).push(i)
161
+ merged
162
+ end
163
+ ops_hash.keys.reduce([]) do |ops_list, type|
164
+ ops_list << { type => ops_hash[type], :indexes => indexes[type] }
165
+ end
166
+ end
167
+
168
+ def combine_results(result, indexes)
169
+ @results ||= {}
170
+ write_errors = result.aggregate_write_errors(indexes)
171
+
172
+ # The Bulk API only returns the first write concern error encountered.
173
+ @write_concern_errors ||= result.aggregate_write_concern_errors(indexes)
174
+
175
+ @results.tap do |results|
176
+ RESULT_FIELDS.each do |field|
177
+ results.merge!(
178
+ field => (results[field] || 0) + result.send(field)
179
+ ) if result.respond_to?(field)
180
+ end
181
+
182
+ results.merge!(
183
+ write_errors: ((results[:write_errors] || []) << write_errors).flatten
184
+ ) if write_errors
185
+
186
+ results.merge!(write_concern_errors: @write_concern_errors) if @write_concern_errors
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,60 @@
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
+ module BulkWrite
17
+
18
+ # Defines behavior for validating and combining delete bulk write operations.
19
+ #
20
+ # @since 2.0.0.
21
+ module Deletable
22
+
23
+ private
24
+
25
+ def valid_doc?(doc)
26
+ doc.respond_to?(:keys)
27
+ end
28
+
29
+ def validate_delete_op!(type, d)
30
+ raise Error::InvalidBulkOperation.new(type, d) unless valid_doc?(d)
31
+ end
32
+
33
+ def deletes(ops, type)
34
+ limit = (type == :delete_one) ? 1 : 0
35
+ ops.collect do |d|
36
+ validate_delete_op!(type, d)
37
+ { q: d, limit: limit }
38
+ end
39
+ end
40
+
41
+ def delete(ops, type, server)
42
+ Operation::Write::BulkDelete.new(
43
+ :deletes => deletes(ops, type),
44
+ :db_name => database.name,
45
+ :coll_name => @collection.name,
46
+ :write_concern => write_concern,
47
+ :ordered => ordered?
48
+ ).execute(server.context)
49
+ end
50
+
51
+ def delete_one(op, server)
52
+ delete(op[:delete_one], __method__, server)
53
+ end
54
+
55
+ def delete_many(op, server)
56
+ delete(op[:delete_many], __method__, server)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,52 @@
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
+ module BulkWrite
17
+
18
+ # Defines behavior for validating and combining insert bulk write operations.
19
+ #
20
+ # @since 2.0.0.
21
+ module Insertable
22
+
23
+ private
24
+
25
+ def valid_doc?(doc)
26
+ doc.respond_to?(:keys)
27
+ end
28
+
29
+ def validate_insert_ops!(type, inserts)
30
+ if inserts.empty?
31
+ raise Error::InvalidBulkOperation.new(type, inserts)
32
+ end
33
+ inserts.each do |i|
34
+ unless valid_doc?(i)
35
+ raise Error::InvalidBulkOperation.new(type, i)
36
+ end
37
+ end
38
+ end
39
+
40
+ def insert_one(op, server)
41
+ validate_insert_ops!(__method__, op[:insert_one])
42
+ Operation::Write::BulkInsert.new(
43
+ :documents => op[:insert_one].flatten,
44
+ :db_name => database.name,
45
+ :coll_name => @collection.name,
46
+ :write_concern => write_concern,
47
+ :ordered => ordered?
48
+ ).execute(server.context)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,48 @@
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 BulkWrite
18
+
19
+ class OrderedBulkWrite
20
+
21
+ include BulkWritable
22
+
23
+ private
24
+
25
+ def ordered?
26
+ true
27
+ end
28
+
29
+ def merged_ops
30
+ merge_consecutive_ops(@operations)
31
+ end
32
+
33
+ def process(result, indexes)
34
+ combine_results(result, indexes)
35
+ raise Error::BulkWriteError.new(@results) if stop?
36
+ end
37
+
38
+ def stop?
39
+ @results.keys.include?(:write_errors)
40
+ end
41
+
42
+ def finalize
43
+ raise Error::BulkWriteError.new(@results) if @results[:write_concern_errors]
44
+ @results
45
+ end
46
+ end
47
+ end
48
+ end