Skip to content

Commit 8e60fc7

Browse files
committed
fixed unit tests
1 parent c075fa7 commit 8e60fc7

File tree

11 files changed

+170
-129
lines changed

11 files changed

+170
-129
lines changed

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Mix.Config
99
# party users, it should be done in your mix.exs file.
1010

1111
config :logger, :console,
12-
level: :debug,
12+
level: :info,
1313
truncate: 1024,
1414
format: "$time [$level] $message ($metadata)\n\n",
1515
metadata: [:module, :function, :line]

jetbrains.svg

Lines changed: 66 additions & 0 deletions
Loading

lib/mongo.ex

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ defmodule Mongo do
4747
"""
4848

4949
import Keywords
50-
import WriteConcern
50+
import Mongo.WriteConcern
5151

5252
require Logger
5353

@@ -311,9 +311,9 @@ defmodule Mongo do
311311
Logger.debug("issue_command: #{inspect type} #{inspect new_cmd}")
312312

313313
with {:ok, session} <- Session.start_implicit_session(topology_pid, type, opts),
314-
{:ok, doc} <- exec_command_session(session, new_cmd, opts),
314+
result <- exec_command_session(session, new_cmd, opts),
315315
:ok <- Session.end_implict_session(topology_pid, session) do
316-
{:ok, doc}
316+
result
317317
else
318318
{:new_connection, _server} -> issue_command(topology_pid, cmd, type, opts)
319319
end
@@ -402,7 +402,7 @@ defmodule Mongo do
402402

403403
opts = Keyword.drop(opts, ~w(limit skip hint collation)a)
404404

405-
with {:ok, doc} <- command(topology_pid, cmd, opts),
405+
with {:ok, doc} <- issue_command(topology_pid, cmd, :read, opts),
406406
do: {:ok, trunc(doc["n"])}
407407
end
408408

@@ -579,9 +579,7 @@ defmodule Mongo do
579579
"""
580580
@spec command(GenServer.server, BSON.document, Keyword.t) :: result(BSON.document)
581581
def command(topology_pid, cmd, opts \\ []) do
582-
rp = ReadPreference.defaults(%{mode: :primary})
583-
rp_opts = [read_preference: Keyword.get(opts, :read_preference, rp)] # todo andere optionen gehen verloren
584-
with {:ok, doc} <- issue_command(topology_pid, cmd, :read, rp_opts) do
582+
with {:ok, doc} <- issue_command(topology_pid, cmd, :write, opts) do
585583
{:ok, doc}
586584
end
587585
end
@@ -643,15 +641,15 @@ defmodule Mongo do
643641
"""
644642
@spec command!(GenServer.server, BSON.document, Keyword.t) :: result!(BSON.document)
645643
def command!(topology_pid, cmd, opts \\ []) do
646-
bangify(command(topology_pid, cmd, opts))
644+
bangify(issue_command(topology_pid, cmd, :write, opts))
647645
end
648646

649647
@doc """
650648
Sends a ping command to the server.
651649
"""
652650
@spec ping(GenServer.server) :: result(BSON.document)
653651
def ping(topology_pid) do
654-
command(topology_pid, [ping: 1], [batch_size: 1])
652+
issue_command(topology_pid, [ping: 1], :read, [batch_size: 1])
655653
end
656654

657655
@doc """
@@ -681,17 +679,17 @@ defmodule Mongo do
681679
insert: coll,
682680
documents: [doc],
683681
ordered: Keyword.get(opts, :ordered),
684-
writeConcern: write_concern, ## todo in der Transaction löschen
682+
writeConcern: write_concern,
685683
bypassDocumentValidation: Keyword.get(opts, :bypass_document_validation)
686684
] |> filter_nils()
687685

688-
with {:ok, doc} <- Mongo.issue_command(topology_pid, cmd, :write, opts) do
686+
with {:ok, doc} <- issue_command(topology_pid, cmd, :write, opts) do
689687
case doc do
690688
%{"writeErrors" => _} -> {:error, %Mongo.WriteError{n: doc["n"], ok: doc["ok"], write_errors: doc["writeErrors"]}}
691689
_ ->
692690
case acknowledged?(write_concern) do
693-
true -> {:ok, %Mongo.InsertOneResult{acknowledged: false}}
694-
false -> {:ok, %Mongo.InsertOneResult{inserted_id: id}}
691+
false -> {:ok, %Mongo.InsertOneResult{acknowledged: false}}
692+
true -> {:ok, %Mongo.InsertOneResult{inserted_id: id}}
695693
end
696694
end
697695
end

lib/mongo/bulk_write.ex

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ defmodule Mongo.BulkWrite do
155155
"""
156156

157157
import Mongo.Utils
158+
import Mongo.WriteConcern
158159
alias Mongo.UnorderedBulk
159160
alias Mongo.OrderedBulk
160161
alias Mongo.BulkWriteResult
161162
alias Mongo.Session.ServerSession
163+
alias Mongo.Session
162164
alias Mongo.Topology
163165

164166
@doc """
@@ -182,15 +184,12 @@ defmodule Mongo.BulkWrite do
182184
@spec write(GenServer.server, (UnorderedBulk.t | OrderedBulk.t), Keyword.t) :: Mongo.BulkWriteResult.t
183185
def write(topology_pid, %UnorderedBulk{} = bulk, opts) do
184186

185-
write_concern = write_concern(opts)
186-
187-
with {:ok, conn, _, _, session_id} <- Topology.select_server(topology_pid, :write, opts),
188-
result = one_bulk_write(conn, bulk, write_concern, Keyword.merge(opts, [lsid: session_id])),
189-
:ok <- checkin_session_id(topology_pid, Keyword.get(opts, :lsid, :implicit), session_id) do
187+
with {:ok, session} <- Session.start_implicit_session(topology_pid, :write, opts),
188+
result = one_bulk_write(session, bulk, opts),
189+
:ok <- Session.end_implict_session(topology_pid, session) do
190190
result
191191
else
192-
{:new_connection, _server} ->
193-
write(topology_pid, bulk, opts)
192+
{:new_connection, _server} -> write(topology_pid, bulk, opts)
194193
end
195194

196195
end
@@ -199,36 +198,23 @@ defmodule Mongo.BulkWrite do
199198

200199
write_concern = write_concern(opts)
201200

202-
empty = %BulkWriteResult{acknowledged: acknowledged(write_concern)}
201+
empty = %BulkWriteResult{acknowledged: acknowledged?(write_concern)}
203202

204-
with {:ok, conn, _, _, session_id} <- Topology.select_server(topology_pid, :write, opts),
205-
{:ok, limits} <- Mongo.limits(conn),
203+
with {:ok, session} <- Session.start_implicit_session(topology_pid, :write, opts),
204+
{:ok, limits} <- get_limits(session),
206205
max_batch_size <- limits.max_write_batch_size,
207206
result = ops
208207
|> get_op_sequence()
209-
|> Enum.map(fn {cmd, docs} -> one_bulk_write_operation(conn, cmd, coll, docs, write_concern, max_batch_size, Keyword.merge(opts, [lsid: session_id])) end)
210-
|> BulkWriteResult.reduce(empty),
211-
:ok <- checkin_session_id(topology_pid, Keyword.get(opts, :lsid, :implicit), session_id) do
208+
|> Enum.map(fn {cmd, docs} -> one_bulk_write_operation(session, cmd, coll, docs, max_batch_size, opts) end)
209+
|> BulkWriteResult.reduce(empty) do
212210

213211
result
214212
else
215-
{:new_connection, _server} ->
216-
write(topology_pid, bulk, opts)
213+
{:new_connection, _server} -> write(topology_pid, bulk, opts)
217214
end
218215

219216
end
220217

221-
##
222-
# returns the current write concerns from `opts`
223-
#
224-
defp write_concern(opts) do
225-
%{
226-
w: Keyword.get(opts, :w),
227-
j: Keyword.get(opts, :j),
228-
wtimeout: Keyword.get(opts, :wtimeout)
229-
} |> filter_nils()
230-
end
231-
232218
##
233219
# Executes one unordered bulk write. The execution order of operation groups is
234220
#
@@ -239,47 +225,34 @@ defmodule Mongo.BulkWrite do
239225
# The function returns a keyword list with the results of each operation group:
240226
# For the details see https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#results
241227
#
242-
defp one_bulk_write(conn, %UnorderedBulk{coll: coll, inserts: inserts, updates: updates, deletes: deletes}, write_concern, opts) do
228+
defp one_bulk_write(session, %UnorderedBulk{coll: coll, inserts: inserts, updates: updates, deletes: deletes}, opts) do
243229

244-
with {:ok, limits} <- Mongo.limits(conn),
230+
with {:ok, limits} <- get_limits(session),
245231
max_batch_size <- limits.max_write_batch_size,
246-
insert_result <- one_bulk_write_operation(conn, :insert, coll, inserts, write_concern, max_batch_size, opts),
247-
update_result <- one_bulk_write_operation(conn, :update, coll, updates, write_concern, max_batch_size, opts),
248-
delete_result <- one_bulk_write_operation(conn, :delete, coll, deletes, write_concern, max_batch_size, opts) do
232+
insert_result <- one_bulk_write_operation(session, :insert, coll, inserts, max_batch_size, opts),
233+
update_result <- one_bulk_write_operation(session, :update, coll, updates, max_batch_size, opts),
234+
delete_result <- one_bulk_write_operation(session, :delete, coll, deletes, max_batch_size, opts) do
249235

250236
[insert_result, update_result, delete_result]
251-
|> BulkWriteResult.reduce(%BulkWriteResult{acknowledged: acknowledged(write_concern)})
237+
|> BulkWriteResult.reduce(%BulkWriteResult{acknowledged: acknowledged?(opts)})
252238
end
253239
end
254240

255-
defp update_session_id(cmd, nil) do
256-
cmd
257-
end
258-
defp update_session_id(cmd, %ServerSession{:session_id => session_id}) do
259-
Keyword.merge(cmd, [lsid: %{id: session_id}])
260-
end
261-
262-
defp checkin_session_id(topology_pid, :implicit, session_id), do: Topology.checkin_session_id(topology_pid, session_id)
263-
defp checkin_session_id(_, _, _), do: :ok
264-
265241
###
266242
# Executes the command `cmd` and collects the result.
267243
#
268-
defp one_bulk_write_operation(conn, cmd, coll, docs, write_concern, max_batch_size, opts) do
269-
with result <- conn |> run_commands(get_cmds(cmd, coll, docs, write_concern, max_batch_size, opts), opts) |> collect(cmd) do
244+
defp one_bulk_write_operation(session, cmd, coll, docs, max_batch_size, opts) do
245+
with result <- session |> run_commands(get_cmds(cmd, coll, docs, max_batch_size, opts), opts) |> collect(cmd) do
270246
result
271247
end
272248
end
273249

274250
##
275251
# Converts the list of operations into insert/update/delete commands
276252
#
277-
defp get_cmds(:insert, coll, docs, write_concern, max_batch_size, opts), do: get_insert_cmds(coll, docs, write_concern, max_batch_size, opts)
278-
defp get_cmds(:update, coll, docs, write_concern, max_batch_size, opts), do: get_update_cmds(coll, docs, write_concern, max_batch_size, opts)
279-
defp get_cmds(:delete, coll, docs, write_concern, max_batch_size, opts), do: get_delete_cmds(coll, docs, write_concern, max_batch_size, opts)
280-
281-
defp acknowledged(%{w: w}) when w > 0, do: true
282-
defp acknowledged(%{}), do: false
253+
defp get_cmds(:insert, coll, docs, max_batch_size, opts), do: get_insert_cmds(coll, docs, max_batch_size, opts)
254+
defp get_cmds(:update, coll, docs, max_batch_size, opts), do: get_update_cmds(coll, docs, max_batch_size, opts)
255+
defp get_cmds(:delete, coll, docs, max_batch_size, opts), do: get_delete_cmds(coll, docs, max_batch_size, opts)
283256

284257
###
285258
# Converts the list of operations into list of lists with same operations.
@@ -357,48 +330,47 @@ defmodule Mongo.BulkWrite do
357330
defp filter_upsert_ids(nil), do: []
358331
defp filter_upsert_ids(upserted), do: Enum.map(upserted, fn doc -> doc["_id"] end)
359332

360-
defp run_commands(conn, {cmds, ids}, opts) do
361-
{Enum.map(cmds, fn cmd -> Mongo.exec_command(conn, cmd, opts) end), ids}
333+
defp run_commands(session, {cmds, ids}, opts) do
334+
{Enum.map(cmds, fn cmd -> Mongo.exec_command_session(session, cmd, opts) end), ids}
362335
end
363-
defp run_commands(conn, cmds, opts) do
364-
Enum.map(cmds, fn cmd -> Mongo.exec_command(conn, cmd, opts) end)
336+
defp run_commands(session, cmds, opts) do
337+
Enum.map(cmds, fn cmd -> Mongo.exec_command_session(session, cmd, opts) end)
365338
end
366339

367-
defp get_insert_cmds(coll, docs, write_concern, max_batch_size, opts) do
340+
defp get_insert_cmds(coll, docs, max_batch_size, opts) do
368341

369342
{ids, docs} = assign_ids(docs)
370343

371344
cmds = docs
372345
|> Enum.chunk_every(max_batch_size)
373-
|> Enum.map(fn inserts -> get_insert_cmd(coll, inserts, write_concern) end)
374-
|> Enum.map(fn cmd -> update_session_id(cmd, Keyword.get(opts, :lsid)) end)
346+
|> Enum.map(fn inserts -> get_insert_cmd(coll, inserts, opts) end)
347+
375348
{cmds, ids}
376349

377350
end
378351

379-
defp get_insert_cmd(coll, inserts, write_concern) do
352+
defp get_insert_cmd(coll, inserts, opts) do
380353

381354
[insert: coll,
382355
documents: inserts,
383-
writeConcern: write_concern] |> filter_nils()
356+
writeConcern: write_concern(opts)] |> filter_nils()
384357

385358
end
386359

387-
defp get_delete_cmds(coll, docs, write_concern, max_batch_size, opts) do
360+
defp get_delete_cmds(coll, docs, max_batch_size, opts) do
388361

389362
docs
390363
|> Enum.chunk_every(max_batch_size)
391-
|> Enum.map(fn deletes -> get_delete_cmd(coll, deletes, write_concern, opts) end)
392-
|> Enum.map(fn cmd -> update_session_id(cmd, Keyword.get(opts, :lsid)) end)
364+
|> Enum.map(fn deletes -> get_delete_cmd(coll, deletes, opts) end)
393365

394366
end
395367

396-
defp get_delete_cmd(coll, deletes, write_concern, opts ) do
368+
defp get_delete_cmd(coll, deletes, opts ) do
397369

398370
[delete: coll,
399371
deletes: Enum.map(deletes, fn delete -> get_delete_doc(delete) end),
400372
ordered: Keyword.get(opts, :ordered),
401-
writeConcern: write_concern] |> filter_nils()
373+
writeConcern: write_concern(opts)] |> filter_nils()
402374

403375
end
404376

@@ -410,21 +382,20 @@ defmodule Mongo.BulkWrite do
410382

411383
end
412384

413-
defp get_update_cmds(coll, docs, write_concern, max_batch_size, opts) do
385+
defp get_update_cmds(coll, docs, max_batch_size, opts) do
414386

415387
docs
416388
|> Enum.chunk_every(max_batch_size)
417-
|> Enum.map(fn updates -> get_update_cmd(coll, updates, write_concern, opts) end)
418-
|> Enum.map(fn cmd -> update_session_id(cmd, Keyword.get(opts, :lsid)) end)
389+
|> Enum.map(fn updates -> get_update_cmd(coll, updates, opts) end)
419390

420391
end
421392

422-
defp get_update_cmd(coll, updates, write_concern, opts) do
393+
defp get_update_cmd(coll, updates, opts) do
423394

424395
[ update: coll,
425396
updates: Enum.map(updates, fn update -> get_update_doc(update) end),
426397
ordered: Keyword.get(opts, :ordered),
427-
writeConcern: write_concern,
398+
writeConcern: write_concern(opts),
428399
bypassDocumentValidation: Keyword.get(opts, :bypass_document_validation)
429400
] |> filter_nils()
430401

@@ -442,4 +413,10 @@ defmodule Mongo.BulkWrite do
442413

443414
end
444415

416+
defp get_limits(session) do
417+
with conn <- Session.connection(session) do
418+
Mongo.limits(conn)
419+
end
420+
end
421+
445422
end

0 commit comments

Comments
 (0)