From 7b9cd09ad85b5bd55f3d49473f783bd1336c61cf Mon Sep 17 00:00:00 2001 From: Isaac Whitfield Date: Sat, 14 Sep 2024 11:30:09 +0100 Subject: [PATCH] Migrate export to use streaming --- lib/cachex/actions/export.ex | 14 ++++++++++++-- lib/cachex/actions/stream.ex | 8 +++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/cachex/actions/export.ex b/lib/cachex/actions/export.ex index fc587ac..a384c0d 100644 --- a/lib/cachex/actions/export.ex +++ b/lib/cachex/actions/export.ex @@ -5,6 +5,10 @@ defmodule Cachex.Actions.Export do # This command is extremely expensive as it turns the entire cache table into # a list, and so should be used sparingly. It's provided purely because it's # the backing implementation of the `dump/3` command. + alias Cachex.Actions.Stream, as: CachexStream + alias Cachex.Query + + # add required imports import Cachex.Spec ############## @@ -20,6 +24,12 @@ defmodule Cachex.Actions.Export do This action should only be used in the case of exports and/or debugging, due to the memory overhead involved, as well as the large concatenations. """ - def execute(cache(name: name), _options), - do: {:ok, :ets.tab2list(name)} + def execute(cache() = cache, options) do + query = Query.create() + batch = Keyword.take(options, [:batch_size]) + + with {:ok, stream} <- CachexStream.execute(cache, query, batch) do + {:ok, Enum.to_list(stream)} + end + end end diff --git a/lib/cachex/actions/stream.ex b/lib/cachex/actions/stream.ex index 0252746..e01481f 100644 --- a/lib/cachex/actions/stream.ex +++ b/lib/cachex/actions/stream.ex @@ -61,7 +61,13 @@ defmodule Cachex.Actions.Stream do # we're starting! :"$start_of_table" -> - :ets.select(name, spec, batch) + case :ets.select(name, spec, batch) do + :"$end_of_table" -> + {:halt, nil} + + continuation -> + continuation + end # we're continuing! continuation ->