diff --git a/src/SQL2Linked/Implementations/ResourceStructuralData.cs b/src/SQL2Linked/Implementations/ResourceStructuralData.cs index cccc6af8a0e031b08006d01c79a7e212a13b32f9..6d3abb1559c7a79473484e12d902de93ba441f95 100644 --- a/src/SQL2Linked/Implementations/ResourceStructuralData.cs +++ b/src/SQL2Linked/Implementations/ResourceStructuralData.cs @@ -136,19 +136,12 @@ namespace SQL2Linked.Implementations AssertToGraphLiteralNode(graph, resourceGraphName, cosc + "terms/resource#deleted", entry.Deleted.ToString().ToLower(), new Uri("http://www.w3.org/2001/XMLSchema#boolean")); Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#deleted {entry.Deleted}'. "); - var targetClass = GetTargetClass(entry); - - SparqlParameterizedString cmdString = new SparqlParameterizedString + // Reinstate the catalog assignments + var cmdString = new SparqlParameterizedString { - CommandText = "SELECT DISTINCT ?g " + - "WHERE {" + - "GRAPH ?g { ?s a <" + targetClass + "> } . " + - "FILTER(contains(STR(?g), \"" + resourceHandleName + "@\"))" + - "}" + CommandText = "SELECT DISTINCT ?o WHERE { <" + resourceGraphName + "> <http://www.w3.org/ns/dcat#catalog> ?o }" }; - var resultSet = WrapRequest(() => RdfStoreConnector.QueryEndpoint.QueryWithResultSet(cmdString.ToString())); - foreach (var result in resultSet) { AssertToGraphUriNode(graph, resourceGraphName, dcat + "catalog", result[0].ToString()); diff --git a/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs b/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs index be7ca6e0c15ebb8b8cc9a8ebd0ad9017519fbf42..d2536f60901c4bc2d31ed73058c60906585c7f82 100644 --- a/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs +++ b/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs @@ -34,7 +34,7 @@ namespace SQL2Linked.Implementations } // check if a triple with dcterms:title '{entry.DisplayName}' already exists in the role graph - var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(new Uri(dcterms + "Title")); + var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(new Uri(dcterms + "title")); if (!getTriplesDctermsTitle.Any()) { diff --git a/src/SQL2Linked/StructuralData.cs b/src/SQL2Linked/StructuralData.cs index 0e077a6574eef59165fa60b3c7645ccbbcce69e6..27888214624a9c7d005e846705f90808ba956124 100644 --- a/src/SQL2Linked/StructuralData.cs +++ b/src/SQL2Linked/StructuralData.cs @@ -3,6 +3,7 @@ using Coscine.Database.Models; using Coscine.Metadata; using Polly; using VDS.RDF; +using VDS.RDF.Query.Algebra; namespace SQL2Linked { @@ -44,24 +45,48 @@ namespace SQL2Linked } } - private void StoreGraphs(IEnumerable<IGraph> graphs) + public void StoreGraphs(IEnumerable<IGraph> graphs) { foreach (var graph in graphs) { try { Console.WriteLine($" ({graph.BaseUri})"); - var exists = WrapRequest(() => RdfStoreConnector.HasGraph(graph.BaseUri)); - if (exists) + + if (graph is WrapperGraph) + { + var wrapperGraph = (WrapperGraph)graph; + // Chunking since the size otherwise can be too large + foreach (var triples in wrapperGraph.AssertList.Chunk(100)) + { + WrapRequest(() => RdfStoreConnector.ReadWriteSparqlConnector.UpdateGraph(graph.BaseUri, triples, new List<Triple>())); + } + // Chunking since the size otherwise can be too large + foreach (var triples in wrapperGraph.RetractList.Chunk(100)) + { + WrapRequest(() => RdfStoreConnector.ReadWriteSparqlConnector.UpdateGraph(graph.BaseUri, new List<Triple>(), triples)); + } + } + else { - Console.WriteLine($" - Graph {graph.BaseUri} exists"); + var exists = WrapRequest(() => RdfStoreConnector.HasGraph(graph.BaseUri)); + if (exists) + { + Console.WriteLine($" - Graph {graph.BaseUri} exists"); - // Clear the existing graph from the store - WrapRequest(() => RdfStoreConnector.ClearGraph(graph.BaseUri)); - Console.WriteLine($" - Cleared Graph {graph.BaseUri}"); + // Clear the existing graph from the store + WrapRequest(() => RdfStoreConnector.ClearGraph(graph.BaseUri)); + Console.WriteLine($" - Cleared Graph {graph.BaseUri}"); + } + + // Chunking since the size otherwise can be too large + // Don't change to only addition of triples, otherwise this could break things + foreach (var triples in graph.Triples.Chunk(100)) + { + WrapRequest(() => RdfStoreConnector.ReadWriteSparqlConnector.UpdateGraph(graph.BaseUri, triples, new List<Triple>())); + } } - // Add the new graph to the store - WrapRequest(() => RdfStoreConnector.AddGraph(graph)); + Console.WriteLine($" - Graph {graph.BaseUri} added successfully"); Console.WriteLine(); } diff --git a/src/SQL2Linked/WrapperGraph.cs b/src/SQL2Linked/WrapperGraph.cs new file mode 100644 index 0000000000000000000000000000000000000000..ce318c397a4e103f9d2369f8aad893bf9ad3677c --- /dev/null +++ b/src/SQL2Linked/WrapperGraph.cs @@ -0,0 +1,40 @@ +using VDS.RDF; + +namespace SQL2Linked +{ + public class WrapperGraph : Graph + { + public List<Triple> AssertList { get; set; } + public List<Triple> RetractList { get; set; } + + public WrapperGraph() : base() + { + AssertList = new List<Triple>(); + RetractList = new List<Triple>(); + } + + public override bool Assert(Triple t) + { + AssertList.Add(t); + return base.Assert(t); + } + + public override bool Assert(IEnumerable<Triple> triples) + { + AssertList.AddRange(triples); + return base.Assert(triples); + } + + public override bool Retract(Triple t) + { + RetractList.Add(t); + return base.Retract(t); + } + + public override bool Retract(IEnumerable<Triple> triples) + { + RetractList.AddRange(triples); + return base.Retract(triples); + } + } +}