From 65de903bae326df872267718e5d7d0517f237fb5 Mon Sep 17 00:00:00 2001
From: Heinrichs <Heinrichs@itc.rwth-aachen.de>
Date: Thu, 15 Dec 2022 13:52:31 +0100
Subject: [PATCH] Fix: Deploy only changed graphs

---
 src/GraphDeployer/Program.cs | 53 ++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 9 deletions(-)

diff --git a/src/GraphDeployer/Program.cs b/src/GraphDeployer/Program.cs
index bfaf7d4..f753bfe 100644
--- a/src/GraphDeployer/Program.cs
+++ b/src/GraphDeployer/Program.cs
@@ -90,24 +90,59 @@ public class Program
                 // Graph Insertion
                 var queries = new List<string>();
                 var turtleFiles = Directory.GetFiles(WorkingFolder, "*.ttl", SearchOption.AllDirectories);
-                foreach (var file in turtleFiles)
+
+                var graphAccumulation = new Dictionary<Uri, (Graph, List<string>)>();
+                Array.ForEach(turtleFiles, (file) =>
                 {
-                    var fileInfo = new FileInfo(file);
                     var graph = new Graph();
                     graph.LoadFromFile(file);
-                    var graphName = graph.BaseUri.ToString();
+                    if (graphAccumulation.ContainsKey(graph.BaseUri))
+                    {
+                        graphAccumulation[graph.BaseUri].Item1.Merge(graph);
+                        graphAccumulation[graph.BaseUri].Item2.Add(file);
+                    } 
+                    else
+                    {
+                        graphAccumulation.Add(graph.BaseUri, (graph, new List<string>() { file }));
+                    }
+                });
 
-                    if (_rdfStoreConnector.HasGraph(graphName))
+                foreach (var kv in graphAccumulation)
+                {
+                    var graph = kv.Value.Item1;
+                    var graphName = kv.Key.ToString();
+
+                    var currentGraph = _rdfStoreConnector.GetGraph(graphName);
+
+                    var graphWasChanged = graph.Triples.Count != currentGraph.Triples.Count
+                        || graph.Triples.Any((triple) => !currentGraph.Triples.Any((currentTriple) =>
+                            (triple.Subject.Equals(currentTriple.Subject) || (triple.Subject.NodeType == NodeType.Blank && currentTriple.Subject.NodeType == NodeType.Blank)
+                            && triple.Predicate.Equals(currentTriple.Predicate) 
+                            && triple.Object.Equals(currentTriple.Object) || (triple.Object.NodeType == NodeType.Blank && currentTriple.Object.NodeType == NodeType.Blank))));
+
+                    if (graphWasChanged)
                     {
-                        _rdfStoreConnector.ClearGraph(graphName);
-                        _logger.LogInformation("Cleared Graph {graphName}", graphName);
+                        if (!currentGraph.IsEmpty)
+                        {
+                            _rdfStoreConnector.ClearGraph(graphName);
+                            _logger.LogInformation("Cleared Graph {graphName}", graphName);
+                        }
+                        else
+                        {
+                            _logger.LogInformation("No Graph {graphName}", graphName);
+                        }
+
+                        foreach (var file in kv.Value.Item2)
+                        {
+                            var fileInfo = new FileInfo(file);
+                            queries.Add($"ld_dir('{fileInfo.DirectoryName[2..].Replace("\\", "/")}', '{fileInfo.Name}', '{graphName}');");
+                        }
                     }
                     else
                     {
-                        _logger.LogInformation("No Graph {graphName}", graphName);
-                    }
+                        _logger.LogInformation("Skipping {graphName}", graphName);
 
-                    queries.Add($"ld_dir('{fileInfo.DirectoryName[2..].Replace("\\", "/")}', '{fileInfo.Name}', '{graphName}');");
+                    }
                 }
                 queries.Add($"rdf_loader_run ();");
                 queries.Add($"DELETE from DB.DBA.load_list where 1=1;");
-- 
GitLab