Commit aa3fcd86 authored by Terri Chu's avatar Terri Chu Committed by Dylan Griffith

Resolve "Improve performance of Search API (Advanced): users scope"

parent d6628cd7
......@@ -82,16 +82,21 @@ class SearchService
end
def redact_unauthorized_results(results_collection)
results = results_collection.to_a
permitted_results = results.select { |object| visible_result?(object) }
redacted_results = results_collection.reject { |object| visible_result?(object) }
redacted_results = (results - permitted_results).each_with_object({}) do |object, memo|
memo[object.id] = { ability: :"read_#{object.to_ability_name}", id: object.id, class_name: object.class.name }
if redacted_results.any?
redacted_log = redacted_results.each_with_object({}) do |object, memo|
memo[object.id] = { ability: :"read_#{object.to_ability_name}", id: object.id, class_name: object.class.name }
end
log_redacted_search_results(redacted_log.values)
return results_collection.id_not_in(redacted_log.keys) if results_collection.is_a?(ActiveRecord::Relation)
end
log_redacted_search_results(redacted_results.values) if redacted_results.any?
return results_collection if results_collection.is_a?(ActiveRecord::Relation)
return results_collection.id_not_in(redacted_results.keys) if results_collection.is_a?(ActiveRecord::Relation)
permitted_results = results_collection - redacted_results
Kaminari.paginate_array(
permitted_results,
......
---
title: Reduce redundant queries for Search API users scope.
merge_request: 33795
author:
type: performance
......@@ -19,7 +19,13 @@ module Gitlab
private
def paginate_with_limit_optimization(relation)
pagination_data = relation.page(params[:page]).per(params[:per_page])
# do not paginate relation if it is already paginated
pagination_data = if relation.respond_to?(:current_page) && relation.current_page == params[:page] && relation.limit_value == params[:per_page]
relation
else
relation.page(params[:page]).per(params[:per_page])
end
return pagination_data unless pagination_data.is_a?(ActiveRecord::Relation)
return pagination_data unless Feature.enabled?(:api_kaminari_count_with_limit)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment