@@ -200,33 +200,10 @@ defmodule Mongo.BulkWrite do
200
200
ops
201
201
|> get_op_sequence ( )
202
202
|> Enum . map ( fn { cmd , docs } -> one_bulk_write_operation ( conn , cmd , coll , docs , write_concern , max_batch_size , opts ) end )
203
- |> Enum . reduce ( empty , fn
204
- { cmd , { count , ids } } , acc -> merge ( cmd , count , ids , acc )
205
- { cmd , { mat , mod , ups , ids } } , acc -> merge ( cmd , mat , mod , ups , ids , acc )
206
- { cmd , count } , acc -> merge ( cmd , count , acc )
207
-
208
- end )
203
+ |> BulkWriteResult . reduce ( empty )
209
204
end
210
205
end
211
206
212
- defp merge ( :insert , count , ids , % BulkWriteResult { :inserted_count => value , :inserted_ids => current_ids } = result ) do
213
- % BulkWriteResult { result | inserted_count: value + count , inserted_ids: current_ids ++ ids }
214
- end
215
- defp merge ( :update , matched , modified , upserts , ids ,
216
- % BulkWriteResult { :matched_count => matched_count ,
217
- :modified_count => modified_count ,
218
- :upserted_count => upserted_count ,
219
- :upserted_ids => current_ids } = result ) do
220
- % BulkWriteResult { result | matched_count: matched_count + matched ,
221
- modified_count: modified_count + modified ,
222
- upserted_count: upserted_count + upserts ,
223
- upserted_ids: current_ids ++ ids }
224
- end
225
- defp merge ( :delete , count , % BulkWriteResult { :deleted_count => value } = result ) do
226
- % BulkWriteResult { result | deleted_count: value + count }
227
- end
228
- defp merge ( _other , _count , result ) , do: result
229
-
230
207
##
231
208
# returns the current write concerns from `opts`
232
209
#
@@ -252,20 +229,12 @@ defmodule Mongo.BulkWrite do
252
229
253
230
with { :ok , limits } <- Mongo . limits ( conn ) ,
254
231
max_batch_size <- limits . max_write_batch_size ,
255
- { _ , { inserts , ids } } <- one_bulk_write_operation ( conn , :insert , coll , inserts , write_concern , max_batch_size , opts ) ,
256
- { _ , { matched , modified , upserts , upsert_ids } } <- one_bulk_write_operation ( conn , :update , coll , updates , write_concern , max_batch_size , opts ) ,
257
- { _ , deletes } <- one_bulk_write_operation ( conn , :delete , coll , deletes , write_concern , max_batch_size , opts ) do
258
-
259
- % BulkWriteResult {
260
- acknowledged: acknowledged ( write_concern ) ,
261
- inserted_count: inserts ,
262
- inserted_ids: ids ,
263
- matched_count: matched ,
264
- deleted_count: deletes ,
265
- modified_count: modified ,
266
- upserted_count: upserts ,
267
- upserted_ids: upsert_ids
268
- }
232
+ insert_result <- one_bulk_write_operation ( conn , :insert , coll , inserts , write_concern , max_batch_size , opts ) ,
233
+ update_result <- one_bulk_write_operation ( conn , :update , coll , updates , write_concern , max_batch_size , opts ) ,
234
+ delete_result <- one_bulk_write_operation ( conn , :delete , coll , deletes , write_concern , max_batch_size , opts ) do
235
+
236
+ [ insert_result , update_result , delete_result ]
237
+ |> BulkWriteResult . reduce ( % BulkWriteResult { acknowledged: acknowledged ( write_concern ) } )
269
238
end
270
239
end
271
240
@@ -274,7 +243,7 @@ defmodule Mongo.BulkWrite do
274
243
#
275
244
defp one_bulk_write_operation ( conn , cmd , coll , docs , write_concern , max_batch_size , opts ) do
276
245
with result <- conn |> run_commands ( get_cmds ( cmd , coll , docs , write_concern , max_batch_size , opts ) , opts ) |> collect ( cmd ) do
277
- { cmd , result }
246
+ result
278
247
end
279
248
end
280
249
@@ -330,35 +299,34 @@ defmodule Mongo.BulkWrite do
330
299
#
331
300
defp collect ( { docs , ids } , :insert ) do
332
301
333
- { docs
334
- |> Enum . map ( fn
335
- { :ok , % { "n" => n } } -> n
336
- { :ok , _other } -> 0
337
- end )
338
- |> Enum . reduce ( 0 , fn x , acc -> x + acc end ) , ids }
339
-
302
+ docs
303
+ |> Enum . map ( fn
304
+ { :ok , % { "n" => n } = doc } -> BulkWriteResult . insert_result ( n , ids , doc [ "writeErrors" ] || [ ] )
305
+ { :ok , _other } -> BulkWriteResult . empty ( )
306
+ end )
307
+ |> BulkWriteResult . reduce ( )
340
308
end
309
+
341
310
defp collect ( docs , :update ) do
342
311
343
312
docs
344
313
|> Enum . map ( fn
345
- { :ok , % { "n" => n , "nModified" => modified , "upserted" => ids } } -> l = length ( ids ) ; { n - l , modified , l , filter_upsert_ids ( ids ) }
346
- { :ok , % { "n" => matched , "nModified" => modified } } -> { matched , modified , 0 , [ ] }
347
- { :ok , _other } -> { 0 , 0 , 0 , [ ] }
348
- end )
349
- |> Enum . reduce ( { 0 , 0 , 0 , [ ] } , fn
350
- { mat , mod , ups , ids } , { s_mat , s_mod , s_ups , all } -> { s_mat + mat , s_mod + mod , s_ups + ups , all ++ ids }
314
+ { :ok , % { "n" => n , "nModified" => modified , "upserted" => ids } = doc } -> l = length ( ids )
315
+ BulkWriteResult . update_result ( n - l , modified , l , filter_upsert_ids ( ids ) , doc [ "writeErrors" ] || [ ] )
316
+ { :ok , % { "n" => matched , "nModified" => modified } = doc } -> BulkWriteResult . update_result ( matched , modified , 0 , [ ] , doc [ "writeErrors" ] || [ ] )
317
+ { :ok , _other } -> BulkWriteResult . empty ( )
351
318
end )
319
+ |> BulkWriteResult . reduce ( )
352
320
353
321
end
354
322
defp collect ( docs , :delete ) do
355
323
356
324
docs
357
325
|> Enum . map ( fn
358
- { :ok , % { "n" => n } } -> n
359
- { :ok , _other } -> 0
326
+ { :ok , % { "n" => n } = doc } -> BulkWriteResult . delete_result ( n , doc [ "writeErrors" ] || [ ] )
327
+ { :ok , _other } -> BulkWriteResult . empty ( )
360
328
end )
361
- |> Enum . reduce ( 0 , fn x , acc -> x + acc end )
329
+ |> BulkWriteResult . reduce ( )
362
330
363
331
end
364
332
0 commit comments