[libgweather/ebassi/parallel-tests: 1/6] Move GWeatherDb loading out of GWeatherLocation




commit 81b59319e976073ccbb29635d800a05b4d709a13
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Fri Nov 19 14:44:06 2021 +0000

    Move GWeatherDb loading out of GWeatherLocation
    
    GWeatherLocation just needs the locations database to be available, and
    we can reuse the API elsewhere.

 libgweather/gweather-location.c | 103 +---------------------------------
 libgweather/gweather-private.c  | 121 ++++++++++++++++++++++++++++++++++++++++
 libgweather/gweather-private.h  |   6 ++
 3 files changed, 128 insertions(+), 102 deletions(-)
---
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index d31bba87..9929a55b 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -158,107 +158,6 @@ location_ref_for_idx (GWeatherDb *db,
     return loc;
 }
 
-static GWeatherDb *world_db;
-
-void
-_gweather_location_reset_world (void)
-{
-    gsize i;
-
-    g_return_if_fail (world_db != NULL);
-
-    /* At this point, we had a leak if the caches are not completely empty. */
-    for (i = 0; i < world_db->locations->len; i++) {
-        if (G_UNLIKELY (g_ptr_array_index (world_db->locations, i) != NULL)) {
-            g_warning ("Location with index %li and name %s is still referenced!",
-                       i,
-                       gweather_location_get_name (g_ptr_array_index (world_db->locations, i)));
-            g_assert_not_reached ();
-        }
-    }
-    for (i = 0; i < world_db->timezones->len; i++) {
-        if (G_UNLIKELY (g_ptr_array_index (world_db->timezones, i) != NULL)) {
-            g_warning ("Timezone with index %li and tzid %s is still referenced!",
-                       i,
-                       gweather_timezone_get_tzid (g_ptr_array_index (world_db->timezones, i)));
-            g_assert_not_reached ();
-        }
-    }
-}
-
-static gpointer
-ensure_world (gpointer dummy G_GNUC_UNUSED)
-{
-    g_autoptr (GError) error = NULL;
-    g_autofree char *filename = NULL;
-    g_autoptr (GMappedFile) map;
-    const char *locations_path;
-    time_t now;
-    struct tm tm;
-    GWeatherDb *db = NULL;
-
-    locations_path = g_getenv ("LIBGWEATHER_LOCATIONS_PATH");
-    if (locations_path != NULL) {
-        filename = g_strdup (locations_path);
-        if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
-            g_warning ("User specified database %s does not exist", filename);
-            g_clear_pointer (&filename, g_free);
-        }
-    }
-
-    if (filename == NULL) {
-        filename = g_build_filename (GWEATHER_BIN_LOCATION_DIR, "Locations.bin", NULL);
-    }
-
-    map = g_mapped_file_new (filename, FALSE, &error);
-    if (map == NULL) {
-        g_critical ("Failed to open database %s: %s", filename, error->message);
-        return NULL;
-    }
-
-    db = g_new0 (GWeatherDb, 1);
-    db->world = db_world_from_data (g_mapped_file_get_contents (map), g_mapped_file_get_length (map));
-    /* This is GWthDB01 */
-    if (db_world_get_magic (db->world) != 0x5747687442443130) {
-        g_free (db);
-        return NULL;
-    }
-
-    db->map = g_steal_pointer (&map);
-
-    db->locations_ref = db_world_get_locations (db->world);
-    db->timezones_ref = db_world_get_timezones (db->world);
-
-    db->locations = g_ptr_array_new ();
-    db->timezones = g_ptr_array_new ();
-
-    g_ptr_array_set_size (db->locations, db_arrayof_location_get_length (db->locations_ref));
-    g_ptr_array_set_size (db->timezones, db_world_timezones_get_length (db->timezones_ref));
-
-    /* Get timestamps for the start and end of this year.
-     * This is used to parse timezone information. */
-    now = time (NULL);
-    tm = *gmtime (&now);
-    tm.tm_mon = 0;
-    tm.tm_mday = 1;
-    tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
-    db->year_start = mktime (&tm);
-    tm.tm_year++;
-    db->year_end = mktime (&tm);
-
-    world_db = db;
-
-    return db;
-}
-
-static void
-gweather_location_ensure_world (void)
-{
-    static GOnce ensure_world_once = G_ONCE_INIT;
-
-    g_once (&ensure_world_once, ensure_world, NULL);
-}
-
 /**
  * gweather_location_get_world:
  *
@@ -275,7 +174,7 @@ gweather_location_get_world (void)
 {
     gweather_location_ensure_world ();
 
-    return location_ref_for_idx (world_db, 0, NULL);
+    return location_ref_for_idx (gweather_get_db (), 0, NULL);
 }
 
 /**
diff --git a/libgweather/gweather-private.c b/libgweather/gweather-private.c
index 16bc49e0..bf5087ca 100644
--- a/libgweather/gweather-private.c
+++ b/libgweather/gweather-private.c
@@ -11,6 +11,127 @@
 /* sign, 3 digits, separator, 4 decimals, nul-char */
 #define DEGREES_STR_SIZE (1 + 3 + 1 + 4 + 1)
 
+static GWeatherDb *world_db;
+
+void
+_gweather_location_reset_world (void)
+{
+    gsize i;
+
+    g_return_if_fail (world_db != NULL);
+
+    /* At this point, we had a leak if the caches are not completely empty. */
+    for (i = 0; i < world_db->locations->len; i++) {
+        if (G_UNLIKELY (g_ptr_array_index (world_db->locations, i) != NULL)) {
+            g_warning ("Location with index %li and name %s is still referenced!",
+                       i,
+                       gweather_location_get_name (g_ptr_array_index (world_db->locations, i)));
+            g_assert_not_reached ();
+        }
+    }
+    for (i = 0; i < world_db->timezones->len; i++) {
+        if (G_UNLIKELY (g_ptr_array_index (world_db->timezones, i) != NULL)) {
+            g_warning ("Timezone with index %li and tzid %s is still referenced!",
+                       i,
+                       gweather_timezone_get_tzid (g_ptr_array_index (world_db->timezones, i)));
+            g_assert_not_reached ();
+        }
+    }
+}
+
+static gpointer
+ensure_world (gpointer dummy G_GNUC_UNUSED)
+{
+    g_autoptr (GError) error = NULL;
+    g_autofree char *filename = NULL;
+    g_autoptr (GMappedFile) map;
+    const char *locations_path;
+    time_t now;
+    struct tm tm;
+    GWeatherDb *db = NULL;
+
+    locations_path = g_getenv ("LIBGWEATHER_LOCATIONS_PATH");
+    if (locations_path != NULL) {
+        filename = g_strdup (locations_path);
+        if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
+            g_warning ("User specified database %s does not exist", filename);
+            g_clear_pointer (&filename, g_free);
+        }
+    }
+
+    if (filename == NULL) {
+        filename = g_build_filename (GWEATHER_BIN_LOCATION_DIR, "Locations.bin", NULL);
+    }
+
+    map = g_mapped_file_new (filename, FALSE, &error);
+    if (map == NULL) {
+        g_critical ("Failed to open database %s: %s", filename, error->message);
+        return NULL;
+    }
+
+    db = g_new0 (GWeatherDb, 1);
+    db->world = db_world_from_data (g_mapped_file_get_contents (map), g_mapped_file_get_length (map));
+    /* This is GWthDB01 */
+    if (db_world_get_magic (db->world) != 0x5747687442443130) {
+        g_free (db);
+        return NULL;
+    }
+
+    db->map = g_steal_pointer (&map);
+
+    db->locations_ref = db_world_get_locations (db->world);
+    db->timezones_ref = db_world_get_timezones (db->world);
+
+    db->locations = g_ptr_array_new ();
+    db->timezones = g_ptr_array_new ();
+
+    g_ptr_array_set_size (db->locations, db_arrayof_location_get_length (db->locations_ref));
+    g_ptr_array_set_size (db->timezones, db_world_timezones_get_length (db->timezones_ref));
+
+    /* Get timestamps for the start and end of this year.
+     * This is used to parse timezone information. */
+    now = time (NULL);
+    tm = *gmtime (&now);
+    tm.tm_mon = 0;
+    tm.tm_mday = 1;
+    tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+    db->year_start = mktime (&tm);
+    tm.tm_year++;
+    db->year_end = mktime (&tm);
+
+    world_db = db;
+
+    return db;
+}
+
+/*< private >
+ * gweather_location_ensure_world:
+ *
+ * Ensures that the locations database is available.
+ */
+void
+gweather_location_ensure_world (void)
+{
+    static GOnce ensure_world_once = G_ONCE_INIT;
+
+    g_once (&ensure_world_once, ensure_world, NULL);
+}
+
+/*< private >
+ * gweather_get_db:
+ *
+ * Retrieves a pointer to the locations database.
+ *
+ * Returns: (transfer none): the root of the locations database
+ */
+GWeatherDb *
+gweather_get_db (void)
+{
+    gweather_location_ensure_world ();
+
+    return world_db;
+}
+
 char *
 _radians_to_degrees_str (gdouble radians)
 {
diff --git a/libgweather/gweather-private.h b/libgweather/gweather-private.h
index 282112d7..e129611a 100644
--- a/libgweather/gweather-private.h
+++ b/libgweather/gweather-private.h
@@ -99,6 +99,12 @@ GWeatherTimezone *
 _gweather_timezone_ref_for_idx (GWeatherDb *db,
                                 guint idx);
 
+void
+gweather_location_ensure_world (void);
+
+GWeatherDb *
+gweather_get_db (void);
+
 /*
  * Weather information.
  */


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