[tracker/dbus-fd-experiment: 8/13] libtracker-client: Add Steroids support



commit ff4c6a5cd5641f3e0ffe37d10cf6b870f3bac59c
Author: Adrien Bustany <abustany gnome org>
Date:   Mon May 24 08:52:02 2010 -0400

    libtracker-client: Add Steroids support

 src/libtracker-client/tracker.c |  224 +++++++++++++++++++++++++++++++++++++++
 src/libtracker-client/tracker.h |   13 +++
 2 files changed, 237 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-client/tracker.c b/src/libtracker-client/tracker.c
index 455fb86..8c76905 100644
--- a/src/libtracker-client/tracker.c
+++ b/src/libtracker-client/tracker.c
@@ -27,6 +27,10 @@
 #include <dbus/dbus-glib-bindings.h>
 
 #include <libtracker-common/tracker-dbus.h>
+#include <tracker-store/tracker-steroids.h>
+
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "tracker.h"
 
@@ -92,6 +96,7 @@
  **/
 
 typedef struct {
+	DBusGConnection *connection;
 	DBusGProxy *proxy_statistics;
 	DBusGProxy *proxy_resources;
 
@@ -141,6 +146,19 @@ typedef struct {
 	guint id;
 } CallbackArray;
 
+struct TrackerResultIterator {
+	int fd;
+	DBusPendingCall *call;
+	int rc;
+	char buffer[STEROIDS_BUFFER_SIZE];
+	int buffer_index;
+	char *large_row_buf;
+
+	guint  n_columns;
+	int   *offsets;
+	char  *data;
+};
+
 #endif /* TRACKER_DISABLE_DEPRECATED */
 
 static gboolean is_service_available (void);
@@ -360,6 +378,8 @@ client_constructed (GObject *object)
 		return;
 	}
 
+	private->connection = connection;
+
 	private->proxy_statistics =
 		dbus_g_proxy_new_for_name (connection,
 		                           TRACKER_DBUS_SERVICE,
@@ -677,6 +697,34 @@ find_conversion (const char  *format,
 	return start;
 }
 
+static int
+iterator_buffer_read_int (TrackerResultIterator *iterator)
+{
+	int result = 0;
+	char *dst = iterator->buffer + iterator->buffer_index;
+
+	result += (((unsigned char)*(dst++)));
+	result += (((unsigned char)*(dst++)) <<  8);
+	result += (((unsigned char)*(dst++)) << 16);
+	result += (((unsigned char)*(dst++)) << 24);
+//	result = *dst | (*(dst + 1) << 8) | (*(dst + 2) << 16) | (*(dst + 3) << 24);
+	iterator->buffer_index += sizeof (int);
+
+	return result;
+}
+
+static void
+pipe_read (int fd, char *dst, int size)
+{
+	ssize_t readsofar = 0;
+
+	while (readsofar < size) {
+		readsofar += read (fd,
+		                   dst + readsofar,
+		                   size - readsofar);
+	}
+}
+
 /**
  * tracker_uri_vprintf_escaped:
  * @format: a standard printf() format string, but notice
@@ -1045,6 +1093,182 @@ tracker_resources_sparql_query (TrackerClient  *client,
 	return table;
 }
 
+TrackerResultIterator*
+tracker_resources_sparql_query_steroids (TrackerClient  *client,
+                                         const gchar    *query,
+                                         GError        **error)
+{
+	TrackerClientPrivate *private;
+	DBusConnection *connection;
+	DBusMessage *message;
+	DBusMessage *reply;
+	guint query_id;
+	DBusError dbus_error;
+	TrackerResultIterator *iterator;
+
+	g_return_val_if_fail (TRACKER_IS_CLIENT (client), NULL);
+	g_return_val_if_fail (query, NULL);
+
+	private = TRACKER_CLIENT_GET_PRIVATE (client);
+
+	connection = dbus_g_connection_get_connection (private->connection);
+
+	dbus_error_init (&dbus_error);
+
+	message = dbus_message_new_method_call (TRACKER_STEROIDS_SERVICE,
+	                                        TRACKER_STEROIDS_PATH,
+	                                        TRACKER_STEROIDS_INTERFACE,
+	                                        "PrepareQuery");
+	dbus_message_append_args (message,
+	                          DBUS_TYPE_STRING, &query,
+	                          DBUS_TYPE_INVALID);
+	reply = dbus_connection_send_with_reply_and_block (connection,
+	                                                   message,
+	                                                   -1,
+	                                                   &dbus_error);
+	dbus_message_unref (message);
+
+	if (!reply) {
+		g_critical ("DBus error in steroids: %s %s", dbus_error.name, dbus_error.message);
+		return NULL;
+	}
+
+	iterator = g_slice_new0 (TrackerResultIterator);
+
+	dbus_message_get_args (reply,
+	                       &dbus_error,
+	                       DBUS_TYPE_UNIX_FD, &iterator->fd,
+	                       DBUS_TYPE_UINT32,  &query_id,
+	                       DBUS_TYPE_INVALID);
+	dbus_message_unref (reply);
+
+	message = dbus_message_new_method_call (TRACKER_STEROIDS_SERVICE,
+	                                        TRACKER_STEROIDS_PATH,
+	                                        TRACKER_STEROIDS_INTERFACE,
+	                                        "Fetch");
+	dbus_message_append_args (message,
+	                          DBUS_TYPE_UINT32, &query_id,
+	                          DBUS_TYPE_INVALID);
+	dbus_connection_send_with_reply (connection,
+	                                 message,
+	                                 &iterator->call,
+	                                 INT_MAX);
+
+	/* Pre fetch the first buffer to initialize the iterator */
+	pipe_read (iterator->fd, iterator->buffer, STEROIDS_BUFFER_SIZE);
+
+	iterator->rc = iterator_buffer_read_int (iterator);
+
+	switch (iterator->rc) {
+	case STEROIDS_RC_ROW:
+	case STEROIDS_RC_LARGEROW:
+		iterator->n_columns = iterator_buffer_read_int (iterator);
+		break;
+	case STEROIDS_RC_DONE:
+		break;
+	case STEROIDS_RC_ERROR:
+		//TODO: Set error
+		tracker_result_iterator_free (iterator);
+		iterator = NULL;
+		break;
+	}
+
+	/* Reset the iterator internal state */
+	iterator->buffer_index = 0;
+
+	return iterator;
+}
+
+void
+tracker_result_iterator_free (TrackerResultIterator *iterator)
+{
+	g_slice_free (TrackerResultIterator, iterator);
+}
+
+guint
+tracker_result_iterator_n_columns (TrackerResultIterator *iterator)
+{
+	g_return_val_if_fail (iterator, 0);
+
+	return iterator->n_columns;
+}
+
+gboolean
+tracker_result_iterator_has_next (TrackerResultIterator *iterator)
+{
+	g_return_val_if_fail (iterator, FALSE);
+
+	return (iterator->rc == STEROIDS_RC_ROW || iterator->rc == STEROIDS_RC_LARGEROW);
+}
+
+void
+tracker_result_iterator_next (TrackerResultIterator  *iterator,
+                              GError                **error)
+{
+	int row_size;
+
+	g_return_if_fail (iterator);
+
+	if (iterator->large_row_buf) {
+		g_free (iterator->large_row_buf);
+		iterator->large_row_buf = NULL;
+	}
+
+	if ((unsigned char)(*(iterator->buffer + iterator->buffer_index)) == STEROIDS_EOP ||
+	    iterator->buffer_index == STEROIDS_BUFFER_SIZE) {
+		pipe_read (iterator->fd, iterator->buffer, STEROIDS_BUFFER_SIZE);
+		iterator->buffer_index = 0;
+	}
+
+	iterator->rc = iterator_buffer_read_int (iterator);
+
+	switch (iterator->rc) {
+		case STEROIDS_RC_ROW:
+			iterator->n_columns = iterator_buffer_read_int (iterator);
+			iterator->offsets = (int *)(iterator->buffer + iterator->buffer_index);
+			iterator->buffer_index += iterator->n_columns * sizeof (int);
+			iterator->data = iterator->buffer + iterator->buffer_index;
+			iterator->buffer_index += iterator->offsets[iterator->n_columns - 1] + 1;
+			break;
+		case STEROIDS_RC_LARGEROW:
+			row_size = iterator_buffer_read_int (iterator);
+			iterator->large_row_buf = malloc (sizeof (char) * row_size);
+			memcpy (iterator->large_row_buf, iterator->buffer, STEROIDS_BUFFER_SIZE);
+			pipe_read (iterator->fd,
+			           iterator->large_row_buf + STEROIDS_BUFFER_SIZE,
+			           row_size - STEROIDS_BUFFER_SIZE);
+			/* The number of columns will always fit into the iterator read buffer,
+			 * since it's an int */
+			iterator->n_columns = iterator_buffer_read_int (iterator);
+			/* For subsequent reads we need additional steps so that we can treat
+			 * iterator->read_buffer and large_row_buf as a single large buffer */
+			iterator->offsets = (int *)(iterator->large_row_buf + 3 * sizeof (int));
+			iterator->data = (char *)iterator->offsets + sizeof (int) * iterator->n_columns;
+			iterator->buffer_index = STEROIDS_BUFFER_SIZE;
+			break;
+		case STEROIDS_RC_DONE:
+			break;
+		default:
+			/* Other type of error */
+			// TODO: Set error
+			break;
+	}
+}
+
+const gchar *
+tracker_result_iterator_value (TrackerResultIterator *iterator,
+                               guint                  column)
+{
+	g_return_val_if_fail (iterator, NULL);
+	g_return_val_if_fail (column < iterator->n_columns, NULL);
+
+	if (column == 0) {
+		return iterator->data;
+	} else {
+		return iterator->data + iterator->offsets[column-1] + 1;
+	}
+}
+
 /**
  * tracker_resources_sparql_update:
  * @client: a #TrackerClient.
diff --git a/src/libtracker-client/tracker.h b/src/libtracker-client/tracker.h
index 0069907..34ddcc9 100644
--- a/src/libtracker-client/tracker.h
+++ b/src/libtracker-client/tracker.h
@@ -49,6 +49,8 @@ typedef struct {
 	GObjectClass parent;
 } TrackerClientClass;
 
+typedef struct TrackerResultIterator TrackerResultIterator;
+
 /**
  * TrackerClientFlags:
  * @TRACKER_CLIENT_ENABLE_WARNINGS: If supplied warnings will be
@@ -122,6 +124,17 @@ void           tracker_resources_load                      (TrackerClient
 GPtrArray *    tracker_resources_sparql_query              (TrackerClient          *client,
                                                             const gchar            *query,
                                                             GError                **error);
+TrackerResultIterator *
+               tracker_resources_sparql_query_steroids     (TrackerClient          *client,
+                                                            const gchar            *query,
+                                                            GError                **error);
+void           tracker_result_iterator_free                (TrackerResultIterator  *iterator);
+guint          tracker_result_iterator_n_columns           (TrackerResultIterator  *iterator);
+gboolean       tracker_result_iterator_has_next            (TrackerResultIterator  *iterator);
+void           tracker_result_iterator_next                (TrackerResultIterator  *iterator,
+                                                            GError                **error);
+const gchar *  tracker_result_iterator_value               (TrackerResultIterator  *iterator,
+                                                            guint                   column);
 void           tracker_resources_sparql_update             (TrackerClient          *client,
                                                             const gchar            *query,
                                                             GError                **error);



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