Commit cb9373cf authored by Matthias Klumpp's avatar Matthias Klumpp
Browse files

Try to reuse initial temporary cache when loading data into pool

Previously the cache created in an AsPool constructor would always get
thrown away immediately when load() was called, which is very common.
Now we try to reuse it and only drop it if actually necessary.
parent 14ff2084
......@@ -579,23 +579,38 @@ as_pool_clear2 (AsPool *pool, GError **error)
AsPoolPrivate *priv = GET_PRIVATE (pool);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex);
/* close & delete session cache */
as_cache_close (priv->cache);
if (g_file_test (priv->cache_fname, G_FILE_TEST_EXISTS)) {
if (g_remove (priv->cache_fname) != 0) {
g_set_error_literal (error,
AS_POOL_ERROR,
AS_POOL_ERROR_OLD_CACHE,
_("Unable to remove old cache."));
return FALSE;
}
}
/* close system cache so it won't be used anymore */
/* close system cache so it won't be used anymore
* (will be loaded explicitly again later, when needed) */
as_cache_close (priv->system_cache);
/* reopen the session cache as a new, pristine one */
return as_cache_open (priv->cache, priv->cache_fname, priv->locale, error);
/* if we were just created, we may be able to reuse the current temporary cache,
* instead of creating a new one (which is a bit wasteful).
* Reuse requires a temporary cache with no elements. */
if ((g_strcmp0 (priv->cache_fname, ":temporary") == 0) &&
(as_cache_count_components (priv->cache, NULL) == 0)) {
/* we can reuse the user cache */
g_debug ("Not clearing user cache: The cache was already empty.");
return TRUE;
} else {
/* it looks like we can not reuse the old cache, so now we need to clear
* the cache for real by deleting the old one and creating a new one */
g_debug ("Clearing user cache.");
as_cache_close (priv->cache);
if (g_file_test (priv->cache_fname, G_FILE_TEST_EXISTS)) {
if (g_remove (priv->cache_fname) != 0) {
g_set_error_literal (error,
AS_POOL_ERROR,
AS_POOL_ERROR_OLD_CACHE,
_("Unable to remove old cache."));
return FALSE;
}
}
/* reopen the session cache as a new, pristine one */
return as_cache_open (priv->cache, priv->cache_fname, priv->locale, error);
}
}
/**
......
......@@ -106,7 +106,7 @@ as_assert_component_lists_equal (GPtrArray *cpts_a, GPtrArray *cpts_b)
guint i;
g_autofree gchar *cpts_a_xml = NULL;
g_autofree gchar *cpts_b_xml = NULL;
GError *error = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(AsMetadata) metad = as_metadata_new ();
/* sort */
......@@ -523,7 +523,7 @@ test_merge_components ()
GPtrArray *suggestions;
AsSuggested *suggested;
GPtrArray *cpt_ids;
GError *error = NULL;
g_autoptr(GError) error = NULL;
/* load the data pool with sample data */
dpool = test_get_sampledata_pool (FALSE);
......@@ -583,6 +583,53 @@ test_search_stemming ()
g_free (tmp);
}
/**
* test_pool_empty:
*
* Test if working on a fresh, empty pool works.
*/
static void
test_pool_empty ()
{
g_autoptr(AsPool) pool = NULL;
g_autoptr(GPtrArray) result = NULL;
g_autoptr(GError) error = NULL;
AsComponent *cpt = NULL;
gboolean ret;
pool = as_pool_new ();
as_pool_clear_metadata_locations (pool);
as_pool_set_locale (pool, "C");
/* test reading from the pool when it wasn't loaded yet */
result = as_pool_get_components_by_id (pool, "org.example.NotThere");
g_assert_cmpint (result->len, ==, 0);
g_clear_pointer (&result, g_ptr_array_unref);
result = as_pool_search (pool, "web");
g_assert_cmpint (result->len, ==, 0);
g_clear_pointer (&result, g_ptr_array_unref);
/* create dummy app to add */
cpt = as_component_new ();
as_component_set_kind (cpt, AS_COMPONENT_KIND_DESKTOP_APP);
as_component_set_id (cpt, "org.freedesktop.FooBar");
as_component_set_name (cpt, "A fooish bar", "C");
as_component_set_summary (cpt, "Foo the bar.", "C");
ret = as_pool_add_component (pool, cpt, &error);
g_object_unref (cpt);
g_assert_no_error (error);
g_assert (ret);
/* try to retrieve the dummy component */
result = as_pool_search (pool, "foo");
g_assert_cmpint (result->len, ==, 1);
cpt = AS_COMPONENT (g_ptr_array_index (result, 0));
g_assert_cmpstr (as_component_get_id (cpt), ==, "org.freedesktop.FooBar");
g_clear_pointer (&result, g_ptr_array_unref);
}
/**
* main:
*/
......@@ -609,6 +656,7 @@ main (int argc, char **argv)
g_test_add_func ("/AppStream/PoolRead", test_pool_read);
g_test_add_func ("/AppStream/PoolReadAsync", test_pool_read_async);
g_test_add_func ("/AppStream/PoolEmpty", test_pool_empty);
g_test_add_func ("/AppStream/Cache", test_cache);
g_test_add_func ("/AppStream/Merges", test_merge_components);
......
Supports Markdown
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