[gnome-software/1479-repositories-settings-optional-repos-can-t-be-disabled] gs-plugin-provenance: Use GHashTable to store list of repositories



commit c1bbcf66648ba1041b7c6eef8d1e8bb87ad23fe6
Author: Milan Crha <mcrha redhat com>
Date:   Tue Oct 5 11:03:31 2021 +0200

    gs-plugin-provenance: Use GHashTable to store list of repositories
    
    This helps with speed, when there are defined a lot of official
    repositories. Use of the GHashTable will help in the following commit.

 plugins/core/gs-plugin-provenance.c | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
---
diff --git a/plugins/core/gs-plugin-provenance.c b/plugins/core/gs-plugin-provenance.c
index 97ff76798..aed28ec9a 100644
--- a/plugins/core/gs-plugin-provenance.c
+++ b/plugins/core/gs-plugin-provenance.c
@@ -19,7 +19,7 @@
 
 struct GsPluginData {
        GSettings               *settings;
-       gchar                   **sources;
+       GHashTable              *repos; /* gchar *name ~> NULL */
 };
 
 static gchar **
@@ -42,8 +42,13 @@ gs_plugin_provenance_settings_changed_cb (GSettings *settings,
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
        if (g_strcmp0 (key, "official-repos") == 0) {
-               g_strfreev (priv->sources);
-               priv->sources = gs_plugin_provenance_get_sources (plugin);
+               /* The keys are stolen by the hash table, thus free only the array */
+               g_autofree gchar **repos = NULL;
+               g_hash_table_remove_all (priv->repos);
+               repos = gs_plugin_provenance_get_sources (plugin);
+               for (guint ii = 0; repos && repos[ii]; ii++) {
+                       g_hash_table_insert (priv->repos, g_steal_pointer (&(repos[ii])), NULL);
+               }
        }
 }
 
@@ -52,9 +57,10 @@ gs_plugin_initialize (GsPlugin *plugin)
 {
        GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData));
        priv->settings = g_settings_new ("org.gnome.software");
+       priv->repos = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
        g_signal_connect (priv->settings, "changed",
                          G_CALLBACK (gs_plugin_provenance_settings_changed_cb), plugin);
-       priv->sources = gs_plugin_provenance_get_sources (plugin);
+       gs_plugin_provenance_settings_changed_cb (priv->settings, "official-repos", plugin);
 
        /* after the package source is set */
        gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "dummy");
@@ -66,7 +72,7 @@ void
 gs_plugin_destroy (GsPlugin *plugin)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
-       g_strfreev (priv->sources);
+       g_hash_table_unref (priv->repos);
        g_object_unref (priv->settings);
 }
 
@@ -74,12 +80,11 @@ static gboolean
 refine_app (GsPlugin             *plugin,
            GsApp                *app,
            GsPluginRefineFlags   flags,
+           GHashTable           *repos,
            GCancellable         *cancellable,
            GError              **error)
 {
-       GsPluginData *priv = gs_plugin_get_data (plugin);
        const gchar *origin;
-       gchar **sources;
 
        /* not required */
        if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_PROVENANCE) == 0)
@@ -87,14 +92,9 @@ refine_app (GsPlugin             *plugin,
        if (gs_app_has_quirk (app, GS_APP_QUIRK_PROVENANCE))
                return TRUE;
 
-       /* nothing to search */
-       sources = priv->sources;
-       if (sources == NULL || sources[0] == NULL)
-               return TRUE;
-
        /* simple case */
        origin = gs_app_get_origin (app);
-       if (origin != NULL && gs_utils_strv_fnmatch (sources, origin)) {
+       if (origin != NULL && g_hash_table_contains (repos, origin)) {
                gs_app_add_quirk (app, GS_APP_QUIRK_PROVENANCE);
                return TRUE;
        }
@@ -103,7 +103,7 @@ refine_app (GsPlugin             *plugin,
         * provenance quirk to the system-configured repositories (but not
         * user-configured ones). */
        if (gs_app_get_kind (app) == AS_COMPONENT_KIND_REPOSITORY &&
-           gs_utils_strv_fnmatch (sources, gs_app_get_id (app))) {
+           g_hash_table_contains (repos, gs_app_get_id (app))) {
                if (gs_app_get_scope (app) != AS_COMPONENT_SCOPE_USER)
                        gs_app_add_quirk (app, GS_APP_QUIRK_PROVENANCE);
                return TRUE;
@@ -118,7 +118,7 @@ refine_app (GsPlugin             *plugin,
                return TRUE;
        if (g_str_has_prefix (origin + 1, "installed:"))
                origin += 10;
-       if (gs_utils_strv_fnmatch (sources, origin + 1)) {
+       if (g_hash_table_contains (repos, origin + 1)) {
                gs_app_add_quirk (app, GS_APP_QUIRK_PROVENANCE);
                return TRUE;
        }
@@ -133,17 +133,19 @@ gs_plugin_refine (GsPlugin             *plugin,
                  GError              **error)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
+       g_autoptr(GHashTable) repos = NULL;
 
        /* nothing to do here */
        if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_PROVENANCE) == 0)
                return TRUE;
+       repos = g_hash_table_ref (priv->repos);
        /* nothing to search */
-       if (priv->sources == NULL || priv->sources[0] == NULL)
+       if (g_hash_table_size (repos) == 0)
                return TRUE;
 
        for (guint i = 0; i < gs_app_list_length (list); i++) {
                GsApp *app = gs_app_list_index (list, i);
-               if (!refine_app (plugin, app, flags, cancellable, error))
+               if (!refine_app (plugin, app, flags, repos, cancellable, error))
                        return FALSE;
        }
 


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