-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/develop' into develop
- Loading branch information
Showing
9 changed files
with
123 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
defmodule Etso.Adapter.Meta do | ||
@moduledoc false | ||
|
||
@type t :: %__MODULE__{repo: Ecto.Repo.t()} | ||
@type t :: %__MODULE__{ | ||
repo: Ecto.Repo.t(), | ||
cache: :ets.tab() | nil, | ||
pid: pid() | nil, | ||
stacktrace: true | false | ||
} | ||
|
||
@enforce_keys ~w(repo)a | ||
defstruct repo: nil | ||
defstruct repo: nil, cache: nil, pid: nil, stacktrace: false | ||
|
||
@behaviour Access | ||
defdelegate get(v, key, default), to: Map | ||
defdelegate fetch(v, key), to: Map | ||
defdelegate get_and_update(v, key, func), to: Map | ||
defdelegate pop(v, key), to: Map | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Etso.ETS.ObjectsSorter do | ||
@moduledoc """ | ||
The ETS Objects Sorter module is responsible for sorting results returned from ETS according to | ||
the sort predicates provided in the query. | ||
""" | ||
|
||
def sort(ets_objects, %Ecto.Query{order_bys: []}) do | ||
ets_objects | ||
end | ||
|
||
def sort(ets_objects, %Ecto.Query{} = query) do | ||
sort_predicates = build_sort_predicates(query) | ||
Enum.sort_by(ets_objects, & &1, &compare(&1, &2, sort_predicates)) | ||
end | ||
|
||
defp build_sort_predicates(%Ecto.Query{} = query) do | ||
Enum.flat_map(query.order_bys, fn %Ecto.Query.QueryExpr{expr: list} -> | ||
Enum.map(list, fn {direction, field} -> | ||
{direction, Enum.find_index(query.select.fields, &(&1 == field))} | ||
end) | ||
end) | ||
end | ||
|
||
defp compare(lhs, rhs, [{direction, index} | predicates]) do | ||
case {direction, Enum.at(lhs, index), Enum.at(rhs, index)} do | ||
{_, lhs, rhs} when lhs == rhs -> compare(lhs, rhs, predicates) | ||
{:asc, lhs, rhs} -> lhs < rhs | ||
{:desc, lhs, rhs} -> lhs > rhs | ||
end | ||
end | ||
|
||
defp compare(_lhs, _rhs, []) do | ||
true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters