Esempio n. 1
0
 public ImportJob(Guid jobId, string label, StoreWorker storeWorker, string contentFileName, RdfFormat importFormat, string graphUri)
     : base(jobId, label, storeWorker)
 {
     _contentFileName = contentFileName;
     _importFormat = importFormat;
     _graphUri = graphUri;
 }
Esempio n. 2
0
        // operations
        public ISerializationFormat Query(string storeName, string queryExpression, IEnumerable <string> defaultGraphUris,
                                          DateTime?ifNotModifiedSince,
                                          SparqlResultsFormat sparqlResultFormat, RdfFormat graphFormat,
                                          Stream responseStream)
        {
            Logging.LogDebug("Query {0} {1}", storeName, queryExpression);
            var commitPoint =
                _storeManager.GetMasterFile(Path.Combine(_baseLocation, storeName)).GetCommitPoints().First();

            if (ifNotModifiedSince.HasValue && ifNotModifiedSince > commitPoint.CommitTime)
            {
                throw new BrightstarStoreNotModifiedException();
            }
            var g            = defaultGraphUris == null ? null : defaultGraphUris.ToArray();
            var query        = ParseSparql(queryExpression);
            var targetFormat = QueryReturnsGraph(query) ? (ISerializationFormat)graphFormat : sparqlResultFormat;

            var cacheKey     = MakeQueryCacheKey(storeName, commitPoint.CommitTime.Ticks, query, g, targetFormat);
            var cachedResult = GetCachedResult(cacheKey);

            if (cachedResult == null)
            {
                // Not in the cache so execute the query on the StoreWorker
                var storeWorker = GetStoreWorker(storeName);
                var cacheStream = new MemoryStream();
                storeWorker.Query(query, targetFormat, cacheStream, g);
                cachedResult = CacheResult(cacheKey, cacheStream.ToArray());
            }
            cachedResult.WriteTo(responseStream);
            return(targetFormat);
        }
 internal SparqlQueryContext()
 {
     AnonymousMembersMap = new List <System.Tuple <string, string> >();
     OrderingDirections  = new List <OrderingDirection>(0);
     SparqlResultsFormat = SparqlResultsFormat.Xml;
     GraphResultsFormat  = RdfFormat.RdfXml;
 }
        /// <summary>
        /// Creates a store import job request object
        /// </summary>
        /// <param name="importFileName">The name of the file to import the data from</param>
        /// <param name="defaultGraphUri">OPTIONAL: The default graph to apply to triples parsed from the import file. If not provided, defaults to the system default graph./</param>
        /// <param name="label">A user-friendly label for the job</param>
        /// <param name="importFormat">The format of the file to be imported</param>
        /// <returns>A new <see cref="JobRequestObject"/> instance</returns>
        public static JobRequestObject CreateImportJob(string importFileName, string defaultGraphUri = null,
                                                       string label = null, RdfFormat importFormat = null)
        {
            if (importFileName == null)
            {
                throw new ArgumentNullException(nameof(importFileName));
            }
            if (string.IsNullOrWhiteSpace(importFileName))
            {
                throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, nameof(importFileName));
            }
            if (defaultGraphUri != null && string.Empty.Equals(defaultGraphUri.Trim()))
            {
                throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, nameof(defaultGraphUri));
            }

            var jobParams = new Dictionary <string, string>
            {
                { "FileName", importFileName },
                { "DefaultGraphUri", defaultGraphUri }
            };

            if (importFormat != null)
            {
                jobParams["ImportFormat"] = importFormat.MediaTypes.First();
            }
            return(new JobRequestObject("Import", jobParams, label));
        }
Esempio n. 5
0
 internal SparqlQueryContext()
 {
     AnonymousMembersMap = new List<System.Tuple<string, string>>();
     OrderingDirections = new List<OrderingDirection>(0);
     SparqlResultsFormat = SparqlResultsFormat.Xml;
     GraphResultsFormat = RdfFormat.RdfXml;
 }
Esempio n. 6
0
 public ImportJob(Guid jobId, string label, StoreWorker storeWorker, string contentFileName, RdfFormat importFormat, string graphUri)
     : base(jobId, label, storeWorker)
 {
     _contentFileName = contentFileName;
     _importFormat    = importFormat;
     _graphUri        = graphUri;
 }
 public SparqlResultModel(string storeName, IBrightstarService service, SparqlRequestObject sparqlRequest, SparqlResultsFormat resultsFormat, RdfFormat graphFormat)
 {
     _storeName     = storeName;
     _sparqlRequest = sparqlRequest;
     _service       = service;
     ResultsFormat  = resultsFormat;
     GraphFormat    = graphFormat;
 }
Esempio n. 8
0
 public SparqlResultModel(string storeName, IBrightstarService service, SparqlRequestObject sparqlRequest, SparqlResultsFormat resultsFormat, RdfFormat graphFormat)
 {
     _storeName = storeName;
     _sparqlRequest = sparqlRequest;
     _service = service;
     ResultsFormat = resultsFormat;
     GraphFormat = graphFormat;
 }
Esempio n. 9
0
 public ExportJob(Guid jobId, string label, StoreWorker storeWorker, string outputFileName, string graphUri, RdfFormat exportFormat)
 {
     _jobId = jobId;
     _label = label;
     _storeWorker = storeWorker;
     _outputFileName = outputFileName;
     _graphUri = graphUri;
     _exportFormat = exportFormat;
 }
Esempio n. 10
0
 public ExportJob(Guid jobId, string label, StoreWorker storeWorker, string outputFileName, string graphUri, RdfFormat exportFormat)
 {
     _jobId          = jobId;
     _label          = label;
     _storeWorker    = storeWorker;
     _outputFileName = outputFileName;
     _graphUri       = graphUri;
     _exportFormat   = exportFormat;
 }
Esempio n. 11
0
        public Guid Import(string contentFileName, string graphUri, RdfFormat importFormat = null, string jobLabel = null)
        {
            Logging.LogDebug("Import {0}, {1}, {2}", contentFileName, graphUri, importFormat);
            var jobId = Guid.NewGuid();
            var job   = new ImportJob(jobId, jobLabel, this, contentFileName, importFormat, graphUri);

            QueueJob(job);
            return(jobId);
        }
 public Stream GetResultsStream(SparqlResultsFormat format, RdfFormat graphFormat, DateTime? ifNotModifiedSince, out ISerializationFormat streamFormat)
 {
     if (_commitId > 0)
     {
         var commitPointInfo = _service.GetCommitPoint(_storeName, _commitId);
         if (commitPointInfo == null) throw new InvalidCommitPointException();
         return _service.ExecuteQuery(commitPointInfo, _sparqlRequest.Query, _sparqlRequest.DefaultGraphUri, format, graphFormat, out streamFormat);
     }
     return _service.ExecuteQuery(_storeName, _sparqlRequest.Query, _sparqlRequest.DefaultGraphUri, ifNotModifiedSince, format, graphFormat, out streamFormat);
 }
 /// <summary>
 /// Creates a new SparqlQueryContext instance
 /// </summary>
 /// <param name="sparqlQuery">sparql query</param>
 /// <param name="anonymousMembersMap">mappings for anonymous types</param>
 /// <param name="orderingDirections">ordering direction for the sort expressions in the SPARQL query</param>
 /// <param name="tableResultsFormat">The requested serialization form for a SPARQL result set</param>
 /// <param name="graphResultsFormat">The requested serialization form for a SPARQL result graph</param>
 public SparqlQueryContext(string sparqlQuery, IEnumerable <System.Tuple <string, string> > anonymousMembersMap,
                           IEnumerable <OrderingDirection> orderingDirections,
                           SparqlResultsFormat tableResultsFormat, RdfFormat graphResultsFormat)
 {
     SparqlQuery         = sparqlQuery;
     AnonymousMembersMap = new List <System.Tuple <string, string> >(anonymousMembersMap);
     OrderingDirections  = new List <OrderingDirection>(orderingDirections);
     SparqlResultsFormat = tableResultsFormat;
     GraphResultsFormat  = graphResultsFormat;
 }
        public Block CreateBlock(string dataGraphIri, string rawRdf, RdfFormat rdfFormat)
        {
            _logger.LogDebug("Creating block with graphIri '{0}' and rdfFormat '{1}'...", dataGraphIri, rdfFormat.GetJenaName());

            try
            {
                HashSet <Triple> triples = GetTriplesFromSerializedModel(rawRdf, rdfFormat);

                long   newIndex    = GenerateNewIndex();
                string newBlockIri = GenerateNewBlockIri(newIndex);

                LastBlockInfo lastBlockInfo         = _repositoryManager.GetLastBlockInfo();
                string        previousBlock         = lastBlockInfo.BlockIri;
                string        previousHash          = lastBlockInfo.BlockHash;
                string        timestamp             = TimestampCreator.CreateTimestampString();
                string        dataHash              = _hashingService.CalculateHash(triples);
                string        stringToCalculateHash = (newIndex + previousBlock + previousHash + timestamp + dataGraphIri + dataHash).Trim();
                string        hash = _hashCalculator.CalculateHash(stringToCalculateHash).ToLower();

                BlockHeader blockHeader = new BlockHeader(
                    dataGraphIri,
                    dataHash,
                    hash,
                    newIndex.ToString(),
                    previousBlock,
                    previousHash,
                    timestamp);
                BlockContent blockContent = new BlockContent(dataGraphIri, triples);

                Block blockToStore = new Block(newBlockIri, blockHeader, blockContent);

                _repositoryManager.PersistBlock(blockToStore, true);

                // MAYBE: Maybe this should be obtained from Triple Store in order to avoid some kind of inconsistency.
                _lastIndex++;

                return(GetBlock(newIndex.ToString()));
            }
            catch (ReadingBlockException ex)
            {
                string msg = "Exception was thrown while getting information about the last block.";
                throw new CreatingBlockException(msg, ex);
            }
            catch (RdfSerializationException ex)
            {
                string msg = String.Format("Exception was thrown while deserializing RDF model from '{0}' format.", rdfFormat);
                throw new CreatingBlockException(msg, ex);
            }
            catch (CalculatingHashException ex)
            {
                throw new CreatingBlockException("Exception was thrown while calculating hash.", ex);
            }
        }
Esempio n. 15
0
        private string ParseBody(RdfFormat contentType)
        {
            var parser = MimeTypesHelper.GetParser(contentType.MediaTypes.First());

            var writer = new WriteToStringHandler(typeof(VDS.RDF.Writing.Formatting.NTriplesFormatter));

            using (var reader = new StreamReader(Request.Body))
            {
                parser.Load(writer, reader);
            }
            return(writer.ToString());
        }
 public Stream GetResultsStream(SparqlResultsFormat format, RdfFormat graphFormat, DateTime?ifNotModifiedSince, out ISerializationFormat streamFormat)
 {
     if (_commitId > 0)
     {
         var commitPointInfo = _service.GetCommitPoint(_storeName, _commitId);
         if (commitPointInfo == null)
         {
             throw new InvalidCommitPointException();
         }
         return(_service.ExecuteQuery(commitPointInfo, _sparqlRequest.Query, _sparqlRequest.DefaultGraphUri, format, graphFormat, out streamFormat));
     }
     return(_service.ExecuteQuery(_storeName, _sparqlRequest.Query, _sparqlRequest.DefaultGraphUri, ifNotModifiedSince, format, graphFormat, out streamFormat));
 }
 public SparqlQueryHandler(ISerializationFormat targetFormat,
                           IEnumerable<string> defaultGraphUris)
 {
     if (targetFormat is SparqlResultsFormat)
     {
         _sparqlResultsFormat = targetFormat as SparqlResultsFormat;
     }
     if (targetFormat is RdfFormat)
     {
         _rdfFormat = targetFormat as RdfFormat;
     }
     if (defaultGraphUris != null)
     {
         _defaultGraphUris = defaultGraphUris.Select(g => new Uri(g)).ToList();
     }
 }
Esempio n. 18
0
 public SparqlQueryHandler(ISerializationFormat targetFormat,
                           IEnumerable <string> defaultGraphUris)
 {
     if (targetFormat is SparqlResultsFormat)
     {
         _sparqlResultsFormat = targetFormat as SparqlResultsFormat;
     }
     if (targetFormat is RdfFormat)
     {
         _rdfFormat = targetFormat as RdfFormat;
     }
     if (defaultGraphUris != null)
     {
         _defaultGraphUris = defaultGraphUris.Select(g => new Uri(g)).ToList();
     }
 }
Esempio n. 19
0
 private static ITripleSink GetWriterSink(RdfFormat exportFormat, TextWriter textWriter)
 {
     if (exportFormat.MatchesMediaType(RdfFormat.NTriples))
     {
         return(new NTriplesWriter(textWriter));
     }
     if (exportFormat.MatchesMediaType(RdfFormat.NQuads))
     {
         return(new NQuadsWriter(textWriter));
     }
     if (exportFormat.MatchesMediaType(RdfFormat.RdfXml))
     {
         var xw = XmlWriter.Create(textWriter, new XmlWriterSettings {
             CloseOutput = false, Indent = true
         });
         return(new RdfXmlWriter(xw));
     }
     throw new BrightstarClientException(String.Format(Strings.ExportJob_UnsupportedExportFormat, exportFormat.MediaTypes[0]));
 }
Esempio n. 20
0
 public SparqlQueryResponse(SparqlQueryProcessingModel model, DateTime? ifNotModifiedSince, SparqlResultsFormat format, RdfFormat graphFormat)
 {
     try
     {
         ISerializationFormat streamFormat;
         var resultStream = model.GetResultsStream(format, graphFormat, ifNotModifiedSince, out streamFormat);
         Contents = resultStream.CopyTo;
         ContentType = streamFormat.ToString();
         StatusCode = HttpStatusCode.OK;
     }
     catch (InvalidCommitPointException)
     {
         StatusCode = HttpStatusCode.NotFound;
     }
     catch (BrightstarStoreNotModifiedException)
     {
         StatusCode = HttpStatusCode.NotModified;
     }
 }
        public static object ToRdfWriter(RdfFormat rdfFormat)
        {
            switch (rdfFormat.GetJenaName())
            {
            case "RDF/XML":
                return(new PrettyRdfXmlWriter());

            case "JSON-LD":
                return(new JsonLdWriter());

            case "TURTLE":
                return(new CompressingTurtleWriter());

            case "N-QUADS":
                return(new NQuadsWriter());

            default:
                throw new RdfParseException("Unknown rdf format");
            }
        }
Esempio n. 22
0
        public Guid Export(string fileName, string graphUri, RdfFormat exportFormat, string jobLabel = null)
        {
            Logging.LogDebug("Export {0}, {1}, {2}", fileName, graphUri, exportFormat.DefaultExtension);
            var jobId     = Guid.NewGuid();
            var exportJob = new ExportJob(jobId, jobLabel, this, fileName, graphUri, exportFormat);

            _jobExecutionStatus.TryAdd(jobId.ToString(),
                                       new JobExecutionStatus
            {
                JobId     = jobId,
                JobStatus = JobStatus.Started,
                Queued    = DateTime.UtcNow,
                Started   = DateTime.UtcNow,
                Label     = jobLabel,
                WaitEvent = new AutoResetEvent(false)
            });
            exportJob.Run((id, ex) =>
            {
                JobExecutionStatus jobExecutionStatus;
                if (_jobExecutionStatus.TryGetValue(id.ToString(), out jobExecutionStatus))
                {
                    jobExecutionStatus.Information     = "Export failed";
                    jobExecutionStatus.ExceptionDetail = GetExceptionDetail(ex);
                    jobExecutionStatus.JobStatus       = JobStatus.TransactionError;
                    jobExecutionStatus.Ended           = DateTime.UtcNow;
                    jobExecutionStatus.WaitEvent.Set();
                }
            },
                          id =>
            {
                JobExecutionStatus jobExecutionStatus;
                if (_jobExecutionStatus.TryGetValue(id.ToString(), out jobExecutionStatus))
                {
                    jobExecutionStatus.Information = "Export completed";
                    jobExecutionStatus.JobStatus   = JobStatus.CompletedOk;
                    jobExecutionStatus.Ended       = DateTime.UtcNow;
                    jobExecutionStatus.WaitEvent.Set();
                }
            });
            return(jobId);
        }
Esempio n. 23
0
        public ISerializationFormat Query(string storeName, ulong commitPointId, string queryExpression,
                                          IEnumerable <string> defaultGraphUris,
                                          SparqlResultsFormat sparqlResultFormat, RdfFormat graphFormat,
                                          Stream responseStream)
        {
            Logging.LogDebug("Query {0}@{1} {2}", storeName, commitPointId, queryExpression);
            var g             = defaultGraphUris == null ? null : defaultGraphUris.ToArray();
            var storeLocation = Path.Combine(_baseLocation, storeName);
            var masterFile    = _storeManager.GetMasterFile(storeLocation);

            if (masterFile.PersistenceType == PersistenceType.Rewrite)
            {
                throw new BrightstarClientException(
                          "Query of past commit points is not supported by the binary page persistence type");
            }
            var commitPoint =
                masterFile.GetCommitPoints().FirstOrDefault(c => c.LocationOffset == commitPointId);

            if (commitPoint == null)
            {
                throw new InvalidCommitPointException(String.Format("Could not find commit point {0} for store {1}.",
                                                                    commitPointId, storeName));
            }

            var query        = ParseSparql(queryExpression);
            var targetFormat = QueryReturnsGraph(query) ? (ISerializationFormat)graphFormat : sparqlResultFormat;
            var cacheKey     = MakeQueryCacheKey(storeName, commitPoint.CommitTime.Ticks, query, g, targetFormat);
            var cachedResult = GetCachedResult(cacheKey);

            if (cachedResult == null)
            {
                var storeWorker = GetStoreWorker(storeName);
                var cacheStream = new MemoryStream();
                storeWorker.Query(commitPointId, query, targetFormat, cacheStream, g);
                cachedResult = CacheResult(cacheKey, cacheStream.ToArray());
            }

            cachedResult.WriteTo(responseStream);
            return(targetFormat);
        }
        public static object ToRdfReader(RdfFormat rdfFormat)
        {
            switch (rdfFormat.GetJenaName())
            {
            case "RDF/XML":
                return(new RdfXmlParser());

            case "JSON-LD":
                return(new JsonLdParser());

            case "TURTLE":
                return(new TurtleParser());

            case "N-QUADS":
                return(new NQuadsParser());

            case "N-TRIPLE":
                return(new NTriplesParser());

            default:
                throw new RdfParseException("Unknown rdf format");
            }
        }
        /// <summary>
        /// Converts rdf string to dotNetRdf graph.
        /// </summary>
        /// <param name="baseUri"></param>
        /// <param name="serializedModel">Literal string read from file.</param>
        /// <param name="rdfFormat"></param>
        /// <returns></returns>
        public IGraph DeserializeGraph(string baseUri, string serializedModel, RdfFormat rdfFormat)
        {
            // TODO: Handle 'Optional' properly
            var reader = RdfFormatsMapper.ToRdfReader(rdfFormat);

            try
            {
                if (reader is IRdfReader)
                {
                    var g = new Graph();
                    if (!String.IsNullOrEmpty(baseUri))
                    {
                        g.BaseUri = new Uri(baseUri);
                    }
                    g.LoadFromString(serializedModel, (IRdfReader)reader);
                    return(g);
                }
                if (reader is IStoreReader)
                {
                    var ts = new TripleStore();
                    ts.LoadFromString(serializedModel, (IStoreReader)reader);
                    var g = ts.Graphs[null];
                    if (!String.IsNullOrEmpty(baseUri))
                    {
                        g.BaseUri = new Uri(baseUri);
                    }
                    return(g);
                }
                return(null);
            }
            catch (IOException ex)
            {
                string msg = String.Format("Unable to parse serialized RDF from '%s' format.", rdfFormat.GetJenaName());
                throw new RdfSerializationException(msg, ex);
            }
        }
 /// <summary>
 /// Starts an export job
 /// </summary>
 /// <param name="store">The store to export data from</param>
 /// <param name="fileName">The name of the file in the brightstar\import folder to write to. This file will be overwritten if it already exists.</param>
 /// <param name="graphUri">The URI of the graph to be exported. If NULL, all graphs in the store are exported.</param>
 /// <param name="exportFormat">The <see cref="BrightstarDB.RdfFormat"/> to when serializing the export data. Currently only NQuads and NTriples are supported. Defaults to NQuads</param>
 /// <param name="label">Optional user-friendly label for the job.</param>
 /// <returns>A JobInfo instance</returns>
 public IJobInfo StartExport(string store, string fileName, string graphUri, RdfFormat exportFormat = null, string label = null)
 {
     if (String.IsNullOrEmpty(store)) throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, "store");
     if (String.IsNullOrEmpty(fileName)) throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, "fileName");
     if (exportFormat == null) exportFormat = RdfFormat.NQuads;
     try
     {
         var jobId = _serverCore.Export(store, fileName, graphUri, exportFormat, label);
         return GetJobInfo(store, jobId.ToString());
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarEventId.ServerCoreException, "Error queing export job {0} {1}", store,
                          fileName);
         throw new BrightstarClientException("Error queing export job in store " + store + ". " + ex.Message, ex);
     }
 }
 /// <summary>
 /// Query a specific commit point of a store
 /// </summary>
 /// <param name="commitPoint">The commit point be queried</param>
 /// <param name="queryExpression">The SPARQL query string</param>
 /// <param name="defaultGraphUris">OPTIONAL: An enumeration over the URIs of the graphs that will be taken together as the default graph for the query. May be NULL to use the built-in default graph</param>
 /// <param name="resultsFormat">OPTIONAL: Specifies the serialization format for the SPARQL results. Defaults to <see cref="SparqlResultsFormat.Xml"/></param>
 /// <param name="graphFormat">OPTIONAL: Specifies the serialization format for RDF graph results. Defaults to <see cref="RdfFormat.RdfXml"/></param>
 /// <returns>A stream containing XML SPARQL results</returns>
 public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression,
                            IEnumerable<string> defaultGraphUris,
                            SparqlResultsFormat resultsFormat = null, RdfFormat graphFormat = null)
 {
     ISerializationFormat streamFormat;
     return ExecuteQuery(commitPoint, queryExpression, defaultGraphUris,
                         resultsFormat ?? SparqlResultsFormat.Xml, graphFormat ?? RdfFormat.RdfXml,
                         out streamFormat);
 }
 /// <summary>
 /// Query a specific commit point of a store
 /// </summary>
 /// <param name="commitPoint">The commit point be queried</param>
 /// <param name="queryExpression">The SPARQL query string</param>
 /// <param name="resultsFormat">OPTIONAL: Specifies the serialization format for the SPARQL results. Defaults to <see cref="SparqlResultsFormat.Xml"/></param>
 /// <param name="graphFormat">OPTIONAL: Specifies the serialization format for RDF graph results. Defaults to <see cref="RdfFormat.RdfXml"/></param>
 /// <returns>A stream containing XML SPARQL results</returns>
 public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression,
                            SparqlResultsFormat resultsFormat = null, RdfFormat graphFormat = null)
 {
     return ExecuteQuery(commitPoint, queryExpression, (IEnumerable<string>)null, resultsFormat, graphFormat);
 }
 /// <summary>
 /// Query the store using a SPARQL query
 /// </summary>
 /// <param name="storeName">The name of the store to query</param>
 /// <param name="queryExpression">SPARQL query string</param>
 /// <param name="defaultGraphUris">An enumeration over the URIs of the graphs that will be taken together as the default graph for the query</param>
 /// <param name="ifNotModifiedSince">OPTIONAL : If this parameter is provided and the store has not been changed since the time specified,
 /// a BrightstarClientException will be raised with the message "Store not modified".</param>
 /// <param name="resultsFormat">OPTIONAL: Specifies the serialization format for the SPARQL results. Defaults to <see cref="SparqlResultsFormat.Xml"/></param>
 /// <param name="graphFormat">OPTIONAL: Specifies the serialization format for RDF graph results. Defaults to <see cref="RdfFormat.RdfXml"/></param>
 /// <returns>A stream containing XML SPARQL result XML</returns>
 public Stream ExecuteQuery(string storeName, string queryExpression, IEnumerable<string> defaultGraphUris,
                            DateTime? ifNotModifiedSince = null,
                            SparqlResultsFormat resultsFormat = null, RdfFormat graphFormat = null)
 {
     ISerializationFormat streamFormat;
     return ExecuteQuery(storeName, queryExpression, defaultGraphUris, ifNotModifiedSince,
                         resultsFormat ?? SparqlResultsFormat.Xml, graphFormat ?? RdfFormat.RdfXml,
                         out streamFormat);
 }
 /// <summary>
 /// Query the store using a SPARQL query
 /// </summary>
 /// <param name="storeName">The name of the store to query</param>
 /// <param name="queryExpression">SPARQL query string</param>
 /// <param name="ifNotModifiedSince">OPTIONAL : If this parameter is provided and the store has not been changed since the time specified,
 /// a BrightstarClientException will be raised with the message "Store not modified".</param>
 /// <param name="resultsFormat">OPTIONAL: Specifies the serialization format for the SPARQL results. Defaults to <see cref="SparqlResultsFormat.Xml"/></param>
 /// <param name="graphFormat">OPTIONAL: Specifies the serialization format for RDF graph results. Defaults to <see cref="RdfFormat.RdfXml"/></param>
 /// <returns>A stream containing XML SPARQL result XML</returns>
 public Stream ExecuteQuery(string storeName, string queryExpression,
                            DateTime? ifNotModifiedSince = new DateTime?(),
                            SparqlResultsFormat resultsFormat = null,
     RdfFormat graphFormat = null)
 {
     return ExecuteQuery(storeName, queryExpression, (string[])null, ifNotModifiedSince, resultsFormat, graphFormat);
 }
Esempio n. 31
0
 public Guid Import(string storeName, string contentFileName, string graphUri, RdfFormat importFormat = null, string jobLabel = null)
 {
     var storeWorker = GetStoreWorker(storeName);
     return storeWorker.Import(contentFileName, graphUri, importFormat, jobLabel);
 }
        public string TriplesToSerialization(HashSet <Triple> triples, RdfFormat rdfFormat)
        {
            var graph = TriplesToGraph(triples);

            return(GraphToSerialization(graph, RdfFormatsMapper.ToRdfWriter(rdfFormat)));
        }
Esempio n. 33
0
 private static ITripleSink GetWriterSink(RdfFormat exportFormat, TextWriter textWriter)
 {
     if (exportFormat == RdfFormat.NTriples)
     {
         return new NTriplesWriter(textWriter);
     } 
     if (exportFormat == RdfFormat.NQuads)
     {
         return new NQuadsWriter(textWriter);
     }
     throw new BrightstarClientException(String.Format(Strings.ExportJob_UnsupportedExportFormat, exportFormat.MediaTypes[0]));
 }
Esempio n. 34
0
 public Guid Export(string fileName, string graphUri, RdfFormat exportFormat, string jobLabel = null)
 {
     Logging.LogDebug("Export {0}, {1}, {2}", fileName, graphUri, exportFormat.DefaultExtension);
     var jobId = Guid.NewGuid();
     var exportJob = new ExportJob(jobId, jobLabel, this, fileName, graphUri, RdfFormat.NQuads);
     _jobExecutionStatus.TryAdd(jobId.ToString(),
                                new JobExecutionStatus
                                    {
                                        JobId = jobId,
                                        JobStatus = JobStatus.Started,
                                        Queued = DateTime.UtcNow,
                                        Started = DateTime.UtcNow,
                                        Label = jobLabel
                                    });
     exportJob.Run((id, ex) =>
                       {
                           JobExecutionStatus jobExecutionStatus;
                           if (_jobExecutionStatus.TryGetValue(id.ToString(), out jobExecutionStatus))
                           {
                               jobExecutionStatus.Information = "Export failed";
                               jobExecutionStatus.ExceptionDetail = new ExceptionDetailObject(ex);
                               jobExecutionStatus.JobStatus = JobStatus.TransactionError;
                               jobExecutionStatus.Ended = DateTime.UtcNow;
                           }
                       },
                   id =>
                       {
                           JobExecutionStatus jobExecutionStatus;
                           if (_jobExecutionStatus.TryGetValue(id.ToString(), out jobExecutionStatus))
                           {
                               jobExecutionStatus.Information = "Export completed";
                               jobExecutionStatus.JobStatus = JobStatus.CompletedOk;
                               jobExecutionStatus.Ended = DateTime.UtcNow;
                           }
                       });
     return jobId;
 }
Esempio n. 35
0
 private RdfFormat GetRequestBodyFormat()
 {
     return(Request.Headers.ContentType == null ? RdfFormat.RdfXml : RdfFormat.GetResultsFormat(Request.Headers.ContentType));
 }
Esempio n. 36
0
        /// <summary>
        /// Creates a store export job request object
        /// </summary>
        /// <param name="exportFileName">The name of the file to export the data to</param>
        /// <param name="graphUri">OPTIONAL: The URI identifier of the graph to be exported. If not provided, all graphs in the store are exported</param>
        /// <param name="exportFormat">The serialization format to use for the exported data. If unspecified or null, export will default to using NQuads format. </param>
        /// <param name="label">A user-friendly label for the job</param>
        /// <returns>A new <see cref="JobRequestObject"/> instance</returns>
        public static JobRequestObject CreateExportJob(string exportFileName, string graphUri = null, RdfFormat exportFormat = null, string label = null)
        {
            if (exportFileName == null) throw new ArgumentNullException("exportFileName");
            if (String.IsNullOrWhiteSpace(exportFileName)) throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, "exportFileName");
            if (graphUri != null && String.IsNullOrWhiteSpace(graphUri)) throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, "graphUri");
            if (exportFormat == null) exportFormat = RdfFormat.NQuads;

            return new JobRequestObject("Export",
                                        new Dictionary<string, string>
                                            {
                                                {"FileName", exportFileName},
                                                {"GraphUri", graphUri},
                                                {"Format", exportFormat.MediaTypes[0]}
                                            },
                                        label);
        }
 /// <summary>
 /// Creates a new SparqlQuerContext instance
 /// </summary>
 /// <param name="sparqlQuery">The SPARQL query to be executed</param>
 /// <param name="tableResultsFormat">The requested serialization form for a SPARQL result set</param>
 /// <param name="graphResultsFormat">The requested serialization form for a SPARQL result graph</param>
 public SparqlQueryContext(string sparqlQuery, SparqlResultsFormat tableResultsFormat, RdfFormat graphResultsFormat) :
     this(sparqlQuery, new System.Tuple <string, string> [0], new OrderingDirection[0], tableResultsFormat, graphResultsFormat)
 {
 }
Esempio n. 38
0
        // operations
        public ISerializationFormat Query(string storeName, string queryExpression, IEnumerable<string> defaultGraphUris,
                                          DateTime? ifNotModifiedSince,
                                          SparqlResultsFormat sparqlResultFormat, RdfFormat graphFormat,
                                          Stream responseStream)
        {
            Logging.LogDebug("Query {0} {1}", storeName, queryExpression);
            var commitPoint =
                _storeManager.GetMasterFile(Path.Combine(_baseLocation, storeName)).GetCommitPoints().First();
            if (ifNotModifiedSince.HasValue && ifNotModifiedSince > commitPoint.CommitTime)
            {
                throw new BrightstarStoreNotModifiedException();
            }
            var g = defaultGraphUris == null ? null : defaultGraphUris.ToArray();
            var query = ParseSparql(queryExpression);
            var targetFormat = QueryReturnsGraph(query) ? (ISerializationFormat) graphFormat : sparqlResultFormat;

            var cacheKey = MakeQueryCacheKey(storeName, commitPoint.CommitTime.Ticks, query, g, targetFormat);
            var cachedResult = GetCachedResult(cacheKey);
            if (cachedResult == null)
            {
                // Not in the cache so execute the query on the StoreWorker
                var storeWorker = GetStoreWorker(storeName);
                var cacheStream = new MemoryStream();
                storeWorker.Query(query, targetFormat, cacheStream, g);
                cachedResult = CacheResult(cacheKey, cacheStream.ToArray());
            }
            cachedResult.WriteTo(responseStream);
            return targetFormat;
        }
Esempio n. 39
0
        /// <summary>
        /// Creates a store import job request object
        /// </summary>
        /// <param name="importFileName">The name of the file to import the data from</param>
        /// <param name="defaultGraphUri">OPTIONAL: The default graph to apply to triples parsed from the import file. If not provided, defaults to the system default graph./</param>
        /// <param name="label">A user-friendly label for the job</param>
        /// <param name="importFormat">The format of the file to be imported</param>
        /// <returns>A new <see cref="JobRequestObject"/> instance</returns>
        public static JobRequestObject CreateImportJob(string importFileName, string defaultGraphUri = null,
                                                       string label = null, RdfFormat importFormat = null)
        {
            if (importFileName == null) throw new ArgumentNullException(nameof(importFileName));
            if (string.IsNullOrWhiteSpace(importFileName))
                throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, nameof(importFileName));
            if (defaultGraphUri != null && string.Empty.Equals(defaultGraphUri.Trim()))
                throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, nameof(defaultGraphUri));

            var jobParams = new Dictionary<string, string>
            {
                {"FileName", importFileName},
                {"DefaultGraphUri", defaultGraphUri}
            };
            if (importFormat != null) jobParams["ImportFormat"] = importFormat.MediaTypes.First();
            return new JobRequestObject("Import", jobParams, label);
        }
Esempio n. 40
0
        public JobsModule(IBrightstarService brightstarService, AbstractStorePermissionsProvider permissionsProvider)
        {
            this.RequiresBrightstarStorePermissionData(permissionsProvider);

            Get["/{storeName}/jobs"] = parameters =>
            {
                var jobsRequestObject = this.Bind <JobsRequestModel>();
                if (jobsRequestObject == null || jobsRequestObject.StoreName == null)
                {
                    return(HttpStatusCode.BadRequest);
                }
                if (jobsRequestObject.Take <= 0)
                {
                    jobsRequestObject.Take = DefaultPageSize;
                }
                var jobs = brightstarService.GetJobInfo(jobsRequestObject.StoreName, jobsRequestObject.Skip,
                                                        jobsRequestObject.Take + 1);
                return(Negotiate.WithPagedList(jobsRequestObject,
                                               jobs.Select(j => j.MakeResponseObject(jobsRequestObject.StoreName)),
                                               jobsRequestObject.Skip, jobsRequestObject.Take, DefaultPageSize,
                                               "jobs"));
            };

            Get["/{storeName}/jobs/{jobId}"] = parameters =>
            {
                var request = this.Bind <JobRequestModel>();
                if (request == null ||
                    request.StoreName == null ||
                    request.JobId == null)
                {
                    return(HttpStatusCode.BadRequest);
                }
                var job = brightstarService.GetJobInfo(request.StoreName, request.JobId);
                if (job == null)
                {
                    return(HttpStatusCode.NotFound);
                }
                var responseDto = job.MakeResponseObject(request.StoreName);
                return(responseDto);
            };

            Post["/{storeName}/jobs"] = parameters =>
            {
                var jobRequestObject = this.Bind <JobRequestObject>();

                // Validate
                if (jobRequestObject == null)
                {
                    return(HttpStatusCode.BadRequest);
                }
                if (String.IsNullOrWhiteSpace(jobRequestObject.JobType))
                {
                    return(HttpStatusCode.BadRequest);
                }

                var storeName = parameters["storeName"];
                var label     = jobRequestObject.Label;
                try
                {
                    IJobInfo queuedJobInfo;
                    switch (jobRequestObject.JobType.ToLowerInvariant())
                    {
                    case "consolidate":
                        AssertPermission(StorePermissions.Admin);
                        queuedJobInfo = brightstarService.ConsolidateStore(storeName, label);
                        break;

                    case "createsnapshot":
                        AssertPermission(StorePermissions.Admin);
                        PersistenceType persistenceType;

                        // Validate TargetStoreName and PersistenceType parameters
                        if (!jobRequestObject.JobParameters.ContainsKey("TargetStoreName") ||
                            String.IsNullOrWhiteSpace(jobRequestObject.JobParameters["TargetStoreName"]) ||
                            !jobRequestObject.JobParameters.ContainsKey("PersistenceType") ||
                            !Enum.TryParse(jobRequestObject.JobParameters["PersistenceType"],
                                           out persistenceType))
                        {
                            return(HttpStatusCode.BadRequest);
                        }
                        // Extract optional commit point parameter
                        ICommitPointInfo commitPoint = null;
                        if (jobRequestObject.JobParameters.ContainsKey("CommitId"))
                        {
                            ulong commitId;
                            if (!UInt64.TryParse(jobRequestObject.JobParameters["CommitId"], out commitId))
                            {
                                return(HttpStatusCode.BadRequest);
                            }
                            commitPoint = brightstarService.GetCommitPoint(storeName, commitId);
                            if (commitPoint == null)
                            {
                                return(HttpStatusCode.BadRequest);
                            }
                        }

                        // Execute
                        queuedJobInfo = brightstarService.CreateSnapshot(
                            storeName, jobRequestObject.JobParameters["TargetStoreName"],
                            persistenceType, commitPoint, label);
                        break;

                    case "export":
                        AssertPermission(StorePermissions.Export);
                        if (!jobRequestObject.JobParameters.ContainsKey("FileName") ||
                            String.IsNullOrWhiteSpace(jobRequestObject.JobParameters["FileName"]))
                        {
                            return(HttpStatusCode.BadRequest);
                        }
                        RdfFormat format = jobRequestObject.JobParameters.ContainsKey("Format")
                                                       ? RdfFormat.GetResultsFormat(
                            jobRequestObject.JobParameters["Format"])
                                                       : RdfFormat.NQuads;

                        queuedJobInfo = brightstarService.StartExport(
                            storeName,
                            jobRequestObject.JobParameters["FileName"],
                            jobRequestObject.JobParameters.ContainsKey("GraphUri") ? jobRequestObject.JobParameters["GraphUri"] : null,
                            format,
                            label);
                        break;

                    case "import":
                        AssertPermission(StorePermissions.TransactionUpdate);
                        if (!jobRequestObject.JobParameters.ContainsKey("FileName") ||
                            String.IsNullOrWhiteSpace(jobRequestObject.JobParameters["FileName"]))
                        {
                            return(HttpStatusCode.BadRequest);
                        }
                        queuedJobInfo = brightstarService.StartImport(
                            storeName,
                            jobRequestObject.JobParameters["FileName"],
                            jobRequestObject.JobParameters.ContainsKey("DefaultGraphUri") ? jobRequestObject.JobParameters["DefaultGraphUri"] : Constants.DefaultGraphUri,
                            label);
                        break;

                    case "repeattransaction":
                        AssertPermission(StorePermissions.Admin);
                        if (!jobRequestObject.JobParameters.ContainsKey("JobId") ||
                            String.IsNullOrWhiteSpace(jobRequestObject.JobParameters["JobId"]))
                        {
                            return(HttpStatusCode.BadRequest);
                        }
                        Guid jobId;
                        if (!Guid.TryParse(jobRequestObject.JobParameters["JobId"], out jobId))
                        {
                            return(HttpStatusCode.BadRequest);
                        }
                        var transaction = brightstarService.GetTransaction(storeName, jobId);
                        if (transaction == null)
                        {
                            return(HttpStatusCode.BadRequest);
                        }
                        queuedJobInfo = brightstarService.ReExecuteTransaction(storeName, transaction, label);
                        break;

                    case "sparqlupdate":
                        AssertPermission(StorePermissions.SparqlUpdate);
                        if (!jobRequestObject.JobParameters.ContainsKey("UpdateExpression") ||
                            String.IsNullOrWhiteSpace(jobRequestObject.JobParameters["UpdateExpression"]))
                        {
                            return(HttpStatusCode.BadRequest);
                        }
                        queuedJobInfo = brightstarService.ExecuteUpdate(
                            storeName,
                            jobRequestObject.JobParameters["UpdateExpression"],
                            false,
                            label);
                        break;

                    case "transaction":
                        AssertPermission(StorePermissions.TransactionUpdate);
                        var preconditions = jobRequestObject.JobParameters.ContainsKey("Preconditions")
                                                        ? jobRequestObject.JobParameters["Preconditions"]
                                                        : null;
                        var nonexistence =
                            jobRequestObject.JobParameters.ContainsKey("NonexistencePreconditions")
                                        ? jobRequestObject.JobParameters["NonexistencePreconditions"]
                                        : null;
                        var deletePatterns = jobRequestObject.JobParameters.ContainsKey("Deletes")
                                                         ? jobRequestObject.JobParameters["Deletes"]
                                                         : null;
                        var insertTriples = jobRequestObject.JobParameters.ContainsKey("Inserts")
                                                        ? jobRequestObject.JobParameters["Inserts"]
                                                        : null;
                        var defaultGraphUri =
                            jobRequestObject.JobParameters.ContainsKey("DefaultGraphUri") &&
                            !String.IsNullOrEmpty(jobRequestObject.JobParameters["DefaultGraphUri"])
                                        ? jobRequestObject.JobParameters["DefaultGraphUri"]
                                        : null;

                        queuedJobInfo = brightstarService.ExecuteTransaction(
                            storeName, new UpdateTransactionData
                        {
                            ExistencePreconditions    = preconditions,
                            NonexistencePreconditions = nonexistence,
                            DeletePatterns            = deletePatterns,
                            InsertData      = insertTriples,
                            DefaultGraphUri = defaultGraphUri
                        },
                            false,
                            label);
                        break;

                    case "updatestats":
                        AssertPermission(StorePermissions.Admin);
                        queuedJobInfo = brightstarService.UpdateStatistics(storeName, label);
                        break;

                    default:
                        return(HttpStatusCode.BadRequest);
                    }

                    var jobUri = (string)storeName + "/jobs/" + queuedJobInfo.JobId;
                    return(Negotiate.WithModel(new JobResponseModel
                    {
                        JobId = queuedJobInfo.JobId,
                        Label = queuedJobInfo.Label,
                        StatusMessage = queuedJobInfo.StatusMessage,
                        JobStatus = queuedJobInfo.GetJobStatusString(),
                        ExceptionInfo = queuedJobInfo.ExceptionInfo,
                        QueuedTime = queuedJobInfo.QueuedTime,
                        StartTime = queuedJobInfo.StartTime,
                        EndTime = queuedJobInfo.EndTime
                    })
                           .WithHeader("Location", jobUri)
                           .WithStatusCode(HttpStatusCode.Created));
                }
                catch (UnauthorizedAccessException)
                {
                    return(HttpStatusCode.Unauthorized);
                }
            };
        }
Esempio n. 41
0
 public NormalizedRdf(RdfDataset rdfDataset, string serializedNormalizedObject, RdfFormat serializationFormat)
 {
     RdfDataset = rdfDataset;
     SerializedNormalizedObject = serializedNormalizedObject;
     SerializationFormat        = serializationFormat;
 }
Esempio n. 42
0
 public Guid Import(string contentFileName, string graphUri, RdfFormat importFormat = null, string jobLabel = null)
 {
     Logging.LogDebug("Import {0}, {1}, {2}", contentFileName, graphUri, importFormat);
     var jobId = Guid.NewGuid();
     var job = new ImportJob(jobId, jobLabel, this, contentFileName, importFormat, graphUri);
     QueueJob(job);
     return jobId;
 }
 /// <summary>
 /// Query the store using a SPARQL query
 /// </summary>
 /// <param name="storeName">The name of the store to query</param>
 /// <param name="queryExpression">SPARQL query string</param>
 /// <param name="defaultGraphUri">The URI of the graph that will be the default graph for the query</param>
 /// <param name="ifNotModifiedSince">OPTIONAL : If this parameter is provided and the store has not been changed since the time specified,
 /// a BrightstarClientException will be raised with the message "Store not modified".</param>
 /// <param name="resultsFormat">OPTIONAL: Specifies the serialization format for the SPARQL results. Defaults to <see cref="SparqlResultsFormat.Xml"/></param>
 /// <param name="graphFormat">OPTIONAL: Specifies the serialization format for RDF graph results. Defaults to <see cref="RdfFormat.RdfXml"/></param>
 /// <returns>A stream containing XML SPARQL result XML</returns>
 public Stream ExecuteQuery(string storeName, string queryExpression,
                            string defaultGraphUri,
                            DateTime? ifNotModifiedSince = new DateTime?(),
                            SparqlResultsFormat resultsFormat = null,
     RdfFormat graphFormat = null)
 {
     return ExecuteQuery(storeName, queryExpression, defaultGraphUri == null ? null : new[] { defaultGraphUri }, ifNotModifiedSince,
                         resultsFormat, graphFormat);
 }
Esempio n. 44
0
 public Guid Export(string storeName, string fileName, string graphUri, RdfFormat exportFormat, string jobLabel = null)
 {
     var storeWorker = GetStoreWorker(storeName);
     return storeWorker.Export(fileName, graphUri, exportFormat, jobLabel);
 }
        /// <summary>
        /// Query a specific commit point of a store
        /// </summary>
        /// <param name="storeName">The name of the store to query</param>
        /// <param name="queryExpression">The SPARQL query string</param>
        /// <param name="ifNotModifiedSince">OPTIONAL : If this parameter has a value and the store has not been changed since the time specified,
        /// a <see cref="BrightstarStoreNotModifiedException"/> will be raised with the message "Store not modified".</param>
        /// <param name="defaultGraphUris">An enumeration over the URIs of the graphs that will be taken together as the default graph for the query. May be NULL to use the built-in default graph</param>
        /// <param name="resultsFormat">Specifies the serialization format for the SPARQL result set returned by the query. May be NULL to indicate that an RDF graph is the expected result.</param>
        /// <param name="graphFormat">Specifies the serialization format for the RDF graph returned by the query. May be NULL to indicate that a SPARQL results set is the expected result.</param>
        /// <param name="streamFormat">Specifies the serialization format used in the returned <see cref="Stream"/>.</param>
        /// <returns>A stream containing the results of executing the query</returns>
        public Stream ExecuteQuery(string storeName, string queryExpression, IEnumerable<string> defaultGraphUris, DateTime? ifNotModifiedSince, 
            SparqlResultsFormat resultsFormat, RdfFormat graphFormat, out ISerializationFormat streamFormat)
        {
            if (storeName == null) throw new ArgumentNullException("storeName");
            if (queryExpression == null) throw new ArgumentNullException("queryExpression");
            if (resultsFormat == null && graphFormat == null) throw new ArgumentException("Either resultsFormat or graphFormat must be non-NULL");

            if (!_serverCore.DoesStoreExist(storeName)) throw new NoSuchStoreException(storeName);
            try
            {
                var pStream = new MemoryStream();
                streamFormat = _serverCore.Query(storeName, queryExpression,
                    defaultGraphUris == null ? null : defaultGraphUris.Where(x => !string.IsNullOrEmpty(x)), 
                    ifNotModifiedSince, resultsFormat,
                    graphFormat, pStream);
                return new MemoryStream(pStream.ToArray());
            }
            catch (BrightstarStoreNotModifiedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Logging.LogError(BrightstarEventId.ServerCoreException, "Error Executing Query {0} {1}", storeName, queryExpression);
                throw new BrightstarClientException("Error querying store " + storeName + " with expression " + queryExpression + ". " + ex.Message, ex);
            }
        }
Esempio n. 46
0
        public Guid Import(string storeName, string contentFileName, string graphUri, RdfFormat importFormat = null, string jobLabel = null)
        {
            var storeWorker = GetStoreWorker(storeName);

            return(storeWorker.Import(contentFileName, graphUri, importFormat, jobLabel));
        }
 /// <summary>
 /// Query a specific commit point of a store
 /// </summary>
 /// <param name="commitPoint">The commit point be queried</param>
 /// <param name="queryExpression">The SPARQL query string</param>
 /// <param name="defaultGraphUri">The URI of the default graph for the query</param>
 /// <param name="resultsFormat">OPTIONAL: Specifies the serialization format for the SPARQL results. Defaults to <see cref="SparqlResultsFormat.Xml"/></param>
 /// <param name="graphFormat">OPTIONAL: Specifies the serialization format for RDF graph results. Defaults to <see cref="RdfFormat.RdfXml"/></param>
 /// <returns>A stream containing XML SPARQL results</returns>
 public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression,
                            string defaultGraphUri, SparqlResultsFormat resultsFormat = null, RdfFormat graphFormat = null)
 {
     return ExecuteQuery(commitPoint, queryExpression, new[] { defaultGraphUri }, resultsFormat, graphFormat);
 }
Esempio n. 48
0
 private static ITripleSink GetWriterSink(RdfFormat exportFormat, TextWriter textWriter)
 {
     if (exportFormat.MatchesMediaType(RdfFormat.NTriples))
     {
         return new NTriplesWriter(textWriter);
     } 
     if (exportFormat.MatchesMediaType(RdfFormat.NQuads))
     {
         return new NQuadsWriter(textWriter);
     }
     if (exportFormat.MatchesMediaType(RdfFormat.RdfXml))
     {
         var xw = XmlWriter.Create(textWriter, new XmlWriterSettings {CloseOutput = false, Indent = true});
         return new RdfXmlWriter(xw);
     }
     throw new BrightstarClientException(String.Format(Strings.ExportJob_UnsupportedExportFormat, exportFormat.MediaTypes[0]));
 }
        /// <summary>
        /// Query a specific commit point of a store
        /// </summary>
        /// <param name="commitPoint">The commit point be queried</param>
        /// <param name="queryExpression">The SPARQL query string</param>
        /// <param name="defaultGraphUris">An enumeration over the URIs of the graphs that will be taken together as the default graph for the query. May be NULL to use the built-in default graph</param>
        /// <param name="resultsFormat">Specifies the serialization format for the SPARQL result set returned by the query. May be NULL to indicate that an RDF graph is the expected result.</param>
        /// <param name="graphFormat">Specifies the serialization format for the RDF graph returned by the query. May be NULL to indicate that a SPARQL results set is the expected result.</param>
        /// <param name="streamFormat">Specifies the serialization format used in the returned <see cref="Stream"/>.</param>
        /// <returns>A stream containing the results of executing the query</returns>
        public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression, IEnumerable<string> defaultGraphUris, 
            SparqlResultsFormat resultsFormat, RdfFormat graphFormat, out ISerializationFormat streamFormat)
        {
            if (queryExpression == null) throw new ArgumentNullException("queryExpression");
            if (resultsFormat == null) resultsFormat = SparqlResultsFormat.Xml;
            if (graphFormat == null) graphFormat = RdfFormat.RdfXml;

            try
            {
                var pStream = new MemoryStream();
                streamFormat = _serverCore.Query(commitPoint.StoreName, commitPoint.Id, queryExpression, defaultGraphUris, resultsFormat, graphFormat, pStream);
                return new MemoryStream(pStream.ToArray());
            }
#if !PORTABLE && !WINDOWS_PHONE
            catch (AggregateException aggregateException)
            {
                Logging.LogError(BrightstarEventId.ServerCoreException,
                                 "Error querying store {0}@{1} with expression {2}", commitPoint.StoreName,
                                 commitPoint.Id, queryExpression);
                if (aggregateException.InnerExceptions.Count == 1)
                {
                    throw new BrightstarClientException(
                        String.Format("Error querying store {0}@{1} with expression {2}. {3}",
                                      commitPoint.StoreName, commitPoint.Id, queryExpression,
                                      aggregateException.InnerExceptions[0].Message));
                }
                var messages = String.Join("; ", aggregateException.InnerExceptions.Select(ex => ex.Message));
                throw new BrightstarClientException(
                    String.Format(
                        "Error querying store {0}@{1} with expression {2}. Multiple errors occurred: {3}",
                        commitPoint.StoreName, commitPoint.Id, queryExpression, messages));
            }
#endif
            catch (Exception ex)
            {
                Logging.LogError(BrightstarEventId.ServerCoreException,
                                 "Error querying store {0}@{1} with expression {2}", commitPoint.StoreName,
                                 commitPoint.Id, queryExpression);
                throw new BrightstarClientException(
                    String.Format("Error querying store {0}@{1} with expression {2}. {3}", commitPoint.StoreName,
                                  commitPoint.Id, queryExpression, ex.Message), ex);
            }
        }
Esempio n. 50
0
 /// <summary>
 /// Creates a new SparqlQueryContext instance
 /// </summary>
 /// <param name="sparqlQuery">sparql query</param>
 /// <param name="anonymousMembersMap">mappings for anonymous types</param>
 /// <param name="orderingDirections">ordering direction for the sort expressions in the SPARQL query</param>
 /// <param name="tableResultsFormat">The requested serialization form for a SPARQL result set</param>
 /// <param name="graphResultsFormat">The requested serialization form for a SPARQL result graph</param>
 public SparqlQueryContext(string sparqlQuery, IEnumerable<System.Tuple<string, string>> anonymousMembersMap,
     IEnumerable<OrderingDirection> orderingDirections,
     SparqlResultsFormat tableResultsFormat, RdfFormat graphResultsFormat)
 {
     SparqlQuery = sparqlQuery;
     AnonymousMembersMap = new List<System.Tuple<string, string>>(anonymousMembersMap);
     OrderingDirections = new List<OrderingDirection>(orderingDirections);
     SparqlResultsFormat = tableResultsFormat;
     GraphResultsFormat = graphResultsFormat;
 }
 public IGraph DeserializeGraph(string serializedModel, RdfFormat rdfFormat)
 {
     return(DeserializeGraph("", serializedModel, rdfFormat));
 }
Esempio n. 52
0
        private string ParseBody( RdfFormat contentType)
        {
            var parser = MimeTypesHelper.GetParser(contentType.MediaTypes.First());

            var writer = new WriteToStringHandler(typeof (VDS.RDF.Writing.Formatting.NTriplesFormatter));
            using (var reader = new StreamReader(Request.Body))
            {
                parser.Load(writer, reader);
            }
            return writer.ToString();
        }
 public SparqlQueryResponse(SparqlQueryProcessingModel model, DateTime?ifNotModifiedSince, SparqlResultsFormat format, RdfFormat graphFormat)
 {
     try
     {
         ISerializationFormat streamFormat;
         var resultStream = model.GetResultsStream(format, graphFormat, ifNotModifiedSince, out streamFormat);
         Contents    = resultStream.CopyTo;
         ContentType = streamFormat.ToString();
         StatusCode  = HttpStatusCode.OK;
     }
     catch (InvalidCommitPointException)
     {
         StatusCode = HttpStatusCode.NotFound;
     }
     catch (BrightstarStoreNotModifiedException)
     {
         StatusCode = HttpStatusCode.NotModified;
     }
 }
Esempio n. 54
0
        /// <summary>
        /// Creates a store export job request object
        /// </summary>
        /// <param name="exportFileName">The name of the file to export the data to</param>
        /// <param name="graphUri">OPTIONAL: The URI identifier of the graph to be exported. If not provided, all graphs in the store are exported</param>
        /// <param name="exportFormat">The serialization format to use for the exported data. If unspecified or null, export will default to using NQuads format. </param>
        /// <param name="label">A user-friendly label for the job</param>
        /// <returns>A new <see cref="JobRequestObject"/> instance</returns>
        public static JobRequestObject CreateExportJob(string exportFileName, string graphUri = null, RdfFormat exportFormat = null, string label = null)
        {
            if (exportFileName == null)
            {
                throw new ArgumentNullException("exportFileName");
            }
            if (String.IsNullOrWhiteSpace(exportFileName))
            {
                throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, "exportFileName");
            }
            if (graphUri != null && String.IsNullOrWhiteSpace(graphUri))
            {
                throw new ArgumentException(Strings.StringParameterMustBeNonEmpty, "graphUri");
            }
            if (exportFormat == null)
            {
                exportFormat = RdfFormat.NQuads;
            }

            return(new JobRequestObject("Export",
                                        new Dictionary <string, string>
            {
                { "FileName", exportFileName },
                { "GraphUri", graphUri },
                { "Format", exportFormat.MediaTypes[0] }
            },
                                        label));
        }
Esempio n. 55
0
        public ISerializationFormat Query(string storeName, ulong commitPointId, string queryExpression,
                                          IEnumerable<string> defaultGraphUris,
                                          SparqlResultsFormat sparqlResultFormat, RdfFormat graphFormat,
                                          Stream responseStream)
        {
            Logging.LogDebug("Query {0}@{1} {2}", storeName, commitPointId, queryExpression);
            var g = defaultGraphUris == null ? null : defaultGraphUris.ToArray();
            var storeLocation = Path.Combine(_baseLocation, storeName);
            var masterFile = _storeManager.GetMasterFile(storeLocation);
            if (masterFile.PersistenceType == PersistenceType.Rewrite)
            {
                throw new BrightstarClientException(
                    "Query of past commit points is not supported by the binary page persistence type");
            }
            var commitPoint =
                masterFile.GetCommitPoints().FirstOrDefault(c => c.LocationOffset == commitPointId);
            if (commitPoint == null)
            {
                throw new InvalidCommitPointException(String.Format("Could not find commit point {0} for store {1}.",
                                                                    commitPointId, storeName));
            }

            var query = ParseSparql(queryExpression);
            var targetFormat = QueryReturnsGraph(query) ? (ISerializationFormat)graphFormat : sparqlResultFormat;
            var cacheKey = MakeQueryCacheKey(storeName, commitPoint.CommitTime.Ticks, query, g, targetFormat);
            var cachedResult = GetCachedResult(cacheKey);
            if (cachedResult == null)
            {
                var storeWorker = GetStoreWorker(storeName);
                var cacheStream = new MemoryStream();
                storeWorker.Query(commitPointId, query, targetFormat, cacheStream, g);
                cachedResult = CacheResult(cacheKey, cacheStream.ToArray());
            }

            cachedResult.WriteTo(responseStream);
            return targetFormat;
        }
 public RdfFormatViewModel(RdfFormat fmt)
 {
     Format      = fmt;
     DisplayName = $"{fmt.DisplayName} ({fmt.DefaultExtension})";
 }
Esempio n. 57
0
        public Guid Export(string storeName, string fileName, string graphUri, RdfFormat exportFormat, string jobLabel = null)
        {
            var storeWorker = GetStoreWorker(storeName);

            return(storeWorker.Export(fileName, graphUri, exportFormat, jobLabel));
        }
 private HashSet <Triple> GetTriplesFromSerializedModel(String serializedModel, RdfFormat rdfFormat)
 {
     VDS.RDF.IGraph deserializeModel = _rdf4jSerializationHandler.DeserializeGraph(serializedModel, rdfFormat);
     return(_rdf4jMapper.GraphToTriples(deserializeModel));
 }
Esempio n. 59
0
 public RdfFormatViewModel(RdfFormat fmt)
 {
     Format = fmt;
     DisplayName = $"{fmt.DisplayName} ({fmt.DefaultExtension})";
 }
Esempio n. 60
0
 /// <summary>
 /// Creates a new SparqlQuerContext instance
 /// </summary>
 /// <param name="sparqlQuery">The SPARQL query to be executed</param>
 /// <param name="tableResultsFormat">The requested serialization form for a SPARQL result set</param>
 /// <param name="graphResultsFormat">The requested serialization form for a SPARQL result graph</param>
 public SparqlQueryContext(string sparqlQuery, SparqlResultsFormat tableResultsFormat, RdfFormat graphResultsFormat) : 
     this(sparqlQuery, new System.Tuple<string, string>[0], new OrderingDirection[0], tableResultsFormat, graphResultsFormat) { }