|
| 1 | +defmodule Mongo.BulkWrite do |
| 2 | + @moduledoc """ |
| 3 | +
|
| 4 | + todo |
| 5 | +
|
| 6 | + Ist immer für eine Collections |
| 7 | +
|
| 8 | + The maxWriteBatchSize limit of a database, which indicates the maximum number of write operations permitted in a write batch, raises from 1,000 to 100,000. |
| 9 | +
|
| 10 | + """ |
| 11 | + |
| 12 | + import Mongo.Utils |
| 13 | + alias Mongo.UnorderedBulk |
| 14 | + |
| 15 | + @doc """ |
| 16 | + Unordered bulk write operations: |
| 17 | + Executes first insert commands, then all update commands and after that all delete commands are executed. If a group (inserts, updates or deletes) exceeds the limit |
| 18 | + maxWriteBatchSize it will be split into chunks. Everything is done in memory, so this use case is limited by memory. A better approach seems to use streaming bulk writes. |
| 19 | + """ |
| 20 | + def bulk_write(topology_pid, %UnorderedBulk{} = bulk, opts \\ []) do |
| 21 | + |
| 22 | + write_concern = %{ |
| 23 | + w: Keyword.get(opts, :w), |
| 24 | + j: Keyword.get(opts, :j), |
| 25 | + wtimeout: Keyword.get(opts, :wtimeout) |
| 26 | + } |> filter_nils() |
| 27 | + |
| 28 | + with {:ok, conn, _, _} <- Mongo.select_server(topology_pid, :write, opts), |
| 29 | + inserts <- conn |> run_commands(get_insert_cmds(bulk, write_concern), opts) |> collect(:inserts), |
| 30 | + updates <- conn |> run_commands(get_update_cmds(bulk, write_concern, opts), opts) |> collect(:updates), |
| 31 | + deletes <- conn |> run_commands(get_delete_cmds(bulk, write_concern, opts), opts) |> collect(:deletes) do |
| 32 | + inserts ++ updates ++ deletes |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def collect(doc, :inserts) do |
| 37 | + |
| 38 | + end |
| 39 | + |
| 40 | + def collect(doc, :updates) do |
| 41 | + |
| 42 | + end |
| 43 | + |
| 44 | + def collect(doc, :deletes) do |
| 45 | + |
| 46 | + end |
| 47 | + |
| 48 | + defp run_commands(conn, cmds, opts) do |
| 49 | + |
| 50 | + IO.puts "Running cmsd #{inspect cmds}" |
| 51 | + |
| 52 | + cmds |
| 53 | + |> Enum.map(fn cmd -> Mongo.direct_command(conn, cmd, opts) end) |
| 54 | + |> Enum.map(fn {:ok, doc} -> {:ok, doc} end) |
| 55 | + end |
| 56 | + |
| 57 | + def get_insert_cmds(%UnorderedBulk{coll: coll, inserts: all_inserts}, write_concern) do |
| 58 | + |
| 59 | + max_batch_size = 10 ## only for test maxWriteBatchSize |
| 60 | + |
| 61 | + {_ids, all_inserts} = assign_ids(all_inserts) |
| 62 | + |
| 63 | + all_inserts |
| 64 | + |> Enum.chunk_every(max_batch_size) |
| 65 | + |> Enum.map(fn inserts -> get_insert_cmd(coll, inserts, write_concern) end) |
| 66 | + |
| 67 | + end |
| 68 | + |
| 69 | + defp get_insert_cmd(coll, inserts, write_concern) do |
| 70 | + filter_nils([insert: coll, documents: inserts, writeConcern: write_concern]) |
| 71 | + end |
| 72 | + |
| 73 | + defp get_delete_cmds(%UnorderedBulk{coll: coll, deletes: all_deletes}, write_concern, opts) do |
| 74 | + |
| 75 | + max_batch_size = 10 ## only for test maxWriteBatchSize |
| 76 | + all_deletes |
| 77 | + |> Enum.chunk_every(max_batch_size) |
| 78 | + |> Enum.map(fn deletes -> get_delete_cmd(coll, deletes, write_concern, opts) end) |
| 79 | + |
| 80 | + end |
| 81 | + |
| 82 | + defp get_delete_cmd(coll, deletes, write_concern, opts ) do |
| 83 | + filter_nils([delete: coll, |
| 84 | + deletes: Enum.map(deletes, fn delete -> get_delete_doc(delete) end), |
| 85 | + ordered: Keyword.get(opts, :ordered), |
| 86 | + writeConcern: write_concern]) |
| 87 | + end |
| 88 | + defp get_delete_doc({filter, collaction, limit}) do |
| 89 | + %{q: filter, limit: limit, collation: collaction} |> filter_nils() |
| 90 | + end |
| 91 | + |
| 92 | + defp get_update_cmds(%UnorderedBulk{coll: coll, updates: all_updates}, write_concern, opts) do |
| 93 | + |
| 94 | + max_batch_size = 10 ## only for test maxWriteBatchSize |
| 95 | + all_updates |
| 96 | + |> Enum.chunk_every(max_batch_size) |
| 97 | + |> Enum.map(fn updates -> get_update_cmd(coll, updates, write_concern, opts) end) |
| 98 | + |
| 99 | + end |
| 100 | + |
| 101 | + defp get_update_cmd(coll, updates, write_concern, opts) do |
| 102 | + [ update: coll, |
| 103 | + updates: Enum.map(updates, fn update -> get_update_doc(update) end), |
| 104 | + ordered: Keyword.get(opts, :ordered), |
| 105 | + writeConcern: write_concern, |
| 106 | + bypassDocumentValidation: Keyword.get(opts, :bypass_document_validation) |
| 107 | + ] |> filter_nils() |
| 108 | + end |
| 109 | + |
| 110 | + defp get_update_doc({filter, update, update_opts}) do |
| 111 | + [ q: filter, |
| 112 | + u: update, |
| 113 | + upsert: Keyword.get(update_opts, :upsert), |
| 114 | + multi: Keyword.get(update_opts, :multi) || false, |
| 115 | + collation: Keyword.get(update_opts, :collation), |
| 116 | + arrayFilters: Keyword.get(update_opts, :filters) |
| 117 | + ] |> filter_nils() |
| 118 | + end |
| 119 | + defp get_update_doc(_other) do |
| 120 | + [] |
| 121 | + end |
| 122 | + |
| 123 | +end |
0 commit comments