tracker r2456 - in branches/libtracker-data: . src/libtracker-data src/trackerd



Author: juergbi
Date: Wed Nov  5 13:53:58 2008
New Revision: 2456
URL: http://svn.gnome.org/viewvc/tracker?rev=2456&view=rev

Log:
2008-11-05  JÃrg Billeter  <j bitron ch>

	* src/libtracker-data/Makefile.am:
	* src/libtracker-data/tracker-data-manager.c:
	* src/libtracker-data/tracker-data-manager.h:
	* src/libtracker-data/tracker-data-search.c:
	* src/libtracker-data/tracker-data-search.h:
	* src/trackerd/tracker-files.c:

	Move Search API to tracker-data-search.[ch]


Added:
   branches/libtracker-data/src/libtracker-data/tracker-data-search.c
   branches/libtracker-data/src/libtracker-data/tracker-data-search.h
Modified:
   branches/libtracker-data/ChangeLog
   branches/libtracker-data/src/libtracker-data/Makefile.am
   branches/libtracker-data/src/libtracker-data/tracker-data-manager.c
   branches/libtracker-data/src/libtracker-data/tracker-data-manager.h
   branches/libtracker-data/src/trackerd/tracker-files.c

Modified: branches/libtracker-data/src/libtracker-data/Makefile.am
==============================================================================
--- branches/libtracker-data/src/libtracker-data/Makefile.am	(original)
+++ branches/libtracker-data/src/libtracker-data/Makefile.am	Wed Nov  5 13:53:58 2008
@@ -13,6 +13,7 @@
 libtracker_data_la_SOURCES = 						\
 	tracker-data-manager.c						\
 	tracker-data-metadata.c						\
+	tracker-data-search.c						\
 	tracker-data-update.c						\
 	tracker-query-tree.c						\
 	tracker-rdf-query.c						\
@@ -21,6 +22,7 @@
 noinst_HEADERS =							\
 	tracker-data-manager.h						\
 	tracker-data-metadata.h						\
+	tracker-data-search.h						\
 	tracker-data-update.h						\
 	tracker-query-tree.h						\
 	tracker-rdf-query.h						\

Modified: branches/libtracker-data/src/libtracker-data/tracker-data-manager.c
==============================================================================
--- branches/libtracker-data/src/libtracker-data/tracker-data-manager.c	(original)
+++ branches/libtracker-data/src/libtracker-data/tracker-data-manager.c	Wed Nov  5 13:53:58 2008
@@ -42,7 +42,6 @@
 #include <libtracker-db/tracker-db-manager.h>
 
 #include "tracker-data-manager.h"
-#include "tracker-query-tree.h"
 
 #define ZLIBBUFSIZ 8192
 
@@ -278,476 +277,6 @@
 }
 
 TrackerDBResultSet *
-tracker_db_search_text (TrackerDBInterface *iface,
-			const gchar	   *service,
-			const gchar	   *search_string,
-			gint		    offset,
-			gint		    limit,
-			gboolean	    save_results,
-			gboolean	    detailed)
-{
-	TrackerDBPrivate    *private;
-	TrackerQueryTree    *tree;
-	TrackerDBResultSet  *result_set, *result;
-	GArray		    *hits;
-	gint		     count;
-	const gchar	    *procedure;
-	GArray		    *services = NULL;
-	GSList		    *duds = NULL;
-	guint		     i = 0;
-
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (service != NULL, NULL);
-	g_return_val_if_fail (search_string != NULL, NULL);
-	g_return_val_if_fail (offset >= 0, NULL);
-
-	private = g_static_private_get (&private_key);
-	g_return_val_if_fail (private != NULL, NULL);
-
-	services = tracker_db_create_array_of_services (service, FALSE);
-	/* FIXME: Do we need both index and services here? We used to have it */
-	tree = tracker_query_tree_new (search_string,
-				       private->config,
-				       private->language,
-				       services);
-	hits = tracker_query_tree_get_hits (tree, offset, limit);
-	result = NULL;
-
-	if (save_results) {
-		tracker_db_interface_start_transaction (iface);
-		tracker_data_manager_exec_proc (iface,
-				      "DeleteSearchResults1",
-				      NULL);
-	}
-
-	count = 0;
-
-	for (i = 0; i < hits->len; i++) {
-		TrackerDBIndexItemRank	rank;
-		gchar		       *str_id;
-
-		if (count >= limit) {
-			break;
-		}
-
-		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
-		str_id = tracker_guint_to_string (rank.service_id);
-
-		/* We save results into SearchResults table instead of
-		 * returing an array of array of strings
-		 */
-		if (save_results) {
-			gchar *str_score;
-
-			str_score = tracker_gint_to_string (rank.score);
-			tracker_data_manager_exec_proc (iface,
-					      "InsertSearchResult1",
-					      str_id,
-					      str_score,
-					      NULL);
-			g_free (str_id);
-			g_free (str_score);
-
-			continue;
-		}
-
-		if (detailed) {
-			if (strcmp (service, "Emails") == 0) {
-				procedure = "GetEmailByID";
-			} else if (strcmp (service, "Applications") == 0) {
-				procedure = "GetApplicationByID";
-			} else {
-				procedure = "GetFileByID2";
-			}
-		} else {
-			procedure = "GetFileByID";
-		}
-
-		result_set = tracker_data_manager_exec_proc (iface,
-						   procedure,
-						   str_id,
-						   NULL);
-		g_free (str_id);
-
-		if (result_set) {
-			gchar *path;
-			guint  columns, i;
-
-			tracker_db_result_set_get (result_set, 0, &path, -1);
-
-				columns = tracker_db_result_set_get_n_columns (result_set);
-
-				if (G_UNLIKELY (!result)) {
-					guint columns;
-
-					columns = tracker_db_result_set_get_n_columns (result_set);
-					result = _tracker_db_result_set_new (columns);
-				}
-
-				_tracker_db_result_set_append (result);
-
-				for (i = 0; i < columns; i++) {
-					GValue value = { 0, };
-
-					_tracker_db_result_set_get_value (result_set, i, &value);
-					_tracker_db_result_set_set_value (result, i, &value);
-					g_value_unset (&value);
-				}
-
-			g_free (path);
-			g_object_unref (result_set);
-		} else {
-			g_message ("Dud hit for search detected");
-			duds = g_slist_prepend (duds, &rank);
-		}
-	}
-
-	if (save_results) {
-		tracker_db_interface_end_transaction (iface);
-	}
-
-	/* Delete duds */
-	if (duds) {
-		TrackerDBIndex *file_index;
-		TrackerDBIndex *email_index;
-		GSList	       *words, *w;
-
-		words = tracker_query_tree_get_words (tree);
-		file_index = tracker_db_index_manager_get_index (TRACKER_DB_INDEX_FILE);
-		email_index = tracker_db_index_manager_get_index (TRACKER_DB_INDEX_EMAIL);
-
-		for (w = words; w; w = w->next) {
-			tracker_db_index_remove_dud_hits (file_index,
-							  (const gchar *) w->data,
-							  duds);
-			tracker_db_index_remove_dud_hits (email_index,
-							  (const gchar *) w->data,
-							  duds);
-		}
-
-		g_slist_free (words);
-		g_slist_free (duds);
-	}
-
-	g_object_unref (tree);
-	g_array_free (hits, TRUE);
-
-	if (!result) {
-		return NULL;
-	}
-
-	if (tracker_db_result_set_get_n_rows (result) == 0) {
-		g_object_unref (result);
-		return NULL;
-	}
-
-	tracker_db_result_set_rewind (result);
-
-	return result;
-}
-
-TrackerDBResultSet *
-tracker_db_search_text_and_mime (TrackerDBInterface  *iface,
-				 const gchar	     *text,
-				 gchar		    **mime_array)
-{
-	TrackerDBPrivate   *private;
-	TrackerQueryTree   *tree;
-	TrackerDBResultSet *result_set1;
-	GArray		   *hits;
-	GArray		   *services;
-	gint		    count = 0;
-	guint		    i;
-
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (text != NULL, NULL);
-	g_return_val_if_fail (mime_array != NULL, NULL);
-
-	private = g_static_private_get (&private_key);
-	g_return_val_if_fail (private != NULL, NULL);
-
-	result_set1 = NULL;
-	services = tracker_db_create_array_of_services (NULL, TRUE);
-
-	tree = tracker_query_tree_new (text,
-				       private->config,
-				       private->language,
-				       services);
-	hits = tracker_query_tree_get_hits (tree, 0, 0);
-
-	for (i = 0, count = 0; i < hits->len; i++) {
-		TrackerDBResultSet     *result_set2;
-		TrackerDBIndexItemRank	rank;
-		gchar		       *str_id, *mimetype;
-
-		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
-
-		str_id = tracker_guint_to_string (rank.service_id);
-		result_set2 = tracker_data_manager_exec_proc (iface,
-						    "GetFileByID",
-						    str_id,
-						    NULL);
-		g_free (str_id);
-
-		if (result_set2) {
-			tracker_db_result_set_get (result_set2, 2, &mimetype, -1);
-
-			if (tracker_string_in_string_list (mimetype, mime_array) != -1) {
-				GValue value = { 0, };
-
-				if (G_UNLIKELY (!result_set1)) {
-					result_set1 = _tracker_db_result_set_new (2);
-				}
-
-				_tracker_db_result_set_append (result_set1);
-
-				/* copy value in column 0 */
-				_tracker_db_result_set_get_value (result_set2, 0, &value);
-				_tracker_db_result_set_set_value (result_set1, 0, &value);
-				g_value_unset (&value);
-
-				/* copy value in column 1 */
-				_tracker_db_result_set_get_value (result_set2, 1, &value);
-				_tracker_db_result_set_set_value (result_set1, 1, &value);
-				g_value_unset (&value);
-
-				count++;
-			}
-
-			g_free (mimetype);
-			g_object_unref (result_set2);
-		}
-
-		if (count > 2047) {
-			g_warning ("Count is > 2047? Breaking for loop in %s, why?",
-				   __FUNCTION__);
-			break;
-		}
-	}
-
-	g_object_unref (tree);
-	g_array_free (hits, TRUE);
-
-	if (!result_set1) {
-		return NULL;
-	}
-
-	if (tracker_db_result_set_get_n_rows (result_set1) == 0) {
-		g_object_unref (result_set1);
-		return NULL;
-	}
-
-	tracker_db_result_set_rewind (result_set1);
-
-	return result_set1;
-}
-
-TrackerDBResultSet *
-tracker_db_search_text_and_location (TrackerDBInterface *iface,
-				     const gchar	*text,
-				     const gchar	*location)
-{
-	TrackerDBPrivate   *private;
-	TrackerDBResultSet *result_set1;
-	TrackerQueryTree   *tree;
-	GArray		   *hits;
-	GArray		   *services;
-	gchar		   *location_prefix;
-	gint		    count;
-	guint		    i;
-
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (text != NULL, NULL);
-	g_return_val_if_fail (location != NULL, NULL);
-
-	private = g_static_private_get (&private_key);
-	g_return_val_if_fail (private != NULL, NULL);
-
-	result_set1 = NULL;
-	location_prefix = g_strconcat (location, G_DIR_SEPARATOR_S, NULL);
-	services = tracker_db_create_array_of_services (NULL, TRUE);
-
-	tree = tracker_query_tree_new (text,
-				       private->config,
-				       private->language,
-				       services);
-	hits = tracker_query_tree_get_hits (tree, 0, 0);
-
-	for (i = 0, count = 0; i < hits->len; i++) {
-		TrackerDBResultSet     *result_set2;
-		TrackerDBIndexItemRank	rank;
-		gchar		       *str_id, *path;
-
-		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
-
-		str_id = tracker_guint_to_string (rank.service_id);
-		result_set2 = tracker_data_manager_exec_proc (iface,
-						    "GetFileByID",
-						    str_id,
-						    NULL);
-		g_free (str_id);
-
-		if (result_set2) {
-			tracker_db_result_set_get (result_set2, 0, &path, -1);
-
-			if (g_str_has_prefix (path, location_prefix) ||
-			    strcmp (path, location) == 0) {
-				GValue value = { 0, };
-
-				if (G_UNLIKELY (!result_set1)) {
-					result_set1 = _tracker_db_result_set_new (2);
-				}
-
-				_tracker_db_result_set_append (result_set1);
-
-				/* copy value in column 0 */
-				_tracker_db_result_set_get_value (result_set2, 0, &value);
-				_tracker_db_result_set_set_value (result_set1, 0, &value);
-				g_value_unset (&value);
-
-				/* copy value in column 1 */
-				_tracker_db_result_set_get_value (result_set2, 1, &value);
-				_tracker_db_result_set_set_value (result_set1, 1, &value);
-				g_value_unset (&value);
-
-				count++;
-			}
-
-			g_object_unref (result_set2);
-		}
-
-		if (count > 2047) {
-			g_warning ("Count is > 2047? Breaking for loop in %s, why?",
-				   __FUNCTION__);
-			break;
-		}
-	}
-
-	g_free (location_prefix);
-	g_object_unref (tree);
-	g_array_free (hits, TRUE);
-
-	if (!result_set1) {
-		return NULL;
-	}
-
-	if (tracker_db_result_set_get_n_rows (result_set1) == 0) {
-		g_object_unref (result_set1);
-		return NULL;
-	}
-
-	tracker_db_result_set_rewind (result_set1);
-
-	return result_set1;
-}
-
-TrackerDBResultSet *
-tracker_db_search_text_and_mime_and_location (TrackerDBInterface  *iface,
-					      const gchar	  *text,
-					      gchar		 **mime_array,
-					      const gchar	  *location)
-{
-	TrackerDBPrivate   *private;
-	TrackerDBResultSet *result_set1;
-	TrackerQueryTree   *tree;
-	GArray		   *hits;
-	GArray		   *services;
-	gchar		   *location_prefix;
-	gint		    count;
-	guint		    i;
-
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (text != NULL, NULL);
-	g_return_val_if_fail (location != NULL, NULL);
-
-	private = g_static_private_get (&private_key);
-	g_return_val_if_fail (private != NULL, NULL);
-
-	result_set1 = NULL;
-	location_prefix = g_strconcat (location, G_DIR_SEPARATOR_S, NULL);
-	services = tracker_db_create_array_of_services (NULL, TRUE);
-
-	tree = tracker_query_tree_new (text,
-				       private->config,
-				       private->language,
-				       services);
-	hits = tracker_query_tree_get_hits (tree, 0, 0);
-
-	for (i = 0, count = 0; i < hits->len; i++) {
-		TrackerDBResultSet     *result_set2;
-		TrackerDBIndexItemRank	rank;
-		gchar		       *str_id, *path, *mimetype;
-
-		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
-
-		str_id = tracker_guint_to_string (rank.service_id);
-		result_set2 = tracker_data_manager_exec_proc (iface,
-						    "GetFileByID",
-						    str_id,
-						    NULL);
-		g_free (str_id);
-
-		if (result_set2) {
-			tracker_db_result_set_get (result_set2,
-						   0, &path,
-						   2, &mimetype,
-						   -1);
-
-			if ((g_str_has_prefix (path, location_prefix) ||
-			     strcmp (path, location) == 0) &&
-			    tracker_string_in_string_list (mimetype, mime_array) != -1) {
-				GValue value = { 0, };
-
-				if (G_UNLIKELY (!result_set1)) {
-					result_set1 = _tracker_db_result_set_new (2);
-				}
-
-				_tracker_db_result_set_append (result_set1);
-
-				/* copy value in column 0 */
-				_tracker_db_result_set_get_value (result_set2, 0, &value);
-				_tracker_db_result_set_set_value (result_set1, 0, &value);
-				g_value_unset (&value);
-
-				/* copy value in column 1 */
-				_tracker_db_result_set_get_value (result_set2, 1, &value);
-				_tracker_db_result_set_set_value (result_set1, 1, &value);
-				g_value_unset (&value);
-
-				count++;
-			}
-
-			g_free (path);
-			g_free (mimetype);
-			g_object_unref (result_set2);
-		}
-
-		if (count > 2047) {
-			g_warning ("Count is > 2047? Breaking for loop in %s, why?",
-				   __FUNCTION__);
-			break;
-		}
-	}
-
-	g_free (location_prefix);
-	g_object_unref (tree);
-	g_array_free (hits, TRUE);
-
-	if (!result_set1) {
-		return NULL;
-	}
-
-	if (tracker_db_result_set_get_n_rows (result_set1) == 0) {
-		g_object_unref (result_set1);
-		return NULL;
-	}
-
-	tracker_db_result_set_rewind (result_set1);
-
-	return result_set1;
-}
-
-TrackerDBResultSet *
 tracker_db_metadata_get (TrackerDBInterface *iface,
 			 const gchar	    *id,
 			 const gchar	    *key)
@@ -1540,137 +1069,6 @@
 	return NULL;
 }
 
-gchar **
-tracker_db_files_get (TrackerDBInterface *iface,
-		      const gchar	 *uri)
-{
-	TrackerDBResultSet *result_set;
-	GPtrArray	   *array;
-
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (uri != NULL, NULL);
-
-	result_set = tracker_data_manager_exec_proc (iface,
-					   "SelectFileChild",
-					   uri,
-					   NULL);
-	array = g_ptr_array_new ();
-
-	if (result_set) {
-		gchar	 *name, *prefix;
-		gboolean  valid = TRUE;
-
-		while (valid) {
-			tracker_db_result_set_get (result_set,
-						   1, &prefix,
-						   2, &name,
-						   -1);
-
-			g_ptr_array_add (array, g_build_filename (prefix, name, NULL));
-
-			g_free (prefix);
-			g_free (name);
-			valid = tracker_db_result_set_iter_next (result_set);
-		}
-
-		g_object_unref (result_set);
-	}
-
-	g_ptr_array_add (array, NULL);
-
-	return (gchar**) g_ptr_array_free (array, FALSE);
-}
-
-TrackerDBResultSet *
-tracker_db_files_get_by_service (TrackerDBInterface *iface,
-				 const gchar	    *service,
-				 gint		     offset,
-				 gint		     limit)
-{
-	TrackerDBResultSet *result_set;
-	gchar		   *str_limit;
-	gchar		   *str_offset;
-
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (service != NULL, NULL);
-
-	str_limit = tracker_gint_to_string (limit);
-	str_offset = tracker_gint_to_string (offset);
-
-	result_set = tracker_data_manager_exec_proc (iface,
-					   "GetByServiceType",
-					   service,
-					   service,
-					   str_offset,
-					   str_limit,
-					   NULL);
-
-	g_free (str_offset);
-	g_free (str_limit);
-
-	return result_set;
-}
-
-TrackerDBResultSet *
-tracker_db_files_get_by_mime (TrackerDBInterface  *iface,
-			      gchar		 **mimes,
-			      gint		   n,
-			      gint		   offset,
-			      gint		   limit,
-			      gboolean		   vfs)
-{
-	TrackerDBResultSet *result_set;
-	gint		    i;
-	gchar		   *service;
-	gchar		   *query;
-	GString		   *str;
-
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (mimes != NULL, NULL);
-	g_return_val_if_fail (offset >= 0, NULL);
-
-	if (vfs) {
-		service = "VFS";
-	} else {
-		service = "Files";
-	}
-
-	str = g_string_new ("SELECT  DISTINCT F.Path || '/' || F.Name AS uri FROM Services F INNER JOIN ServiceKeywordMetaData M ON F.ID = M.ServiceID WHERE M.MetaDataID = (SELECT ID FROM MetaDataTypes WHERE MetaName ='File:Mime') AND (M.MetaDataValue IN ");
-
-	g_string_append_printf (str, "('%s'", mimes[0]);
-
-	for (i = 1; i < n; i++) {
-		g_string_append_printf (str, ", '%s'", mimes[i]);
-	}
-
-	g_string_append_printf (str,
-				")) AND (F.ServiceTypeID in (select TypeId from ServiceTypes where TypeName = '%s' or Parent = '%s')) LIMIT %d,%d",
-				service,
-				service,
-				offset,
-				limit);
-
-	query = g_string_free (str, FALSE);
-	result_set = tracker_db_interface_execute_query (iface, NULL, query);
-	g_free (query);
-
-	return result_set;
-}
-
-TrackerDBResultSet *
-tracker_db_keywords_get_list (TrackerDBInterface *iface,
-			      const gchar	 *service)
-{
-	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
-	g_return_val_if_fail (service != NULL, NULL);
-
-	return tracker_data_manager_exec_proc (iface,
-				     "GetKeywordList",
-				     service,
-				     service,
-				     NULL);
-}
-
 TrackerFieldData *
 tracker_db_get_metadata_field (TrackerDBInterface *iface,
 			       const gchar	  *service,

Modified: branches/libtracker-data/src/libtracker-data/tracker-data-manager.h
==============================================================================
--- branches/libtracker-data/src/libtracker-data/tracker-data-manager.h	(original)
+++ branches/libtracker-data/src/libtracker-data/tracker-data-manager.h	Wed Nov  5 13:53:58 2008
@@ -38,7 +38,7 @@
 
 G_BEGIN_DECLS
 
-void		    tracker_data_manager_init				 (TrackerConfig       *config,
+void		    tracker_data_manager_init			 (TrackerConfig       *config,
 								  TrackerLanguage     *language,
 								  TrackerDBIndex      *file_index,
 								  TrackerDBIndex      *email_index);
@@ -80,51 +80,16 @@
 								  const gchar	      *id,
 								  const gchar	      *key);
 
-/* Search API */
-TrackerDBResultSet *tracker_db_search_text			 (TrackerDBInterface  *iface,
-								  const gchar	      *service,
-								  const gchar	      *search_string,
-								  gint		       offset,
-								  gint		       limit,
-								  gboolean	       save_results,
-								  gboolean	       detailed);
-TrackerDBResultSet *tracker_db_search_text_and_mime		 (TrackerDBInterface  *iface,
-								  const gchar	      *text,
-								  gchar		     **mime_array);
-TrackerDBResultSet *tracker_db_search_text_and_location		 (TrackerDBInterface  *iface,
-								  const gchar	      *text,
-								  const gchar	      *location);
-TrackerDBResultSet *tracker_db_search_text_and_mime_and_location (TrackerDBInterface  *iface,
-								  const gchar	      *text,
-								  gchar		     **mime_array,
-								  const gchar	      *location);
-
 /* Service API */
 gchar *		    tracker_db_service_get_by_entity		 (TrackerDBInterface  *iface,
 								  const gchar	      *id);
 
 /* Files API */
-gchar **	    tracker_db_files_get			 (TrackerDBInterface  *iface,
-								  const gchar	      *folder_uri);
-TrackerDBResultSet *tracker_db_files_get_by_service		 (TrackerDBInterface  *iface,
-								  const gchar	      *service,
-								  gint		       offset,
-								  gint		       limit);
-TrackerDBResultSet *tracker_db_files_get_by_mime		 (TrackerDBInterface  *iface,
-								  gchar		     **mimes,
-								  gint		       n,
-								  gint		       offset,
-								  gint		       limit,
-								  gboolean	       vfs);
 guint32		    tracker_db_file_get_id			 (const gchar         *service_type,
 								  const gchar	      *uri);
 gchar *		    tracker_db_file_get_id_as_string		 (const gchar	      *service_type,
 								  const gchar	      *uri);
 
-/* Keywords API */
-TrackerDBResultSet *tracker_db_keywords_get_list		 (TrackerDBInterface  *iface,
-								  const gchar	      *service);
-
 /* Miscellaneous API */
 gchar *		    tracker_db_get_field_name			 (const gchar	      *service,
 								  const gchar	      *meta_name);

Added: branches/libtracker-data/src/libtracker-data/tracker-data-search.c
==============================================================================
--- (empty file)
+++ branches/libtracker-data/src/libtracker-data/tracker-data-search.c	Wed Nov  5 13:53:58 2008
@@ -0,0 +1,624 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc gnome org)
+ * Copyright (C) 2007, Jason Kivlighn (jkivlighn gmail com)
+ * Copyright (C) 2007, Creative Commons (http://creativecommons.org)
+ * Copyright (C) 2008, Nokia
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <libtracker-common/tracker-type-utils.h>
+
+#include <libtracker-db/tracker-db-index.h>
+#include <libtracker-db/tracker-db-interface-sqlite.h>
+#include <libtracker-db/tracker-db-index-manager.h>
+#include <libtracker-db/tracker-db-manager.h>
+
+#include "tracker-data-manager.h"
+#include "tracker-data-search.h"
+#include "tracker-query-tree.h"
+
+TrackerDBResultSet *
+tracker_db_search_text (TrackerDBInterface *iface,
+			const gchar	   *service,
+			const gchar	   *search_string,
+			gint		    offset,
+			gint		    limit,
+			gboolean	    save_results,
+			gboolean	    detailed)
+{
+	TrackerQueryTree    *tree;
+	TrackerDBResultSet  *result_set, *result;
+	GArray		    *hits;
+	gint		     count;
+	const gchar	    *procedure;
+	GArray		    *services = NULL;
+	GSList		    *duds = NULL;
+	guint		     i = 0;
+
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (service != NULL, NULL);
+	g_return_val_if_fail (search_string != NULL, NULL);
+	g_return_val_if_fail (offset >= 0, NULL);
+
+	services = tracker_db_create_array_of_services (service, FALSE);
+	/* FIXME: Do we need both index and services here? We used to have it */
+	tree = tracker_query_tree_new (search_string,
+				       tracker_data_manager_get_config (),
+				       tracker_data_manager_get_language (),
+				       services);
+	hits = tracker_query_tree_get_hits (tree, offset, limit);
+	result = NULL;
+
+	if (save_results) {
+		tracker_db_interface_start_transaction (iface);
+		tracker_data_manager_exec_proc (iface,
+				      "DeleteSearchResults1",
+				      NULL);
+	}
+
+	count = 0;
+
+	for (i = 0; i < hits->len; i++) {
+		TrackerDBIndexItemRank	rank;
+		gchar		       *str_id;
+
+		if (count >= limit) {
+			break;
+		}
+
+		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
+		str_id = tracker_guint_to_string (rank.service_id);
+
+		/* We save results into SearchResults table instead of
+		 * returing an array of array of strings
+		 */
+		if (save_results) {
+			gchar *str_score;
+
+			str_score = tracker_gint_to_string (rank.score);
+			tracker_data_manager_exec_proc (iface,
+					      "InsertSearchResult1",
+					      str_id,
+					      str_score,
+					      NULL);
+			g_free (str_id);
+			g_free (str_score);
+
+			continue;
+		}
+
+		if (detailed) {
+			if (strcmp (service, "Emails") == 0) {
+				procedure = "GetEmailByID";
+			} else if (strcmp (service, "Applications") == 0) {
+				procedure = "GetApplicationByID";
+			} else {
+				procedure = "GetFileByID2";
+			}
+		} else {
+			procedure = "GetFileByID";
+		}
+
+		result_set = tracker_data_manager_exec_proc (iface,
+						   procedure,
+						   str_id,
+						   NULL);
+		g_free (str_id);
+
+		if (result_set) {
+			gchar *path;
+			guint  columns, i;
+
+			tracker_db_result_set_get (result_set, 0, &path, -1);
+
+				columns = tracker_db_result_set_get_n_columns (result_set);
+
+				if (G_UNLIKELY (!result)) {
+					guint columns;
+
+					columns = tracker_db_result_set_get_n_columns (result_set);
+					result = _tracker_db_result_set_new (columns);
+				}
+
+				_tracker_db_result_set_append (result);
+
+				for (i = 0; i < columns; i++) {
+					GValue value = { 0, };
+
+					_tracker_db_result_set_get_value (result_set, i, &value);
+					_tracker_db_result_set_set_value (result, i, &value);
+					g_value_unset (&value);
+				}
+
+			g_free (path);
+			g_object_unref (result_set);
+		} else {
+			g_message ("Dud hit for search detected");
+			duds = g_slist_prepend (duds, &rank);
+		}
+	}
+
+	if (save_results) {
+		tracker_db_interface_end_transaction (iface);
+	}
+
+	/* Delete duds */
+	if (duds) {
+		TrackerDBIndex *file_index;
+		TrackerDBIndex *email_index;
+		GSList	       *words, *w;
+
+		words = tracker_query_tree_get_words (tree);
+		file_index = tracker_db_index_manager_get_index (TRACKER_DB_INDEX_FILE);
+		email_index = tracker_db_index_manager_get_index (TRACKER_DB_INDEX_EMAIL);
+
+		for (w = words; w; w = w->next) {
+			tracker_db_index_remove_dud_hits (file_index,
+							  (const gchar *) w->data,
+							  duds);
+			tracker_db_index_remove_dud_hits (email_index,
+							  (const gchar *) w->data,
+							  duds);
+		}
+
+		g_slist_free (words);
+		g_slist_free (duds);
+	}
+
+	g_object_unref (tree);
+	g_array_free (hits, TRUE);
+
+	if (!result) {
+		return NULL;
+	}
+
+	if (tracker_db_result_set_get_n_rows (result) == 0) {
+		g_object_unref (result);
+		return NULL;
+	}
+
+	tracker_db_result_set_rewind (result);
+
+	return result;
+}
+
+TrackerDBResultSet *
+tracker_db_search_text_and_mime (TrackerDBInterface  *iface,
+				 const gchar	     *text,
+				 gchar		    **mime_array)
+{
+	TrackerQueryTree   *tree;
+	TrackerDBResultSet *result_set1;
+	GArray		   *hits;
+	GArray		   *services;
+	gint		    count = 0;
+	guint		    i;
+
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (text != NULL, NULL);
+	g_return_val_if_fail (mime_array != NULL, NULL);
+
+	result_set1 = NULL;
+	services = tracker_db_create_array_of_services (NULL, TRUE);
+
+	tree = tracker_query_tree_new (text,
+				       tracker_data_manager_get_config (),
+				       tracker_data_manager_get_language (),
+				       services);
+	hits = tracker_query_tree_get_hits (tree, 0, 0);
+
+	for (i = 0, count = 0; i < hits->len; i++) {
+		TrackerDBResultSet     *result_set2;
+		TrackerDBIndexItemRank	rank;
+		gchar		       *str_id, *mimetype;
+
+		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
+
+		str_id = tracker_guint_to_string (rank.service_id);
+		result_set2 = tracker_data_manager_exec_proc (iface,
+						    "GetFileByID",
+						    str_id,
+						    NULL);
+		g_free (str_id);
+
+		if (result_set2) {
+			tracker_db_result_set_get (result_set2, 2, &mimetype, -1);
+
+			if (tracker_string_in_string_list (mimetype, mime_array) != -1) {
+				GValue value = { 0, };
+
+				if (G_UNLIKELY (!result_set1)) {
+					result_set1 = _tracker_db_result_set_new (2);
+				}
+
+				_tracker_db_result_set_append (result_set1);
+
+				/* copy value in column 0 */
+				_tracker_db_result_set_get_value (result_set2, 0, &value);
+				_tracker_db_result_set_set_value (result_set1, 0, &value);
+				g_value_unset (&value);
+
+				/* copy value in column 1 */
+				_tracker_db_result_set_get_value (result_set2, 1, &value);
+				_tracker_db_result_set_set_value (result_set1, 1, &value);
+				g_value_unset (&value);
+
+				count++;
+			}
+
+			g_free (mimetype);
+			g_object_unref (result_set2);
+		}
+
+		if (count > 2047) {
+			g_warning ("Count is > 2047? Breaking for loop in %s, why?",
+				   __FUNCTION__);
+			break;
+		}
+	}
+
+	g_object_unref (tree);
+	g_array_free (hits, TRUE);
+
+	if (!result_set1) {
+		return NULL;
+	}
+
+	if (tracker_db_result_set_get_n_rows (result_set1) == 0) {
+		g_object_unref (result_set1);
+		return NULL;
+	}
+
+	tracker_db_result_set_rewind (result_set1);
+
+	return result_set1;
+}
+
+TrackerDBResultSet *
+tracker_db_search_text_and_location (TrackerDBInterface *iface,
+				     const gchar	*text,
+				     const gchar	*location)
+{
+	TrackerDBResultSet *result_set1;
+	TrackerQueryTree   *tree;
+	GArray		   *hits;
+	GArray		   *services;
+	gchar		   *location_prefix;
+	gint		    count;
+	guint		    i;
+
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (text != NULL, NULL);
+	g_return_val_if_fail (location != NULL, NULL);
+
+	result_set1 = NULL;
+	location_prefix = g_strconcat (location, G_DIR_SEPARATOR_S, NULL);
+	services = tracker_db_create_array_of_services (NULL, TRUE);
+
+	tree = tracker_query_tree_new (text,
+				       tracker_data_manager_get_config (),
+				       tracker_data_manager_get_language (),
+				       services);
+	hits = tracker_query_tree_get_hits (tree, 0, 0);
+
+	for (i = 0, count = 0; i < hits->len; i++) {
+		TrackerDBResultSet     *result_set2;
+		TrackerDBIndexItemRank	rank;
+		gchar		       *str_id, *path;
+
+		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
+
+		str_id = tracker_guint_to_string (rank.service_id);
+		result_set2 = tracker_data_manager_exec_proc (iface,
+						    "GetFileByID",
+						    str_id,
+						    NULL);
+		g_free (str_id);
+
+		if (result_set2) {
+			tracker_db_result_set_get (result_set2, 0, &path, -1);
+
+			if (g_str_has_prefix (path, location_prefix) ||
+			    strcmp (path, location) == 0) {
+				GValue value = { 0, };
+
+				if (G_UNLIKELY (!result_set1)) {
+					result_set1 = _tracker_db_result_set_new (2);
+				}
+
+				_tracker_db_result_set_append (result_set1);
+
+				/* copy value in column 0 */
+				_tracker_db_result_set_get_value (result_set2, 0, &value);
+				_tracker_db_result_set_set_value (result_set1, 0, &value);
+				g_value_unset (&value);
+
+				/* copy value in column 1 */
+				_tracker_db_result_set_get_value (result_set2, 1, &value);
+				_tracker_db_result_set_set_value (result_set1, 1, &value);
+				g_value_unset (&value);
+
+				count++;
+			}
+
+			g_object_unref (result_set2);
+		}
+
+		if (count > 2047) {
+			g_warning ("Count is > 2047? Breaking for loop in %s, why?",
+				   __FUNCTION__);
+			break;
+		}
+	}
+
+	g_free (location_prefix);
+	g_object_unref (tree);
+	g_array_free (hits, TRUE);
+
+	if (!result_set1) {
+		return NULL;
+	}
+
+	if (tracker_db_result_set_get_n_rows (result_set1) == 0) {
+		g_object_unref (result_set1);
+		return NULL;
+	}
+
+	tracker_db_result_set_rewind (result_set1);
+
+	return result_set1;
+}
+
+TrackerDBResultSet *
+tracker_db_search_text_and_mime_and_location (TrackerDBInterface  *iface,
+					      const gchar	  *text,
+					      gchar		 **mime_array,
+					      const gchar	  *location)
+{
+	TrackerDBResultSet *result_set1;
+	TrackerQueryTree   *tree;
+	GArray		   *hits;
+	GArray		   *services;
+	gchar		   *location_prefix;
+	gint		    count;
+	guint		    i;
+
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (text != NULL, NULL);
+	g_return_val_if_fail (location != NULL, NULL);
+
+	result_set1 = NULL;
+	location_prefix = g_strconcat (location, G_DIR_SEPARATOR_S, NULL);
+	services = tracker_db_create_array_of_services (NULL, TRUE);
+
+	tree = tracker_query_tree_new (text,
+				       tracker_data_manager_get_config (),
+				       tracker_data_manager_get_language (),
+				       services);
+	hits = tracker_query_tree_get_hits (tree, 0, 0);
+
+	for (i = 0, count = 0; i < hits->len; i++) {
+		TrackerDBResultSet     *result_set2;
+		TrackerDBIndexItemRank	rank;
+		gchar		       *str_id, *path, *mimetype;
+
+		rank = g_array_index (hits, TrackerDBIndexItemRank, i);
+
+		str_id = tracker_guint_to_string (rank.service_id);
+		result_set2 = tracker_data_manager_exec_proc (iface,
+						    "GetFileByID",
+						    str_id,
+						    NULL);
+		g_free (str_id);
+
+		if (result_set2) {
+			tracker_db_result_set_get (result_set2,
+						   0, &path,
+						   2, &mimetype,
+						   -1);
+
+			if ((g_str_has_prefix (path, location_prefix) ||
+			     strcmp (path, location) == 0) &&
+			    tracker_string_in_string_list (mimetype, mime_array) != -1) {
+				GValue value = { 0, };
+
+				if (G_UNLIKELY (!result_set1)) {
+					result_set1 = _tracker_db_result_set_new (2);
+				}
+
+				_tracker_db_result_set_append (result_set1);
+
+				/* copy value in column 0 */
+				_tracker_db_result_set_get_value (result_set2, 0, &value);
+				_tracker_db_result_set_set_value (result_set1, 0, &value);
+				g_value_unset (&value);
+
+				/* copy value in column 1 */
+				_tracker_db_result_set_get_value (result_set2, 1, &value);
+				_tracker_db_result_set_set_value (result_set1, 1, &value);
+				g_value_unset (&value);
+
+				count++;
+			}
+
+			g_free (path);
+			g_free (mimetype);
+			g_object_unref (result_set2);
+		}
+
+		if (count > 2047) {
+			g_warning ("Count is > 2047? Breaking for loop in %s, why?",
+				   __FUNCTION__);
+			break;
+		}
+	}
+
+	g_free (location_prefix);
+	g_object_unref (tree);
+	g_array_free (hits, TRUE);
+
+	if (!result_set1) {
+		return NULL;
+	}
+
+	if (tracker_db_result_set_get_n_rows (result_set1) == 0) {
+		g_object_unref (result_set1);
+		return NULL;
+	}
+
+	tracker_db_result_set_rewind (result_set1);
+
+	return result_set1;
+}
+
+gchar **
+tracker_db_files_get (TrackerDBInterface *iface,
+		      const gchar	 *uri)
+{
+	TrackerDBResultSet *result_set;
+	GPtrArray	   *array;
+
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (uri != NULL, NULL);
+
+	result_set = tracker_data_manager_exec_proc (iface,
+					   "SelectFileChild",
+					   uri,
+					   NULL);
+	array = g_ptr_array_new ();
+
+	if (result_set) {
+		gchar	 *name, *prefix;
+		gboolean  valid = TRUE;
+
+		while (valid) {
+			tracker_db_result_set_get (result_set,
+						   1, &prefix,
+						   2, &name,
+						   -1);
+
+			g_ptr_array_add (array, g_build_filename (prefix, name, NULL));
+
+			g_free (prefix);
+			g_free (name);
+			valid = tracker_db_result_set_iter_next (result_set);
+		}
+
+		g_object_unref (result_set);
+	}
+
+	g_ptr_array_add (array, NULL);
+
+	return (gchar**) g_ptr_array_free (array, FALSE);
+}
+
+TrackerDBResultSet *
+tracker_db_files_get_by_service (TrackerDBInterface *iface,
+				 const gchar	    *service,
+				 gint		     offset,
+				 gint		     limit)
+{
+	TrackerDBResultSet *result_set;
+	gchar		   *str_limit;
+	gchar		   *str_offset;
+
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (service != NULL, NULL);
+
+	str_limit = tracker_gint_to_string (limit);
+	str_offset = tracker_gint_to_string (offset);
+
+	result_set = tracker_data_manager_exec_proc (iface,
+					   "GetByServiceType",
+					   service,
+					   service,
+					   str_offset,
+					   str_limit,
+					   NULL);
+
+	g_free (str_offset);
+	g_free (str_limit);
+
+	return result_set;
+}
+
+TrackerDBResultSet *
+tracker_db_files_get_by_mime (TrackerDBInterface  *iface,
+			      gchar		 **mimes,
+			      gint		   n,
+			      gint		   offset,
+			      gint		   limit,
+			      gboolean		   vfs)
+{
+	TrackerDBResultSet *result_set;
+	gint		    i;
+	gchar		   *service;
+	gchar		   *query;
+	GString		   *str;
+
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (mimes != NULL, NULL);
+	g_return_val_if_fail (offset >= 0, NULL);
+
+	if (vfs) {
+		service = "VFS";
+	} else {
+		service = "Files";
+	}
+
+	str = g_string_new ("SELECT  DISTINCT F.Path || '/' || F.Name AS uri FROM Services F INNER JOIN ServiceKeywordMetaData M ON F.ID = M.ServiceID WHERE M.MetaDataID = (SELECT ID FROM MetaDataTypes WHERE MetaName ='File:Mime') AND (M.MetaDataValue IN ");
+
+	g_string_append_printf (str, "('%s'", mimes[0]);
+
+	for (i = 1; i < n; i++) {
+		g_string_append_printf (str, ", '%s'", mimes[i]);
+	}
+
+	g_string_append_printf (str,
+				")) AND (F.ServiceTypeID in (select TypeId from ServiceTypes where TypeName = '%s' or Parent = '%s')) LIMIT %d,%d",
+				service,
+				service,
+				offset,
+				limit);
+
+	query = g_string_free (str, FALSE);
+	result_set = tracker_db_interface_execute_query (iface, NULL, query);
+	g_free (query);
+
+	return result_set;
+}
+
+TrackerDBResultSet *
+tracker_db_keywords_get_list (TrackerDBInterface *iface,
+			      const gchar	 *service)
+{
+	g_return_val_if_fail (TRACKER_IS_DB_INTERFACE (iface), NULL);
+	g_return_val_if_fail (service != NULL, NULL);
+
+	return tracker_data_manager_exec_proc (iface,
+				     "GetKeywordList",
+				     service,
+				     service,
+				     NULL);
+}
+

Added: branches/libtracker-data/src/libtracker-data/tracker-data-search.h
==============================================================================
--- (empty file)
+++ branches/libtracker-data/src/libtracker-data/tracker-data-search.h	Wed Nov  5 13:53:58 2008
@@ -0,0 +1,79 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc gnome org)
+ * Copyright (C) 2007, Jason Kivlighn (jkivlighn gmail com)
+ * Copyright (C) 2007, Creative Commons (http://creativecommons.org)
+ * Copyright (C) 2008, Nokia
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ */
+
+#ifndef __TRACKER_DATA_SEARCH_H__
+#define __TRACKER_DATA_SEARCH_H__
+
+#include <glib.h>
+
+#include <libtracker-common/tracker-field.h>
+#include <libtracker-common/tracker-field-data.h>
+#include <libtracker-common/tracker-language.h>
+#include <libtracker-common/tracker-ontology.h>
+
+#include <libtracker-db/tracker-db-interface.h>
+#include <libtracker-db/tracker-db-file-info.h>
+#include <libtracker-db/tracker-db-index.h>
+
+G_BEGIN_DECLS
+
+/* Search API */
+TrackerDBResultSet *tracker_db_search_text			 (TrackerDBInterface  *iface,
+								  const gchar	      *service,
+								  const gchar	      *search_string,
+								  gint		       offset,
+								  gint		       limit,
+								  gboolean	       save_results,
+								  gboolean	       detailed);
+TrackerDBResultSet *tracker_db_search_text_and_mime		 (TrackerDBInterface  *iface,
+								  const gchar	      *text,
+								  gchar		     **mime_array);
+TrackerDBResultSet *tracker_db_search_text_and_location		 (TrackerDBInterface  *iface,
+								  const gchar	      *text,
+								  const gchar	      *location);
+TrackerDBResultSet *tracker_db_search_text_and_mime_and_location (TrackerDBInterface  *iface,
+								  const gchar	      *text,
+								  gchar		     **mime_array,
+								  const gchar	      *location);
+
+/* Files API */
+gchar **	    tracker_db_files_get			 (TrackerDBInterface  *iface,
+								  const gchar	      *folder_uri);
+TrackerDBResultSet *tracker_db_files_get_by_service		 (TrackerDBInterface  *iface,
+								  const gchar	      *service,
+								  gint		       offset,
+								  gint		       limit);
+TrackerDBResultSet *tracker_db_files_get_by_mime		 (TrackerDBInterface  *iface,
+								  gchar		     **mimes,
+								  gint		       n,
+								  gint		       offset,
+								  gint		       limit,
+								  gboolean	       vfs);
+
+/* Keywords API */
+TrackerDBResultSet *tracker_db_keywords_get_list		 (TrackerDBInterface  *iface,
+								  const gchar	      *service);
+
+G_END_DECLS
+
+#endif /* __TRACKER_DATA_SEARCH_H__ */

Modified: branches/libtracker-data/src/trackerd/tracker-files.c
==============================================================================
--- branches/libtracker-data/src/trackerd/tracker-files.c	(original)
+++ branches/libtracker-data/src/trackerd/tracker-files.c	Wed Nov  5 13:53:58 2008
@@ -33,9 +33,11 @@
 #include <libtracker-db/tracker-db-dbus.h>
 #include <libtracker-db/tracker-db-manager.h>
 
+#include <libtracker-data/tracker-data-manager.h>
+#include <libtracker-data/tracker-data-search.h>
+
 #include "tracker-dbus.h"
 #include "tracker-files.h"
-#include <libtracker-data/tracker-data-manager.h>
 #include "tracker-marshal.h"
 #include "tracker-indexer-client.h"
 



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