[rygel] tracker: Don't fail on existing resources



commit 4db66c7c852c5a4b1935ebd5c595fa638ad6ae04
Author: Jens Georg <mail jensge org>
Date:   Thu May 19 17:26:33 2011 +0200

    tracker: Don't fail on existing resources
    
    If we try to add a resource with a URI that already exists in tracker,
    don't error out but return the resource URI of the existing resource.

 .../tracker/rygel-tracker-insertion-query.vala     |   29 +++++++++++++++++++-
 1 files changed, 28 insertions(+), 1 deletions(-)
---
diff --git a/src/plugins/tracker/rygel-tracker-insertion-query.vala b/src/plugins/tracker/rygel-tracker-insertion-query.vala
index c59191b..23bfe1c 100644
--- a/src/plugins/tracker/rygel-tracker-insertion-query.vala
+++ b/src/plugins/tracker/rygel-tracker-insertion-query.vala
@@ -37,8 +37,17 @@ public class Rygel.Tracker.InsertionQuery : Query {
     private const string MINER_GRAPH =
                               "urn:uuid:472ed0cc-40ff-4e37-9c0c-062d78656540";
 
+    private const string RESOURCE_ID_QUERY_TEMPLATE =
+        "SELECT ?resource WHERE { ?resource a nie:DataObject; nie:url '%s' }";
+
+    private const string RESOURCE_NOT_BOUND_TEMPLATE =
+        "OPTIONAL { ?resource a nie:DataObject; nie:url '%s' } " +
+        "FILTER (!bound(?resource))";
+
     public string id;
 
+    private string uri;
+
     public InsertionQuery (MediaItem item, string category) {
         var type = "nie:DataObject";
         var file = File.new_for_uri (item.uris[0]);
@@ -85,6 +94,8 @@ public class Rygel.Tracker.InsertionQuery : Query {
         }
 
         base (triplets);
+
+        this.uri = item.uris[0];
     }
 
     public override async void execute (ResourcesIface resources)
@@ -97,10 +108,26 @@ public class Rygel.Tracker.InsertionQuery : Query {
 
         this.id = result[0,0].lookup (TEMP_ID);
 
+        // Item already existed
+        if (this.id == null) {
+            var ids = yield resources.sparql_query
+                                        (this.get_resource_id_query ());
+
+            this.id = ids[0,0];
+        }
+
         debug ("Created item in Tracker store with ID '%s'", this.id);
     }
 
     public override string to_string () {
-        return "INSERT { " + base.to_string () + " }";
+        var query = "INSERT { " + base.to_string () + " }";
+        query += "WHERE {" + RESOURCE_NOT_BOUND_TEMPLATE.printf (this.uri) +
+                 "}";
+
+        return query;
+    }
+
+    private string get_resource_id_query () {
+        return RESOURCE_ID_QUERY_TEMPLATE.printf (this.uri);
     }
 }



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