Skip to content
Snippets Groups Projects
Select Git revision
  • 7891acae286c3ba929f3432064561101733afdd3
  • master default protected
  • gitkeep
  • dev protected
  • Issue/2464-invalidateMeta
  • Issue/2309-docs
  • Issue/2462-removeTraces
  • Hotfix/2459-EncodingPath
  • Hotfix/2452-linkedDeletion
  • Issue/1792-newMetadataStructure
  • Hotfix/2371-fixGitLabinRCV
  • Fix/xxxx-activateGitlab
  • Issue/2349-gitlabHttps
  • Issue/2287-guestRole
  • Issue/2102-gitLabResTypeRCV
  • Hotfix/2254-fixContentLenghtCalculation
  • Fix/xxxx-resourceVisibility
  • Issue/1951-quotaImplementation
  • Issue/2162-fixFolderResponse
  • Issue/2158-emailServicedesk
  • Hotfix/2141-fileUploadErrors
  • v3.3.4
  • v3.3.3
  • v3.3.2
  • v3.3.1
  • v3.3.0
  • v3.2.3
  • v3.2.2
  • v3.2.1
  • v3.2.0
  • v3.1.2
  • v3.1.1
  • v3.1.0
  • v3.0.6
  • v3.0.5
  • v3.0.4
  • v3.0.3
  • v3.0.2
  • v3.0.1
  • v3.0.0
  • v2.8.2
41 results

BlobController.cs

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    StructuralData.cs 5.36 KiB
    using Coscine.Configuration;
    using Coscine.Database.Models;
    using Coscine.Metadata;
    using Polly;
    using VDS.RDF;
    
    namespace SQL2Linked
    {
        public abstract class StructuralData<S, T> where S : class where T : DatabaseModel<S>, new()
        {
            public T Model { get; init; }
            public ConsulConfiguration Configuration { get; init; }
            public RdfStoreConnector RdfStoreConnector { get; init; }
            public static string Prefix { get; set; }
    
            public StructuralData()
            {
                Configuration = new ConsulConfiguration();
                RdfStoreConnector = new RdfStoreConnector(Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url"));
                Model = new T();
                Prefix = Configuration.GetStringAndWait("coscine/global/epic/prefix");
                // 100 second timeout
                var timeout = 100000;
                RdfStoreConnector.QueryEndpoint.Timeout = timeout;
                RdfStoreConnector.UpdateEndpoint.Timeout = timeout;
                RdfStoreConnector.ReadWriteSparqlConnector.Timeout = timeout;
            }
    
            public abstract IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<S> entries);
    
            public virtual IEnumerable<S> GetAll()
            {
                return Model.GetAll();
            }
    
            public void Migrate(bool dummyMode)
            {
                var spacer = new string('-', 35);
                Console.WriteLine($"\n{spacer}\n{typeof(T).Name}\n{spacer}");
                var graphs = ConvertToLinkedData(GetAll());
                if (!dummyMode)
                {
                    StoreGraphs(graphs);
                }
            }
    
            private void StoreGraphs(IEnumerable<IGraph> graphs)
            {
                foreach (var graph in graphs)
                {
                    try
                    {
                        Console.WriteLine($" ({graph.BaseUri})");
                        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}");
                        }
                        // Add the new graph to the store
                        WrapRequest(() => RdfStoreConnector.AddGraph(graph));
                        Console.WriteLine($" - Graph {graph.BaseUri} added successfully");
                        Console.WriteLine();
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine($"Error on ({graph.BaseUri}):");
                        Console.Error.WriteLine(e);
                        Console.Error.WriteLine(e.InnerException);
                        Console.Error.WriteLine();
                    }
                }
            }
    
            public void AssertToGraphUriNode(IGraph graph, string graphSubject, string graphPredicate, string? graphObject)
            {
                if (graphObject != null)
                {
                    graph.Assert(
                        new Triple(
                            graph.CreateUriNode(new Uri(graphSubject)),
                            graph.CreateUriNode(new Uri(graphPredicate)),
                            graph.CreateUriNode(new Uri(graphObject))
                        )
                    );
                }
            }
    
            public void AssertToGraphLiteralNode(IGraph graph, string graphSubject, string graphPredicate, string? graphObject, Uri? objectType = null)
            {
                if (graphObject != null)
                {
                    graph.Assert(
                        new Triple(
                            graph.CreateUriNode(new Uri(graphSubject)),
                            graph.CreateUriNode(new Uri(graphPredicate)),
                            graph.CreateLiteralNode(graphObject, objectType)
                        )
                    );
                }
            }
    
            public void AssertToGraphBlankAndUriNode(IGraph graph, IBlankNode graphSubject, string graphPredicate, string? graphObject)
            {
                if (graphObject != null)
                {
                    graph.Assert(
                        new Triple(
                            graphSubject,
                            graph.CreateUriNode(new Uri(graphPredicate)),
                            graph.CreateUriNode(new Uri(graphObject))
                        )
                    );
                }
            }
    
            /// <summary>
            /// Retry Virtuoso Requests since they sometimes just fail
            /// </summary>
            /// <typeparam name="W"></typeparam>
            /// <param name="function"></param>
            /// <returns></returns>
            public void WrapRequest(Action action)
            {
                Policy
                    .Handle<Exception>()
                    .WaitAndRetry(5, retryNumber => TimeSpan.FromMilliseconds(200))
                    .Execute(() => action.Invoke());
            }
    
            /// <summary>
            /// Retry Virtuoso Requests since they sometimes just fail
            /// </summary>
            /// <typeparam name="W"></typeparam>
            /// <param name="function"></param>
            /// <returns></returns>
            public W WrapRequest<W>(Func<W> function)
            {
                return Policy
                    .Handle<Exception>()
                    .WaitAndRetry(5, retryNumber => TimeSpan.FromMilliseconds(200))
                    .ExecuteAndCapture(() => function.Invoke()).Result;
            }
        }
    }