Skip to content

Commit

Permalink
Use IO lists to build insert events SQL statement
Browse files Browse the repository at this point in the history
Fixes #23.
  • Loading branch information
slashdotdash committed Oct 28, 2016
2 parents 746549a + 7bc0c2e commit 7d5e75e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
1 change: 0 additions & 1 deletion bench/storage/append_events_bench.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule AppendEventsBench do
alias EventStore.Storage

setup_all do
Code.require_file("event_factory.ex", "test")
Application.ensure_all_started(:eventstore)
end

Expand Down
1 change: 0 additions & 1 deletion bench/storage/read_events_bench.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule ReadEventsBench do
alias EventStore.Storage

setup_all do
Code.require_file("event_factory.ex", "test")
Application.ensure_all_started(:eventstore)
end

Expand Down
4 changes: 3 additions & 1 deletion config/bench.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ config :eventstore, EventStore.Storage,
username: "postgres",
password: "postgres",
database: "eventstore_bench",
hostname: "localhost"
hostname: "localhost",
pool_size: 10,
extensions: [{Postgrex.Extensions.Calendar, []}]
23 changes: 19 additions & 4 deletions lib/event_store/sql/statements.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,32 @@ RETURNING stream_id;
end

def create_events(number_of_events \\ 1) do
insert = "INSERT INTO events (event_id, stream_id, stream_version, correlation_id, event_type, data, metadata, created_at) VALUES"
insert = ["INSERT INTO events (event_id, stream_id, stream_version, correlation_id, event_type, data, metadata, created_at) VALUES"]

params =
1..number_of_events
|> Enum.map(fn event_number ->
index = (event_number - 1) * 8
"($#{index + 1}, $#{index + 2}, $#{index + 3}, $#{index + 4}, $#{index + 5}, $#{index + 6}, $#{index + 7}, $#{index + 8})"
event_params = [
"($",
Integer.to_string(index + 1), ", $",
Integer.to_string(index + 2), ", $",
Integer.to_string(index + 3), ", $",
Integer.to_string(index + 4), ", $",
Integer.to_string(index + 5), ", $",
Integer.to_string(index + 6), ", $",
Integer.to_string(index + 7), ", $",
Integer.to_string(index + 8), ")"
]

if event_number == number_of_events do
event_params
else
[event_params, ","]
end
end)
|> Enum.join(",")

insert <> " " <> params <> ";"
[insert, " ", params, ";"]
end

def create_subscription do
Expand Down
3 changes: 1 addition & 2 deletions lib/event_store/storage/appender.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule EventStore.Storage.Appender do

defp build_insert_parameters(events) do
events
|> Enum.map(fn(event) ->
|> Enum.flat_map(fn event ->
[
event.event_id,
event.stream_id,
Expand All @@ -42,7 +42,6 @@ defmodule EventStore.Storage.Appender do
event.created_at,
]
end)
|> List.flatten
end

defp handle_response({:ok, %Postgrex.Result{num_rows: 0}}, stream_id) do
Expand Down
6 changes: 4 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule EventStore.Mixfile do
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(:bench), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

defp deps do
Expand All @@ -42,7 +43,7 @@ defmodule EventStore.Mixfile do
{:fsm, "~> 0.2"},
{:markdown, github: "devinus/markdown", only: :dev},
{:mix_test_watch, "~> 0.2", only: :dev},
{:poison, "~> 3.0", only: :test},
{:poison, "~> 3.0", only: [:bench, :test]},
{:poolboy, "~> 1.5"},
{:postgrex, "~> 0.12"},
{:uuid, "~> 1.1", only: [:bench, :test]}
Expand All @@ -68,7 +69,8 @@ EventStore using PostgreSQL for persistence.
defp aliases do
[
"es.setup": ["event_store.create"],
"es.reset": ["event_store.drop", "event_store.create"]
"es.reset": ["event_store.drop", "event_store.create"],
"benchmark": ["es.reset", "app.start", "bench"],
]
end
end

0 comments on commit 7d5e75e

Please sign in to comment.