[tracker/wip/carlosg/domain-ontologies: 48/124] libtracker-data: Use flags for ro/use_mutex in TrackerDBInterface



commit ef346a155344c9360883e06562ffd17fc95b4fad
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Jun 9 03:39:17 2017 +0200

    libtracker-data: Use flags for ro/use_mutex in TrackerDBInterface
    
    Caller is now responsible of setting the USE_MUTEX flag, instead of
    having the interface poke the DB manager.

 src/libtracker-data/Makefile.am                    |   10 ++++
 src/libtracker-data/meson.build                    |    8 +++
 .../tracker-data-enum-types.c.template             |   44 ++++++++++++++++
 .../tracker-data-enum-types.h.template             |   26 +++++++++
 src/libtracker-data/tracker-db-interface-sqlite.c  |   54 ++++++++------------
 src/libtracker-data/tracker-db-interface-sqlite.h  |    7 ++-
 src/libtracker-data/tracker-db-manager.c           |    9 +++-
 7 files changed, 124 insertions(+), 34 deletions(-)
---
diff --git a/src/libtracker-data/Makefile.am b/src/libtracker-data/Makefile.am
index a47fd84..539040c 100644
--- a/src/libtracker-data/Makefile.am
+++ b/src/libtracker-data/Makefile.am
@@ -19,9 +19,17 @@ AM_CPPFLAGS =                                          \
 libtracker_datadir = $(libdir)/tracker-$(TRACKER_API_VERSION)
 libtracker_data_LTLIBRARIES = libtracker-data.la
 
+tracker-data-enum-types.h: tracker-data-enum-types.h.template
+       $(AM_V_GEN) (cd $(srcdir) && $(GLIB_MKENUMS) --template tracker-data-enum-types.h.template 
tracker-db-interface-sqlite.h) > $@
+
+tracker-data-enum-types.c: tracker-data-enum-types.c.template
+       $(AM_V_GEN) (cd $(srcdir) && $(GLIB_MKENUMS) --template tracker-data-enum-types.c.template 
tracker-db-interface-sqlite.h) > $@
+
 libtracker_data_la_SOURCES =                           \
        $(top_builddir)/src/ontologies/tracker-gresources.c \
        $(top_builddir)/src/ontologies/tracker-gresources.h \
+       tracker-data-enum-types.c                      \
+       tracker-data-enum-types.h                      \
        tracker-vala-namespace.vala                    \
        tracker-sparql-expression.vala                 \
        tracker-sparql-pattern.vala                    \
@@ -91,6 +99,8 @@ gsettings_SCHEMAS = org.freedesktop.Tracker.DB.gschema.xml
 
 # Dist / Clean rules for all data
 BUILT_SOURCES = \
+       tracker-data-enum-types.c                     \
+       tracker-data-enum-types.h                     \
        libtracker_data_la_vala.stamp
 
 CLEANFILES = \
diff --git a/src/libtracker-data/meson.build b/src/libtracker-data/meson.build
index 81548e8..3230e82 100644
--- a/src/libtracker-data/meson.build
+++ b/src/libtracker-data/meson.build
@@ -25,6 +25,12 @@ libtracker_data_vala = static_library('tracker-sparql-query',
     include_directories: [commoninc, configinc, srcinc],
 )
 
+tracker_data_enums = gnome.mkenums('tracker-data-enum-types',
+    sources: 'tracker-db-interface-sqlite.h',
+    c_template: 'tracker-data-enum-types.c.template',
+    h_template: 'tracker-data-enum-types.h.template',
+)
+
 tracker_data_dependencies = [
     tracker_sparql_intermediate_dep, gvdb_dep, sqlite, uuid,
 ]
@@ -52,6 +58,8 @@ libtracker_data = library('tracker-data',
     'tracker-ontologies.c',
     'tracker-property.c',
     tracker_common_enum_header, tracker_common_parser_sha1_header,
+    tracker_data_enums[0],
+    tracker_data_enums[1],
     c_args: tracker_c_args,
     install: true,
     install_dir: tracker_internal_libs_dir,
diff --git a/src/libtracker-data/tracker-data-enum-types.c.template 
b/src/libtracker-data/tracker-data-enum-types.c.template
new file mode 100644
index 0000000..a1d89e4
--- /dev/null
+++ b/src/libtracker-data/tracker-data-enum-types.c.template
@@ -0,0 +1,44 @@
+/*** BEGIN file-header ***/
+#include <config.h>
+
+#include "tracker-data-enum-types.h"
+
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+/* enumerations from "@filename@" */
+#include "@filename@"
+/*** END file-production ***/
+
+
+/*** BEGIN value-header ***/
+GType
+@enum_name@_get_type (void)
+{
+  static volatile gsize g_define_type_id__volatile = 0;
+ 
+  if (g_once_init_enter (&g_define_type_id__volatile)) {
+    static const G@Type@Value values[] = {
+/*** END value-header ***/
+
+/*** BEGIN value-production ***/
+      { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
+/*** END value-production ***/
+
+/*** BEGIN value-tail ***/
+      { 0, NULL, NULL }
+    };
+    GType g_define_type_id = 
+       g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
+      
+    g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
+  }
+    
+  return g_define_type_id__volatile;
+}
+
+/*** END value-tail ***/
+
+/*** BEGIN file-tail ***/
+
+/*** END file-tail ***/
diff --git a/src/libtracker-data/tracker-data-enum-types.h.template 
b/src/libtracker-data/tracker-data-enum-types.h.template
new file mode 100644
index 0000000..ad55e7b
--- /dev/null
+++ b/src/libtracker-data/tracker-data-enum-types.h.template
@@ -0,0 +1,26 @@
+/*** BEGIN file-header ***/
+
+#ifndef __TRACKER_DATA_ENUM_TYPES_H__
+#define __TRACKER_DATA_ENUM_TYPES_H__
+
+#include <glib-object.h>
+#include "tracker-db-interface-sqlite.h"
+
+G_BEGIN_DECLS
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+
+/* enumerations from "@filename@" */
+/*** END file-production ***/
+
+/*** BEGIN value-header ***/
+GType @enum_name@_get_type (void) G_GNUC_CONST;
+#define TRACKER_TYPE_@ENUMSHORT@ (@enum_name@_get_type ())
+/*** END value-header ***/
+
+/*** BEGIN file-tail ***/
+G_END_DECLS
+
+#endif /* __TRACKER_ENUMS_TYPES_H__ */
+/*** END file-tail ***/
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index adee777..fd2c0d4 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -53,6 +53,7 @@
 
 #include "tracker-db-interface-sqlite.h"
 #include "tracker-db-manager.h"
+#include "tracker-data-enum-types.h"
 
 #define UNKNOWN_STATUS 0.5
 
@@ -83,7 +84,7 @@ struct TrackerDBInterface {
        /* Number of active cursors */
        gint n_active_cursors;
 
-       guint ro : 1;
+       guint flags;
        GCancellable *cancellable;
 
        TrackerDBStatementLru select_stmt_lru;
@@ -95,9 +96,8 @@ struct TrackerDBInterface {
 
        gchar *fts_properties;
 
-       /* Used if TRACKER_DB_MANAGER_ENABLE_MUTEXES is set */
+       /* Used if TRACKER_DB_INTERFACE_USE_MUTEX is set */
        GMutex mutex;
-       guint use_mutex;
 
        /* Wal */
        TrackerDBWalCallback wal_hook;
@@ -153,7 +153,7 @@ static gboolean            db_cursor_iter_next                      (TrackerDBCu
 enum {
        PROP_0,
        PROP_FILENAME,
-       PROP_RO
+       PROP_FLAGS
 };
 
 enum {
@@ -1436,14 +1436,14 @@ initialize_functions (TrackerDBInterface *db_interface)
 static inline void
 tracker_db_interface_lock (TrackerDBInterface *iface)
 {
-       if (iface->use_mutex)
+       if (iface->flags & TRACKER_DB_INTERFACE_USE_MUTEX)
                g_mutex_lock (&iface->mutex);
 }
 
 static inline void
 tracker_db_interface_unlock (TrackerDBInterface *iface)
 {
-       if (iface->use_mutex)
+       if (iface->flags & TRACKER_DB_INTERFACE_USE_MUTEX)
                g_mutex_unlock (&iface->mutex);
 }
 
@@ -1456,7 +1456,7 @@ open_database (TrackerDBInterface  *db_interface,
 
        g_assert (db_interface->filename != NULL);
 
-       if (!db_interface->ro) {
+       if ((db_interface->flags & TRACKER_DB_INTERFACE_READONLY) == 0) {
                mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
        } else {
                mode = SQLITE_OPEN_READONLY;
@@ -1525,8 +1525,8 @@ tracker_db_interface_sqlite_set_property (GObject       *object,
        db_iface = TRACKER_DB_INTERFACE (object);
 
        switch (prop_id) {
-       case PROP_RO:
-               db_iface->ro = g_value_get_boolean (value);
+       case PROP_FLAGS:
+               db_iface->flags = g_value_get_flags (value);
                break;
        case PROP_FILENAME:
                db_iface->filename = g_value_dup_string (value);
@@ -1547,8 +1547,8 @@ tracker_db_interface_sqlite_get_property (GObject    *object,
        db_iface = TRACKER_DB_INTERFACE (object);
 
        switch (prop_id) {
-       case PROP_RO:
-               g_value_set_boolean (value, db_iface->ro);
+       case PROP_FLAGS:
+               g_value_set_flags (value, db_iface->flags);
                break;
        case PROP_FILENAME:
                g_value_set_string (value, db_iface->filename);
@@ -1931,32 +1931,22 @@ tracker_db_interface_class_init (TrackerDBInterfaceClass *class)
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
        g_object_class_install_property (object_class,
-                                        PROP_RO,
-                                        g_param_spec_boolean ("read-only",
-                                                              "Read only",
-                                                              "Whether the connection is read only",
-                                                              FALSE,
-                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+                                        PROP_FLAGS,
+                                        g_param_spec_flags ("flags",
+                                                            "Flags",
+                                                            "Interface flags",
+                                                            TRACKER_TYPE_DB_INTERFACE_FLAGS, 0,
+                                                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
-prepare_database (TrackerDBInterface *db_interface)
+tracker_db_interface_init (TrackerDBInterface *db_interface)
 {
        db_interface->dynamic_statements = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                                  NULL,
                                                                  (GDestroyNotify) g_object_unref);
 }
 
-static void
-tracker_db_interface_init (TrackerDBInterface *db_interface)
-{
-       db_interface->ro = FALSE;
-       db_interface->use_mutex = (tracker_db_manager_get_flags (NULL, NULL) &
-                                  TRACKER_DB_MANAGER_ENABLE_MUTEXES) != 0;
-
-       prepare_database (db_interface);
-}
-
 void
 tracker_db_interface_set_max_stmt_cache_size (TrackerDBInterface         *db_interface,
                                               TrackerDBStatementCacheType cache_type,
@@ -2345,9 +2335,9 @@ tracker_db_interface_execute_vquery (TrackerDBInterface  *db_interface,
 }
 
 TrackerDBInterface *
-tracker_db_interface_sqlite_new (const gchar  *filename,
-                                 gboolean      readonly,
-                                 GError      **error)
+tracker_db_interface_sqlite_new (const gchar              *filename,
+                                 TrackerDBInterfaceFlags   flags,
+                                 GError                  **error)
 {
        TrackerDBInterface *object;
        GError *internal_error = NULL;
@@ -2356,7 +2346,7 @@ tracker_db_interface_sqlite_new (const gchar  *filename,
                                 NULL,
                                 &internal_error,
                                 "filename", filename,
-                                "read-only", !!readonly,
+                                "flags", flags,
                                 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 8f638c9..5c81c66 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.h
+++ b/src/libtracker-data/tracker-db-interface-sqlite.h
@@ -35,8 +35,13 @@ G_BEGIN_DECLS
 typedef void (*TrackerDBWalCallback) (TrackerDBInterface *iface,
                                       gint                n_pages);
 
+typedef enum {
+       TRACKER_DB_INTERFACE_READONLY  = 1 << 0,
+       TRACKER_DB_INTERFACE_USE_MUTEX = 1 << 1
+} TrackerDBInterfaceFlags;
+
 TrackerDBInterface *tracker_db_interface_sqlite_new                    (const gchar              *filename,
-                                                                        gboolean                  readonly,
+                                                                        TrackerDBInterfaceFlags   flags,
                                                                         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 3e857b2..193ec0d 100644
--- a/src/libtracker-data/tracker-db-manager.c
+++ b/src/libtracker-data/tracker-db-manager.c
@@ -1311,8 +1311,15 @@ tracker_db_manager_get_db_interfaces (GError   **error,
                TrackerDB db = va_arg (args, TrackerDB);
 
                if (!connection) {
+                       TrackerDBInterfaceFlags flags = 0;
+
+                       if (readonly)
+                               flags |= TRACKER_DB_INTERFACE_READONLY;
+                       if (tracker_db_manager_get_flags (NULL, NULL) & TRACKER_DB_MANAGER_ENABLE_MUTEXES)
+                               flags |= TRACKER_DB_INTERFACE_USE_MUTEX;
+
                        connection = tracker_db_interface_sqlite_new (dbs[db].abs_filename,
-                                                                     readonly,
+                                                                     flags,
                                                                      &internal_error);
 
                        if (internal_error) {


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