Skip to content

Commit 864bf70

Browse files
committed
Fixed #96
1 parent fb2edcb commit 864bf70

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ dist: bionic
22
sudo: required
33
language: elixir
44

5-
elixir: 1.12.1
6-
otp_release: 24.0
5+
elixir: 1.10.2
6+
otp_release: 22.0
77

88
addons:
99
apt:

lib/mongo/ordered_bulk.ex

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ defmodule Mongo.OrderedBulk do
7676
%OrderedBulk{coll: coll}
7777
end
7878

79+
@doc """
80+
Returns true, if the bulk is empty, that means it contains no inserts, updates or deletes operations
81+
"""
82+
def empty?(%OrderedBulk{ops: []}) do
83+
true
84+
end
85+
def empty?(_other) do
86+
false
87+
end
88+
7989
@doc """
8090
Appends a bulk operation to the ordered bulk.
8191
"""
@@ -207,8 +217,14 @@ defmodule Mongo.OrderedBulk do
207217
op, {bulk, l} -> {:cont, {push(op, bulk), l - 1}}
208218
end,
209219
fn
210-
{bulk, 0} -> {:cont, bulk}
211-
{bulk, _} -> {:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
220+
{bulk, _} ->
221+
case empty?(bulk) do
222+
true ->
223+
{:cont, bulk}
224+
225+
false ->
226+
{:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
227+
end
212228
end)
213229
end
214230
def write(_enum, _top, _coll, limit, _opts) when limit < 1 do

lib/mongo/unordered_bulk.ex

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ defmodule Mongo.UnorderedBulk do
7777
%UnorderedBulk{coll: coll}
7878
end
7979

80+
@doc """
81+
Returns true, if the bulk is empty, that means it contains no inserts, updates or deletes operations
82+
"""
83+
def empty?(%UnorderedBulk{inserts: [], updates: [], deletes: []}) do
84+
true
85+
end
86+
def empty?(_other) do
87+
false
88+
end
89+
8090
@doc """
8191
Adds the two unordered bulks together.
8292
"""
@@ -243,8 +253,14 @@ defmodule Mongo.UnorderedBulk do
243253
op, {bulk, l} -> {:cont, {push(op, bulk), l - 1}}
244254
end,
245255
fn
246-
{bulk, 0} -> {:cont, bulk}
247-
{bulk, _} -> {:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
256+
{bulk, _} ->
257+
case empty?(bulk) do
258+
true ->
259+
{:cont, bulk}
260+
261+
false ->
262+
{:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
263+
end
248264
end)
249265
end
250266
def write(_enum, _top, _coll, limit, _opts) when limit < 1 do

test/mongo/bulk_writes_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ defmodule Mongo.BulkWritesTest do
3030

3131
end
3232

33+
test "check unordered bulk with limit", top do
34+
coll = unique_collection()
35+
36+
[batch_1, batch_2] = 1..49
37+
|> Stream.map(fn i -> Mongo.BulkOps.get_insert_one(%{number: i}) end)
38+
|> Mongo.UnorderedBulk.write(top.pid, coll, 25)
39+
|> Enum.map(& &1)
40+
41+
assert %{:inserted_count => 25} == Map.take(batch_1, [:inserted_count])
42+
assert %{:inserted_count => 24} == Map.take(batch_2, [:inserted_count])
43+
end
44+
45+
test "check ordered bulk with limit", top do
46+
coll = unique_collection()
47+
48+
[batch_1, batch_2] = 1..49
49+
|> Stream.map(fn i -> Mongo.BulkOps.get_insert_one(%{number: i}) end)
50+
|> Mongo.OrderedBulk.write(top.pid, coll, 25)
51+
|> Enum.map(& &1)
52+
53+
assert %{:inserted_count => 25} == Map.take(batch_1, [:inserted_count])
54+
assert %{:inserted_count => 24} == Map.take(batch_2, [:inserted_count])
55+
end
56+
57+
3358
test "check ordered bulk", top do
3459
coll = unique_collection()
3560

0 commit comments

Comments
 (0)