diff options
author | Mark Felder <feld@FreeBSD.org> | 2019-07-15 17:10:27 -0500 |
---|---|---|
committer | Mark Felder <feld@FreeBSD.org> | 2019-07-15 17:10:27 -0500 |
commit | ffb4eb9779ddd28ecee84c06e8dc58f4a4daaa38 (patch) | |
tree | b397d1192c69a7d089c86d41b6e09e89954ea798 /lib/pleroma/repo_streamer.ex | |
parent | e912f81c828cc7e1d2c0dff8daed3ad52f407a61 (diff) | |
parent | 03bcb40883dafa2886110e2b625c4cc5c21106f1 (diff) | |
download | pleroma-ffb4eb9779ddd28ecee84c06e8dc58f4a4daaa38.tar.gz |
Merge branch 'develop' into feature/matstodon-statuses-by-name
Diffstat (limited to 'lib/pleroma/repo_streamer.ex')
-rw-r--r-- | lib/pleroma/repo_streamer.ex | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/pleroma/repo_streamer.ex b/lib/pleroma/repo_streamer.ex new file mode 100644 index 000000000..a4b71a1bb --- /dev/null +++ b/lib/pleroma/repo_streamer.ex @@ -0,0 +1,34 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.RepoStreamer do + alias Pleroma.Repo + import Ecto.Query + + def chunk_stream(query, chunk_size) do + Stream.unfold(0, fn + :halt -> + {[], :halt} + + last_id -> + query + |> order_by(asc: :id) + |> where([r], r.id > ^last_id) + |> limit(^chunk_size) + |> Repo.all() + |> case do + [] -> + {[], :halt} + + records -> + last_id = List.last(records).id + {records, last_id} + end + end) + |> Stream.take_while(fn + [] -> false + _ -> true + end) + end +end |