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
@@ -0,0 +1,67 @@
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 Operation
17
+ module Write
18
+
19
+ module BulkMergable
20
+
21
+ # Aggregate the write errors returned from this result.
22
+ #
23
+ # @example Aggregate the write errors.
24
+ # result.aggregate_write_errors([0, 1, 2, 3])
25
+ #
26
+ # @param [ Array ] indexes The indexes of each operation as they
27
+ # were listed in the Bulk API.
28
+ #
29
+ # @return [ Array ] The aggregate write errors.
30
+ #
31
+ # @since 2.0.0
32
+ def aggregate_write_errors(indexes)
33
+ @replies.reduce(nil) do |errors, reply|
34
+ if reply.documents.first['writeErrors']
35
+ write_errors = reply.documents.first['writeErrors'].collect do |we|
36
+ we.merge!('index' => indexes[we['index']])
37
+ end
38
+ (errors || []) << write_errors if write_errors
39
+ end
40
+ end
41
+ end
42
+
43
+ # Aggregate the write concern errors returned from this result.
44
+ #
45
+ # @example Aggregate the write concern errors.
46
+ # result.aggregate_write_concern_errors([0, 1, 2, 3])
47
+ #
48
+ # @param [ Array ] indexes The indexes of each operation as they
49
+ # were listed in the Bulk API.
50
+ #
51
+ # @return [ Array ] The aggregate write concern errors.
52
+ #
53
+ # @since 2.0.0
54
+ def aggregate_write_concern_errors(indexes)
55
+ @replies.each_with_index.reduce(nil) do |errors, (reply, i)|
56
+ if write_concern_errors = reply.documents.first['writeConcernErrors']
57
+ (errors || []) << write_concern_errors.reduce(nil) do |errs, wce|
58
+ wce.merge!('index' => indexes[wce['index']])
59
+ (errs || []) << write_concern_error
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -56,14 +56,13 @@ module Mongo
56
56
  class BulkUpdate
57
57
  include Executable
58
58
  include Specifiable
59
- include Batchable
60
59
 
61
60
  # Execute the update operation.
62
61
  #
63
62
  # @example Execute the operation.
64
63
  # operation.execute(context)
65
64
  #
66
- # @params [ Mongo::Server::Context ] The context for this operation.
65
+ # @param [ Mongo::Server::Context ] context The context for this operation.
67
66
  #
68
67
  # @return [ Result ] The operation result.
69
68
  #
@@ -81,7 +80,7 @@ module Mongo
81
80
  # @example Set a write concern.
82
81
  # new_op = operation.write_concern(:w => 2)
83
82
  #
84
- # @params [ Hash ] The write concern.
83
+ # @param [ Hash ] wc The write concern.
85
84
  #
86
85
  # @since 2.0.0
87
86
  def write_concern(wc = nil)
@@ -92,19 +91,10 @@ module Mongo
92
91
  end
93
92
  end
94
93
 
95
- # The index of each update as it was added onto the bulk object.
96
- #
97
- # @params [ Integer ] The index of each update.
98
- #
99
- # @since 2.0.0
100
- def indexes
101
- @spec[:indexes] || []
102
- end
103
-
104
94
  private
105
95
 
106
96
  def execute_write_command(context)
107
- Result.new(Command::Update.new(spec).execute(context)).set_indexes(indexes)
97
+ Result.new(Command::Update.new(spec).execute(context))
108
98
  end
109
99
 
110
100
  def execute_message(context)
@@ -112,13 +102,13 @@ module Mongo
112
102
  context.with_connection do |connection|
113
103
  result = LegacyResult.new(connection.dispatch([ m, gle ].compact))
114
104
  if stop_sending?(result)
115
- return result.set_indexes(indexes)
105
+ return result
116
106
  else
117
107
  result.reply
118
108
  end
119
109
  end
120
110
  end
121
- LegacyResult.new(replies.compact.empty? ? nil : replies).set_indexes(indexes)
111
+ LegacyResult.new(replies.compact.empty? ? nil : replies)
122
112
  end
123
113
 
124
114
  def stop_sending?(result)
@@ -144,10 +134,6 @@ module Mongo
144
134
  end
145
135
  end
146
136
 
147
- def batch_key
148
- UPDATES
149
- end
150
-
151
137
  def initialize_copy(original)
152
138
  @spec = original.spec.dup
153
139
  @spec[UPDATES] = original.spec[UPDATES].dup
@@ -17,6 +17,6 @@ require 'mongo/operation/write/command/delete'
17
17
  require 'mongo/operation/write/command/insert'
18
18
  require 'mongo/operation/write/command/update'
19
19
  require 'mongo/operation/write/command/drop_index'
20
- require 'mongo/operation/write/command/ensure_index'
20
+ require 'mongo/operation/write/command/create_index'
21
21
  require 'mongo/operation/write/command/create_user'
22
22
  require 'mongo/operation/write/command/remove_user'
@@ -20,12 +20,10 @@ module Mongo
20
20
  # A MongoDB ensure index write command operation.
21
21
  #
22
22
  # @example Create an ensure index command operation.
23
- # Write::Command::EnsureIndex.new({
24
- # :index => { :foo => 1 },
23
+ # Write::Command::CreateIndex.new({
24
+ # :indexes => [{ :key => { :foo => 1 }, :name => 'foo_1', :unique => true }],
25
25
  # :db_name => 'test',
26
- # :coll_name => 'test_coll',
27
- # :index_name => 'foo_1'
28
- # :options => { :unique => true }
26
+ # :coll_name => 'test_coll'
29
27
  # })
30
28
  #
31
29
  # @since 2.0.0
@@ -42,10 +40,7 @@ module Mongo
42
40
  #
43
41
  # @since 2.0.0
44
42
  def selector
45
- {
46
- :createIndexes => coll_name,
47
- :indexes => [ options.merge(key: index, name: index_name) ],
48
- }
43
+ { :createIndexes => coll_name, :indexes => indexes }
49
44
  end
50
45
  end
51
46
  end
@@ -42,10 +42,10 @@ module Mongo
42
42
  #
43
43
  # @since 2.0.0
44
44
  def selector
45
- { :delete => coll_name,
46
- :deletes => deletes,
47
- :writeConcern => write_concern.options,
48
- :ordered => ordered?
45
+ { delete: coll_name,
46
+ deletes: deletes,
47
+ writeConcern: write_concern.options,
48
+ ordered: ordered?
49
49
  }
50
50
  end
51
51
  end
@@ -41,7 +41,7 @@ module Mongo
41
41
  #
42
42
  # @since 2.0.0
43
43
  def selector
44
- { :deleteIndexes => coll_name, :index => index_name }
44
+ { :dropIndexes => coll_name, :index => index_name }
45
45
  end
46
46
  end
47
47
  end
@@ -41,10 +41,10 @@ module Mongo
41
41
  #
42
42
  # @since 2.0.0
43
43
  def selector
44
- { :insert => coll_name,
45
- :documents => documents,
46
- :writeConcern => write_concern.options,
47
- :ordered => ordered?
44
+ { insert: coll_name,
45
+ documents: documents,
46
+ writeConcern: write_concern.options,
47
+ ordered: ordered?
48
48
  }
49
49
  end
50
50
  end
@@ -46,10 +46,10 @@ module Mongo
46
46
  #
47
47
  # @return [ Hash ] The selector describing this update operation.
48
48
  def selector
49
- { :update => coll_name,
50
- :updates => updates,
51
- :writeConcern => write_concern.options,
52
- :ordered => ordered?
49
+ { update: coll_name,
50
+ updates: updates,
51
+ writeConcern: write_concern.options,
52
+ ordered: ordered?
53
53
  }
54
54
  end
55
55
  end
@@ -17,13 +17,13 @@ module Mongo
17
17
  module Operation
18
18
  module Write
19
19
 
20
- # A MongoDB ensure index operation.
20
+ # A MongoDB create index operation.
21
21
  #
22
22
  # @note If a server with version >= 2.5.5 is being used, a write command
23
23
  # operation will be created and sent instead.
24
24
  #
25
- # @example Create the ensure index operation.
26
- # Write::EnsureIndex.new({
25
+ # @example Create the create index operation.
26
+ # Write::CreateIndex.new({
27
27
  # :index => { :name => 1, :age => -1 },
28
28
  # :db_name => 'test',
29
29
  # :coll_name => 'test_coll',
@@ -40,7 +40,7 @@ module Mongo
40
40
  # write command.
41
41
  #
42
42
  # @since 2.0.0
43
- class EnsureIndex
43
+ class CreateIndex
44
44
  include Executable
45
45
  include Specifiable
46
46
 
@@ -49,7 +49,7 @@ module Mongo
49
49
  # @example Execute the operation.
50
50
  # operation.execute(context)
51
51
  #
52
- # @params [ Mongo::Server::Context ] The context for this operation.
52
+ # @param [ Mongo::Server::Context ] context The context for this operation.
53
53
  #
54
54
  # @return [ Result ] The result of the operation.
55
55
  #
@@ -74,9 +74,14 @@ module Mongo
74
74
  end
75
75
  end
76
76
 
77
+ def index_documents
78
+ indexes.dup.each do |index|
79
+ index[:ns] = namespace
80
+ end
81
+ end
82
+
77
83
  def message
78
- document = options.merge(ns: namespace, key: index, name: index_name)
79
- Protocol::Insert.new(db_name, Index::COLLECTION, [ document ])
84
+ Protocol::Insert.new(db_name, Index::COLLECTION, index_documents)
80
85
  end
81
86
  end
82
87
  end
@@ -40,7 +40,7 @@ module Mongo
40
40
  # @example Execute the operation.
41
41
  # operation.execute(context)
42
42
  #
43
- # @param [ Mongo::Server::Context ] The context for this operation.
43
+ # @param [ Mongo::Server::Context ] context The context for this operation.
44
44
  #
45
45
  # @return [ Result ] The operation result.
46
46
  #
@@ -53,7 +53,7 @@ module Mongo
53
53
  # @example Execute the operation.
54
54
  # operation.execute(context)
55
55
  #
56
- # @params [ Mongo::Server::Context ] The context for this operation.
56
+ # @param [ Mongo::Server::Context ] context The context for this operation.
57
57
  #
58
58
  # @return [ Result ] The result.
59
59
  #
@@ -42,7 +42,7 @@ module Mongo
42
42
  # @example Execute the operation.
43
43
  # operation.execute(context)
44
44
  #
45
- # @params [ Mongo::Server::Context ] The context for this operation.
45
+ # @param [ Mongo::Server::Context ] context The context for this operation.
46
46
  #
47
47
  # @return [ Result ] The result of the operation.
48
48
  #
@@ -48,7 +48,7 @@ module Mongo
48
48
  # @example Execute the operation.
49
49
  # operation.execute(context)
50
50
  #
51
- # @params [ Mongo::Server::Context ] The context for this operation.
51
+ # @param [ Mongo::Server::Context ] context The context for this operation.
52
52
  #
53
53
  # @return [ Result ] The operation result.
54
54
  #
@@ -0,0 +1,83 @@
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 Operation
17
+ module Write
18
+
19
+ module LegacyBulkMergable
20
+
21
+ # Aggregate the write errors returned from this result.
22
+ #
23
+ # @example Aggregate the write errors.
24
+ # result.aggregate_write_errors([0, 1, 2, 3])
25
+ #
26
+ # @param [ Array ] indexes The indexes of each operation as they
27
+ # were listed in the Bulk API.
28
+ #
29
+ # @return [ Array ] The aggregate write errors.
30
+ #
31
+ # @since 2.0.0
32
+ def aggregate_write_errors(indexes)
33
+ @replies.each_with_index.reduce(nil) do |errors, (reply, i)|
34
+ if reply_write_errors?(reply)
35
+ errors ||= []
36
+ errors << { 'errmsg' => reply.documents.first[Error::ERROR],
37
+ 'index' => indexes[i],
38
+ 'code' => reply.documents.first[Error::CODE] }
39
+ end
40
+ errors
41
+ end
42
+ end
43
+
44
+ # Aggregate the write concern errors returned from this result.
45
+ #
46
+ # @example Aggregate the write concern errors.
47
+ # result.aggregate_write_concern_errors([0, 1, 2, 3])
48
+ #
49
+ # @param [ Array ] indexes The indexes of each operation as they
50
+ # were listed in the Bulk API.
51
+ #
52
+ # @return [ Array ] The aggregate write concern errors.
53
+ #
54
+ # @since 2.0.0
55
+ def aggregate_write_concern_errors(indexes)
56
+ @replies.each_with_index.reduce(nil) do |errors, (reply, i)|
57
+ if error = reply_write_errors?(reply)
58
+ errors ||= []
59
+ if note = reply.documents.first['wnote'] || reply.documents.first['jnote']
60
+ code = reply.documents.first['code'] || Error::BAD_VALUE
61
+ error_string = "#{code}: #{note}"
62
+ elsif error == 'timeout'
63
+ code = reply.documents.first['code'] || Error::UNKNOWN_ERROR
64
+ error_string = "#{code}: #{error}"
65
+ end
66
+ errors << { 'errmsg' => error_string,
67
+ 'index' => indexes[i],
68
+ 'code' => code } if error_string
69
+ end
70
+ errors
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def reply_write_errors?(reply)
77
+ reply.documents.first[Error::ERROR] ||
78
+ reply.documents.first[Error::ERRMSG]
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -36,7 +36,7 @@ module Mongo
36
36
  # @example Execute the operation.
37
37
  # operation.execute(context)
38
38
  #
39
- # @params [ Mongo::Server::Context ] The context for this operation.
39
+ # @param [ Mongo::Server::Context ] context The context for this operation.
40
40
  #
41
41
  # @return [ Result ] The operation result.
42
42
  #
@@ -56,7 +56,7 @@ module Mongo
56
56
  # @example Execute the operation.
57
57
  # operation.execute(context)
58
58
  #
59
- # @params [ Mongo::Server::Context ] The context for this operation.
59
+ # @param [ Mongo::Server::Context ] context The context for this operation.
60
60
  #
61
61
  # @return [ Result ] The operation result.
62
62
  #
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2014-2015 MongoDB, Inc.
1
+ # Copyright (C) 2015 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -12,4 +12,4 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'mongo/bulk/bulk_write'
15
+ require 'mongo/options/mapper'
@@ -0,0 +1,78 @@
1
+ # Copyright (C) 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 Options
17
+
18
+ # Utility class for various options mapping behaviour.
19
+ #
20
+ # @since 2.0.0
21
+ module Mapper
22
+ extend self
23
+
24
+ # Transforms the provided options to a new set of options given the
25
+ # provided mapping.
26
+ #
27
+ # @example Transform the options.
28
+ # Mapper.transform({ name: 1 }, { :name => :nombre })
29
+ #
30
+ # @param [ Hash ] options The options to transform
31
+ # @param [ Hash ] mappings The key mappings.
32
+ #
33
+ # @return [ Hash ] The transformed options.
34
+ #
35
+ # @since 2.0.0
36
+ def transform(options, mappings)
37
+ options.reduce({}) do |transformed, (key, value)|
38
+ transformed[mappings[key] || key] = value
39
+ transformed
40
+ end
41
+ end
42
+
43
+ # Coverts all the keys of the options to strings.
44
+ #
45
+ # @example Convert all option keys to strings.
46
+ # Mapper.transform({ :name => 1 })
47
+ #
48
+ # @param [ Hash ] options The options to transform.
49
+ #
50
+ # @return [ Hash ] The transformed options.
51
+ #
52
+ # @since 2.0.0
53
+ def transform_keys_to_strings(options)
54
+ options.reduce({}) do |transformed, (key, value)|
55
+ transformed[key.to_s] = value
56
+ transformed
57
+ end
58
+ end
59
+
60
+ # Coverts all the symbol values to strings.
61
+ #
62
+ # @example Convert all option symbol values to strings.
63
+ # Mapper.transform({ :name => 1 })
64
+ #
65
+ # @param [ Hash ] options The options to transform.
66
+ #
67
+ # @return [ Hash ] The transformed options.
68
+ #
69
+ # @since 2.0.0
70
+ def transform_values_to_strings(options)
71
+ options.reduce({}) do |transformed, (key, value)|
72
+ transformed[key] = value.is_a?(Symbol) ? value.to_s : value
73
+ transformed
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end