Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
4b4a166
Fixed non-flat :orelse query crashing OTP 23.0
Feb 5, 2021
93a1a12
Merge branch 'feature/gh-10-flat-orelse' into develop
evadne Feb 5, 2021
70c73fe
→ 0.1.3
evadne Feb 5, 2021
e3cb62a
Merge branch 'release/0.1.3'
evadne Feb 5, 2021
1a11abc
Merge tag '0.1.3' into develop
evadne Feb 5, 2021
b0cfd39
Added LICENSE
evadne Mar 2, 2021
368e935
→ 0.1.4
evadne Mar 2, 2021
bb862c9
Merge branch 'release/0.1.4'
evadne Mar 2, 2021
7d2a022
Merge tag '0.1.4' into develop
evadne Mar 2, 2021
daad287
Fixed reporting of errors on conflicting primary key
evadne Mar 4, 2021
8405418
Merge branch 'feature/gh-7-unique-constraint-error' into develop
evadne Mar 6, 2021
3ec4235
→ 0.1.5
evadne Mar 6, 2021
bde63b9
Merge branch 'release/0.1.5'
evadne Mar 6, 2021
98a7ca1
Merge tag '0.1.5' into develop
evadne Mar 6, 2021
918a698
Added support for is_nil in queries
evadne Jun 25, 2021
16ceb01
Merge branch 'feature/allow-is-nil' into develop
evadne Jul 5, 2021
94de59a
→ 0.1.6
evadne Jul 5, 2021
40447b8
Merge branch 'release/0.1.6'
evadne Jul 5, 2021
2b310ba
Merge tag '0.1.6' into develop
evadne Jul 5, 2021
81329ff
Added insert_all test
evadne May 20, 2022
3b1f7e0
Raised Ecto requirement to 3.8.3+
evadne May 20, 2022
3404465
Added additional test for insert_all
evadne May 20, 2022
338922b
Merge branch 'feature/14-insert-all' into develop
evadne May 20, 2022
fd6f6a9
Amended Etso with Ecto 3.8.2
evadne May 20, 2022
b866e27
Merge branch 'feature/19-ecto-3.8' into develop
evadne May 20, 2022
f5516f7
Added ability to sort results
evadne May 20, 2022
0722d4e
Merge branch 'feature/12-preload-order' into develop
evadne May 20, 2022
f1e94c5
Added support for parallel preloading
evadne May 20, 2022
8fc610e
Merge branch 'feature/13-parallel-preload' into develop
evadne May 20, 2022
61c83fe
Amended README
evadne May 20, 2022
d9ea5bb
Merge branch 'feature/13-parallel-preload' into develop
evadne May 20, 2022
33a65d1
→ 1.0.0
evadne May 20, 2022
3efc87c
Merge branch 'release/1.0.0'
evadne May 20, 2022
7f4ab68
Merge tag '1.0.0' into develop
evadne May 20, 2022
0101357
Corrected file name of the Objects Sorter
evadne May 20, 2022
b696474
Merge branch 'feature/objects-sorter' into develop
evadne May 20, 2022
e870e10
→ 1.0.1
evadne May 20, 2022
a868f6f
Merge branch 'release/1.0.1'
evadne May 20, 2022
371d3dc
Merge tag '1.0.1' into develop
evadne May 20, 2022
2af8316
Added support for json_extract_path() in select queries
heywhy Jun 28, 2022
6663c1e
Added support for delete_all
evadne Jun 28, 2022
85ca01e
Merge branch 'feature/gh-20-json-extract-path' into develop
evadne Jun 29, 2022
3f31f68
Attributed author of json_extract_path() support in README
evadne Jun 29, 2022
14d0422
Merge branch 'feature/gh-20-attribution' into develop
evadne Jun 29, 2022
d502276
→ 1.1.0
evadne Jun 29, 2022
8c5743e
Merge branch 'release/1.1.0'
evadne Jun 29, 2022
cae311e
Merge tag '1.1.0' into develop
evadne Jun 29, 2022
67fb68c
single commit
prem-prakash Jan 15, 2024
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
176 changes: 176 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.

"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.

"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.

"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.

"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.

"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).

"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.

"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."

"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.

2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.

3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.

4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:

(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.

You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.

5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.

6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.

7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.

8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.

9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Using Etso is a two-step process. First, include it in your application’s depe
```elixir
defp deps do
[
{:etso, "~> 0.1.2"}
{:etso, "~> 1.1.0"}
]
end
```
Expand Down Expand Up @@ -101,6 +101,16 @@ The Author also wishes to thank the following individuals:

- [Masanori Iwasaki][curi1119], for [support of `for…in` queries][pr-6].

- [Kohei Noda][pobo380], for [fixing nested `{:orelse, …}` queries][pr-10].

- [William Martins][wmartins], for [fixing primary key unicity check issues][gh-7].

- [Soichiro Nishizawa][soichiro-nishizawa], for [providing the implementation of sorted results][gh-12].

- [Doug W.][harmon25], for [providing insights into parallel preloads][gh-13].

- [Atanda Rasheed][heywhy], for [support of JSON extract paths][gh-20].

[erlang-ets]: http://erlang.org/doc/man/ets.html
[northwind]: https://github.com/evadne/etso/tree/master/test/support/northwind
[northwind-importer]: https://github.com/evadne/etso/tree/master/test/support/northwind/importer.ex
Expand All @@ -110,3 +120,13 @@ The Author also wishes to thank the following individuals:
[wojtekmach]: https://github.com/wojtekmach
[curi1119]: https://github.com/curi1119
[pr-6]: https://github.com/evadne/etso/pull/6
[pobo380]: https://github.com/pobo380
[pr-10]: https://github.com/evadne/etso/pull/10
[wmartins]: https://github.com/wmartins
[gh-7]: https://github.com/evadne/etso/issues/7
[soichiro-nishizawa]: https://github.com/soichiro-nishizawa
[gh-12]: https://github.com/evadne/etso/issues/12
[harmon25]: https://github.com/harmon25
[gh-13]: https://github.com/evadne/etso/issues/13
[heywhy]: https://github.com/heywhy
[gh-20]: https://github.com/evadne/etso/issues/20
24 changes: 20 additions & 4 deletions lib/etso/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ defmodule Etso.Adapter do
"""

@behaviour Ecto.Adapter
@behaviour Ecto.Adapter.Schema
@behaviour Ecto.Adapter.Queryable
@behaviour Ecto.Adapter.Schema
@behaviour Ecto.Adapter.Storage

@impl Ecto.Adapter
defmacro __before_compile__(_opts), do: :ok

@doc false
@impl Ecto.Adapter
def ensure_all_started(_config, _type), do: {:ok, []}

@doc false
@impl Ecto.Adapter
def init(config) do
{:ok, repo} = Keyword.fetch(config, :repo)
child_spec = __MODULE__.Supervisor.child_spec(repo)
Expand All @@ -31,25 +35,37 @@ defmodule Etso.Adapter do
end

@doc false
@impl Ecto.Adapter
def checkout(_, _, fun), do: fun.()

@doc false
@impl Ecto.Adapter
def checked_out?(_), do: false

@doc false
@impl Ecto.Adapter
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
@impl Ecto.Adapter
def dumpers(:binary_id, type), do: [type, Ecto.UUID]
def dumpers(:embed_id, type), do: [type, Ecto.UUID]
def dumpers(_, type), do: [type]

for module <- [__MODULE__.Behaviour.Schema, __MODULE__.Behaviour.Queryable] do
for {name, arity} <- module.__info__(:functions) do
for {implementation_module, behaviour_module} <- [
{__MODULE__.Behaviour.Schema, Ecto.Adapter.Schema},
{__MODULE__.Behaviour.Queryable, Ecto.Adapter.Queryable},
{__MODULE__.Behaviour.Storage, Ecto.Adapter.Storage}
] do
for {name, arity} <- implementation_module.__info__(:functions) do
args = Enum.map(1..arity, &{:"arg_#{&1}", [], Elixir})

@doc false
@impl behaviour_module
def unquote(name)(unquote_splicing(args)) do
unquote(module).unquote(name)(unquote_splicing(args))
unquote(implementation_module).unquote(name)(unquote_splicing(args))
end
end
end
Expand Down
49 changes: 44 additions & 5 deletions lib/etso/adapter/behaviour/queryable.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
defmodule Etso.Adapter.Behaviour.Queryable do
@moduledoc false
@behaviour Ecto.Adapter.Queryable

alias Etso.Adapter.TableRegistry
alias Etso.ETS.MatchSpecification
alias Etso.ETS.ObjectsSorter

def prepare(:all, query) do
{:nocache, query}
@impl Ecto.Adapter.Queryable
def prepare(:all, %Ecto.Query{} = query) do
{:nocache, {:select, query}}
end

def execute(%{repo: repo}, _, {:nocache, query}, params, _) do
@impl Ecto.Adapter.Queryable
def prepare(:delete_all, %Ecto.Query{wheres: []} = query) do
{:nocache, {:delete_all_objects, query}}
end

@impl Ecto.Adapter.Queryable
def prepare(:delete_all, %Ecto.Query{wheres: _} = query) do
{:nocache, {:match_delete, query}}
end

@impl Ecto.Adapter.Queryable
def execute(%{repo: repo}, _, {:nocache, {:select, 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}
ets_count = length(ets_objects)
{ets_count, ObjectsSorter.sort(ets_objects, query)}
end

@impl Ecto.Adapter.Queryable
def execute(%{repo: repo}, _, {:nocache, {:delete_all_objects, query}}, params, _) do
{_, schema} = query.from.source
{:ok, ets_table} = TableRegistry.get_table(repo, schema)
ets_match = MatchSpecification.build(query, params)
ets_objects = query.select && ObjectsSorter.sort(:ets.select(ets_table, [ets_match]), query)
ets_count = :ets.info(ets_table, :size)
true = :ets.delete_all_objects(ets_table)
{ets_count, ets_objects || nil}
end

@impl Ecto.Adapter.Queryable
def execute(%{repo: repo}, _, {:nocache, {:match_delete, query}}, params, _) do
{_, schema} = query.from.source
{:ok, ets_table} = TableRegistry.get_table(repo, schema)
ets_match = MatchSpecification.build(query, params)
ets_objects = query.select && ObjectsSorter.sort(:ets.select(ets_table, [ets_match]), query)
{ets_match_head, ets_match_body, _} = ets_match
ets_match = {ets_match_head, ets_match_body, [true]}
ets_count = :ets.select_delete(ets_table, [ets_match])
{ets_count, ets_objects || nil}
end

def stream(%{repo: repo}, _, {:nocache, query}, params, options) do
@impl Ecto.Adapter.Queryable
def stream(%{repo: repo}, _, {:nocache, {:select, query}}, params, options) do
{_, schema} = query.from.source
{:ok, ets_table} = TableRegistry.get_table(repo, schema)
ets_match = MatchSpecification.build(query, params)
Expand Down
10 changes: 8 additions & 2 deletions lib/etso/adapter/behaviour/schema.ex
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
defmodule Etso.Adapter.Behaviour.Schema do
@moduledoc false
@behaviour Ecto.Adapter.Schema

alias Etso.Adapter.TableRegistry
alias Etso.ETS.TableStructure

@impl 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(%{repo: repo}, %{schema: schema}, _, entries, _, _, _) do
@impl Ecto.Adapter.Schema
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

@impl Ecto.Adapter.Schema
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, []}
if ets_result, do: {:ok, []}, else: {:invalid, [unique: "primary_key"]}
end

@impl Ecto.Adapter.Schema
def update(%{repo: repo}, %{schema: schema}, fields, filters, [], _) do
{:ok, ets_table} = TableRegistry.get_table(repo, schema)
[key_name] = schema.__schema__(:primary_key)
Expand All @@ -33,6 +38,7 @@ defmodule Etso.Adapter.Behaviour.Schema do
if ets_result, do: {:ok, []}, else: {:error, :stale}
end

@impl Ecto.Adapter.Schema
def delete(%{repo: repo}, %{schema: schema}, filters, _) do
{:ok, ets_table} = TableRegistry.get_table(repo, schema)
[key_name] = schema.__schema__(:primary_key)
Expand Down
7 changes: 7 additions & 0 deletions lib/etso/adapter/behaviour/storage.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Etso.Adapter.Behaviour.Storage do
@moduledoc false
@behaviour Ecto.Adapter.Storage
def storage_status(_options), do: :up
def storage_up(_options), do: :ok
def storage_down(_options), do: :ok
end
Loading