-
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.
- Loading branch information
Showing
15 changed files
with
291 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
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,3 +1,9 @@ | ||
defmodule Etso do | ||
@moduledoc false | ||
@moduledoc """ | ||
Top-level module for Etso. | ||
""" | ||
|
||
@type repo :: Ecto.Repo.t() | ||
@type schema :: module() | ||
@type table :: :ets.tab() | ||
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 |
---|---|---|
@@ -1,36 +1,56 @@ | ||
defmodule Etso.Adapter do | ||
@moduledoc """ | ||
Used as an Adapter in Repo modules, which transparently spins up one ETS table for | ||
each Schema used with the Repo, namespaced to the Repo to allow concurrent running | ||
of multiple Repositories. | ||
Used as an Adapter in Repo modules, which transparently spins up one ETS table for each Schema | ||
used with the Repo, namespaced to the Repo to allow concurrent running of multiple Repositories. | ||
The Etso Adapter implements the `Ecto.Adapter.Schema` and `Ecto.Adapter.Queryable` behaviours. | ||
To use the Etso Adapter in your application, define a Repo like this: | ||
defmodule MyApp.Repo do | ||
@otp_app Mix.Project.config()[:app] | ||
use Ecto.Repo, otp_app: @otp_app, adapter: Etso.Adapter | ||
end | ||
""" | ||
|
||
@behaviour Ecto.Adapter | ||
@behaviour Ecto.Adapter.Schema | ||
@behaviour Ecto.Adapter.Queryable | ||
|
||
defmacro __before_compile__(_opts), do: :ok | ||
|
||
@doc false | ||
def ensure_all_started(_config, _type), do: {:ok, []} | ||
|
||
@doc false | ||
def init(config) do | ||
{:ok, repo} = Keyword.fetch(config, :repo) | ||
child_spec = __MODULE__.Supervisor.child_spec(repo) | ||
adapter_meta = %{repo: repo} | ||
adapter_meta = %__MODULE__.Meta{repo: repo} | ||
{:ok, child_spec, adapter_meta} | ||
end | ||
|
||
@doc false | ||
def checkout(_, _, fun), do: fun.() | ||
|
||
@doc false | ||
def loaders(:binary_id, type), do: [Ecto.UUID, type] | ||
def loaders(:embed_id, type), do: [Ecto.UUID, type] | ||
def loaders(_, type), do: [type] | ||
|
||
@doc false | ||
def dumpers(:binary_id, type), do: [type, Ecto.UUID] | ||
def dumpers(:embed_id, type), do: [type, Ecto.UUID] | ||
def dumpers(_, type), do: [type] | ||
|
||
defp get_table(adapter_meta, schema) do | ||
__MODULE__.TableRegistry.get_table(adapter_meta.repo, schema) | ||
end | ||
for module <- [__MODULE__.Behaviour.Schema, __MODULE__.Behaviour.Queryable] do | ||
for {name, arity} <- module.__info__(:functions) do | ||
args = Enum.map(1..arity, &{:"arg_#{&1}", [], Elixir}) | ||
|
||
use __MODULE__.Behaviour.Schema | ||
use __MODULE__.Behaviour.Queryable | ||
@doc false | ||
def unquote(name)(unquote_splicing(args)) do | ||
unquote(module).unquote(name)(unquote_splicing(args)) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,50 @@ | ||
defmodule Etso.Adapter.Behaviour.Queryable do | ||
defmacro __using__(_) do | ||
quote do | ||
@behaviour Ecto.Adapter.Queryable | ||
|
||
def prepare(:all, query) do | ||
{:nocache, query} | ||
end | ||
|
||
def execute(adapter_meta, _, {:nocache, query}, params, _) do | ||
{_, schema} = query.from.source | ||
ets_table = get_table(adapter_meta, schema) | ||
ets_match = Etso.ETS.MatchSpecification.build(query, params) | ||
ets_objects = :ets.select(ets_table, [ets_match]) | ||
{length(ets_objects), ets_objects} | ||
end | ||
|
||
def stream(adapter_meta, _, {:nocache, query}, params, options) do | ||
{_, schema} = query.from.source | ||
ets_table = get_table(adapter_meta, schema) | ||
ets_match = Etso.ETS.MatchSpecification.build(query, params) | ||
ets_limit = Keyword.get(options, :max_rows, 500) | ||
stream_start_fun = fn -> stream_start(ets_table, ets_match, ets_limit) end | ||
stream_next_fun = fn acc -> stream_next(acc) end | ||
stream_after_fun = fn acc -> stream_after(ets_table, acc) end | ||
Stream.resource(stream_start_fun, stream_next_fun, stream_after_fun) | ||
end | ||
|
||
defp stream_start(ets_table, ets_match, ets_limit) do | ||
:ets.safe_fixtable(ets_table, true) | ||
:ets.select(ets_table, [ets_match], ets_limit) | ||
end | ||
|
||
defp stream_next(:"$end_of_table") do | ||
{:halt, :ok} | ||
end | ||
|
||
defp stream_next({ets_objects, ets_continuation}) do | ||
{[{length(ets_objects), ets_objects}], :ets.select(ets_continuation)} | ||
end | ||
|
||
defp stream_after(ets_table, :ok) do | ||
:ets.safe_fixtable(ets_table, false) | ||
end | ||
|
||
defp stream_after(_, acc) do | ||
acc | ||
end | ||
end | ||
@moduledoc false | ||
|
||
alias Etso.Adapter.TableRegistry | ||
alias Etso.ETS.MatchSpecification | ||
|
||
def prepare(:all, query) do | ||
{:nocache, query} | ||
end | ||
|
||
def execute(%{repo: repo}, _, {:nocache, query}, params, _) do | ||
{_, schema} = query.from.source | ||
{:ok, ets_table} = TableRegistry.get_table(repo, schema) | ||
ets_match = MatchSpecification.build(query, params) | ||
ets_objects = :ets.select(ets_table, [ets_match]) | ||
{length(ets_objects), ets_objects} | ||
end | ||
|
||
def stream(%{repo: repo}, _, {:nocache, query}, params, options) do | ||
{_, schema} = query.from.source | ||
{:ok, ets_table} = TableRegistry.get_table(repo, schema) | ||
ets_match = MatchSpecification.build(query, params) | ||
ets_limit = Keyword.get(options, :max_rows, 500) | ||
stream_start_fun = fn -> stream_start(ets_table, ets_match, ets_limit) end | ||
stream_next_fun = fn acc -> stream_next(acc) end | ||
stream_after_fun = fn acc -> stream_after(ets_table, acc) end | ||
Stream.resource(stream_start_fun, stream_next_fun, stream_after_fun) | ||
end | ||
|
||
defp stream_start(ets_table, ets_match, ets_limit) do | ||
:ets.safe_fixtable(ets_table, true) | ||
:ets.select(ets_table, [ets_match], ets_limit) | ||
end | ||
|
||
defp stream_next(:"$end_of_table") do | ||
{:halt, :ok} | ||
end | ||
|
||
defp stream_next({ets_objects, ets_continuation}) do | ||
{[{length(ets_objects), ets_objects}], :ets.select(ets_continuation)} | ||
end | ||
|
||
defp stream_after(ets_table, :ok) do | ||
:ets.safe_fixtable(ets_table, false) | ||
end | ||
|
||
defp stream_after(_, acc) do | ||
acc | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,53 @@ | ||
defmodule Etso.Adapter.Behaviour.Schema do | ||
defmacro __using__(_) do | ||
quote do | ||
@behaviour Ecto.Adapter.Schema | ||
|
||
def autogenerate(:id), do: :erlang.unique_integer() | ||
def autogenerate(:binary_id), do: Ecto.UUID.bingenerate() | ||
def autogenerate(:embed_id), do: Ecto.UUID.bingenerate() | ||
|
||
def insert_all(adapter_meta, schema_meta, _header, entries, on_conflict, returning, options) do | ||
%{schema: schema, source: source} = schema_meta | ||
ets_table = get_table(adapter_meta, schema) | ||
ets_field_names = Etso.ETS.TableStructure.field_names(schema) | ||
ets_changes = Etso.ETS.TableStructure.entries_to_tuples(ets_field_names, entries) | ||
ets_result = :ets.insert_new(ets_table, ets_changes) | ||
if ets_result, do: {length(ets_changes), nil}, else: {0, nil} | ||
end | ||
|
||
def insert(adapter_meta, schema_meta, fields, on_conflict, returning, options) do | ||
%{schema: schema, source: source} = schema_meta | ||
ets_table = get_table(adapter_meta, schema) | ||
ets_field_names = Etso.ETS.TableStructure.field_names(schema) | ||
ets_changes = Etso.ETS.TableStructure.fields_to_tuple(ets_field_names, fields) | ||
ets_result = :ets.insert_new(ets_table, ets_changes) | ||
if ets_result, do: {:ok, []}, else: {:invalid, []} | ||
end | ||
|
||
def update(adapter_meta, schema_meta, fields, filters, [] = returning, _options) do | ||
%{schema: schema, source: source} = schema_meta | ||
[key_name] = schema.__schema__(:primary_key) | ||
[{^key_name, key}] = filters | ||
ets_updates = build_ets_updates(schema, fields) | ||
ets_table = get_table(adapter_meta, schema) | ||
ets_result = :ets.update_element(ets_table, key, ets_updates) | ||
if ets_result, do: {:ok, []}, else: {:error, :stale} | ||
end | ||
|
||
def delete(adapter_meta, schema_meta, filters, _options) do | ||
%{schema: schema, source: source} = schema_meta | ||
[key_name] = schema.__schema__(:primary_key) | ||
[{^key_name, key}] = filters | ||
ets_table = get_table(adapter_meta, schema) | ||
ets_result = :ets.delete(ets_table, key) | ||
{:ok, []} | ||
end | ||
|
||
defp build_ets_updates(schema, fields) do | ||
ets_field_names = Etso.ETS.TableStructure.field_names(schema) | ||
|
||
for {field_name, field_value} <- fields do | ||
position_fun = fn x -> x == field_name end | ||
position = 1 + Enum.find_index(ets_field_names, position_fun) | ||
{position, field_value} | ||
end | ||
end | ||
@moduledoc false | ||
|
||
alias Etso.Adapter.TableRegistry | ||
alias Etso.ETS.TableStructure | ||
|
||
def autogenerate(:id), do: :erlang.unique_integer() | ||
def autogenerate(:binary_id), do: Ecto.UUID.bingenerate() | ||
def autogenerate(:embed_id), do: Ecto.UUID.bingenerate() | ||
|
||
def insert_all(%{repo: repo}, %{schema: schema}, _, entries, _, _, _) do | ||
{:ok, ets_table} = TableRegistry.get_table(repo, schema) | ||
ets_field_names = TableStructure.field_names(schema) | ||
ets_changes = TableStructure.entries_to_tuples(ets_field_names, entries) | ||
ets_result = :ets.insert_new(ets_table, ets_changes) | ||
if ets_result, do: {length(ets_changes), nil}, else: {0, nil} | ||
end | ||
|
||
def insert(%{repo: repo}, %{schema: schema}, fields, _, _, _) do | ||
{:ok, ets_table} = TableRegistry.get_table(repo, schema) | ||
ets_field_names = TableStructure.field_names(schema) | ||
ets_changes = TableStructure.fields_to_tuple(ets_field_names, fields) | ||
ets_result = :ets.insert_new(ets_table, ets_changes) | ||
if ets_result, do: {:ok, []}, else: {:invalid, []} | ||
end | ||
|
||
def update(%{repo: repo}, %{schema: schema}, fields, filters, [], _) do | ||
{:ok, ets_table} = TableRegistry.get_table(repo, schema) | ||
[key_name] = schema.__schema__(:primary_key) | ||
[{^key_name, key}] = filters | ||
ets_updates = build_ets_updates(schema, fields) | ||
ets_result = :ets.update_element(ets_table, key, ets_updates) | ||
if ets_result, do: {:ok, []}, else: {:error, :stale} | ||
end | ||
|
||
def delete(%{repo: repo}, %{schema: schema}, filters, _) do | ||
{:ok, ets_table} = TableRegistry.get_table(repo, schema) | ||
[key_name] = schema.__schema__(:primary_key) | ||
[{^key_name, key}] = filters | ||
:ets.delete(ets_table, key) | ||
{:ok, []} | ||
end | ||
|
||
defp build_ets_updates(schema, fields) do | ||
ets_field_names = TableStructure.field_names(schema) | ||
|
||
for {field_name, field_value} <- fields do | ||
position_fun = fn x -> x == field_name end | ||
position = 1 + Enum.find_index(ets_field_names, position_fun) | ||
{position, field_value} | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
defmodule Etso.Adapter.Meta do | ||
defstruct repo: nil, meta_table_name: nil | ||
@moduledoc false | ||
|
||
@type t :: %__MODULE__{ | ||
repo: module(), | ||
meta_table_name: term() | ||
} | ||
@type t :: %__MODULE__{repo: Ecto.Repo.t()} | ||
@enforce_keys ~w(repo)a | ||
defstruct repo: nil | ||
end |
Oops, something went wrong.