Skip to content

Commit 49b3afe

Browse files
committed
chore: add sandbox mocks for new ets commands
1 parent 3b95606 commit 49b3afe

File tree

1 file changed

+99
-8
lines changed

1 file changed

+99
-8
lines changed

lib/cache/sandbox.ex

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ defmodule Cache.Sandbox do
189189
end)
190190
end
191191

192+
# Redis Compatibility
192193
def pipeline(_cache_name, _commands, _opts) do
193194
raise "Not Implemented"
194195
end
@@ -205,11 +206,101 @@ defmodule Cache.Sandbox do
205206
raise "Not Implemented"
206207
end
207208

208-
def scan(_cache_name, _scan_opts, _opts) do
209+
def scan(cache_name, _scan_opts, _opts) do
210+
Agent.get(cache_name, fn state ->
211+
{:ok, Map.keys(state)}
212+
end)
213+
end
214+
215+
def hash_scan(cache_name, key, _scan_opts, _opts) do
216+
Agent.get(cache_name, fn state ->
217+
case Map.get(state, key) do
218+
nil -> {:ok, []}
219+
map when is_map(map) -> {:ok, Map.keys(map)}
220+
_ -> {:ok, []}
221+
end
222+
end)
223+
end
224+
225+
# ETS & DETS Compatibility
226+
def member(cache_name, key) do
227+
Agent.get(cache_name, fn state ->
228+
Map.has_key?(state, key)
229+
end)
230+
end
231+
232+
def update_counter(cache_name, key, {_pos, incr}) do
233+
Agent.get_and_update(cache_name, fn state ->
234+
current_value = state[key] || 0
235+
new_value = current_value + incr
236+
{new_value, Map.put(state, key, new_value)}
237+
end)
238+
end
239+
240+
def insert_raw(cache_name, data) when is_tuple(data) do
241+
{key, value} = data
242+
Agent.update(cache_name, fn state ->
243+
Map.put(state, key, value)
244+
end)
245+
246+
true
247+
end
248+
249+
def insert_raw(cache_name, data) when is_list(data) do
250+
Agent.update(cache_name, fn state ->
251+
Enum.reduce(data, state, fn {key, value}, acc ->
252+
Map.put(acc, key, value)
253+
end)
254+
end)
255+
256+
true
257+
end
258+
259+
def match_object(_cache_name, _pattern) do
209260
raise "Not Implemented"
210261
end
211262

212-
def hash_scan(_cache_name, _key, _scan_opts, _opts) do
263+
def match_object(_cache_name, _pattern, _limit) do
264+
raise "Not Implemented"
265+
end
266+
267+
def select(_cache_name, _match_spec) do
268+
raise "Not Implemented"
269+
end
270+
271+
def select(_cache_name, _match_spec, _limit) do
272+
raise "Not Implemented"
273+
end
274+
275+
def info(_cache_name) do
276+
raise "Not Implemented"
277+
end
278+
279+
def info(_cache_name, _item) do
280+
raise "Not Implemented"
281+
end
282+
283+
def select_delete(_cache_name, _match_spec) do
284+
raise "Not Implemented"
285+
end
286+
287+
def match_delete(_cache_name, _pattern) do
288+
raise "Not Implemented"
289+
end
290+
291+
def to_dets(_cache_name, _dets_table) do
292+
raise "Not Implemented"
293+
end
294+
295+
def from_dets(_cache_name, _dets_table) do
296+
raise "Not Implemented"
297+
end
298+
299+
def to_ets(_cache_name) do
300+
raise "Not Implemented"
301+
end
302+
303+
def from_ets(_cache_name, _ets_table) do
213304
raise "Not Implemented"
214305
end
215306

@@ -261,12 +352,12 @@ defmodule Cache.Sandbox do
261352

262353
defp serialize_path_and_get_value(cache_name, key, path) do
263354
path = JSON.serialize_path(path)
264-
Agent.get(cache_name, fn state ->
265-
case get_in(state, [key | String.split(path, ".")]) do
266-
nil -> {:error, ErrorMessage.not_found("ERR Path '$.#{path}' does not exist")}
267-
value -> {:ok, value}
268-
end
269-
end)
355+
Agent.get(cache_name, fn state ->
356+
case get_in(state, [key | String.split(path, ".")]) do
357+
nil -> {:error, ErrorMessage.not_found("ERR Path '$.#{path}' does not exist")}
358+
value -> {:ok, value}
359+
end
360+
end)
270361
end
271362

272363
defp enum_length(m) when is_map(m), do: m |> Map.to_list() |> enum_length()

0 commit comments

Comments
 (0)