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

Refactor/remove unecessary inspect calls #84

Merged
Show file tree
Hide file tree
Changes from 5 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
29 changes: 19 additions & 10 deletions lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,27 @@ if Code.ensure_loaded?(Ecto) do
end

def load(source, batch, item) do
case fetch(source, batch, item) do
{:error, _message} ->
batch = normalize_key(batch, source.default_params)
{batch_key, item_key, item} = get_keys(batch, item)
entry = {item_key, item}
{batch_key, item_key, item} =
batch
|> normalize_key(source.default_params)
|> get_keys(item)

update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([entry]), &MapSet.put(&1, entry))
end)
if fetched?(source.results, batch_key, item_key) do
source
else
entry = {item_key, item}

_ ->
source
update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([entry]), &MapSet.put(&1, entry))
end)
end
end

defp fetched?(results, batch_key, item_key) do
case Map.fetch(results, batch_key) do
:error -> false
{:ok, {:error, _reason}} -> false
{:ok, {:ok, batch}} -> Map.has_key?(batch, item_key)
pmargreff marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down
27 changes: 19 additions & 8 deletions lib/dataloader/kv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,25 @@ defmodule Dataloader.KV do
end

def load(source, batch_key, id) do
case fetch(source, batch_key, id) do
{:error, _message} ->
update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([id]), &MapSet.put(&1, id))
end)

_ ->
source
if fetched?(source.results, batch_key, id) do
source
else
update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([id]), &MapSet.put(&1, id))
end)
end
end

defp fetched?(results, batch_key, id) do
with {:ok, batch} <- Map.fetch(results, batch_key) do
case Map.fetch(batch, id) do
:error -> false
{:ok, {:error, _reason}} -> false
{:ok, _item} -> true
end
else
:error ->
false
end
end

Expand Down