Skip to content

Commit c312137

Browse files
committed
Fixed #19
1 parent c5a75bc commit c312137

File tree

3 files changed

+62
-39
lines changed

3 files changed

+62
-39
lines changed

lib/mongo/bulk_write.ex

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ defmodule Mongo.BulkWrite do
157157
import Mongo.Utils
158158
alias Mongo.UnorderedBulk
159159
alias Mongo.OrderedBulk
160+
alias Mongo.BulkWriteResult
160161

161162
@doc """
162163
Executes unordered and ordered bulk writes.
@@ -176,7 +177,7 @@ defmodule Mongo.BulkWrite do
176177
If a group (inserts, updates or deletes) exceeds the limit `maxWriteBatchSize` it will be split into chunks.
177178
Everything is done in memory, so this use case is limited by memory. A better approach seems to use streaming bulk writes.
178179
"""
179-
@spec write(GenServer.server, (UnorderedBulk.t | OrderedBulk.t), Keyword.t) :: Keyword.t
180+
@spec write(GenServer.server, (UnorderedBulk.t | OrderedBulk.t), Keyword.t) :: Mongo.BulkWriteResult.t
180181
def write(topology_pid, %UnorderedBulk{} = bulk, opts) do
181182

182183
write_concern = write_concern(opts)
@@ -190,16 +191,7 @@ defmodule Mongo.BulkWrite do
190191

191192
write_concern = write_concern(opts)
192193

193-
empty = %{
194-
acknowledged: acknowledged(write_concern),
195-
insertedCount: 0,
196-
matchedCount: 0,
197-
deletedCount: 0,
198-
upsertedCount: 0,
199-
modifiedCount: 0,
200-
upsertedIds: [],
201-
insertedIds: [],
202-
}
194+
empty = %BulkWriteResult{acknowledged: acknowledged(write_concern)}
203195

204196
with {:ok, conn, _, _} <- Mongo.select_server(topology_pid, :write, opts),
205197
{:ok, limits} <- Mongo.limits(conn),
@@ -217,21 +209,21 @@ defmodule Mongo.BulkWrite do
217209
end
218210
end
219211

220-
defp merge(:insert, count, ids, %{:insertedCount => value, :insertedIds => current_ids} = result) do
221-
%{result | insertedCount: value + count, insertedIds: current_ids ++ ids}
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}
222214
end
223215
defp merge(:update, matched, modified, upserts, ids,
224-
%{:matchedCount => matched_count,
225-
:modifiedCount => modified_count,
226-
:upsertedCount => upserted_count,
227-
:upsertedIds => current_ids} = result) do
228-
%{result | matchedCount: matched_count + matched,
229-
modifiedCount: modified_count + modified,
230-
upsertedCount: upserted_count + upserts,
231-
upsertedIds: current_ids ++ 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}
232224
end
233-
defp merge(:delete, count, %{:deletedCount => value} = result) do
234-
%{result | deletedCount: value + count}
225+
defp merge(:delete, count, %BulkWriteResult{:deleted_count => value} = result) do
226+
%BulkWriteResult{result | deleted_count: value + count}
235227
end
236228
defp merge(_other, _count, result), do: result
237229

@@ -264,15 +256,15 @@ defmodule Mongo.BulkWrite do
264256
{_, {matched, modified, upserts, upsert_ids}} <- one_bulk_write_operation(conn, :update, coll, updates, write_concern, max_batch_size, opts),
265257
{_, deletes} <- one_bulk_write_operation(conn, :delete, coll, deletes, write_concern, max_batch_size, opts) do
266258

267-
%{
259+
%BulkWriteResult{
268260
acknowledged: acknowledged(write_concern),
269-
insertedCount: inserts,
270-
insertedIds: ids,
271-
matchedCount: matched,
272-
deletedCount: deletes,
273-
modifiedCount: modified,
274-
upsertedCount: upserts,
275-
upsertedIds: upsert_ids
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
276268
}
277269
end
278270
end

lib/mongo/results.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,33 @@ defmodule Mongo.UpdateResult do
6262

6363
defstruct [acknowledged: true, matched_count: 0, modified_count: 0, upserted_ids: []]
6464
end
65+
66+
defmodule Mongo.BulkWriteResult do
67+
68+
@moduledoc """
69+
The successful result struct of `Mongo.BulkWrite.write`. Its fields are:
70+
71+
* `:acknowledged` - Write-concern
72+
* `:matched_count` - Number of matched documents
73+
* `:modified_count` - Number of modified documents
74+
* `:inserted_count` - Number of inserted documents
75+
* `:deleted_count` - Number of deleted documents
76+
* `:upserted_count` - Number of upserted documents
77+
* `:upserted_ids` - If the operation was an upsert, the upserted ids
78+
* `:inserted_ids` - If the operation was an insert, the inserted ids
79+
"""
80+
81+
@type t :: %__MODULE__{
82+
acknowledged: boolean,
83+
matched_count: non_neg_integer,
84+
modified_count: non_neg_integer,
85+
inserted_count: non_neg_integer,
86+
deleted_count: non_neg_integer,
87+
upserted_count: non_neg_integer,
88+
upserted_ids: list(BSON.ObjectId.t),
89+
inserted_ids: list(BSON.ObjectId.t)
90+
}
91+
92+
defstruct [acknowledged: true, matched_count: 0, modified_count: 0, inserted_count: 0, deleted_count: 0, upserted_count: 0, inserted_ids: [], upserted_ids: []]
93+
94+
end

test/mongo/bulk_writes_test.exs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Mongo.BulkWritesTest do
55
alias Mongo.OrderedBulk
66
alias Mongo.BulkWrite
77
alias Mongo.BulkOps
8+
alias Mongo.BulkWriteResult
89

910
setup_all do
1011
assert {:ok, pid} = Mongo.TestConnection.connect
@@ -26,9 +27,9 @@ defmodule Mongo.BulkWritesTest do
2627
|> UnorderedBulk.delete_one(%{kind: "dog"})
2728
|> UnorderedBulk.delete_one(%{kind: "dog"})
2829

29-
result = BulkWrite.write(top.pid, bulk, w: 1)
30+
%BulkWriteResult{} = result = BulkWrite.write(top.pid, bulk, w: 1)
3031

31-
assert %{:insertedCount => 3, :matchedCount => 3, :deletedCount => 3 } == Map.take(result, [:insertedCount, :matchedCount, :deletedCount])
32+
assert %{:inserted_count => 3, :matched_count => 3, :deleted_count => 3 } == Map.take(result, [:inserted_count, :matched_count, :deleted_count])
3233
assert {:ok, 0} == Mongo.count(top.pid, coll, %{})
3334

3435
end
@@ -49,9 +50,9 @@ defmodule Mongo.BulkWritesTest do
4950
|> OrderedBulk.delete_one(%{kind: "cat"})
5051
|> OrderedBulk.delete_one(%{kind: "cat"})
5152

52-
result = BulkWrite.write(top.pid, bulk, w: 1)
53+
%BulkWriteResult{} = result = BulkWrite.write(top.pid, bulk, w: 1)
5354

54-
assert %{:insertedCount => 3, :matchedCount => 6, :deletedCount => 3 } == Map.take(result, [:insertedCount, :matchedCount, :deletedCount])
55+
assert %{:inserted_count => 3, :matched_count => 6, :deleted_count => 3 } == Map.take(result, [:inserted_count, :matched_count, :deleted_count])
5556
assert {:ok, 0} == Mongo.count(top.pid, coll, %{})
5657

5758
end
@@ -85,9 +86,9 @@ defmodule Mongo.BulkWritesTest do
8586
|> UnorderedBulk.delete_one(%{kind: "dog"})
8687
|> UnorderedBulk.delete_one(%{kind: "dog"})
8788

88-
result = BulkWrite.write(top.pid, bulk, w: 1)
89+
%BulkWriteResult{} = result = BulkWrite.write(top.pid, bulk, w: 1)
8990

90-
assert %{:upsertedCount => 3, :matchedCount => 1, :deletedCount => 3} == Map.take(result, [:upsertedCount, :matchedCount, :deletedCount])
91+
assert %{:upserted_count => 3, :matched_count => 1, :deleted_count => 3} == Map.take(result, [:upserted_count, :matched_count, :deleted_count])
9192
assert {:ok, 0} == Mongo.count(top.pid, coll, %{})
9293

9394
end
@@ -106,9 +107,9 @@ defmodule Mongo.BulkWritesTest do
106107
|> OrderedBulk.delete_one(%{kind: "dog"})
107108
|> OrderedBulk.delete_one(%{kind: "dog"})
108109

109-
result = BulkWrite.write(top.pid, bulk, w: 1)
110+
%BulkWriteResult{} = result = BulkWrite.write(top.pid, bulk, w: 1)
110111

111-
assert %{:upsertedCount => 3, :matchedCount => 2, :deletedCount => 3, :modifiedCount => 1} == Map.take(result, [:upsertedCount, :matchedCount, :deletedCount, :modifiedCount])
112+
assert %{:upserted_count => 3, :matched_count => 2, :deleted_count => 3, :modified_count => 1} == Map.take(result, [:upserted_count, :matched_count, :deleted_count, :modified_count])
112113
assert {:ok, 0} == Mongo.count(top.pid, coll, %{})
113114

114115
end

0 commit comments

Comments
 (0)