[tracker/wip/carlosg/anonymous-nodes: 44/46] libtracker-data: Implement anonymous blank nodes




commit 3d39067597aa2d2bf2f9aeecaa202a699501a25d
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Sep 28 12:01:40 2021 +0200

    libtracker-data: Implement anonymous blank nodes
    
    Now that resources are dealt with as ROWIDs despite them being
    named or blank nodes, and that blank nodes don't actually get
    a generated URN that is stored in the database, we can make this
    a switch to either query on URI + bnode name, or just URI.
    
    This flag enables the blank node semantics defined by the SPARQL
    spec, where blank nodes cannot be matched by name.
    
    This is exposed as a TrackerDBManager flag, unused at the moment.

 src/libtracker-data/tracker-data-update.c | 14 ++++++++++++++
 src/libtracker-data/tracker-db-manager.h  |  1 +
 src/libtracker-data/tracker-sparql.c      | 18 +++++++++++++-----
 3 files changed, 28 insertions(+), 5 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 26cd393e8..d0b68edce 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -685,6 +685,8 @@ tracker_data_update_ensure_resource (TrackerData  *data,
                                      const gchar  *uri,
                                      GError      **error)
 {
+       TrackerDBManager *db_manager;
+       TrackerDBManagerFlags db_flags;
        TrackerDBInterface *iface;
        TrackerDBStatement *stmt = NULL;
        GError *inner_error = NULL;
@@ -697,6 +699,18 @@ tracker_data_update_ensure_resource (TrackerData  *data,
                return *value;
        }
 
+       db_manager = tracker_data_manager_get_db_manager (data->manager);
+       db_flags = tracker_db_manager_get_flags (db_manager, NULL, NULL);
+
+       if ((db_flags & TRACKER_DB_MANAGER_ANONYMOUS_BNODES) == 0 &&
+           g_str_has_prefix (uri, "urn:bnode:")) {
+               gchar *end;
+
+               id = g_ascii_strtoll (&uri[strlen ("urn:bnode:")], &end, 10);
+               if (id != 0 && end == &uri[strlen (uri)])
+                       return id;
+       }
+
        iface = tracker_data_manager_get_writable_db_interface (data->manager);
 
        stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE, 
&inner_error,
diff --git a/src/libtracker-data/tracker-db-manager.h b/src/libtracker-data/tracker-db-manager.h
index e6d76e26e..664b55362 100644
--- a/src/libtracker-data/tracker-db-manager.h
+++ b/src/libtracker-data/tracker-db-manager.h
@@ -48,6 +48,7 @@ typedef enum {
        TRACKER_DB_MANAGER_FTS_IGNORE_NUMBERS    = 1 << 7,
        TRACKER_DB_MANAGER_IN_MEMORY             = 1 << 8,
        TRACKER_DB_MANAGER_SKIP_VERSION_CHECK    = 1 << 9,
+       TRACKER_DB_MANAGER_ANONYMOUS_BNODES      = 1 << 10,
 } TrackerDBManagerFlags;
 
 typedef enum {
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index 52eaf5798..0761a4773 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -570,6 +570,12 @@ static inline void
 _append_literal_sql (TrackerSparql         *sparql,
                      TrackerLiteralBinding *binding)
 {
+       TrackerDBManager *db_manager;
+       TrackerDBManagerFlags flags;
+
+       db_manager = tracker_data_manager_get_db_manager (sparql->data_manager);
+       flags = tracker_db_manager_get_flags (db_manager, NULL, NULL);
+
        if (TRACKER_BINDING (binding)->data_type == TRACKER_PROPERTY_TYPE_RESOURCE) {
                _append_string (sparql,
                                "COALESCE((SELECT ID FROM Resource WHERE Uri = ");
@@ -586,11 +592,13 @@ _append_literal_sql (TrackerSparql         *sparql,
 
                _append_string (sparql, "), ");
 
-               _append_string (sparql, "NULLIF(REPLACE(");
-               _append_literal_binding (sparql, binding);
-               _append_string (sparql, ", 'urn:bnode:', ''), ");
-               _append_literal_binding (sparql, binding);
-               _append_string (sparql, "), ");
+               if ((flags & TRACKER_DB_MANAGER_ANONYMOUS_BNODES) == 0) {
+                       _append_string (sparql, "NULLIF(REPLACE(");
+                       _append_literal_binding (sparql, binding);
+                       _append_string (sparql, ", 'urn:bnode:', ''), ");
+                       _append_literal_binding (sparql, binding);
+                       _append_string (sparql, "), ");
+               }
 
                _append_string (sparql, "0) ");
        }


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