Skip to content

Commit

Permalink
Fixed #96
Browse files Browse the repository at this point in the history
  • Loading branch information
zookzook committed Jun 6, 2021
1 parent fb2edcb commit 864bf70
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ dist: bionic
sudo: required
language: elixir

elixir: 1.12.1
otp_release: 24.0
elixir: 1.10.2
otp_release: 22.0

addons:
apt:
Expand Down
20 changes: 18 additions & 2 deletions lib/mongo/ordered_bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ defmodule Mongo.OrderedBulk do
%OrderedBulk{coll: coll}
end

@doc """
Returns true, if the bulk is empty, that means it contains no inserts, updates or deletes operations
"""
def empty?(%OrderedBulk{ops: []}) do
true
end
def empty?(_other) do
false
end

@doc """
Appends a bulk operation to the ordered bulk.
"""
Expand Down Expand Up @@ -207,8 +217,14 @@ defmodule Mongo.OrderedBulk do
op, {bulk, l} -> {:cont, {push(op, bulk), l - 1}}
end,
fn
{bulk, 0} -> {:cont, bulk}
{bulk, _} -> {:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
{bulk, _} ->
case empty?(bulk) do
true ->
{:cont, bulk}

false ->
{:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
end
end)
end
def write(_enum, _top, _coll, limit, _opts) when limit < 1 do
Expand Down
20 changes: 18 additions & 2 deletions lib/mongo/unordered_bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ defmodule Mongo.UnorderedBulk do
%UnorderedBulk{coll: coll}
end

@doc """
Returns true, if the bulk is empty, that means it contains no inserts, updates or deletes operations
"""
def empty?(%UnorderedBulk{inserts: [], updates: [], deletes: []}) do
true
end
def empty?(_other) do
false
end

@doc """
Adds the two unordered bulks together.
"""
Expand Down Expand Up @@ -243,8 +253,14 @@ defmodule Mongo.UnorderedBulk do
op, {bulk, l} -> {:cont, {push(op, bulk), l - 1}}
end,
fn
{bulk, 0} -> {:cont, bulk}
{bulk, _} -> {:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
{bulk, _} ->
case empty?(bulk) do
true ->
{:cont, bulk}

false ->
{:cont, BulkWrite.write(top, bulk, opts), {new(coll), limit - 1}}
end
end)
end
def write(_enum, _top, _coll, limit, _opts) when limit < 1 do
Expand Down
25 changes: 25 additions & 0 deletions test/mongo/bulk_writes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ defmodule Mongo.BulkWritesTest do

end

test "check unordered bulk with limit", top do
coll = unique_collection()

[batch_1, batch_2] = 1..49
|> Stream.map(fn i -> Mongo.BulkOps.get_insert_one(%{number: i}) end)
|> Mongo.UnorderedBulk.write(top.pid, coll, 25)
|> Enum.map(& &1)

assert %{:inserted_count => 25} == Map.take(batch_1, [:inserted_count])
assert %{:inserted_count => 24} == Map.take(batch_2, [:inserted_count])
end

test "check ordered bulk with limit", top do
coll = unique_collection()

[batch_1, batch_2] = 1..49
|> Stream.map(fn i -> Mongo.BulkOps.get_insert_one(%{number: i}) end)
|> Mongo.OrderedBulk.write(top.pid, coll, 25)
|> Enum.map(& &1)

assert %{:inserted_count => 25} == Map.take(batch_1, [:inserted_count])
assert %{:inserted_count => 24} == Map.take(batch_2, [:inserted_count])
end


test "check ordered bulk", top do
coll = unique_collection()

Expand Down

0 comments on commit 864bf70

Please sign in to comment.