Skip to content
Snippets Groups Projects
Commit 5b984b08 authored by Petar Hristov's avatar Petar Hristov :speech_balloon:
Browse files

Merge branch 'Issue/1792-newMetadataStructure' into 'dev'

New: Use the new metadata structure (coscine/issues#1792)

See merge request !39
parents 44d75ce0 f0a91244
No related branches found
No related tags found
2 merge requests!55Release: Sprint Feb 10, 2023 - Sprint Feb 23, 2023,!39New: Use the new metadata structure (coscine/issues#1792)
Pipeline #922125 passed
# Coscine Resource Types
This library contains all resource type implementations for Coscine (RDS, RDS-S3, RDS-S3-WORM, Linked, Gitlab).
......@@ -18,7 +18,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Coscine.Metadata" Version="2.*-*" />
<PackageReference Include="Coscine.Metadata" Version="*-*" />
<PackageReference Include="GitLabApiClient" Version="1.8.1-beta.5" />
</ItemGroup>
......
......@@ -15,6 +15,7 @@ public class LinkedResourceType : BaseResourceType
private readonly string _epicPrefix;
private readonly string _predicate = "https://purl.org/coscine/terms/linked#body";
private readonly RdfStoreConnector _rdfStoreConnector;
private readonly CoscineLDPHelper _coscineLDPHelper;
public LinkedResourceType(LinkedResourceTypeConfiguration linkedResourceTypeConfiguration) : base(linkedResourceTypeConfiguration)
{
......@@ -27,11 +28,7 @@ public class LinkedResourceType : BaseResourceType
{
_rdfStoreConnector = new();
}
}
private string GetGraphName(string id, string key)
{
return $"{GetSubjectName(id, key)}&data";
_coscineLDPHelper = new CoscineLDPHelper(_rdfStoreConnector, _epicPrefix);
}
private string GetGraphNameForFilter(string id, string key)
......@@ -58,11 +55,12 @@ public class LinkedResourceType : BaseResourceType
key = "/" + key;
}
var graph = _rdfStoreConnector.GetGraph(GetGraphName(id, key));
var urlId = _coscineLDPHelper.GetId(id, key, true, false, "data");
var graph = _rdfStoreConnector.GetGraph(urlId);
if (!graph.IsEmpty)
{
_rdfStoreConnector.DeleteGraph(GetGraphName(id, key));
_rdfStoreConnector.DeleteGraph(urlId);
}
return Task.CompletedTask;
......@@ -75,14 +73,15 @@ public class LinkedResourceType : BaseResourceType
key = "/" + key;
}
var graph = _rdfStoreConnector.GetGraph(GetGraphName(id, key));
var urlId = _coscineLDPHelper.GetId(id, key, true, false, "data");
var graph = _rdfStoreConnector.GetGraph(urlId);
if (graph.IsEmpty)
{
return Task.FromResult<ResourceEntry?>(null);
}
var triples = graph.GetTriplesWithSubjectPredicate(graph.CreateUriNode(new Uri(GetSubjectName(id, key))), graph.CreateUriNode(new Uri(_predicate)));
var triples = graph.GetTriplesWithPredicate(graph.CreateUriNode(new Uri(_predicate)));
if (!triples.Any())
{
......@@ -125,6 +124,24 @@ public class LinkedResourceType : BaseResourceType
return GetEntry(id, key).Result;
}).Where(x => x != null).Cast<ResourceEntry>().ToList();
// Add the new results as well
var currentDataList = _rdfStoreConnector.ListData(id);
if (currentDataList != null)
{
var resourceGraphUri = $"https://purl.org/coscine/resources/{id}";
var currentDataPaths = currentDataList.Select((entry) => entry?[..entry.LastIndexOf("/")].Replace(resourceGraphUri, ""));
var bothDataPaths = currentDataPaths.Where((currentDataPath) => resultList.Any((resultEntry) => resultEntry.Key == currentDataPath));
var newDataPaths = currentDataPaths.Where((currentDataPath) => !resultList.Any((resultEntry) => resultEntry.Key == currentDataPath));
// Remove the ones which are in both since the ones from the new structure would have the newest information
resultList.RemoveAll((entry) => bothDataPaths.Any((dataPath) => dataPath == entry.Key));
resultList.AddRange(currentDataPaths
.Where(x => x is not null)
.Select((x) => GetEntry(id, x!).Result)
.Where(x => x != null).Cast<ResourceEntry>());
}
return Task.FromResult(resultList);
}
......@@ -135,14 +152,16 @@ public class LinkedResourceType : BaseResourceType
key = "/" + key;
}
var graph = _rdfStoreConnector.GetGraph(GetGraphName(id, key));
var urlId = _coscineLDPHelper.GetId(id, key, true, false, "data");
var graph = _rdfStoreConnector.GetGraph(urlId);
if (graph.IsEmpty)
{
return Task.FromResult<Stream?>(null);
}
var triples = graph.GetTriplesWithSubjectPredicate(graph.CreateUriNode(new Uri(GetSubjectName(id, key))), graph.CreateUriNode(new Uri(_predicate)));
var triples = graph.GetTriplesWithPredicate(graph.CreateUriNode(new Uri(_predicate)));
if (!triples.Any())
{
......@@ -173,19 +192,21 @@ public class LinkedResourceType : BaseResourceType
Array.Resize(ref buffer, size);
var graph = _rdfStoreConnector.GetGraph(GetGraphName(id, key));
var urlId = _coscineLDPHelper.GetId(id, key, true, false, "data");
var graph = _rdfStoreConnector.GetGraph(urlId);
if (!graph.IsEmpty)
{
var triples = graph.GetTriplesWithSubjectPredicate(graph.CreateUriNode(new Uri(GetSubjectName(id, key))), graph.CreateUriNode(new Uri(_predicate)));
var triples = graph.GetTriplesWithPredicate(graph.CreateUriNode(new Uri(_predicate)));
graph.Retract(triples.ToArray());
}
else
{
_rdfStoreConnector.CreateNamedGraph(GetGraphName(id, key));
_rdfStoreConnector.CreateNamedGraph(urlId);
}
graph.Assert(new Triple(graph.CreateUriNode(new Uri(GetSubjectName(id, key))), graph.CreateUriNode(new Uri(_predicate)), graph.CreateLiteralNode(Encoding.UTF8.GetString(buffer))));
graph.Assert(new Triple(graph.CreateUriNode(urlId), graph.CreateUriNode(new Uri(_predicate)), graph.CreateLiteralNode(Encoding.UTF8.GetString(buffer))));
_rdfStoreConnector.AddGraph(graph);
......
......@@ -18,7 +18,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Coscine.Metadata" Version="2.*-*" />
<PackageReference Include="Coscine.Metadata" Version="*-*" />
<PackageReference Include="dotNetRDF" Version="2.7.4" />
</ItemGroup>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment