-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
mnesia.ex
276 lines (229 loc) · 6.46 KB
/
mnesia.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
defmodule Ash.DataLayer.Mnesia do
@moduledoc """
An Mnesia backed Ash Datalayer.
In your application intialization, you will need to call `Mnesia.create_schema([node()])`.
Additionally, you will want to create your mnesia tables there.
This data layer is *extremely unoptimized*, fetching all records from a table and filtering them
in place. This is primarily used for testing the behavior of data layers in Ash. If it was improved,
it could be a viable data layer.
"""
@behaviour Ash.DataLayer
@mnesia %Ash.Dsl.Section{
name: :mnesia,
describe: """
A section for configuring the mnesia data layer
""",
examples: [
"""
mnesia do
table :custom_table
end
"""
],
schema: [
table: [
type: :atom,
doc: "The table name to use, defaults to the name of the resource"
]
]
}
use Ash.Dsl.Extension, sections: [@mnesia]
alias Ash.Actions.Sort
alias Ash.Dsl.Extension
alias :mnesia, as: Mnesia
def start(api) do
Mnesia.create_schema([node()])
Mnesia.start()
Code.ensure_compiled(api)
api
|> Ash.Api.resources()
|> Enum.each(fn resource ->
attributes =
resource |> Ash.Resource.Info.attributes() |> Enum.map(& &1.name) |> Enum.sort()
case Ash.Resource.Info.primary_key(resource) do
[] ->
resource
|> table()
|> Mnesia.create_table(attributes: attributes)
_ ->
resource
|> table()
|> Mnesia.create_table(attributes: [:_pkey | attributes])
end
end)
end
def table(resource) do
Extension.get_opt(resource, [:ets], :private?, resource, true)
end
defmodule Query do
@moduledoc false
defstruct [:api, :resource, :filter, :limit, :sort, relationships: %{}, offset: 0]
end
@impl true
def can?(_, :async_engine), do: true
def can?(_, :composite_primary_key), do: true
def can?(_, :upsert), do: true
def can?(_, :create), do: true
def can?(_, :read), do: true
def can?(_, :update), do: true
def can?(_, :destroy), do: true
def can?(_, :sort), do: true
def can?(_, :filter), do: true
def can?(_, :limit), do: true
def can?(_, :offset), do: true
def can?(_, :boolean_filter), do: true
def can?(_, :transact), do: true
def can?(_, {:join, resource}) do
# This is to ensure that these can't join, which is necessary for testing
# if someone needs to use these both and *actually* needs real joins for private
# ets resources then we can talk about making this only happen in ash tests
not (Ash.DataLayer.data_layer(resource) == Ash.DataLayer.Ets &&
Ash.DataLayer.Ets.private?(resource))
end
def can?(_, {:filter_expr, _}), do: true
def can?(_, :nested_expressions), do: true
def can?(_, {:sort, _}), do: true
def can?(_, _), do: false
@impl true
def resource_to_query(resource, api) do
%Query{
resource: resource,
api: api
}
end
@impl true
def in_transaction?(_), do: Mnesia.is_transaction()
@impl true
def limit(query, offset, _), do: {:ok, %{query | limit: offset}}
@impl true
def offset(query, offset, _), do: {:ok, %{query | offset: offset}}
@impl true
def filter(query, filter, _resource) do
{:ok, %{query | filter: filter}}
end
@impl true
def sort(query, sort, _resource) do
{:ok, %{query | sort: sort}}
end
@impl true
def run_query(
%Query{
api: api,
resource: resource,
filter: filter,
offset: offset,
limit: limit,
sort: sort
},
_resource
) do
records =
Mnesia.transaction(fn ->
Mnesia.select(table(resource), [{:_, [], [:"$_"]}])
end)
case records do
{:aborted, reason} ->
{:error, reason}
{:atomic, records} ->
attributes =
resource |> Ash.Resource.Info.attributes() |> Enum.map(& &1.name) |> Enum.sort()
elements_to_drop =
case Ash.Resource.Info.primary_key(resource) do
[] ->
1
_ ->
2
end
structified_records =
Enum.map(records, fn record ->
struct(
resource,
Enum.zip(attributes, Enum.drop(Tuple.to_list(record), elements_to_drop))
)
end)
api
|> Ash.Filter.Runtime.filter_matches(structified_records, filter)
|> case do
{:ok, filtered} ->
offset_records =
filtered
|> Sort.runtime_sort(sort)
|> Enum.drop(offset || 0)
limited_records =
if limit do
Enum.take(offset_records, limit)
else
offset_records
end
{:ok, limited_records}
{:error, error} ->
{:error, error}
end
end
end
@impl true
def create(resource, changeset) do
{:ok, record} = Ash.Changeset.apply_attributes(changeset)
pkey =
resource
|> Ash.Resource.Info.primary_key()
|> Enum.map(fn attr ->
Map.get(record, attr)
end)
values =
resource
|> Ash.Resource.Info.attributes()
|> Enum.sort_by(& &1.name)
|> Enum.map(&Map.get(record, &1.name))
values_with_primary_key =
case pkey do
[] ->
List.to_tuple([table(resource) | values])
pkey_values ->
List.to_tuple([table(resource) | [pkey_values | values]])
end
result =
Mnesia.transaction(fn ->
Mnesia.write(values_with_primary_key)
end)
case result do
{:atomic, _} -> {:ok, record}
{:aborted, error} -> {:error, error}
end
end
@impl true
def destroy(resource, %{data: record}) do
pkey =
resource
|> Ash.Resource.Info.primary_key()
|> Enum.map(&Map.get(record, &1))
result =
Mnesia.transaction(fn ->
Mnesia.delete({table(resource), pkey})
end)
case result do
{:atomic, _} -> :ok
{:aborted, error} -> {:error, error}
end
end
@impl true
def update(resource, changeset) do
create(resource, changeset)
end
@impl true
def upsert(resource, changeset, _) do
create(resource, changeset)
end
@impl true
def transaction(_, func) do
case Mnesia.transaction(func) do
{:atomic, result} -> {:ok, result}
{:aborted, reason} -> {:error, reason}
end
end
@impl true
@spec rollback(term, term) :: no_return
def rollback(_, value) do
Mnesia.abort(value)
end
end