[tracker/update-blank-variant] libtracker-data, tracker-store, tests: Refactor update_blank to immediately use a GVariant



commit 6b4325fa77fd6b35549a97f90caaccc0d51e9581
Author: Philip Van Hoof <philip codeminded be>
Date:   Wed Feb 16 15:44:20 2011 +0100

    libtracker-data, tracker-store, tests: Refactor update_blank to immediately use a GVariant
    
    This avoids a conversion from a GPtrArray to GVariant in the IPC
    layer and should because of that be a little bit faster. The
    affected unit test was using the GPtrArray based API.

 src/libtracker-data/libtracker-data.vapi          |    2 +-
 src/libtracker-data/tracker-data-update.c         |    6 +-
 src/libtracker-data/tracker-data-update.h         |    2 +-
 src/libtracker-data/tracker-sparql-query.vala     |   42 +++++++------
 src/tracker-store/tracker-resources.vala          |   18 +-----
 src/tracker-store/tracker-steroids.vala           |   18 +-----
 src/tracker-store/tracker-store.vala              |    4 +-
 tests/libtracker-data/tracker-sparql-blank-test.c |   66 +++++++++++++++-----
 8 files changed, 80 insertions(+), 78 deletions(-)
---
diff --git a/src/libtracker-data/libtracker-data.vapi b/src/libtracker-data/libtracker-data.vapi
index 4b29ed2..f5176b4 100644
--- a/src/libtracker-data/libtracker-data.vapi
+++ b/src/libtracker-data/libtracker-data.vapi
@@ -167,7 +167,7 @@ namespace Tracker {
 		public void commit_transaction () throws DBInterfaceError;
 		public void rollback_transaction ();
 		public void update_sparql (string update) throws Sparql.Error;
-		public GLib.GenericArray<GLib.GenericArray<GLib.HashTable<string,string>>> update_sparql_blank (string update) throws Sparql.Error;
+		public GLib.Variant update_sparql_blank (string update) throws Sparql.Error;
 		public void load_turtle_file (GLib.File file) throws Sparql.Error;
 		public void notify_transaction (bool start_timer);
 		public void delete_statement (string? graph, string subject, string predicate, string object) throws Sparql.Error, DateError;
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 9674cc5..9aab5a5 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -2614,14 +2614,14 @@ tracker_data_rollback_transaction (void)
 	}
 }
 
-static GPtrArray *
+static GVariant *
 update_sparql (const gchar  *update,
                gboolean      blank,
                GError      **error)
 {
 	GError *actual_error = NULL;
 	TrackerSparqlQuery *sparql_query;
-	GPtrArray *blank_nodes;
+	GVariant *blank_nodes;
 
 	g_return_val_if_fail (update != NULL, NULL);
 
@@ -2657,7 +2657,7 @@ tracker_data_update_sparql (const gchar  *update,
 	update_sparql (update, FALSE, error);
 }
 
-GPtrArray *
+GVariant *
 tracker_data_update_sparql_blank (const gchar  *update,
                                   GError      **error)
 {
diff --git a/src/libtracker-data/tracker-data-update.h b/src/libtracker-data/tracker-data-update.h
index 37399f3..92b1045 100644
--- a/src/libtracker-data/tracker-data-update.h
+++ b/src/libtracker-data/tracker-data-update.h
@@ -78,7 +78,7 @@ void     tracker_data_notify_transaction            (gboolean
 void     tracker_data_rollback_transaction          (void);
 void     tracker_data_update_sparql                 (const gchar               *update,
                                                      GError                   **error);
-GPtrArray *
+GVariant *
          tracker_data_update_sparql_blank           (const gchar               *update,
                                                      GError                   **error);
 void     tracker_data_update_buffer_flush           (GError                   **error);
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index aea63e3..cf19f9b 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -416,7 +416,8 @@ public class Tracker.Sparql.Query : Object {
 		}
 	}
 
-	public GenericArray<GenericArray<HashTable<string,string>>>? execute_update (bool blank) throws GLib.Error {
+	public Variant? execute_update (bool blank) throws GLib.Error {
+		Variant result = null;
 		assert (update_extensions);
 
 		scanner = new SparqlScanner ((char*) query_string, (long) query_string.length);
@@ -435,21 +436,24 @@ public class Tracker.Sparql.Query : Object {
 
 		parse_prologue ();
 
-		GenericArray<GenericArray<HashTable<string,string>>> blank_nodes = null;
+		// SPARQL update supports multiple operations in a single query
+		VariantBuilder? ublank_nodes = null;
+
 		if (blank) {
-			blank_nodes = new GenericArray<GenericArray<HashTable<string,string>>> ();
+			ublank_nodes = new VariantBuilder ((VariantType) "aaa{ss}");
 		}
 
-		// SPARQL update supports multiple operations in a single query
-
 		while (current () != SparqlTokenType.EOF) {
 			switch (current ()) {
 			case SparqlTokenType.WITH:
 			case SparqlTokenType.INSERT:
 			case SparqlTokenType.DELETE:
-				GenericArray<HashTable<string,string>> ptr = execute_insert_or_delete (blank);
-				if (ptr != null) {
-					blank_nodes.add (ptr);
+				if (blank) {
+					ublank_nodes.open ((VariantType) "aa{ss}");
+					execute_insert_or_delete (ublank_nodes);
+					ublank_nodes.close ();
+				} else {
+					execute_insert_or_delete (null);
 				}
 				break;
 			case SparqlTokenType.DROP:
@@ -468,7 +472,11 @@ public class Tracker.Sparql.Query : Object {
 			accept (SparqlTokenType.SEMICOLON);
 		}
 
-		return blank_nodes;
+		if (blank) {
+			result = ublank_nodes.end ();
+		}
+
+		return result;
 	}
 
 	DBStatement prepare_for_exec (string sql) throws DBInterfaceError, Sparql.Error, DateError {
@@ -568,7 +576,9 @@ public class Tracker.Sparql.Query : Object {
 		}
 	}
 
-	GenericArray<HashTable<string,string>>? execute_insert_or_delete (bool blank) throws GLib.Error {
+	void execute_insert_or_delete (VariantBuilder? update_blank_nodes) throws GLib.Error {
+		bool blank = true;
+
 		// INSERT or DELETE
 
 		if (accept (SparqlTokenType.WITH)) {
@@ -661,12 +671,6 @@ public class Tracker.Sparql.Query : Object {
 
 		this.delete_statements = delete_statements;
 
-		GenericArray<HashTable<string,string>> update_blank_nodes = null;
-
-		if (blank) {
-			update_blank_nodes = new GenericArray<HashTable<string,string>> ();
-		}
-
 		// iterate over all solutions
 		while (cursor.next ()) {
 			// blank nodes in construct templates are per solution
@@ -686,8 +690,8 @@ public class Tracker.Sparql.Query : Object {
 			// iterate over each triple in the template
 			parse_construct_triples_block (var_value_map);
 
-			if (blank) {
-				update_blank_nodes.add ((owned) blank_nodes);
+			if (blank && update_blank_nodes != null) {
+				update_blank_nodes.add_value ((owned) blank_nodes);
 			}
 
 			Data.update_buffer_might_flush ();
@@ -703,8 +707,6 @@ public class Tracker.Sparql.Query : Object {
 		bindings = null;
 
 		context = context.parent_context;
-
-		return update_blank_nodes;
 	}
 
 	internal string resolve_prefixed_name (string prefix, string local_name) throws Sparql.Error {
diff --git a/src/tracker-store/tracker-resources.vala b/src/tracker-store/tracker-resources.vala
index d556c1f..3ef3bb4 100644
--- a/src/tracker-store/tracker-resources.vala
+++ b/src/tracker-store/tracker-resources.vala
@@ -147,23 +147,7 @@ public class Tracker.Resources : Object {
 		var request = DBusRequest.begin (sender, "Resources.SparqlUpdateBlank");
 		request.debug ("query: %s", update);
 		try {
-			var blank_nodes = yield Tracker.Store.sparql_update_blank (update, Tracker.Store.Priority.HIGH, sender);
-
-			request.end ();
-
-			var builder = new VariantBuilder ((VariantType) "aaa{ss}");
-
-			for (int i = 0; i < blank_nodes.length; i++) {
-				var inner_array = blank_nodes[i];
-
-				builder.open ((VariantType) "aa{ss}");
-				for (int j = 0; j < inner_array.length; j++) {
-					builder.add_value (inner_array[j]);
-				}
-				builder.close ();
-			}
-
-			return builder.end ();
+			return yield Tracker.Store.sparql_update_blank (update, Tracker.Store.Priority.HIGH, sender);
 		} catch (DBInterfaceError.NO_SPACE ie) {
 			throw new Sparql.Error.NO_SPACE (ie.message);
 		} catch (Error e) {
diff --git a/src/tracker-store/tracker-steroids.vala b/src/tracker-store/tracker-steroids.vala
index e0f4792..f41c9ed 100644
--- a/src/tracker-store/tracker-steroids.vala
+++ b/src/tracker-store/tracker-steroids.vala
@@ -120,23 +120,7 @@ public class Tracker.Steroids : Object {
 
 				return null;
 			} else {
-				var blank_nodes = yield Tracker.Store.sparql_update_blank ((string) query, priority, sender);
-
-				request.end ();
-
-				var builder = new VariantBuilder ((VariantType) "aaa{ss}");
-
-				for (int i = 0; i < blank_nodes.length; i++) {
-					var inner_array = blank_nodes[i];
-
-					builder.open ((VariantType) "aa{ss}");
-					for (int j = 0; j < inner_array.length; j++) {
-						builder.add_value (inner_array[j]);
-					}
-					builder.close ();
-				}
-
-				return builder.end ();
+				return yield Tracker.Store.sparql_update_blank ((string) query, priority, sender);
 			}
 		} catch (DBInterfaceError.NO_SPACE ie) {
 			throw new Sparql.Error.NO_SPACE (ie.message);
diff --git a/src/tracker-store/tracker-store.vala b/src/tracker-store/tracker-store.vala
index 19828bc..27dcf4d 100644
--- a/src/tracker-store/tracker-store.vala
+++ b/src/tracker-store/tracker-store.vala
@@ -73,7 +73,7 @@ public class Tracker.Store {
 
 	class UpdateTask : Task {
 		public string query;
-		public GenericArray<GenericArray<HashTable<string,string>>> blank_nodes;
+		public Variant blank_nodes;
 		public Priority priority;
 	}
 
@@ -314,7 +314,7 @@ public class Tracker.Store {
 		}
 	}
 
-	public static async GenericArray<GenericArray<HashTable<string,string>>> sparql_update_blank (string sparql, Priority priority, string client_id) throws Error {
+	public static async Variant sparql_update_blank (string sparql, Priority priority, string client_id) throws Error {
 		var task = new UpdateTask ();
 		task.type = TaskType.UPDATE_BLANK;
 		task.query = sparql;
diff --git a/tests/libtracker-data/tracker-sparql-blank-test.c b/tests/libtracker-data/tracker-sparql-blank-test.c
index c20f8a7..9406684 100644
--- a/tests/libtracker-data/tracker-sparql-blank-test.c
+++ b/tests/libtracker-data/tracker-sparql-blank-test.c
@@ -34,8 +34,11 @@ static void
 test_blank (void)
 {
 	GError *error;
-	GPtrArray *updates, *solutions;
-	GHashTable *blank_nodes[2];
+	GVariant *updates;
+	GVariantIter iter;
+	GVariant *rows;
+	guint len = 0;
+	gchar *solutions[3][3];
 
 	error = NULL;
 
@@ -57,31 +60,60 @@ test_blank (void)
 
 	/* perform update in transaction */
 
-	updates = tracker_data_update_sparql_blank (
-	                                            "INSERT { _:foo a rdfs:Resource } "
+	updates = tracker_data_update_sparql_blank ("INSERT { _:foo a rdfs:Resource } "
 	                                            "INSERT { _:foo a rdfs:Resource . _:bar a rdfs:Resource } ",
 	                                            &error);
 	g_assert_no_error (error);
+	g_assert (updates != NULL);
+
+	g_variant_iter_init (&iter, updates);
+	while ((rows = g_variant_iter_next_value (&iter))) {
+		GVariantIter sub_iter;
+		GVariant *sub_value;
+
+		g_variant_iter_init (&sub_iter, rows);
+
+		while ((sub_value = g_variant_iter_next_value (&sub_iter))) {
+			gchar *a = NULL, *b = NULL;
+			GVariantIter sub_sub_iter;
+			GVariant *sub_sub_value;
+
+			g_variant_iter_init (&sub_sub_iter, sub_value);
+
+			while ((sub_sub_value = g_variant_iter_next_value (&sub_sub_iter))) {
+				g_variant_get (sub_sub_value, "{ss}", &a, &b);
+				solutions[len][0] = a;
+				solutions[len][1] = b;
+				len++;
+				g_assert_cmpint (len, <=, 3);
+				g_variant_unref (sub_sub_value);
+			}
+			g_variant_unref (sub_value);
+		}
+		g_variant_unref (rows);
+	}
 
-	g_assert_cmpint (updates->len, ==, 2);
-
-	solutions = updates->pdata[0];
-	g_assert_cmpint (solutions->len, ==, 1);
-	blank_nodes[0] = solutions->pdata[0];
+	g_assert_cmpint (len, ==, 3);
 
-	solutions = updates->pdata[1];
-	g_assert_cmpint (solutions->len, ==, 1);
-	blank_nodes[1] = solutions->pdata[0];
+	g_assert_cmpstr (solutions[0][0], ==, "foo");
+	g_assert (solutions[0][1] != NULL);
 
-	g_assert (g_hash_table_lookup (blank_nodes[0], "foo") != NULL);
-	g_assert (g_hash_table_lookup (blank_nodes[1], "foo") != NULL);
-	g_assert (g_hash_table_lookup (blank_nodes[1], "bar") != NULL);
+	g_assert_cmpstr (solutions[1][0], ==, "foo");
+	g_assert (solutions[1][1] != NULL);
 
-	g_assert_cmpstr (g_hash_table_lookup (blank_nodes[0], "foo"), !=, g_hash_table_lookup (blank_nodes[1], "foo"));
+	g_assert_cmpstr (solutions[2][0], ==, "bar");
+	g_assert (solutions[2][1] != NULL);
 
 	/* cleanup */
 
-	g_ptr_array_free (updates, TRUE);
+	g_free (solutions[0][0]);
+	g_free (solutions[0][1]);
+	g_free (solutions[1][0]);
+	g_free (solutions[1][1]);
+	g_free (solutions[2][0]);
+	g_free (solutions[2][1]);
+
+	g_variant_unref (updates);
 
 	tracker_data_manager_shutdown ();
 }



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