Skip to content
Permalink
Browse files
Use IO lists to build insert events SQL statement
Fixes #23.
  • Loading branch information
slashdotdash committed Oct 28, 2016
2 parents 746549a + 7bc0c2e commit 7d5e75ea7e433b1c670a46a8505e257629e7b30a
Showing with 27 additions and 11 deletions.
  1. +0 −1 bench/storage/append_events_bench.exs
  2. +0 −1 bench/storage/read_events_bench.exs
  3. +3 −1 config/bench.exs
  4. +19 −4 lib/event_store/sql/statements.ex
  5. +1 −2 lib/event_store/storage/appender.ex
  6. +4 −2 mix.exs
@@ -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

@@ -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

@@ -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, []}]
@@ -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
@@ -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,
@@ -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
@@ -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
@@ -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]}
@@ -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.