[tracker-miners/wip/carlosg/perf-squeeze: 2/16] libtracker-miner: Add notifier query to check for file existence



commit 91e6b2f24a3a42fa784590861cde7b727116147c
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Jul 4 13:39:49 2020 +0200

    libtracker-miner: Add notifier query to check for file existence
    
    When handling a move event, the miner checks that the source file existed
    previously by checking the information element URN. Add an extra file
    notifier query that checks the nfo:FileDataObject existence in the
    tracker:FileSystem graph instead.
    
    This avoids having to poke every other graph in order to get this URN.

 src/libtracker-miner/tracker-file-notifier.c | 86 ++++++++++++++++++++++++++++
 src/libtracker-miner/tracker-file-notifier.h |  2 +
 src/libtracker-miner/tracker-miner-fs.c      |  6 +-
 3 files changed, 90 insertions(+), 4 deletions(-)
---
diff --git a/src/libtracker-miner/tracker-file-notifier.c b/src/libtracker-miner/tracker-file-notifier.c
index 987b1a8f3..5768d141d 100644
--- a/src/libtracker-miner/tracker-file-notifier.c
+++ b/src/libtracker-miner/tracker-file-notifier.c
@@ -83,6 +83,7 @@ typedef struct {
 
        TrackerSparqlStatement *content_query;
        TrackerSparqlStatement *urn_query;
+       TrackerSparqlStatement *exists_query;
 
        GTimer *timer;
 
@@ -711,6 +712,30 @@ sparql_urn_ensure_statement (TrackerFileNotifier  *notifier,
        return priv->urn_query;
 }
 
+static TrackerSparqlStatement *
+sparql_exists_ensure_statement (TrackerFileNotifier  *notifier,
+                               GError              **error)
+{
+       TrackerFileNotifierPrivate *priv;
+
+       priv = tracker_file_notifier_get_instance_private (notifier);
+
+       if (priv->exists_query)
+               return priv->exists_query;
+
+       priv->exists_query =
+               tracker_sparql_connection_query_statement (priv->connection,
+                                                          "ASK "
+                                                          "{"
+                                                          "  GRAPH tracker:FileSystem {"
+                                                          "    ~file a nfo:FileDataObject ."
+                                                          "  }"
+                                                          "}",
+                                                          priv->cancellable,
+                                                          error);
+       return priv->exists_query;
+}
+
 static void
 query_execute_cb (TrackerSparqlStatement *statement,
                   GAsyncResult           *res,
@@ -1526,6 +1551,7 @@ tracker_file_notifier_finalize (GObject *object)
 
        g_clear_object (&priv->content_query);
        g_clear_object (&priv->urn_query);
+       g_clear_object (&priv->exists_query);
 
        g_object_unref (priv->crawler);
        g_object_unref (priv->monitor);
@@ -1943,6 +1969,66 @@ tracker_file_notifier_get_file_iri (TrackerFileNotifier *notifier,
        return iri;
 }
 
+gboolean
+tracker_file_notifier_query_file_exists (TrackerFileNotifier *notifier,
+                                        GFile               *file)
+{
+       TrackerFileNotifierPrivate *priv;
+       GFile *canonical;
+       gboolean found;
+
+       g_return_val_if_fail (TRACKER_IS_FILE_NOTIFIER (notifier), FALSE);
+       g_return_val_if_fail (G_IS_FILE (file), FALSE);
+
+       priv = tracker_file_notifier_get_instance_private (notifier);
+
+       if (G_UNLIKELY (priv->connection == NULL)) {
+               return FALSE;
+       }
+
+       canonical = tracker_file_system_get_file (priv->file_system,
+                                                 file,
+                                                 G_FILE_TYPE_REGULAR,
+                                                 NULL);
+       if (!canonical) {
+               return FALSE;
+       }
+
+       found = tracker_file_system_get_property_full (priv->file_system,
+                                                      canonical,
+                                                      quark_property_mimetype,
+                                                      NULL);
+
+       if (!found) {
+               TrackerSparqlCursor *cursor;
+               TrackerSparqlStatement *statement;
+               gchar *uri;
+
+               /* Fetch data for this file synchronously */
+               statement = sparql_exists_ensure_statement (notifier, NULL);
+               if (!statement)
+                       return FALSE;
+
+               uri = g_file_get_uri (file);
+               tracker_sparql_statement_bind_string (statement, "file", uri);
+               g_free (uri);
+
+               cursor = tracker_sparql_statement_execute (statement, NULL, NULL);
+               if (!cursor)
+                       return FALSE;
+
+               if (!tracker_sparql_cursor_next (cursor, NULL, NULL)) {
+                       g_object_unref (cursor);
+                       return FALSE;
+               }
+
+               found = tracker_sparql_cursor_get_boolean (cursor, 0);
+               g_object_unref (cursor);
+       }
+
+       return found;
+}
+
 static gboolean
 file_notifier_invalidate_file_iri_foreach (GFile    *file,
                                            gpointer  user_data)
diff --git a/src/libtracker-miner/tracker-file-notifier.h b/src/libtracker-miner/tracker-file-notifier.h
index 592dfee2c..f9cd69202 100644
--- a/src/libtracker-miner/tracker-file-notifier.h
+++ b/src/libtracker-miner/tracker-file-notifier.h
@@ -92,6 +92,8 @@ const gchar * tracker_file_notifier_get_file_iri (TrackerFileNotifier     *notif
 void          tracker_file_notifier_invalidate_file_iri (TrackerFileNotifier *notifier,
                                                          GFile               *file,
                                                          gboolean             recursive);
+gboolean      tracker_file_notifier_query_file_exists (TrackerFileNotifier *notifier,
+                                                      GFile               *file);
 
 GFileType     tracker_file_notifier_get_file_type (TrackerFileNotifier *notifier,
                                                    GFile               *file);
diff --git a/src/libtracker-miner/tracker-miner-fs.c b/src/libtracker-miner/tracker-miner-fs.c
index fe393253a..862e514b6 100644
--- a/src/libtracker-miner/tracker-miner-fs.c
+++ b/src/libtracker-miner/tracker-miner-fs.c
@@ -1455,7 +1455,6 @@ item_move (TrackerMinerFS *fs,
 {
        gchar     *uri, *source_uri, *sparql;
        GFileInfo *file_info;
-       const gchar *source_iri;
        gboolean source_exists;
        TrackerDirectoryFlags source_flags, flags;
        gboolean recursive;
@@ -1470,9 +1469,8 @@ item_move (TrackerMinerFS *fs,
                                       NULL, NULL);
 
        /* Get 'source' ID */
-       source_iri = tracker_file_notifier_get_file_iri (fs->priv->file_notifier,
-                                                        source_file, TRUE);
-       source_exists = (source_iri != NULL);
+       source_exists = tracker_file_notifier_query_file_exists (fs->priv->file_notifier,
+                                                                source_file);
 
        if (!file_info) {
                gboolean retval;


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