[gnome-software] Only tokenize the search string once



commit 66c137c6b7bde5331fb0e0b3c76938c15099ecb9
Author: Richard Hughes <richard hughsie com>
Date:   Thu Jun 22 13:31:38 2017 +0100

    Only tokenize the search string once
    
    This is partially for efficiency, and also so we can return for an invalid
    search before we even create any thread.
    
    Original patch by Iain Lane <iain orangesquash org uk>, many thanks.

 lib/gs-plugin-loader.c       |   19 ++++++++++++++++---
 plugins/dummy/gs-self-test.c |   20 ++++++++++++++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)
---
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index 34cbad6..b21af1a 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -183,6 +183,7 @@ typedef struct {
        GPtrArray                       *catlist;
        GsPluginJob                     *plugin_job;
        gboolean                         anything_ran;
+       gchar                           **tokens;
 } GsPluginLoaderHelper;
 
 static GsPluginLoaderHelper *
@@ -204,6 +205,7 @@ gs_plugin_loader_helper_free (GsPluginLoaderHelper *helper)
                g_object_unref (helper->plugin_job);
        if (helper->catlist != NULL)
                g_ptr_array_unref (helper->catlist);
+       g_strfreev (helper->tokens);
        g_slice_free (GsPluginLoaderHelper, helper);
 }
 
@@ -614,9 +616,7 @@ gs_plugin_loader_call_vfunc (GsPluginLoaderHelper *helper,
        case GS_PLUGIN_ACTION_SEARCH:
                {
                        GsPluginSearchFunc plugin_func = func;
-                       g_auto(GStrv) tokens = NULL;
-                       tokens = as_utils_search_tokenize (gs_plugin_job_get_search (helper->plugin_job));
-                       ret = plugin_func (plugin, tokens, list,
+                       ret = plugin_func (plugin, helper->tokens, list,
                                           cancellable, &error_local);
                }
                break;
@@ -3395,6 +3395,19 @@ gs_plugin_loader_job_process_async (GsPluginLoader *plugin_loader,
        g_task_set_task_data (task, helper, (GDestroyNotify) gs_plugin_loader_helper_free);
        gs_plugin_loader_job_debug (helper);
 
+       /* pre-tokenize search */
+       if (action == GS_PLUGIN_ACTION_SEARCH) {
+               const gchar *search = gs_plugin_job_get_search (plugin_job);
+               helper->tokens = as_utils_search_tokenize (search);
+               if (helper->tokens == NULL) {
+                       g_task_return_new_error (task,
+                                                GS_PLUGIN_ERROR,
+                                                GS_PLUGIN_ERROR_NOT_SUPPORTED,
+                                                "failed to tokenize %s", search);
+                       return;
+               }
+       }
+
        /* run in a thread */
        g_task_run_in_thread (task, gs_plugin_loader_process_thread_cb);
 }
diff --git a/plugins/dummy/gs-self-test.c b/plugins/dummy/gs-self-test.c
index c8071dc..56dd3a5 100644
--- a/plugins/dummy/gs-self-test.c
+++ b/plugins/dummy/gs-self-test.c
@@ -371,6 +371,23 @@ gs_plugins_dummy_search_func (GsPluginLoader *plugin_loader)
 }
 
 static void
+gs_plugins_dummy_search_invalid_func (GsPluginLoader *plugin_loader)
+{
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsAppList) list = NULL;
+       g_autoptr(GsPluginJob) plugin_job = NULL;
+
+       /* get search result based on addon keyword */
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_SEARCH,
+                                        "search", "X",
+                                        NULL);
+       list = gs_plugin_loader_job_process (plugin_loader, plugin_job, NULL, &error);
+       gs_test_flush_main_context ();
+       g_assert_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_NOT_SUPPORTED);
+       g_assert (list == NULL);
+}
+
+static void
 gs_plugins_dummy_url_to_app_func (GsPluginLoader *plugin_loader)
 {
        g_autoptr(GError) error = NULL;
@@ -688,6 +705,9 @@ main (int argc, char **argv)
        g_test_add_data_func ("/gnome-software/plugins/dummy/search",
                              plugin_loader,
                              (GTestDataFunc) gs_plugins_dummy_search_func);
+       g_test_add_data_func ("/gnome-software/plugins/dummy/search{invalid}",
+                             plugin_loader,
+                             (GTestDataFunc) gs_plugins_dummy_search_invalid_func);
        g_test_add_data_func ("/gnome-software/plugins/dummy/url-to-app",
                              plugin_loader,
                              (GTestDataFunc) gs_plugins_dummy_url_to_app_func);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]