[gnome-photos/sam/tracker3: 13/27] Port queries to Tracker 3




commit 3f335f9b3bb934f4bed193e053158b6e9f5d1d5e
Author: Sam Thursfield <sam afuera me uk>
Date:   Mon Jun 8 13:13:36 2020 +0200

    Port queries to Tracker 3

 src/photos-item-manager.c                   |  2 +-
 src/photos-query-builder.c                  | 65 ++++++++++++++++-------------
 src/photos-source.c                         | 13 +++---
 src/photos-tracker-queue.c                  | 19 ++++++++-
 src/queries/all.sparql.template             | 20 +++++----
 src/queries/collections.sparql.template     |  2 +-
 src/queries/favorite-photos.sparql.template | 18 +++++---
 src/queries/photos.sparql.template          | 19 ++++++---
 8 files changed, 101 insertions(+), 57 deletions(-)
---
diff --git a/src/photos-item-manager.c b/src/photos-item-manager.c
index 74ac4a6c..a431ebb7 100644
--- a/src/photos-item-manager.c
+++ b/src/photos-item-manager.c
@@ -717,7 +717,7 @@ photos_item_manager_wait_for_changes_timeout (gpointer user_data)
       g_autoptr (PhotosQuery) query = NULL;
       g_autofree gchar *sparql = NULL;
 
-      sparql = g_strdup_printf ("SELECT ?urn nie:url (?urn) WHERE { ?urn nie:url '%s' }", uri);
+      sparql = g_strdup_printf ("SELECT ?urn nie:isStoredAs (?urn) WHERE { ?urn nie:isStoredAs '%s' }", uri);
       query = photos_query_new (NULL, sparql);
       photos_tracker_queue_select (self->queue,
                                    query,
diff --git a/src/photos-query-builder.c b/src/photos-query-builder.c
index a614d82f..dcc365de 100644
--- a/src/photos-query-builder.c
+++ b/src/photos-query-builder.c
@@ -53,7 +53,8 @@ photos_query_builder_query (PhotosSearchContextState *state,
                             PhotosOffsetController *offset_cntrlr)
 {
   PhotosSparqlTemplate *template;
-  const gchar *projection = NULL;
+  const gchar *outer_projection = NULL;
+  const gchar *inner_projection = NULL;
   g_autofree gchar *item_pattern = NULL;
   g_autofree gchar *search_filter = NULL;
   g_autofree gchar *source_filter = NULL;
@@ -63,29 +64,33 @@ photos_query_builder_query (PhotosSearchContextState *state,
 
   template = photos_base_manager_get_sparql_template (state->srch_typ_mngr, flags);
 
-  projection = "?urn "
-               "nie:url (?urn) "
-               "nfo:fileName (?urn) "
-               "nie:mimeType (?urn) "
-               "nie:title (?urn) "
-               "tracker:coalesce (nco:fullname (?creator), nco:fullname (?publisher), '') "
-               "tracker:coalesce (nfo:fileLastModified (?urn), nie:contentLastModified (?urn)) AS ?mtime "
-               "nao:identifier (?urn) "
-               "rdf:type (?urn) "
-               "nie:dataSource(?urn) "
-               "( EXISTS { ?urn nao:hasTag nao:predefined-tag-favorite } ) "
-               "( EXISTS { ?urn nco:contributor ?contributor FILTER ( ?contributor != ?creator ) } ) "
-               "tracker:coalesce(nfo:fileCreated (?urn), nie:contentCreated (?urn)) "
-               "nfo:width (?urn) "
-               "nfo:height (?urn) "
-               "nfo:equipment (?urn) "
-               "nfo:orientation (?urn) "
-               "nmm:exposureTime (?urn) "
-               "nmm:fnumber (?urn) "
-               "nmm:focalLength (?urn) "
-               "nmm:isoSpeed (?urn) "
-               "nmm:flash (?urn) "
-               "slo:location (?urn) ";
+  outer_projection = "?urn ?file ?filename ?mimetype ?title ?author_name ?mtime ?identifier ?type 
?datasource "
+                     "( EXISTS { ?urn nao:hasTag nao:predefined-tag-favorite } ) AS ?is_favorite"
+                     "?has_contributor ?ctime ?width ?height ?equipment ?orientation ?exposure_time ?fnumber 
"
+                     "?focal_length ?isospeed ?flash ?location ";
+
+  inner_projection = "?urn "
+                     "nie:isStoredAs (?urn) AS ?file "
+                     "nfo:fileName (?file) AS ?filename "
+                     "nie:mimeType (?urn) AS ?mimetype "
+                     "nie:title (?urn) AS ?title "
+                     "tracker:coalesce (nco:fullname (?creator), nco:fullname (?publisher), '') AS 
?author_name "
+                     "tracker:coalesce (nfo:fileLastModified (?file), nie:contentLastModified (?urn)) AS 
?mtime "
+                     "nao:identifier (?urn) AS ?identifier "
+                     "rdf:type (?urn) AS ?type "
+                     "nie:dataSource(?urn) AS ?datasource "
+                     "( EXISTS { ?urn nco:contributor ?contributor FILTER ( ?contributor != ?creator ) } ) 
AS ?has_contributor "
+                     "tracker:coalesce(nfo:fileCreated (?file), nie:contentCreated (?urn)) AS ?ctime "
+                     "nfo:width (?urn) AS ?width "
+                     "nfo:height (?urn) AS ?height "
+                     "nfo:equipment (?urn) AS ?equipment "
+                     "nfo:orientation (?urn) AS ?orientation "
+                     "nmm:exposureTime (?urn) AS ?exposure_time "
+                     "nmm:fnumber (?urn) AS ?fnumber "
+                     "nmm:focalLength (?urn) AS ?focal_length "
+                     "nmm:isoSpeed (?urn) AS ?isospeed "
+                     "nmm:flash (?urn) AS ?flash "
+                     "slo:location (?urn) AS ?location ";
 
   item_pattern = photos_base_manager_get_where (state->item_mngr, flags);
 
@@ -112,7 +117,8 @@ photos_query_builder_query (PhotosSearchContextState *state,
     }
 
   sparql = photos_sparql_template_get_sparql (template,
-                                              "projection", projection,
+                                              "outer_projection", outer_projection,
+                                              "inner_projection", inner_projection,
                                               "collections_default_filter", collections_default_filter,
                                               "item_pattern", item_pattern,
                                               "photos_default_filter", photos_default_filter,
@@ -180,7 +186,8 @@ PhotosQuery *
 photos_query_builder_count_query (PhotosSearchContextState *state, gint flags)
 {
   PhotosSparqlTemplate *template;
-  const gchar *projection = NULL;
+  const gchar *inner_projection = NULL;
+  const gchar *outer_projection = NULL;
   g_autofree gchar *item_pattern = NULL;
   g_autofree gchar *search_filter = NULL;
   g_autofree gchar *source_filter = NULL;
@@ -189,7 +196,8 @@ photos_query_builder_count_query (PhotosSearchContextState *state, gint flags)
 
   template = photos_base_manager_get_sparql_template (state->srch_typ_mngr, flags);
 
-  projection = "COUNT(?urn) ";
+  outer_projection = "?count ";
+  inner_projection = "COUNT(?urn) AS ?count ";
 
   item_pattern = photos_base_manager_get_where (state->item_mngr, flags);
 
@@ -200,7 +208,8 @@ photos_query_builder_count_query (PhotosSearchContextState *state, gint flags)
     }
 
   sparql = photos_sparql_template_get_sparql (template,
-                                              "projection", projection,
+                                              "outer_projection", outer_projection,
+                                              "inner_projection", inner_projection,
                                               "collections_default_filter", collections_default_filter,
                                               "item_pattern", item_pattern,
                                               "photos_default_filter", photos_default_filter,
diff --git a/src/photos-source.c b/src/photos-source.c
index db6f2de9..4e2bc483 100644
--- a/src/photos-source.c
+++ b/src/photos-source.c
@@ -94,9 +94,10 @@ photos_source_build_filter_local (void)
         continue;
 
       tracker_uri = photos_utils_convert_path_to_uri (tracker_dirs[i]);
-      g_string_append_printf (tracker_filter, " || fn:contains (nie:url (?urn), '%s')", tracker_uri);
+      g_string_append_printf (tracker_filter, " || fn:contains (nie:isStoredAs (?urn), '%s')", tracker_uri);
     }
 
+  /* FIXME: this could be done differently for 3.0, see 
https://gitlab.gnome.org/GNOME/tracker-miners/-/merge_requests/206 */
   path = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
   desktop_uri = photos_utils_convert_path_to_uri (path);
 
@@ -109,11 +110,11 @@ photos_source_build_filter_local (void)
   export_path = g_build_filename (path, PHOTOS_EXPORT_SUBPATH, NULL);
   export_uri = photos_utils_convert_path_to_uri (export_path);
 
-  filter = g_strdup_printf ("(((fn:contains (nie:url (?urn), '%s')"
-                            "   || fn:contains (nie:url (?urn), '%s')"
-                            "   || fn:contains (nie:url (?urn), '%s')"
+  filter = g_strdup_printf ("(((fn:contains (nie:isStoredAs (?urn), '%s')"
+                            "   || fn:contains (nie:isStoredAs (?urn), '%s')"
+                            "   || fn:contains (nie:isStoredAs (?urn), '%s')"
                             "   %s)"
-                            "  && !fn:contains (nie:url (?urn), '%s'))"
+                            "  && !fn:contains (nie:isStoredAs (?urn), '%s'))"
                             " || fn:starts-with (nao:identifier (?urn), '%s')"
                             " || (?urn = nfo:image-category-screenshot))",
                             desktop_uri,
@@ -146,7 +147,7 @@ photos_source_build_filter_resource (PhotosSource *self)
 
       root = g_mount_get_root (self->mount);
       uri = g_file_get_uri (root);
-      filter = g_strdup_printf ("(fn:starts-with (nie:url (?urn), '%s'))", uri);
+      filter = g_strdup_printf ("(fn:starts-with (nie:isStoredAs (?urn), '%s'))", uri);
     }
   else
     {
diff --git a/src/photos-tracker-queue.c b/src/photos-tracker-queue.c
index d2bdbaa6..ecba0dae 100644
--- a/src/photos-tracker-queue.c
+++ b/src/photos-tracker-queue.c
@@ -274,6 +274,8 @@ static gboolean
 photos_tracker_queue_initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
 {
   PhotosTrackerQueue *self = PHOTOS_TRACKER_QUEUE (initable);
+  TrackerSparqlConnectionFlags tracker_flags;
+  g_autoptr (GFile) store = NULL;
   gboolean ret_val = FALSE;
 
   G_LOCK (init_lock);
@@ -290,8 +292,21 @@ photos_tracker_queue_initable_init (GInitable *initable, GCancellable *cancellab
 
   g_assert_no_error (self->initialization_error);
 
-  self->connection = tracker_sparql_connection_bus_new (
-    "org.freedesktop.Tracker3.Miner.Files", NULL, NULL, &self->initialization_error);
+  /* Same flags that tracker-miner-fs uses by default. See:
+   * https://gitlab.gnome.org/GNOME/tracker-miners/-/blob/master/src/miners/fs/tracker-main.c#L735 and
+   * 
https://gitlab.gnome.org/GNOME/tracker-miners/-/blob/master/data/org.freedesktop.Tracker.FTS.gschema.xml */
+  tracker_flags = TRACKER_SPARQL_CONNECTION_FLAGS_FTS_ENABLE_UNACCENT |
+                  TRACKER_SPARQL_CONNECTION_FLAGS_FTS_ENABLE_STOP_WORDS |
+                  TRACKER_SPARQL_CONNECTION_FLAGS_FTS_IGNORE_NUMBERS;
+
+  store = g_file_new_build_filename (g_get_user_data_dir (), "gnome-photos", NULL);
+
+  self->connection = tracker_sparql_connection_new (tracker_flags,
+                                                    store,
+                                                    tracker_sparql_get_ontology_nepomuk (),
+                                                    cancellable,
+                                                    &self->initialization_error);
+
   if (G_UNLIKELY (self->initialization_error != NULL))
     goto out;
 
diff --git a/src/queries/all.sparql.template b/src/queries/all.sparql.template
index 1cef98e8..ac3f1f04 100644
--- a/src/queries/all.sparql.template
+++ b/src/queries/all.sparql.template
@@ -1,7 +1,8 @@
-SELECT {{projection}}
+SELECT {{outer_projection}}
+FROM NAMED tracker:Pictures
 {
     {
-        SELECT {{projection}}
+        SELECT {{inner_projection}}
         {
             {
                 SELECT ?urn COUNT(?item) AS ?count
@@ -16,13 +17,16 @@ SELECT {{projection}}
     }
     UNION
     {
-        SELECT {{projection}}
+        SELECT {{inner_projection}}
         {
-            ?urn a nmm:Photo .
-            OPTIONAL { ?urn nco:creator ?creator . }
-            OPTIONAL { ?urn nco:publisher ?publisher . }
-            {{item_pattern}}
-            FILTER ({{photos_default_filter}} && {{source_filter}} && {{search_filter}})
+            SERVICE <dbus:org.freedesktop.Tracker3.Miner.Files>
+            {
+                ?urn a nmm:Photo .
+                OPTIONAL { ?urn nco:creator ?creator . }
+                OPTIONAL { ?urn nco:publisher ?publisher . }
+                {{item_pattern}}
+                FILTER ({{photos_default_filter}} && {{source_filter}} && {{search_filter}})
+            }
         }
         GROUP BY ?urn
     }
diff --git a/src/queries/collections.sparql.template b/src/queries/collections.sparql.template
index 20b35cd6..8aa8550b 100644
--- a/src/queries/collections.sparql.template
+++ b/src/queries/collections.sparql.template
@@ -1,4 +1,4 @@
-SELECT {{projection}}
+SELECT {{inner_projection}}
 {
     {
         SELECT ?urn COUNT(?item) AS ?count
diff --git a/src/queries/favorite-photos.sparql.template b/src/queries/favorite-photos.sparql.template
index 0885a08a..edfe6006 100644
--- a/src/queries/favorite-photos.sparql.template
+++ b/src/queries/favorite-photos.sparql.template
@@ -1,11 +1,19 @@
-SELECT {{projection}}
+SELECT {{outer_projection}}
+FROM tracker:Pictures
 {
     ?urn a nmm:Photo .
     ?urn a nmm:Photo; nao:hasTag nao:predefined-tag-favorite .
-    OPTIONAL { ?urn nco:creator ?creator . }
-    OPTIONAL { ?urn nco:publisher ?publisher . }
-    {{item_pattern}}
-    FILTER ({{photos_default_filter}} && {{source_filter}} && {{search_filter}})
+    SERVICE <dbus:org.freedesktop.Tracker3.Miner.Files>
+    {
+        SELECT {{inner_projection}}
+        {
+            ?urn a nmm:Photo .
+            OPTIONAL { ?urn nco:creator ?creator . }
+            OPTIONAL { ?urn nco:publisher ?publisher . }
+            {{item_pattern}}
+            FILTER ({{photos_default_filter}} && {{source_filter}} && {{search_filter}})
+        }
+    }
 }
 GROUP BY ?urn
 {{order}}
diff --git a/src/queries/photos.sparql.template b/src/queries/photos.sparql.template
index 4eb10b74..75e4689a 100644
--- a/src/queries/photos.sparql.template
+++ b/src/queries/photos.sparql.template
@@ -1,10 +1,17 @@
-SELECT {{projection}}
+SELECT {{outer_projection}}
+FROM tracker:Pictures
 {
-    ?urn a nmm:Photo .
-    OPTIONAL { ?urn nco:creator ?creator . }
-    OPTIONAL { ?urn nco:publisher ?publisher . }
-    {{item_pattern}}
-    FILTER ({{photos_default_filter}} && {{source_filter}} && {{search_filter}})
+    SERVICE <dbus:org.freedesktop.Tracker3.Miner.Files>
+    {
+        SELECT {{inner_projection}}
+        {
+            ?urn a nmm:Photo .
+            OPTIONAL { ?urn nco:creator ?creator . }
+            OPTIONAL { ?urn nco:publisher ?publisher . }
+            {{item_pattern}}
+            FILTER ({{photos_default_filter}} && {{source_filter}} && {{search_filter}})
+        }
+    }
 }
 GROUP BY ?urn
 {{order}}


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