diff --git a/Userland/Applications/Assistant/Providers.cpp b/Userland/Applications/Assistant/Providers.cpp index 0e679beae7..e2382697b6 100644 --- a/Userland/Applications/Assistant/Providers.cpp +++ b/Userland/Applications/Assistant/Providers.cpp @@ -126,13 +126,13 @@ void FileProvider::query(DeprecatedString const& query, Functioncancel(); - m_fuzzy_match_work = Threading::BackgroundAction>::construct( - [this, query](auto& task) { + m_fuzzy_match_work = Threading::BackgroundAction>>::construct( + [this, query](auto& task) -> Optional> { NonnullRefPtrVector results; for (auto& path : m_full_path_cache) { if (task.is_cancelled()) - return results; + return {}; auto match_result = fuzzy_match(query, path); if (!match_result.matched) @@ -145,7 +145,9 @@ void FileProvider::query(DeprecatedString const& query, Function ErrorOr { - on_complete(move(results)); + if (results.has_value()) + on_complete(move(results.value())); + return {}; }); } diff --git a/Userland/Applications/Assistant/Providers.h b/Userland/Applications/Assistant/Providers.h index 4525403ba7..4e44d4169b 100644 --- a/Userland/Applications/Assistant/Providers.h +++ b/Userland/Applications/Assistant/Providers.h @@ -153,7 +153,7 @@ public: void build_filesystem_cache(); private: - RefPtr>> m_fuzzy_match_work; + RefPtr>>> m_fuzzy_match_work; bool m_building_cache { false }; Vector m_full_path_cache; Queue m_work_queue; diff --git a/Userland/Applications/Assistant/main.cpp b/Userland/Applications/Assistant/main.cpp index 4be33eaaaa..d2b4ea61ab 100644 --- a/Userland/Applications/Assistant/main.cpp +++ b/Userland/Applications/Assistant/main.cpp @@ -1,11 +1,12 @@ /* * Copyright (c) 2021, Spencer Dixon - * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022-2023, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include "Providers.h" +#include #include #include #include @@ -73,46 +74,44 @@ class ResultRow final : public GUI::Button { RefPtr m_context_menu; }; +template class Database { public: - explicit Database(AppState& state) + explicit Database(AppState& state, Array, ProviderCount>& providers) : m_state(state) + , m_providers(providers) { - m_providers.append(make_ref_counted()); - m_providers.append(make_ref_counted()); - m_providers.append(make_ref_counted()); - m_providers.append(make_ref_counted()); - m_providers.append(make_ref_counted()); } Function)> on_new_results; void search(DeprecatedString const& query) { - for (auto& provider : m_providers) { - provider.query(query, [=, this](auto results) { - did_receive_results(query, results); - }); + auto should_display_precached_results = false; + for (size_t i = 0; i < ProviderCount; ++i) { + auto& result_array = m_result_cache.ensure(query); + if (result_array.at(i) == nullptr) { + m_providers[i]->query(query, [this, query, i](auto results) { + { + Threading::MutexLocker db_locker(m_mutex); + auto& result_array = m_result_cache.ensure(query); + if (result_array.at(i) != nullptr) + return; + result_array[i] = make>(results); + } + on_result_cache_updated(); + }); + } else { + should_display_precached_results = true; + } } + if (should_display_precached_results) + on_result_cache_updated(); } private: - void did_receive_results(DeprecatedString const& query, NonnullRefPtrVector const& results) + void on_result_cache_updated() { - { - Threading::MutexLocker db_locker(m_mutex); - auto& cache_entry = m_result_cache.ensure(query); - - for (auto& result : results) { - auto found = cache_entry.find_if([&result](auto& other) { - return result.equals(other); - }); - - if (found.is_end()) - cache_entry.append(result); - } - } - Threading::MutexLocker state_locker(m_state.lock); auto new_results = m_result_cache.find(m_state.last_query); if (new_results == m_result_cache.end()) @@ -121,21 +120,28 @@ private: // NonnullRefPtrVector will provide dual_pivot_quick_sort references rather than pointers, // and dual_pivot_quick_sort requires being able to construct the underlying type on the // stack. Assistant::Result is pure virtual, thus cannot be constructed on the stack. - auto& sortable_results = static_cast>&>(new_results->value); + Vector> all_results; + for (auto const& results_for_provider : new_results->value) { + if (results_for_provider == nullptr) + continue; + for (auto const& result : *results_for_provider) { + all_results.append(result); + } + } - dual_pivot_quick_sort(sortable_results, 0, static_cast(sortable_results.size() - 1), [](auto& a, auto& b) { + dual_pivot_quick_sort(all_results, 0, static_cast(all_results.size() - 1), [](auto& a, auto& b) { return a->score() > b->score(); }); - on_new_results(new_results->value); + on_new_results(all_results); } AppState& m_state; - NonnullRefPtrVector m_providers; + Array, ProviderCount> m_providers; Threading::Mutex m_mutex; - HashMap> m_result_cache; + HashMap>, ProviderCount>> m_result_cache; }; } @@ -163,7 +169,14 @@ ErrorOr serenity_main(Main::Arguments arguments) window->set_minimizable(false); Assistant::AppState app_state; - Assistant::Database db { app_state }; + Array, 5> providers = { + make_ref_counted(), + make_ref_counted(), + make_ref_counted(), + make_ref_counted(), + make_ref_counted() + }; + Assistant::Database db { app_state, providers }; auto container = TRY(window->set_main_widget()); container->set_fill_with_background_color(true);