Commit 2c8ce4f6 authored by Fabio Pelosin's avatar Fabio Pelosin

[SourcesManager] Improve search performance

parent 3fe56ec1
...@@ -59,18 +59,21 @@ module Pod ...@@ -59,18 +59,21 @@ module Pod
# @return [Array<Set>] The sets that contain the search term. # @return [Array<Set>] The sets that contain the search term.
# #
def search_by_name(query, full_text_search = false) def search_by_name(query, full_text_search = false)
set_names = [] if full_text_search
updated_search_index.each do |name, set_data| set_names = []
text = name.dup updated_search_index.each do |name, set_data|
if full_text_search text = name.dup
text << set_data['authors'].to_s if set_data['authors'] if full_text_search
text << set_data['summary'] if set_data['summary'] text << set_data['authors'].to_s if set_data['authors']
text << set_data['description'] if set_data['description'] text << set_data['summary'] if set_data['summary']
text << set_data['description'] if set_data['description']
end
set_names << name if text.downcase.include?(query.downcase)
end end
set_names << name if text.downcase.include?(query.downcase) sets = set_names.sort.map { |name| aggregate.represenative_set(name) }
else
sets = aggregate.search_by_name(query, false)
end end
sets = set_names.sort.map { |name| aggregate.represenative_set(name) }
if sets.empty? if sets.empty?
extra = ", author, summary, or description" if full_text_search extra = ", author, summary, or description" if full_text_search
raise Informative, "Unable to find a pod with name#{extra} matching `#{query}`" raise Informative, "Unable to find a pod with name#{extra} matching `#{query}`"
...@@ -86,22 +89,28 @@ module Pod ...@@ -86,22 +89,28 @@ module Pod
# - description # - description
# - authors # - authors
# #
# @note This operation is fairly expensive, because of the YAML
# conversion.
#
# @return [Hash{String => String}] The up to date search data. # @return [Hash{String => String}] The up to date search data.
# #
def updated_search_index def updated_search_index
if search_index_path.exist? unless @updated_search_index
stored_index = YAML.load(search_index_path.read) if search_index_path.exist?
if stored_index && stored_index.is_a?(Hash) stored_index = YAML.load(search_index_path.read)
search_index = aggregate.update_search_index(stored_index) if stored_index && stored_index.is_a?(Hash)
search_index = aggregate.update_search_index(stored_index)
else
search_index = aggregate.generate_search_index
end
else else
search_index = aggregate.generate_search_index search_index = aggregate.generate_search_index
end end
else
search_index = aggregate.generate_search_index
end
File.open(search_index_path, 'w') {|f| f.write(search_index.to_yaml) } File.open(search_index_path, 'w') {|f| f.write(search_index.to_yaml) }
search_index @updated_search_index = search_index
end
@updated_search_index
end end
# @return [Pathname] The path where the search index should be stored. # @return [Pathname] The path where the search index should be stored.
......
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