[evolution-data-server/openismus-work] EDataBook: Added hash table to bookkeep the directly loaded backends



commit abb8c53f9f1381d837e64630e4be238ac39f2eab
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Tue Nov 6 14:46:22 2012 +0900

    EDataBook: Added hash table to bookkeep the directly loaded backends
    
    EModule is a GTypeModule, those must always exist. Now keep a global
    hash table to ensure we only ever load them once and just use/unuse
    them on the EDataBooks which might be created in a process.

 addressbook/libedata-book/e-data-book.c |   68 +++++++++++++++++++++++--------
 1 files changed, 51 insertions(+), 17 deletions(-)
---
diff --git a/addressbook/libedata-book/e-data-book.c b/addressbook/libedata-book/e-data-book.c
index 8d7f131..6ffbfc6 100644
--- a/addressbook/libedata-book/e-data-book.c
+++ b/addressbook/libedata-book/e-data-book.c
@@ -114,7 +114,6 @@ typedef struct {
 	} d;
 } OperationData;
 
-
 typedef struct {
 	GAsyncReadyCallback callback;
 	gpointer            user_data;
@@ -127,6 +126,55 @@ typedef struct {
 	GCond               sync_result_condition;
 } DirectOperationData;
 
+static DirectOperationData *direct_operation_data_push (EDataBook          *book,
+							OperationID         opid,
+							GAsyncReadyCallback callback,
+							gpointer            user_data,
+							GCancellable       *cancellable,
+							gpointer            source_tag,
+							gboolean            sync_call);
+static void                 direct_operation_data_free (DirectOperationData *data);
+static void                 direct_operation_complete  (DirectOperationData *data);
+static void                 direct_operation_wait      (DirectOperationData *data);
+static void                 e_data_book_respond_close  (EDataBook *book,
+							guint opid,
+							GError *error);
+
+/* EModule's can never be free'd, however the use count can change
+ * Here we ensure that there is only one ever created by way of
+ * static variables and locks
+ */
+static GHashTable *modules_table = NULL;
+G_LOCK_DEFINE (modules_table);
+
+G_DEFINE_TYPE (EDataBook, e_data_book, G_TYPE_OBJECT)
+
+static EModule *
+load_module (const gchar *module_path)
+{
+	EModule *module = NULL;
+
+	G_LOCK (modules_table);
+
+	if (!modules_table)
+		modules_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+	module = g_hash_table_lookup (modules_table, module_path);
+
+	if (!module) {
+		module = e_module_new (module_path);
+		if (!module)
+			g_warning ("Failed to open EModule at path: %s", module_path);
+		else
+			g_hash_table_insert (modules_table, g_strdup (module_path), module);
+	}
+
+	G_UNLOCK (modules_table);
+
+	return module;
+}
+
+
 static DirectOperationData *
 direct_operation_data_push (EDataBook          *book,
 			    OperationID         opid,
@@ -204,13 +252,6 @@ direct_operation_wait (DirectOperationData *data)
 	g_mutex_unlock (&data->sync_result_mutex);
 }
 
-static void e_data_book_respond_close (EDataBook *book,
-				       guint opid,
-				       GError *error);
-
-
-G_DEFINE_TYPE (EDataBook, e_data_book, G_TYPE_OBJECT)
-
 static gchar *
 construct_bookview_path (void)
 {
@@ -1535,7 +1576,6 @@ data_book_dispose (GObject *object)
 
 	if (priv->direct_module) {
 		g_type_module_unuse (G_TYPE_MODULE (priv->direct_module));
-		g_object_unref (priv->direct_module);
 		priv->direct_module = NULL;
 	}
 
@@ -1688,15 +1728,12 @@ e_data_book_new_direct (ESourceRegistry *registry,
 	g_return_val_if_fail (backend_path && backend_path[0], NULL);
 	g_return_val_if_fail (backend_factory_name && backend_factory_name[0], NULL);
 
-	module = e_module_new (backend_path);
-	if (!module) {
-		g_warning ("Failed to open EModule at path: %s", backend_path);
+	module = load_module (backend_path);
+	if (!module)
 		return NULL;
-	}
 
 	if (!g_type_module_use (G_TYPE_MODULE (module))) {
 		g_warning ("Failed to load EModule at path: %s", backend_path);
-		g_object_unref (module);
 		return NULL;
 	}
 
@@ -1705,7 +1742,6 @@ e_data_book_new_direct (ESourceRegistry *registry,
 		g_warning ("Failed to get backend factory '%s' from EModule at path: %s",
 			   backend_factory_name, backend_path);
 		g_type_module_unuse (G_TYPE_MODULE (module));
-		g_object_unref (module);
 		return NULL;
 	}
 
@@ -1855,8 +1891,6 @@ e_data_book_close_finish (EDataBook *book,
 	res = g_simple_async_result_get_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (result));
 	g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error);
 
-	g_print ("Closed the book: %s\n", res ? "success" : "failed");
-
 	return res;
 }
 



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