[tracker] libtracker-data: Pass "readonly" argument to TrackerDBInterface constructor



commit e4ca501a257ea01503e0c58bab4777360254279a
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Oct 23 13:14:16 2016 +0200

    libtracker-data: Pass "readonly" argument to TrackerDBInterface constructor
    
    Instead of having two almost identical call stacks to obtain rw/ro
    interfaces, put it all together in one with a readonly argument.

 src/libtracker-data/tracker-db-interface-sqlite.c |   24 +------
 src/libtracker-data/tracker-db-interface-sqlite.h |    3 +-
 src/libtracker-data/tracker-db-manager.c          |   85 +++++----------------
 3 files changed, 23 insertions(+), 89 deletions(-)
---
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index 2074ffb..62fcea5 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -2299,6 +2299,7 @@ tracker_db_interface_execute_vquery (TrackerDBInterface  *db_interface,
 
 TrackerDBInterface *
 tracker_db_interface_sqlite_new (const gchar  *filename,
+                                 gboolean      readonly,
                                  GError      **error)
 {
        TrackerDBInterface *object;
@@ -2308,28 +2309,7 @@ tracker_db_interface_sqlite_new (const gchar  *filename,
                                 NULL,
                                 &internal_error,
                                 "filename", filename,
-                                NULL);
-
-       if (internal_error) {
-               g_propagate_error (error, internal_error);
-               return NULL;
-       }
-
-       return object;
-}
-
-TrackerDBInterface *
-tracker_db_interface_sqlite_new_ro (const gchar  *filename,
-                                    GError      **error)
-{
-       TrackerDBInterface *object;
-       GError *internal_error = NULL;
-
-       object = g_initable_new (TRACKER_TYPE_DB_INTERFACE,
-                                NULL,
-                                &internal_error,
-                                "filename", filename,
-                                "read-only", TRUE,
+                                "read-only", !!readonly,
                                 NULL);
 
        if (internal_error) {
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.h 
b/src/libtracker-data/tracker-db-interface-sqlite.h
index a7e265e..a612296 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.h
+++ b/src/libtracker-data/tracker-db-interface-sqlite.h
@@ -35,8 +35,7 @@ G_BEGIN_DECLS
 typedef void (*TrackerDBWalCallback) (gint n_pages);
 
 TrackerDBInterface *tracker_db_interface_sqlite_new                    (const gchar              *filename,
-                                                                        GError                  **error);
-TrackerDBInterface *tracker_db_interface_sqlite_new_ro                 (const gchar              *filename,
+                                                                        gboolean                  readonly,
                                                                         GError                  **error);
 gint64              tracker_db_interface_sqlite_get_last_insert_id     (TrackerDBInterface       *interface);
 void                tracker_db_interface_sqlite_enable_shared_cache    (void);
diff --git a/src/libtracker-data/tracker-db-manager.c b/src/libtracker-data/tracker-db-manager.c
index febaf06..44c2321 100644
--- a/src/libtracker-data/tracker-db-manager.c
+++ b/src/libtracker-data/tracker-db-manager.c
@@ -146,9 +146,9 @@ static gboolean            db_exec_no_reply                        (TrackerDBInt
 static TrackerDBInterface *db_interface_create                      (TrackerDB            db,
                                                                      GError             **error);
 static TrackerDBInterface *tracker_db_manager_get_db_interfaces     (GError             **error,
-                                                                     gint                 num, ...);
-static TrackerDBInterface *tracker_db_manager_get_db_interfaces_ro  (GError             **error,
-                                                                     gint                 num, ...);
+                                                                     gboolean             readonly,
+                                                                     gint                 num,
+                                                                     ...);
 static void                db_remove_locale_file                    (void);
 
 static gboolean              initialized;
@@ -323,7 +323,7 @@ db_interface_get (TrackerDB   type,
                   path,
                   db_type_to_string (type));
 
-       iface = tracker_db_interface_sqlite_new (path,
+       iface = tracker_db_interface_sqlite_new (path, FALSE,
                                                 &internal_error);
 
        if (internal_error) {
@@ -1197,14 +1197,12 @@ tracker_db_manager_init (TrackerDBManagerFlags   flags,
 
        initialized = TRUE;
 
+       resources_iface = tracker_db_manager_get_db_interfaces (&internal_error,
+                                                               (flags & TRACKER_DB_MANAGER_READONLY) != 0,
+                                                               1, TRACKER_DB_METADATA);
        if (flags & TRACKER_DB_MANAGER_READONLY) {
-               resources_iface = tracker_db_manager_get_db_interfaces_ro (&internal_error, 1,
-                                                                          TRACKER_DB_METADATA);
                /* libtracker-direct does not use per-thread interfaces */
                global_iface = resources_iface;
-       } else {
-               resources_iface = tracker_db_manager_get_db_interfaces (&internal_error, 1,
-                                                                       TRACKER_DB_METADATA);
        }
 
        if (internal_error) {
@@ -1213,7 +1211,8 @@ tracker_db_manager_init (TrackerDBManagerFlags   flags,
 
                        perform_recreate (first_time, &new_error);
                        if (!new_error) {
-                               resources_iface = tracker_db_manager_get_db_interfaces (&internal_error, 1,
+                               resources_iface = tracker_db_manager_get_db_interfaces (&internal_error,
+                                                                                       FALSE, 1,
                                                                                        TRACKER_DB_METADATA);
                        } else {
                                /* Most serious error is the recreate one here */
@@ -1365,8 +1364,10 @@ tracker_db_manager_get_file (TrackerDB db)
  * returns: (caller-owns): a database connection
  **/
 static TrackerDBInterface *
-tracker_db_manager_get_db_interfaces (GError **error,
-                                      gint num, ...)
+tracker_db_manager_get_db_interfaces (GError   **error,
+                                      gboolean   readonly,
+                                      gint       num,
+                                      ...)
 {
        gint n_args;
        va_list args;
@@ -1381,6 +1382,7 @@ tracker_db_manager_get_db_interfaces (GError **error,
 
                if (!connection) {
                        connection = tracker_db_interface_sqlite_new (dbs[db].abs_filename,
+                                                                     readonly,
                                                                      &internal_error);
 
                        if (internal_error) {
@@ -1416,57 +1418,6 @@ tracker_db_manager_get_db_interfaces (GError **error,
        return connection;
 }
 
-static TrackerDBInterface *
-tracker_db_manager_get_db_interfaces_ro (GError **error,
-                                         gint num, ...)
-{
-       gint n_args;
-       va_list args;
-       TrackerDBInterface *connection = NULL;
-       GError *internal_error = NULL;
-
-       g_return_val_if_fail (initialized != FALSE, NULL);
-
-       va_start (args, num);
-       for (n_args = 1; n_args <= num; n_args++) {
-               TrackerDB db = va_arg (args, TrackerDB);
-
-               if (!connection) {
-                       connection = tracker_db_interface_sqlite_new_ro (dbs[db].abs_filename,
-                                                                        &internal_error);
-
-                       if (internal_error) {
-                               g_propagate_error (error, internal_error);
-                               connection = NULL;
-                               goto end_on_error;
-                       }
-
-                       db_set_params (connection,
-                                      dbs[db].cache_size,
-                                      dbs[db].page_size,
-                                      &internal_error);
-
-                       if (internal_error) {
-                               g_propagate_error (error, internal_error);
-                               connection = NULL;
-                               goto end_on_error;
-                       }
-
-               } else {
-                       db_exec_no_reply (connection,
-                                         "ATTACH '%s' as '%s'",
-                                         dbs[db].abs_filename,
-                                         dbs[db].name);
-               }
-       }
-
-       end_on_error:
-
-       va_end (args);
-
-       return connection;
-}
-
 /**
  * tracker_db_manager_get_db_interface:
  *
@@ -1493,8 +1444,12 @@ tracker_db_manager_get_db_interface (void)
 
        /* Ensure the interface is there */
        if (!interface) {
-               interface = tracker_db_manager_get_db_interfaces (&internal_error, 1,
-                                                                 TRACKER_DB_METADATA);
+               TrackerDBManagerFlags flags;
+
+               flags = tracker_db_manager_get_flags (NULL, NULL);
+               interface = tracker_db_manager_get_db_interfaces (&internal_error,
+                                                                 (flags & TRACKER_DB_MANAGER_READONLY) != 0,
+                                                                 1, TRACKER_DB_METADATA);
 
                if (internal_error) {
                        g_critical ("Error opening database: %s", internal_error->message);


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