Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated db_connection to v2 #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/grakn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ defmodule Grakn do
"""
@spec query(conn(), Grakn.Query.t(), Keyword.t()) :: any()
def query(conn, query, opts \\ []) do
DBConnection.execute(get_conn(conn), query, [], with_transaction_config(opts))
case DBConnection.execute(get_conn(conn), query, [], with_transaction_config(opts)) do
{:ok, _query, result} -> {:ok, result}
otherwise -> otherwise
end
end

@doc """
Expand All @@ -56,12 +59,21 @@ defmodule Grakn do
"""
@spec query!(conn(), Grakn.Query.t(), Keyword.t()) :: any()
def query!(conn, %Grakn.Query{} = query, opts \\ []) do
DBConnection.execute!(get_conn(conn), query, [], with_transaction_config(opts))
case DBConnection.execute!(get_conn(conn), query, [], with_transaction_config(opts)) do
{:ok, _query, result} -> {:ok, result}
otherwise -> otherwise
end
end

@spec command(conn(), Grakn.Command.t(), Keyword.t()) :: any()
def command(conn, %Grakn.Command{} = command, opts \\ []) do
DBConnection.execute(get_conn(conn), command, [], with_transaction_config(opts))
case DBConnection.execute(get_conn(conn), command, [], with_transaction_config(opts)) do
{:ok, _command, result} ->
{:ok, result}

otherwise ->
otherwise
end
end

@doc """
Expand Down Expand Up @@ -170,14 +182,14 @@ defmodule Grakn do
defp with_start_config(opts) do
opts
|> Keyword.put_new(:pool_size, get_config(:pool_size, 4))
|> Keyword.put_new(:pool, DBConnection.Poolboy)
|> Keyword.put_new(:pool, DBConnection.ConnectionPool)
end

defp with_transaction_config(opts) do
opts_with_defaults =
opts
|> Keyword.put_new(:pool_size, get_config(:pool_size, 4))
|> Keyword.put_new(:pool, DBConnection.Poolboy)
|> Keyword.put_new(:pool, DBConnection.ConnectionPool)
|> Keyword.put_new(:pool_timeout, get_config(:pool_timeout, 30_000))
|> Keyword.put_new(:timeout, get_config(:timeout, @default_timeout))
|> Keyword.put_new(:queue, get_config(:queue, true))
Expand Down
18 changes: 9 additions & 9 deletions lib/grakn/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ defmodule Grakn.Channel do
end
end

@spec command(t(), Grakn.Command.command(), keyword(), keyword()) ::
{:ok, any()} | {:error, any()}
def command(channel, :get_keyspaces, _, opts) do
@spec command(t(), %Grakn.Command{}, keyword(), keyword()) ::
{:ok, %Grakn.Command{}, any()} | {:error, any()}
def command(channel, %Grakn.Command{command: :get_keyspaces} = cmd, _, opts) do
request = Keyspace.Keyspace.Retrieve.Req.new()

case Keyspace.KeyspaceService.Stub.retrieve(channel, request, opts) do
{:ok, %Keyspace.Keyspace.Retrieve.Res{names: names}} ->
{:ok, names}
{:ok, cmd, names}

{:error, reason} ->
{:error, reason}
Expand All @@ -108,25 +108,25 @@ defmodule Grakn.Channel do
end
end

def command(channel, :create_keyspace, [name: name], opts) do
def command(channel, %Grakn.Command{command: :create_keyspace} = cmd, [name: name], opts) do
request = Keyspace.Keyspace.Create.Req.new(name: name)

case Keyspace.KeyspaceService.Stub.create(channel, request, opts) do
{:ok, %Keyspace.Keyspace.Create.Res{}} -> {:ok, nil}
{:ok, %Keyspace.Keyspace.Create.Res{}} -> {:ok, cmd, nil}
error -> error
end
end

def command(channel, :delete_keyspace, [name: name], opts) do
def command(channel, %Grakn.Command{command: :delete_keyspace} = cmd, [name: name], opts) do
request = Keyspace.Keyspace.Delete.Req.new(name: name)

case Keyspace.KeyspaceService.Stub.delete(channel, request, opts) do
{:ok, %Keyspace.Keyspace.Delete.Res{}} -> {:ok, nil}
{:ok, %Keyspace.Keyspace.Delete.Res{}} -> {:ok, cmd, nil}
error -> error
end
end

def command(channel, :close_session, [session_id: session_id], opts) do
def command(channel, %Grakn.Command{command: :close_session}, [session_id: session_id], opts) do
close_session(channel, session_id, opts)
end

Expand Down
5 changes: 4 additions & 1 deletion lib/grakn/concept/attribute.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ defmodule Grakn.Concept.Attribute do
@spec value(Concept.t(), DBConnection.t()) :: {:ok, any()} | {:error, any()}
def value(%{id: concept_id} = concept, conn, opts \\ []) do
with :ok <- assert_is_attribute(concept) do
DBConnection.execute(conn, Action.attribute_value(), [concept_id], opts)
case DBConnection.execute(conn, Action.attribute_value(), [concept_id], opts) do
{:ok, _query, result} -> {:ok, result}
otherwise -> otherwise
end
end
end

Expand Down
24 changes: 21 additions & 3 deletions lib/grakn/concept/schema_concept.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,35 @@ defmodule Grakn.Concept.SchemaConcept do

@spec get(String.t(), DBConnection.t(), keyword()) :: {:ok, Concept.t()} | {:error, any()}
def get(label, conn, opts \\ []) do
DBConnection.execute(conn, Action.get_schema_concept(), [label], opts)
case DBConnection.execute(conn, Action.get_schema_concept(), [label], opts) do
{:ok, _query, result} ->
{:ok, result}

otherwise ->
otherwise
end
end

@spec label(Concept.t(), DBConnection.t(), keyword()) :: {:ok, String.t()} | {:error, any()}
def label(%{id: concept_id}, conn, opts \\ []) do
DBConnection.execute(conn, Action.concept_label(), [concept_id], opts)
case DBConnection.execute(conn, Action.concept_label(), [concept_id], opts) do
{:ok, _query, result} ->
{:ok, result}

otherwise ->
otherwise
end
end

@spec attribute_types(Concept.t(), DBConnection.t(), keyword()) ::
{:ok, [Concept.t()]} | {:error, any()}
def attribute_types(%{id: concept_id}, conn, opts \\ []) do
DBConnection.execute(conn, Action.get_attribute_types(), [concept_id], opts)
case DBConnection.execute(conn, Action.get_attribute_types(), [concept_id], opts) do
{:ok, _query, result} ->
{:ok, result}

otherwise ->
otherwise
end
end
end
10 changes: 9 additions & 1 deletion lib/grakn/concept/thing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ defmodule Grakn.Concept.Thing do
{:ok, any()} | {:error, any()}
def get_attributes(%{id: concept_id} = concept, attribute_types, conn, opts \\ []) do
with :ok <- assert_is_thing(concept) do
DBConnection.execute(conn, Action.attributes_by_type(), [concept_id, attribute_types], opts)
case DBConnection.execute(
conn,
Action.attributes_by_type(),
[concept_id, attribute_types],
opts
) do
{:ok, _query, result} -> {:ok, result}
otherwise -> otherwise
end
end
end

Expand Down
43 changes: 34 additions & 9 deletions lib/grakn/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ defmodule Grakn.Protocol do
{:error, Error.exception("Cannot commit if transaction is not open"), state}
end

def handle_execute(%{graql: graql}, _params, opts, %{transaction: tx} = state)
def handle_execute(%{graql: graql} = query, _params, opts, %{transaction: tx} = state)
when transaction_open?(tx) do
case Transaction.query(tx, graql, opts) do
{:ok, result} ->
{:ok, result, state}
{:ok, query, result, state}

{:error, reason} ->
message =
Expand All @@ -96,20 +96,20 @@ defmodule Grakn.Protocol do
{:error, Error.exception("Cannot execute a query before starting a tranaction"), state}
end

def handle_execute(%Grakn.Command{command: command, params: params}, _, opts, state) do
def handle_execute(%Grakn.Command{params: params} = cmd, _, opts, state) do
state.channel
|> Channel.command(command, params, timeout: opts[:timeout])
|> handle_result(state)
|> Channel.command(cmd, params, timeout: opts[:timeout])
|> handle_result(cmd, state)
end

# Handle internal concept actions
def handle_execute(%Grakn.Concept.Action{name: action_name}, params, _, state)
def handle_execute(%Grakn.Concept.Action{name: action_name} = query, params, _, state)
when is_atom(action_name) and is_list(params) do
%{transaction: tx} = state

Transaction
|> apply(action_name, [tx | params])
|> handle_result(state)
|> handle_result(query, state)
end

def handle_rollback(_opts, %{transaction: tx} = state) do
Expand All @@ -120,8 +120,33 @@ defmodule Grakn.Protocol do
{:ok, nil, %{state | transaction: nil}}
end

defp handle_result({:ok, result}, state), do: {:ok, result, state}
defp handle_result({:error, error}, state), do: {error_status(error), error, state}
@doc """
DBConnection callback
"""
def handle_status(_, %{transaction_status: status} = state) do
{status, state}
end

@impl DBConnection
def handle_prepare(query, opts, state), do: {:ok, query, state}

@impl DBConnection
def handle_fetch(query, cursor, opts, state), do: {:cont, "", state}

@impl DBConnection
def handle_declare(query, params, opt, state), do: {:ok, query, "", state}

@impl DBConnection
def handle_deallocate(query, cursor, opts, state), do: {:ok, "", state}

@impl DBConnection
def handle_close(query, opts, state), do: {:ok, "", state}

defp handle_result({:ok, result}, query, state), do: {:ok, query, result, state}
defp handle_result({:ok, _, result}, query, state), do: {:ok, query, result, state}
defp handle_result({:error, error}, _query, state), do: {error_status(error), error, state}

def ping(state), do: {:ok, state}

defp error_status(%GRPC.RPCError{message: message}) when is_binary(message) do
if message =~ ~r/noproc|shutdown/, do: :disconnect, else: :error
Expand Down
5 changes: 2 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ defmodule GraknElixir.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:db_connection, "~> 1.1.0"},
{:db_connection, "~> 2.2.0"},
{:multix, github: "taxfix/multix"},
{:ex2ms, "~> 1.6"},
{:poolboy, "~> 1.5.1"},
{:grpc, github: "elixir-grpc/grpc", ref: "6edfd9cb9ce8f19dabd8a3ae68ecd48149d36c2a"},
{:protobuf, "~> 0.5.3"},
{:ex_doc, ">= 0.0.0", only: :dev},
{:earmark, ">= 0.0.0", only: :dev},
{:dialyxir, "~> 1.0.0-rc.6", only: [:dev], runtime: false},
{:dialyxir, "~> 1.0.0", only: [:dev], runtime: false},
{:credo, "~> 1.0", only: [:dev, :test], runtime: false},
{:benchee, "~> 0.13", only: :dev}
]
Expand Down
7 changes: 3 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"cowboy": {:git, "https://github.com/elixir-grpc/cowboy.git", "db1b09fb06038415e5c643282554c0b9f8e6a976", [tag: "grpc-2.6.3"]},
"cowlib": {:git, "https://github.com/elixir-grpc/cowlib.git", "1cc32e27d917bfe615da6957006fd9f8d6e604bd", [tag: "grpc-2.7.3"]},
"credo": {:hex, :credo, "1.2.2", "f57faf60e0a12b0ba9fd4bad07966057fde162b33496c509b95b027993494aab", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8f2623cd8c895a6f4a55ef10f3fdf6a55a9ca7bef09676bd835551687bf8a740"},
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm", "5f0a16a58312a610d5eb0b07506280c65f5137868ad479045f2a2dc4ced80550"},
"db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"},
"deep_merge": {:hex, :deep_merge, "0.2.0", "c1050fa2edf4848b9f556fba1b75afc66608a4219659e3311d9c9427b5b680b3", [:mix], [], "hexpm", "e3bf435a54ed27b0ba3a01eb117ae017988804e136edcbe8a6a14c310daa966e"},
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.6", "78e97d9c0ff1b5521dd68041193891aebebce52fc3b93463c0a6806874557d7d", [:mix], [{:erlex, "~> 0.2.1", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "49496d63267bc1a4614ffd5f67c45d9fc3ea62701a6797975bc98bc156d2763f"},
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm", "b42a23e9bd92d65d16db2f75553982e58519054095356a418bb8320bbacb58b1"},
"erlex": {:hex, :erlex, "0.2.4", "23791959df45fe8f01f388c6f7eb733cc361668cbeedd801bf491c55a029917b", [:mix], [], "hexpm", "4a12ebc7cd8f24f2d0fce93d279fa34eb5068e0e885bb841d558c4d83c52c439"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex2ms": {:hex, :ex2ms, "1.6.0", "f39bbd9ff1b0f27b3f707bab2d167066dd8965e7df1149b962d94c74615d0e09", [:mix], [], "hexpm", "0d1ab5e08421af5cd69146efb408dbb1ff77f38a2f4df5f086f2512dc8cf65bf"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "dc87f778d8260da0189a622f62790f6202af72f2f3dee6e78d91a18dd2fcd137"},
"grpc": {:git, "https://github.com/elixir-grpc/grpc.git", "6edfd9cb9ce8f19dabd8a3ae68ecd48149d36c2a", [ref: "6edfd9cb9ce8f19dabd8a3ae68ecd48149d36c2a"]},
Expand All @@ -19,7 +19,6 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "adf0218695e22caeda2820eaba703fa46c91820d53813a2223413da3ef4ba515"},
"multix": {:git, "https://github.com/taxfix/multix.git", "43848163fa59033328bee2ea691ab3f3fae71a0a", []},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm", "5c040b8469c1ff1b10093d3186e2e10dbe483cd73d79ec017993fb3985b8a9b3"},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm", "8f7168911120e13419e086e78d20e4d1a6776f1eee2411ac9f790af10813389f"},
"protobuf": {:hex, :protobuf, "0.5.4", "2e1b8eec211aff034ad8a14e3674220b0158bfb9a3c7128ac9d2a1ed1b3724d3", [:mix], [], "hexpm", "994348a4592408bc99c132603b0fdb686a2b5df0321a8eb1a582ec2bd3495886"},
"ranch": {:git, "https://github.com/ninenines/ranch", "3190aef88aea04d6dce8545fe9b4574288903f44", [ref: "1.7.1"]},
}