|
| 1 | +defmodule Ecto.Adapters.SQL.Stage do |
| 2 | + @moduledoc """ |
| 3 | + A `GenStage` process that encapsulates a SQL transaction. |
| 4 | + """ |
| 5 | + |
| 6 | + @doc """ |
| 7 | + Start link a `GenStage` process that will run a transaction for its duration. |
| 8 | +
|
| 9 | + The first argument is the pool, the second argument is the `GenStage` type, |
| 10 | + the third argument is the start function, the fourth argument is the handle |
| 11 | + function, the fifth argument is the stop function and the optional sixth |
| 12 | + argument are the options. |
| 13 | +
|
| 14 | + The start function is a o-arity anonymous function. This is called after the |
| 15 | + transaction begins but before `start_link/6` returns. It should return the |
| 16 | + `state` or call `MyRepo.rollback/1` to stop the `GenStage`. |
| 17 | +
|
| 18 | + The handle function is a 2-arity anonymous function. If the `GenStage` type is |
| 19 | + a `:producer`, then the first argument is the `demand` from a `GenStage` |
| 20 | + `handle_demand` callback. Otherwise the first argument is the events from a |
| 21 | + `GenStage` `handle_events` callback. The second argument is the state. This |
| 22 | + function returns a 2-tuple, with first element as events (empty list for |
| 23 | + `:consumer`) and second element as the `state`. This function can roll back |
| 24 | + and stop the `GenStage` using `MyRepo.rollback/1`. |
| 25 | +
|
| 26 | + The stop function is a 2-arity anonymous function. The first argument is the |
| 27 | + terminate reason and the second argument is the `state`. This function will |
| 28 | + only be called if connection is alive and the transaction has not been rolled |
| 29 | + back. If this function returns the transaction is committed. This function can |
| 30 | + roll back and stop the `GenStage` using `MyRepo.rollback/1`. |
| 31 | +
|
| 32 | + The `GenStage` process will behave like a `Flow` stage: |
| 33 | +
|
| 34 | + * It will stop with reason `:normal` when the last consumer cancels |
| 35 | + * It will notify consumers that it is done when all producers have cancelled |
| 36 | + or notified that they are done or halted |
| 37 | + * It will cancel new and remaining producers when all producers have |
| 38 | + notified that they are done or halted and it is a `:consumer` |
| 39 | + * It will not send demand to new producers when all producers have notified |
| 40 | + that they are done or halted and it is a `:consumer_producer` |
| 41 | +
|
| 42 | + ### Options |
| 43 | +
|
| 44 | + * `:name` - A name to register the started process (see the `:name` option |
| 45 | + in `GenServer.start_link/3`) |
| 46 | +
|
| 47 | + See the "Shared options" section at the `Ecto.Repo` documentation. All options |
| 48 | + are passed to the `GenStage` on init. |
| 49 | +
|
| 50 | + ### Example |
| 51 | +
|
| 52 | + start = fn() -> Post end |
| 53 | + handle = |
| 54 | + fn(entries, schema) -> |
| 55 | + MyRepo.insert_all(schema, entries) |
| 56 | + {[], schema} |
| 57 | + end |
| 58 | + stop = |
| 59 | + fn |
| 60 | + :normal, _ -> :ok |
| 61 | + reason, _ -> MyRepo.rollback(reason) |
| 62 | + end |
| 63 | + Ecto.Adapters.SQL.Stage.start_link(MyRepo, :consumer, start, handle, stop) |
| 64 | + """ |
| 65 | + @spec start_link(repo :: module, :producer, |
| 66 | + start :: (() -> state), |
| 67 | + handle_demand :: ((demand :: pos_integer, state) -> {[any], state}), |
| 68 | + stop :: ((reason :: any, state) -> any), opts :: Keyword.t) :: |
| 69 | + GenServer.on_start when state: var |
| 70 | + @spec start_link(repo :: module, :producer_consumer, |
| 71 | + start :: (() -> state), |
| 72 | + handle_events :: (([any], state) -> {[any], state}), |
| 73 | + stop :: ((reason :: any, state) -> any), opts :: Keyword.t) :: |
| 74 | + GenServer.on_start when state: var |
| 75 | + @spec start_link(repo :: module, :consumer, |
| 76 | + start :: (() -> state), |
| 77 | + handle_events :: (([any], state) -> {[], state}), |
| 78 | + stop :: ((reason :: any, state) -> any), opts :: Keyword.t) :: |
| 79 | + GenServer.on_start when state: var |
| 80 | + def start_link(repo, type, start, handle, stop, opts \\ []) do |
| 81 | + Ecto.Adapters.SQL.stage(repo, type, start, handle, stop, opts) |
| 82 | + end |
| 83 | + |
| 84 | + @doc """ |
| 85 | + Starts a `GenStage` producer that emits all entries from the data store |
| 86 | + matching the given query. SQL adapters, such as Postgres and MySQL, will use |
| 87 | + a separate transaction to enumerate the stream. |
| 88 | +
|
| 89 | + May raise `Ecto.QueryError` if query validation fails. |
| 90 | +
|
| 91 | + ## Options |
| 92 | +
|
| 93 | + * `:prefix` - The prefix to run the query on (such as the schema path |
| 94 | + in Postgres or the database in MySQL). This overrides the prefix set |
| 95 | + in the query |
| 96 | +
|
| 97 | + * `:max_rows` - The number of rows to load from the database as we stream. |
| 98 | + It is supported at least by Postgres and MySQL and defaults to 500. |
| 99 | +
|
| 100 | + See the "Shared options" section at the `Ecto.Repo` documentation. |
| 101 | +
|
| 102 | + ## Example |
| 103 | +
|
| 104 | + # Print all post titles |
| 105 | + query = from p in Post, |
| 106 | + select: p.title |
| 107 | + {:ok, stage} = Ecto.Adapters.SQL.stream(MyRepo, query) |
| 108 | + stage |
| 109 | + |> Flow.from_stage() |
| 110 | + |> Flow.each(&IO.inspect/1) |
| 111 | + |> Flow.start_link() |
| 112 | + """ |
| 113 | + |
| 114 | + @callback stream(repo :: module, queryable :: Ecto.Query.t, opts :: Keyword.t) :: |
| 115 | + GenServer.on_start() |
| 116 | + def stream(repo, queryable, opts \\ []) do |
| 117 | + stream = apply(repo, :stream, [queryable, opts]) |
| 118 | + start = |
| 119 | + fn() -> |
| 120 | + acc = {:suspend, {0, []}} |
| 121 | + {:suspended, _, cont} = Enumerable.reduce(stream, acc, &stream_reduce/2) |
| 122 | + {repo, :cont, cont} |
| 123 | + end |
| 124 | + start_link(repo, :producer, start, &stream_handle/2, &stream_stop/2, opts) |
| 125 | + end |
| 126 | + |
| 127 | + ## Helpers |
| 128 | + |
| 129 | + defp stream_reduce(v, {1, acc}) do |
| 130 | + {:suspend, {0, [v | acc]}} |
| 131 | + end |
| 132 | + defp stream_reduce(v, {n, acc}) do |
| 133 | + {:cont, {n-1, [v | acc]}} |
| 134 | + end |
| 135 | + |
| 136 | + defp stream_handle(n, {repo, :cont, cont}) when n > 0 do |
| 137 | + case cont.({:cont, {n, []}}) do |
| 138 | + {:suspended, {0, acc}, cont} -> |
| 139 | + {Enum.reverse(acc), {repo, :cont, cont}} |
| 140 | + {status, {_, acc}} when status in [:halted, :done] -> |
| 141 | + GenStage.async_notify(self(), {:producer, status}) |
| 142 | + {Enum.reverse(acc), {repo, status}} |
| 143 | + end |
| 144 | + end |
| 145 | + defp stream_handle(_, {_repo, status} = state) do |
| 146 | + GenStage.async_notify(self(), {:producer, status}) |
| 147 | + {[], state} |
| 148 | + end |
| 149 | + |
| 150 | + defp stream_stop(reason, {repo, :cont, cont}) do |
| 151 | + _ = cont.({:halt, {0, []}}) |
| 152 | + stream_stop(repo, reason) |
| 153 | + end |
| 154 | + defp stream_stop(reason, {repo, status}) when status in [:halted, :done] do |
| 155 | + stream_stop(repo, reason) |
| 156 | + end |
| 157 | + |
| 158 | + defp stream_stop(_, :normal) do |
| 159 | + :ok |
| 160 | + end |
| 161 | + defp stream_stop(repo, reason) do |
| 162 | + apply(repo, :rollback, [reason]) |
| 163 | + end |
| 164 | +end |
0 commit comments