From 991cab61bf270c09b0a23d92b6d2a7ad38d50676 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 22 Dec 2019 22:13:36 -0500 Subject: [PATCH] Revert "more aggressive preloading" This reverts commit 556cd39afd9a997980ec602edeb8087959764461. --- lib/philomena/textile/renderer.ex | 18 +++++++----------- lib/philomena_web/comment_loader.ex | 9 --------- .../controllers/image_controller.ex | 8 ++------ lib/philomena_web/image_loader.ex | 8 +------- lib/philomena_web/plugs/advert_plug.ex | 2 +- 5 files changed, 11 insertions(+), 34 deletions(-) diff --git a/lib/philomena/textile/renderer.ex b/lib/philomena/textile/renderer.ex index c3d9fcf8..850cea46 100644 --- a/lib/philomena/textile/renderer.ex +++ b/lib/philomena/textile/renderer.ex @@ -101,21 +101,17 @@ defmodule Philomena.Textile.Renderer do end defp find_images(text_segments) do - text_segments - |> Enum.flat_map(fn t -> - Regex.scan(~r|>>(\d+)|, t, capture: :all_but_first) - |> Enum.map(fn [first] -> String.to_integer(first) end) - end) - |> find_image_ids() - end + image_ids = + text_segments + |> Enum.flat_map(fn t -> + Regex.scan(~r|>>(\d+)|, t, capture: :all_but_first) + |> Enum.map(fn [first] -> String.to_integer(first) end) + end) - defp find_image_ids([]), do: %{} - defp find_image_ids(image_ids) do Image |> where([i], i.id in ^image_ids) |> where([i], i.hidden_from_users == false) - |> join(:left, [i], _ in assoc(i, :tags)) - |> preload([_i, t], tags: t) + |> preload(:tags) |> Repo.all() |> Map.new(fn image -> {image.id, image} end) end diff --git a/lib/philomena_web/comment_loader.ex b/lib/philomena_web/comment_loader.ex index df034ef5..542d9461 100644 --- a/lib/philomena_web/comment_loader.ex +++ b/lib/philomena_web/comment_loader.ex @@ -3,15 +3,6 @@ defmodule PhilomenaWeb.CommentLoader do alias Philomena.Repo import Ecto.Query - # More aggressive preloading skipped due to overhead - # from scrivener: - # - # |> join(:left, [c], _ in assoc(c, :image)) - # |> join(:left, [c, _i], _ in assoc(c, :user)) - # |> join(:left, [_c, _i, u], _ in assoc(u, :awards)) - # |> join(:left, [_c, _i, _u, a], _ in assoc(a, :badge)) - # |> preload([_c, i, u, a, b], [:deleted_by, image: i, user: {u, awards: {a, badge: b}}]) - def load_comments(conn, image) do pref = load_direction(conn.assigns.current_user) diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 097eab42..74eb8282 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -147,12 +147,8 @@ defmodule PhilomenaWeb.ImageController do |> where(id: ^id) |> join(:inner_lateral, [i], _ in fragment("SELECT COUNT(*) FROM tag_changes t WHERE t.image_id = ?", i.id)) |> join(:inner_lateral, [i, _], _ in fragment("SELECT COUNT(*) FROM source_changes s WHERE s.image_id = ?", i.id)) - |> join(:left, [i, _tc, _sc], _ in assoc(i, :user)) - |> join(:left, [_i, _tc, _sc, u], _ in assoc(u, :awards)) - |> join(:left, [_i, _tc, _sc, _u, a], _ in assoc(a, :badge)) - |> join(:left, [i, _tc, _sc, _u, _a, _b], _ in assoc(i, :tags)) - |> preload([_i, _tc, _sc, u, a, b, t], [:deleter, tags: t, user: {u, awards: {a, badge: b}}]) - |> select([i, tc, sc, _u], {i, tc.count, sc.count}) + |> preload([:tags, :deleter, user: [awards: :badge]]) + |> select([i, t, s], {i, t.count, s.count}) |> Repo.one() |> case do nil -> diff --git a/lib/philomena_web/image_loader.ex b/lib/philomena_web/image_loader.ex index d8cf1211..6b4125a8 100644 --- a/lib/philomena_web/image_loader.ex +++ b/lib/philomena_web/image_loader.ex @@ -20,7 +20,7 @@ defmodule PhilomenaWeb.ImageLoader do sort_queries = Keyword.get(options, :queries, []) sort_sorts = Keyword.get(options, :sorts, [%{created_at: :desc}]) pagination = Keyword.get(options, :pagination, conn.assigns.image_pagination) - queryable = Keyword.get(options, :queryable, default_queryable()) + queryable = Keyword.get(options, :queryable, Image |> preload(:tags)) user = conn.assigns.current_user filter = conn.assigns.compiled_filter @@ -126,10 +126,4 @@ defmodule PhilomenaWeb.ImageLoader do [{tag, description, dnp_entries}] end defp render_bodies(tags, _conn), do: tags - - defp default_queryable do - Image - |> join(:left, [i], _ in assoc(i, :tags)) - |> preload([_i, t], tags: t) - end end diff --git a/lib/philomena_web/plugs/advert_plug.ex b/lib/philomena_web/plugs/advert_plug.ex index 0b864c2b..09246743 100644 --- a/lib/philomena_web/plugs/advert_plug.ex +++ b/lib/philomena_web/plugs/advert_plug.ex @@ -24,4 +24,4 @@ defmodule PhilomenaWeb.AdvertPlug do do: true defp show_ads?(_user), do: true -end +end \ No newline at end of file