[tracker/libtracker-bus: 7/51] libtracker-bus: Refactoring the bus-cursor to have the array one in vala



commit d2fbedbe801a3bfaf07e6e45cbe1072b0ac344e1
Author: Philip Van Hoof <philip codeminded be>
Date:   Mon Jul 19 12:05:14 2010 +0200

    libtracker-bus: Refactoring the bus-cursor to have the array one in vala

 src/libtracker-bus/Makefile.am                     |    9 +-
 src/libtracker-bus/tracker-array-cursor.vala       |   68 ++++++++
 ...racker-bus-cursor.c => tracker-bus-fd-cursor.c} |  177 +++++++-------------
 src/libtracker-bus/tracker-bus.vala                |    7 +-
 4 files changed, 140 insertions(+), 121 deletions(-)
---
diff --git a/src/libtracker-bus/Makefile.am b/src/libtracker-bus/Makefile.am
index 1d95674..f2950c3 100644
--- a/src/libtracker-bus/Makefile.am
+++ b/src/libtracker-bus/Makefile.am
@@ -16,12 +16,13 @@ lib_LTLIBRARIES = libtracker-bus- TRACKER_API_VERSION@.la
 libtracker_busincludedir = $(includedir)/tracker-$(TRACKER_API_VERSION)/libtracker-bus
 
 libtracker_bus_la_VALASOURCES = \
-	tracker-bus.vala
+	tracker-bus.vala			\
+	tracker-array-cursor.vala
 
 libtracker_bus_ TRACKER_API_VERSION@_la_SOURCES = 	\
-	libtracker-bus.vala.stamp			\
-	$(libtracker_bus_la_VALASOURCES:.vala=.c) \
-	tracker-bus-cursor.c
+	libtracker-bus.vala.stamp						\
+	$(libtracker_bus_la_VALASOURCES:.vala=.c)		\
+	tracker-bus-fd-cursor.c
 
 libtracker_bus_ TRACKER_API_VERSION@_la_LDFLAGS = 	\
 	-shared -fPIC -module -avoid-version
diff --git a/src/libtracker-bus/tracker-array-cursor.vala b/src/libtracker-bus/tracker-array-cursor.vala
new file mode 100644
index 0000000..d82b5cd
--- /dev/null
+++ b/src/libtracker-bus/tracker-array-cursor.vala
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2010, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+internal class Tracker.Bus.ArrayCursor : Tracker.Sparql.Cursor {
+	int rows;
+	int current_row;
+	char **results;
+	int cols;
+
+	public ArrayCursor (owned char** results, int rows, int cols) {
+		this.rows = rows;
+		this.cols = cols;
+		this.results = results;
+	}
+
+	public override int n_columns { get { return cols; } }
+
+	public override unowned string? get_string (int column, out long length = null) {
+		char **row;
+		unowned string str;
+
+		if (rows < 1) {
+			return null;
+		}
+
+		row = results + current_row;
+		str =  (string) row[column];
+
+		if (&length != null) {
+			length = str.length;
+		}
+
+		return str;
+	}
+
+	public override bool next (Cancellable? cancellable = null) throws GLib.Error {
+		if (current_row >= rows - 1) {
+			return false;
+		}
+		current_row++;
+		return true;
+	}
+
+	public override async bool next_async (Cancellable? cancellable = null) throws GLib.Error {
+		/* This cursor isn't blocking, it's fine to just call next here */
+		return next (cancellable);
+	}
+
+	public override void rewind () {
+		current_row = 0;
+	}
+}
diff --git a/src/libtracker-bus/tracker-bus-cursor.c b/src/libtracker-bus/tracker-bus-fd-cursor.c
similarity index 52%
rename from src/libtracker-bus/tracker-bus-cursor.c
rename to src/libtracker-bus/tracker-bus-fd-cursor.c
index e10f0bd..d63adf8 100644
--- a/src/libtracker-bus/tracker-bus-cursor.c
+++ b/src/libtracker-bus/tracker-bus-fd-cursor.c
@@ -32,20 +32,21 @@
 
 #include "tracker-bus.h"
 
-#define TRACKER_TYPE_BUS_CURSOR           (tracker_bus_cursor_get_type ())
-#define TRACKER_BUS_CURSOR(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), TRACKER_TYPE_BUS_CURSOR, TrackerBusCursor))
-#define TRACKER_BUS_CURSOR_CLASS(c)       (G_TYPE_CHECK_CLASS_CAST ((c),      TRACKER_TYPE_BUS_CURSOR, TrackerBusCursorClass))
-#define TRACKER_IS_BUS_CURSOR(obj)        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TRACKER_TYPE_BUS_CURSOR))
-#define TRACKER_IS_BUS_CURSOR_CLASS(c)    (G_TYPE_CHECK_CLASS_TYPE ((o),      TRACKER_TYPE_BUS_CURSOR))
-#define TRACKER_BUS_CURSOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),  TRACKER_TYPE_BUS_CURSOR, TrackerBusCursorClass))
+#ifdef HAVE_DBUS_FD_PASSING
+
+#define TRACKER_TYPE_BUS_FD_CURSOR           (tracker_bus_fd_cursor_get_type ())
+#define TRACKER_BUS_FD_CURSOR(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), TRACKER_TYPE_BUS_FD_CURSOR, TrackerBusFDCursor))
+#define TRACKER_BUS_FD_CURSOR_CLASS(c)       (G_TYPE_CHECK_CLASS_CAST ((c),      TRACKER_TYPE_BUS_FD_CURSOR, TrackerBusFDCursorClass))
+#define TRACKER_IS_BUS_FD_CURSOR(obj)        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TRACKER_TYPE_BUS_FD_CURSOR))
+#define TRACKER_IS_BUS_FD_CURSOR_CLASS(c)    (G_TYPE_CHECK_CLASS_TYPE ((o),      TRACKER_TYPE_BUS_FD_CURSOR))
+#define TRACKER_BUS_FD_CURSOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),  TRACKER_TYPE_BUS_FD_CURSOR, TrackerBusFDCursorClass))
 
-typedef struct TrackerBusCursor TrackerBusCursor;
-typedef struct TrackerBusCursorClass TrackerBusCursorClass;
+typedef struct TrackerBusFDCursor TrackerBusFDCursor;
+typedef struct TrackerBusFDCursorClass TrackerBusFDCursorClass;
 
-struct TrackerBusCursor {
+struct TrackerBusFDCursor {
 	TrackerSparqlCursor parent_instance;
 
-#ifdef HAVE_DBUS_FD_PASSING
 	char *buffer;
 	int buffer_index;
 	long buffer_size;
@@ -53,33 +54,25 @@ struct TrackerBusCursor {
 	guint n_columns;
 	int *offsets;
 	char *data;
-#else  /* HAVE_DBUS_FD_PASSING */
-	gint n_columns;
-	gint rows;
-	gint current_row;
-	gchar **results;
-#endif /* HAVE_DBUS_FD_PASSING */
 };
 
-struct TrackerBusCursorClass {
+struct TrackerBusFDCursorClass {
 	TrackerSparqlCursorClass parent_class;
 };
 
-GType tracker_bus_cursor_get_type (void);
-void  tracker_bus_cursor_finalize (GObject *object);
+GType tracker_bus_fd_cursor_get_type (void);
+void  tracker_bus_fd_cursor_finalize (GObject *object);
 
-G_DEFINE_TYPE (TrackerBusCursor, tracker_bus_cursor, TRACKER_SPARQL_TYPE_CURSOR)
+G_DEFINE_TYPE (TrackerBusFDCursor, tracker_bus_fd_cursor, TRACKER_SPARQL_TYPE_CURSOR)
 
 static void
-tracker_bus_cursor_rewind (TrackerBusCursor *cursor)
+tracker_bus_fd_cursor_rewind (TrackerBusFDCursor *cursor)
 {
 	/* FIXME: Implement */
 }
 
-#ifdef HAVE_DBUS_FD_PASSING
-
 static inline int
-buffer_read_int (TrackerBusCursor *cursor)
+buffer_read_int (TrackerBusFDCursor *cursor)
 {
 	int v = *((int *)(cursor->buffer + cursor->buffer_index));
 
@@ -88,14 +81,12 @@ buffer_read_int (TrackerBusCursor *cursor)
 	return v;
 }
 
-#endif /* HAVE_DBUS_FD_PASSING */
 
 static gboolean
-tracker_bus_cursor_iter_next (TrackerBusCursor  *cursor,
-              	              GCancellable      *cancellable,
-              	              GError           **error)
+tracker_bus_fd_cursor_iter_next (TrackerBusFDCursor  *cursor,
+                                 GCancellable        *cancellable,
+                                 GError             **error)
 {
-#ifdef HAVE_DBUS_FD_PASSING
 	int last_offset;
 
 	if (cursor->buffer_index >= cursor->buffer_size) {
@@ -107,6 +98,7 @@ tracker_bus_cursor_iter_next (TrackerBusCursor  *cursor,
 	 * iteration = [4 bytes for number of columns,
 	 *              4 bytes for last offset]
 	 */
+
 	cursor->n_columns = buffer_read_int (cursor);
 	cursor->offsets = (int *)(cursor->buffer + cursor->buffer_index);
 	cursor->buffer_index += sizeof (int) * (cursor->n_columns - 1);
@@ -114,20 +106,14 @@ tracker_bus_cursor_iter_next (TrackerBusCursor  *cursor,
 	last_offset = buffer_read_int (cursor);
 	cursor->data = cursor->buffer + cursor->buffer_index;
 	cursor->buffer_index += last_offset + 1;
-#else  /* HAVE_DBUS_FD_PASSING */
-	if (cursor->current_row >= (gint) cursor->rows - 1) {
-		return FALSE;
-	}
 
-	cursor->current_row++;
-#endif /* HAVE_DBUS_FD_PASSING */
 	return TRUE;
 }
 
 static void
-tracker_bus_cursor_iter_next_thread (GSimpleAsyncResult *res,
-                                     GObject            *object,
-                                     GCancellable       *cancellable)
+tracker_bus_fd_cursor_iter_next_thread (GSimpleAsyncResult *res,
+                                        GObject            *object,
+                                        GCancellable       *cancellable)
 {
 	/* This is stolen from the direct access work, 
 	 * do we REALLY need to do this in the next thread? 
@@ -136,7 +122,7 @@ tracker_bus_cursor_iter_next_thread (GSimpleAsyncResult *res,
 	GError *error = NULL;
 	gboolean result;
 
-	result = tracker_bus_cursor_iter_next (TRACKER_BUS_CURSOR (object), cancellable, &error);
+	result = tracker_bus_fd_cursor_iter_next (TRACKER_BUS_FD_CURSOR (object), cancellable, &error);
 	if (error) {
 		g_simple_async_result_set_from_error (res, error);
 	} else {
@@ -145,21 +131,21 @@ tracker_bus_cursor_iter_next_thread (GSimpleAsyncResult *res,
 }
 
 static void
-tracker_bus_cursor_iter_next_async (TrackerBusCursor    *cursor,
-                                    GCancellable        *cancellable,
-                                    GAsyncReadyCallback  callback,
-                                    gpointer             user_data)
+tracker_bus_fd_cursor_iter_next_async (TrackerBusFDCursor    *cursor,
+                                       GCancellable          *cancellable,
+                                       GAsyncReadyCallback    callback,
+                                       gpointer               user_data)
 {
 	GSimpleAsyncResult *res;
 
-	res = g_simple_async_result_new (G_OBJECT (cursor), callback, user_data, tracker_bus_cursor_iter_next_async);
-	g_simple_async_result_run_in_thread (res, tracker_bus_cursor_iter_next_thread, 0, cancellable);
+	res = g_simple_async_result_new (G_OBJECT (cursor), callback, user_data, tracker_bus_fd_cursor_iter_next_async);
+	g_simple_async_result_run_in_thread (res, tracker_bus_fd_cursor_iter_next_thread, 0, cancellable);
 }
 
 static gboolean
-tracker_bus_cursor_iter_next_finish (TrackerBusCursor *cursor,
-                                     GAsyncResult     *res,
-                                     GError          **error)
+tracker_bus_fd_cursor_iter_next_finish (TrackerBusFDCursor *cursor,
+                                        GAsyncResult       *res,
+                                        GError            **error)
 {
 	if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) {
 		return FALSE;
@@ -168,45 +154,29 @@ tracker_bus_cursor_iter_next_finish (TrackerBusCursor *cursor,
 }
 
 static guint
-tracker_bus_cursor_get_n_columns (TrackerBusCursor *cursor)
+tracker_bus_fd_cursor_get_n_columns (TrackerBusFDCursor *cursor)
 {
 	return cursor->n_columns;
 }
 
 static const gchar *
-tracker_bus_cursor_get_string (TrackerBusCursor *cursor,
-	                           guint             column,
-	                           gint             *length)
+tracker_bus_fd_cursor_get_string (TrackerBusFDCursor *cursor,
+                                  guint               column,
+                                  glong              *length)
 {
 	const gchar *str = NULL;
 
 	if (length) {
 		*length = 0;
 	}
-	
-#ifdef HAVE_DBUS_FD_PASSING
-	g_return_val_if_fail (column < tracker_bus_cursor_get_n_columns (cursor), NULL);
+
+	g_return_val_if_fail (column < tracker_bus_fd_cursor_get_n_columns (cursor), NULL);
 
 	if (column == 0) {
 		str = cursor->data;
 	} else {
 		str = cursor->data + cursor->offsets[column - 1] + 1;
 	}
-#else  /* HAVE_DBUS_FD_PASSING */
-	gchar **row;
-
-	g_return_val_if_fail (column < tracker_bus_cursor_get_n_columns (cursor), NULL);
-
-	if (cursor->rows < 1) {
-		return NULL;
-	}
-
-	g_return_val_if_fail (cursor->current_row < (gint) cursor->rows, NULL);
-
-	row = cursor->results + cursor->current_row;
-
-	str = row[column];
-#endif /* HAVE_DBUS_FD_PASSING */
 
 	if (length) {
 		*length = strlen (str);
@@ -216,54 +186,52 @@ tracker_bus_cursor_get_string (TrackerBusCursor *cursor,
 }
 
 static void
-tracker_bus_cursor_class_init (TrackerBusCursorClass *class)
+tracker_bus_fd_cursor_class_init (TrackerBusFDCursorClass *class)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (class);
 	TrackerSparqlCursorClass *sparql_cursor_class = TRACKER_SPARQL_CURSOR_CLASS (class);
 
-	object_class->finalize = tracker_bus_cursor_finalize;
+	object_class->finalize = tracker_bus_fd_cursor_finalize;
 
-	sparql_cursor_class->get_n_columns = (gint (*) (TrackerSparqlCursor *)) tracker_bus_cursor_get_n_columns;
-	sparql_cursor_class->get_string = (const gchar * (*) (TrackerSparqlCursor *, gint, gint*)) tracker_bus_cursor_get_string;
-	sparql_cursor_class->next = (gboolean (*) (TrackerSparqlCursor *, GCancellable *, GError **)) tracker_bus_cursor_iter_next;
-	sparql_cursor_class->next_async = (void (*) (TrackerSparqlCursor *, GCancellable *, GAsyncReadyCallback, gpointer)) tracker_bus_cursor_iter_next_async;
-	sparql_cursor_class->next_finish = (gboolean (*) (TrackerSparqlCursor *, GAsyncResult *, GError **)) tracker_bus_cursor_iter_next_finish;
-	sparql_cursor_class->rewind = (void (*) (TrackerSparqlCursor *)) tracker_bus_cursor_rewind;
+	sparql_cursor_class->get_n_columns = (gint (*) (TrackerSparqlCursor *)) tracker_bus_fd_cursor_get_n_columns;
+	sparql_cursor_class->get_string = (const gchar * (*) (TrackerSparqlCursor *, gint, glong*)) tracker_bus_fd_cursor_get_string;
+	sparql_cursor_class->next = (gboolean (*) (TrackerSparqlCursor *, GCancellable *, GError **)) tracker_bus_fd_cursor_iter_next;
+	sparql_cursor_class->next_async = (void (*) (TrackerSparqlCursor *, GCancellable *, GAsyncReadyCallback, gpointer)) tracker_bus_fd_cursor_iter_next_async;
+	sparql_cursor_class->next_finish = (gboolean (*) (TrackerSparqlCursor *, GAsyncResult *, GError **)) tracker_bus_fd_cursor_iter_next_finish;
+	sparql_cursor_class->rewind = (void (*) (TrackerSparqlCursor *)) tracker_bus_fd_cursor_rewind;
 }
 
 void
-tracker_bus_cursor_init (TrackerBusCursor *cursor)
+tracker_bus_fd_cursor_init (TrackerBusFDCursor *cursor)
 {
 }
 
 void
-tracker_bus_cursor_finalize (GObject *object)
+tracker_bus_fd_cursor_finalize (GObject *object)
 {
-	TrackerBusCursor *cursor;
+	TrackerBusFDCursor *cursor;
 
-	cursor = TRACKER_BUS_CURSOR (object);
+	cursor = TRACKER_BUS_FD_CURSOR (object);
 
-#ifdef HAVE_DBUS_FD_PASSING
 	g_free (cursor->buffer);
-#else  /* HAVE_DBUS_FD_PASSING */
-	g_strfreev (cursor->results);
-#endif /* HAVE_DBUS_FD_PASSING */
 
-	G_OBJECT_CLASS (tracker_bus_cursor_parent_class)->finalize (object);
+	G_OBJECT_CLASS (tracker_bus_fd_cursor_parent_class)->finalize (object);
 }
 
-// Public API
+#endif /* HAVE_DBUS_FD_PASSING */
+
+/* Public API */
 
 TrackerSparqlCursor *
-tracker_bus_query (DBusGConnection  *gconnection,
-                   const gchar      *query,
-                   GError          **error)
+tracker_bus_fd_query (DBusGConnection  *gconnection,
+                      const gchar      *query,
+                      GError          **error)
 {
 #ifdef HAVE_DBUS_FD_PASSING
 	DBusConnection *connection;
 	DBusMessage *message;
 	DBusMessageIter iter;
-	TrackerBusCursor *cursor;
+	TrackerBusFDCursor *cursor;
 	GError *inner_error = NULL;
 	int pipefd[2];
 
@@ -288,7 +256,7 @@ tracker_bus_query (DBusGConnection  *gconnection,
 	dbus_message_iter_append_basic (&iter, DBUS_TYPE_UNIX_FD, &pipefd[1]);
 	close (pipefd[1]);
 
-	cursor = g_object_new (TRACKER_TYPE_BUS_CURSOR, NULL);
+	cursor = g_object_new (TRACKER_TYPE_BUS_FD_CURSOR, NULL);
 
 	tracker_dbus_send_and_splice (connection,
 	                              message,
@@ -305,26 +273,9 @@ tracker_bus_query (DBusGConnection  *gconnection,
 		cursor = NULL;
 	}
 	return TRACKER_SPARQL_CURSOR (cursor);
-#else  /* HAVE_DBUS_FD_PASSING */
-	return NULL;	
+#else /* HAVE_DBUS_FD_PASSING */
+	g_assert_not_reached ();
+	return NULL;
 #endif /* HAVE_DBUS_FD_PASSING */
 }
 
-TrackerSparqlCursor *
-tracker_bus_query_results_to_cursor (char **results,
-	                                 int    rows,
-	                                 int    cols)
-{
-	TrackerBusCursor *cursor;
-	
-	cursor = g_object_new (TRACKER_TYPE_BUS_CURSOR, NULL);
-	
-#ifndef HAVE_DBUS_FD_PASSING
-	cursor->rows = rows;
-	cursor->current_row = -1;
-	cursor->n_columns = cols;
-	cursor->results = results;
-#endif /* HAVE_DBUS_FD_PASSING */
-
-	return TRACKER_SPARQL_CURSOR (cursor);
-}
diff --git a/src/libtracker-bus/tracker-bus.vala b/src/libtracker-bus/tracker-bus.vala
index 9ac0cd2..c7e1c9f 100644
--- a/src/libtracker-bus/tracker-bus.vala
+++ b/src/libtracker-bus/tracker-bus.vala
@@ -29,8 +29,7 @@ private interface Tracker.Bus.Resources : GLib.Object {
 }
 
 // Imported DBus FD API until we have support with Vala
-public extern Tracker.Sparql.Cursor tracker_bus_query (DBus.Connection connection, string query) throws GLib.Error;
-public extern Tracker.Sparql.Cursor tracker_bus_query_results_to_cursor (owned char **results, int rows, int cols);
+public extern Tracker.Sparql.Cursor tracker_bus_fd_query (DBus.Connection connection, string query) throws GLib.Error;
 
 // Actual class definition
 public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
@@ -75,10 +74,10 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
 
 		
 		if (use_steroids) {
-			return tracker_bus_query (connection, sparql);
+			return tracker_bus_fd_query (connection, sparql);
 		} else {
 			string[,] results = resources.SparqlQuery (sparql);
-			return tracker_bus_query_results_to_cursor ((owned) results, results.length[0], results.length[1]);
+			return new Tracker.Bus.ArrayCursor ((owned) results, results.length[0], results.length[1]);
 		}
 	}
 



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