public SparqlResultModel(string storeName, IBrightstarService service, SparqlRequestObject sparqlRequest, SparqlResultsFormat resultsFormat) { _storeName = storeName; _sparqlRequest = sparqlRequest; _service = service; ResultsFormat = resultsFormat; }
public string GetString(SparqlResultsFormat format, IRdfWriter graphWriter = null) { switch (ResultType) { case BrightstarSparqlResultsType.VariableBindings: case BrightstarSparqlResultsType.Boolean: var stringWriter = new System.IO.StringWriter(); var sparqlXmlWriter = GetSparqlWriter(format); sparqlXmlWriter.Save(_resultSet, stringWriter); return stringWriter.GetStringBuilder().ToString(); case BrightstarSparqlResultsType.Graph: if (graphWriter == null) { #if WINDOWS_PHONE // Cannot use DTD because the mobile version of XmlWriter doesn't support writing a DOCTYPE. graphWriter = new RdfXmlWriter(WriterCompressionLevel.High, false); #else graphWriter = new RdfXmlWriter(); #endif } return StringWriter.Write(_graph, graphWriter); default: throw new BrightstarInternalException( String.Format("Unrecognized result type when serializing results string: {0}", ResultType)); } }
public static string ExecuteSparqlQuery(this IStore store, string sparqlExpression, SparqlResultsFormat resultsFormat) { var query = ParseSparql(sparqlExpression); var resultsStream = new MemoryStream(); store.ExecuteSparqlQuery(query, resultsFormat.WithEncoding(new UTF8Encoding(false)), resultsStream); var ret = Encoding.UTF8.GetString(resultsStream.ToArray()); return ret; }
public static string Query(this StoreWorker storeWorker, string sparqlExpression, SparqlResultsFormat resultsFormat, string[] defaultGraphUris) { var query = ParseSparql(sparqlExpression); using (var resultsStream = new MemoryStream()) { storeWorker.Query(query, resultsFormat.WithEncoding(new UTF8Encoding(false)), resultsStream, defaultGraphUris); return Encoding.UTF8.GetString(resultsStream.ToArray()); } }
public SparqlResultModel(string storeName, ulong commitId, IBrightstarService service, SparqlRequestObject sparqlRequest, SparqlResultsFormat resultsFormat, RdfFormat graphFormat) { _storeName = storeName; _commitId = commitId; _sparqlRequest = sparqlRequest; _service = service; ResultsFormat = resultsFormat; GraphFormat = graphFormat; }
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); }
private ISparqlResultsWriter GetSparqlWriter(SparqlResultsFormat format) { var ext = format.DefaultExtension; if (ext.Equals(SparqlResultsFormat.Xml.DefaultExtension)) return new SparqlXmlWriter(); if (ext.Equals(SparqlResultsFormat.Json.DefaultExtension)) return new SparqlJsonWriter(); if (ext.Equals(SparqlResultsFormat.Tsv.DefaultExtension)) return new SparqlTsvWriter(); if (ext.Equals(SparqlResultsFormat.Csv.DefaultExtension)) return new SparqlCsvWriter(); throw new BrightstarInternalException("Unsupported SPARQL results format"); }
private static ISparqlResultsWriter GetWriter(SparqlResultsFormat format) { if (format == SparqlResultsFormat.Csv) { return new SparqlCsvWriter(); } if (format == SparqlResultsFormat.Tsv) { return new SparqlTsvWriter(); } if (format == SparqlResultsFormat.Json) { return new SparqlJsonWriter(); } return new SparqlXmlWriter(); }
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(); } }
public string AsString(SparqlResultsFormat format) { var g = new VDS.RDF.Graph(); var results = new List<SparqlResult>(); foreach (var graphUri in Graphs) { var s = new Set(); s.Add(SparqlResultVariableName, g.CreateUriNode(new Uri(graphUri))); results.Add(new SparqlResult(s)); } var rs = new SparqlResultSet(results); var writer = GetWriter(format); var sw = new StringWriter(); writer.Save(rs, sw); sw.Flush(); return sw.ToString(); }
public SparqlQueryResponse(SparqlQueryProcessingModel model, DateTime? ifNotModifiedSince, SparqlResultsFormat format) { try { var resultStream = model.GetResultsStream(format, ifNotModifiedSince); Contents = resultStream.CopyTo; ContentType = format.MediaTypes[0]; StatusCode = HttpStatusCode.OK; } catch (InvalidCommitPointException) { StatusCode = HttpStatusCode.NotFound; } catch (BrightstarStoreNotModifiedException) { StatusCode = HttpStatusCode.NotModified; } }
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; } }
// 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; }
/// <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> /// <returns>A stream containing XML SPARQL results</returns> public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression, string defaultGraphUri, SparqlResultsFormat resultsFormat = null) { return ExecuteQuery(commitPoint, queryExpression, new[] {defaultGraphUri}, resultsFormat); }
/// <summary> /// Query the store using a SPARQL query /// </summary> /// <param name="storeName">Store to query</param> /// <param name="queryExpression">SPARQL query string</param> /// <param name="defaultGraphUris"></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: The serialization format for the SPARQL results set. Defaults to <see cref="SparqlResultsFormat.Xml"/>.</param> /// <returns>A stream containing XML SPARQL result XML</returns> /// <remarks>If the <paramref name="ifNotModifiedSince"/> parameter is used by an application, then the default caching provided /// by this class will be bypassed</remarks> public Stream ExecuteQuery(string storeName, string queryExpression, IEnumerable<string> defaultGraphUris, DateTime? ifNotModifiedSince = null, SparqlResultsFormat resultsFormat = null) { ValidateStoreName(storeName); if (queryExpression == null) throw new ArgumentNullException("queryExpression", Strings.BrightstarServiceClient_QueryMustNotBeNull); if (String.Empty.Equals(queryExpression)) throw new ArgumentException(Strings.BrightstarServiceClient_QueryMustNotBeEmptyString, "queryExpression"); var g = defaultGraphUris == null ? null : defaultGraphUris.ToArray(); string cacheKey = null; CachedQueryResult cachedResult = null; if (ifNotModifiedSince == null && _queryCache != null) { cacheKey = storeName + "_" + queryExpression.GetHashCode(); if (defaultGraphUris != null) { cacheKey = cacheKey + "_" + String.Join(",", g).GetHashCode(); } cachedResult = _queryCache.Lookup<CachedQueryResult>(cacheKey); if (cachedResult != null) { ifNotModifiedSince = cachedResult.Timestamp; } } try { var mediaType = resultsFormat == null ? SparqlResultsFormat.Xml.MediaTypes.First() + "; charset=utf-8" : resultsFormat.ToString(); var resultStream = _service.ExecuteQuery(storeName, queryExpression, defaultGraphUris == null ? null : g.ToArray(), ifNotModifiedSince, mediaType); if (_queryCache != null && cacheKey != null && LastResponseTimestamp.HasValue) { using (var streamReader = new StreamReader(resultStream)) { var resultString = streamReader.ReadToEnd(); cachedResult = new CachedQueryResult(LastResponseTimestamp.Value, resultString); _queryCache.Insert(cacheKey, cachedResult, CachePriority.Normal); return new MemoryStream(streamReader.CurrentEncoding.GetBytes(cachedResult.Result)); } } else { return resultStream; } } catch (FaultException<ExceptionDetail> fault) { if (cachedResult != null) { if (fault.Detail.Type.Equals("BrightstarDB.Client.BrightstarClientException") && fault.Detail.InnerException != null && fault.Detail.InnerException.Type.Equals("BrightstarDB.BrightstarStoreNotModifiedException")) { // Cached result is still fine return new MemoryStream( resultsFormat == null ? Encoding.UTF8.GetBytes(cachedResult.Result) : resultsFormat.Encoding.GetBytes(cachedResult.Result)); } } throw new BrightstarClientException(fault); } }
/// <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> /// <returns>A stream containing XML SPARQL result XML</returns> public Stream ExecuteQuery(string storeName, string queryExpression, DateTime? ifNotModifiedSince = null, SparqlResultsFormat resultsFormat = null) { return ExecuteQuery(storeName, queryExpression, (IEnumerable<string>) null, ifNotModifiedSince, resultsFormat); }
public string Query(string storeName, string queryExpression, IEnumerable<string> defaultGraphUris, SparqlResultsFormat resultsFormat) { var g = defaultGraphUris == null ? null : defaultGraphUris.ToArray(); Logging.LogDebug("Query {0} {1}", storeName, queryExpression); var currentCommitPoint = _storeManager.GetMasterFile(Path.Combine(_baseLocation, storeName)).GetLatestCommitPoint(); var cacheKey = MakeQueryCacheKey(storeName, currentCommitPoint.CommitTime.Ticks, queryExpression, g, resultsFormat); var cachedResult = GetCachedResult(cacheKey); if (cachedResult != null) { Logging.LogDebug("Returning cached result for query Query {0} {1}", storeName, queryExpression); return cachedResult; } var storeWorker = GetStoreWorker(storeName); var result = storeWorker.Query(queryExpression, resultsFormat, g); //add to cache CacheResult(cacheKey, result); return result; }
private static void WriteResults(SparqlResultsFormat resultsFormat, Stream responseStream, string results) { StreamWriter streamWriter; if (resultsFormat.DefaultExtension.Equals(SparqlResultsFormat.Xml.DefaultExtension) && !resultsFormat.Encoding.Equals(Encoding.Unicode)) { // We need to rewrite the XML, otherwise the encoding setting is wrong XDocument resultsDoc = XDocument.Parse(results); streamWriter = new StreamWriter(responseStream, resultsFormat.Encoding); resultsDoc.Save(streamWriter); streamWriter.Flush(); } else { streamWriter = new StreamWriter(responseStream, resultsFormat.Encoding); streamWriter.Write(results); streamWriter.Flush(); } }
public SparqlResultModel(string storeName, IBrightstarService service, SparqlRequestObject sparqlRequest, SparqlResultsFormat resultsFormat, RdfFormat graphFormat) { _storeName = storeName; _sparqlRequest = sparqlRequest; _service = service; ResultsFormat = resultsFormat; GraphFormat = graphFormat; }
/// <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"> </param> /// <returns>A stream containing XML SPARQL results</returns> public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression, SparqlResultsFormat resultsFormat) { throw new NotImplementedException(); }
// operations public void Query(string storeName, string queryExpression, DateTime?ifNotModifiedSince, SparqlResultsFormat resultsFormat, Stream responseStream) { try { 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 cachedResult = GetCachedResult(storeName, commitPoint.CommitTime.Ticks, queryExpression, resultsFormat); if (cachedResult != null) { WriteResults(resultsFormat, responseStream, cachedResult); return; } var storeWorker = GetStoreWorker(storeName); var results = storeWorker.Query(queryExpression, resultsFormat); CacheResult(storeName, commitPoint.CommitTime.Ticks, queryExpression, resultsFormat, results); WriteResults(resultsFormat, responseStream, results); } catch (Exception) { responseStream.Close(); throw; } }
public void Query(string storeName, ulong commitPointId, string queryExpression, SparqlResultsFormat resultsFormat, Stream responseStream) { try { Logging.LogDebug("Query {0}@{1} {2}", storeName, commitPointId, queryExpression); 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 cachedResult = GetCachedResult(storeName, commitPoint.CommitTime.Ticks, queryExpression, resultsFormat); if (cachedResult != null) { Logging.LogDebug("Returning cached result for query. Store={0}@{1}, queryExpression={2}", storeName, commitPointId, queryExpression); WriteResults(resultsFormat, responseStream, cachedResult); return; } var storeWorker = GetStoreWorker(storeName); var results = storeWorker.Query(commitPointId, queryExpression, resultsFormat); CacheResult(storeName, commitPoint.CommitTime.Ticks, queryExpression, resultsFormat, results); WriteResults(resultsFormat, responseStream, results); } catch (Exception) { responseStream.Close(); throw; } }
private void CacheResult(string storeName, long commitTime, string queryString, SparqlResultsFormat format, string results) { var key = MakeQueryCacheKey(storeName, commitTime, queryString.GetHashCode(), format); _queryCache.Insert(key, results, CachePriority.Normal); }
private string GetCachedResult(string storeName, long commitTime, string queryString, SparqlResultsFormat format) { var key = MakeQueryCacheKey(storeName, commitTime, queryString.GetHashCode(), format); return(_queryCache.Lookup <String>(key)); }
// operations public void Query(string storeName, string queryExpression, IEnumerable<string> defaultGraphUris, DateTime? ifNotModifiedSince, SparqlResultsFormat resultsFormat, Stream responseStream) { try { 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 cacheKey = MakeQueryCacheKey(storeName, commitPoint.CommitTime.Ticks, queryExpression, g, resultsFormat); var cachedResult = GetCachedResult(cacheKey); if (cachedResult != null) { WriteResults(resultsFormat, responseStream, cachedResult); return; } var storeWorker = GetStoreWorker(storeName); var results = storeWorker.Query(queryExpression, resultsFormat, g); CacheResult(cacheKey, results); WriteResults(resultsFormat, responseStream, results); } catch (Exception) { #if PORTABLE System.CloseExtensions.Close(responseStream); #else responseStream.Close(); #endif throw; } }
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; } catch (NoSuchStoreException) { StatusCode = HttpStatusCode.NotFound; } }
public void Query(string storeName, ulong commitPointId, string queryExpression,IEnumerable<string> defaultGraphUris, SparqlResultsFormat resultsFormat, Stream responseStream) { try { 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 cacheKey = MakeQueryCacheKey(storeName, commitPoint.CommitTime.Ticks, queryExpression, g, resultsFormat); var cachedResult = GetCachedResult(cacheKey); if (cachedResult != null) { Logging.LogDebug("Returning cached result for query. Store={0}@{1}, queryExpression={2}", storeName, commitPointId, queryExpression); WriteResults(resultsFormat, responseStream, cachedResult); return; } var storeWorker = GetStoreWorker(storeName); var results = storeWorker.Query(commitPointId, queryExpression, resultsFormat, g); CacheResult(cacheKey, results); WriteResults(resultsFormat, responseStream, results); } catch(Exception) { #if PORTABLE System.CloseExtensions.Close(responseStream); #else responseStream.Close(); #endif throw; } }
public ActionResult Query(string storename, string query) { // Determine what results format to return to the client string bestMimeType = MimeParse.MimeParse.BestMatch(SparqlResultsFormat.AllMediaTypes.ToList(), Request.Headers["Accept"]); var callback = Request["callback"]; if (!String.IsNullOrEmpty(callback) && Request.Headers["Accept"].Contains("*/*")) { // Workaround for jQuery clients that will send Accept */* for a JSONP request bestMimeType = SparqlResultsFormat.Json.MediaTypes[0]; } SparqlResultsFormat resultsFormat = null; if (!String.IsNullOrEmpty(bestMimeType)) { resultsFormat = SparqlResultsFormat.GetResultsFormat(bestMimeType); } if (resultsFormat == null) { throw new HttpException(406, "Not Acceptable"); } if (query == null && Request.HttpMethod.ToLower().Equals("post") && Request.ContentType.Equals("application/sparql-query")) { try { string q; using (var streamReader = new StreamReader(Request.InputStream)) { q = streamReader.ReadToEnd(); if (string.IsNullOrEmpty(q)) { throw new HttpException(400, "No query in request body"); } } // Execute the query var client = BrightstarService.GetClient(); var results = client.ExecuteQuery(storename, q, DateTime.MinValue, resultsFormat); return(new FileStreamResult(results, resultsFormat.ToString())); } catch (Exception ex) { throw new HttpException(500, "Unable to execute query " + ex.Message, ex); } } if (query != null) { try { var client = BrightstarService.GetClient(); if (resultsFormat.DefaultExtension.Equals(SparqlResultsFormat.Json.DefaultExtension) && !String.IsNullOrEmpty(callback)) { // Handle JSONP request var results = client.ExecuteQuery(storename, query, DateTime.MinValue, resultsFormat); using (var stringReader = new StreamReader(results)) { var rawJson = stringReader.ReadToEnd(); return(new ContentResult { ContentType = bestMimeType, Content = callback + "(" + rawJson + ");", ContentEncoding = Encoding.UTF8 }); } } else { var results = client.ExecuteQuery(storename, query, DateTime.MinValue, resultsFormat); return(new FileStreamResult(results, resultsFormat.ToString())); } } catch (Exception ex) { throw new HttpException(500, "Unable to execute query " + ex.Message, ex); } } MimeParse.MimeParse.BestMatch(new [] { "application/xbel+xml", "text/xml" }, "text/*;q=0.5,*; q=0.1"); // 'text/xml' throw new HttpException(400, "Missing parameters or incorrect request format"); }
private static string MakeQueryCacheKey(string storeName, long commitTime, string query, IEnumerable<string> defaultGraphUris, SparqlResultsFormat format) { var graphHashCode = defaultGraphUris == null ? 0 : String.Join(",", defaultGraphUris).GetHashCode(); return storeName + "_" + commitTime + "_" + query.GetHashCode() + "_" + graphHashCode + "." + format.DefaultExtension; }
private static void TestSparqlPostSucceeds(string storeName, string query, IEnumerable <string> defaultGraphUris, IEnumerable <string> namedGraphUris, MediaRange accept, SparqlResultsFormat expectedQueryFormat, Action <Mock <IBrightstarService> > brightstarSetup) { // Setup var brightstar = new Mock <IBrightstarService>(); ISerializationFormat format = expectedQueryFormat; brightstar.Setup(s => s.ExecuteQuery(storeName, query, defaultGraphUris, null, expectedQueryFormat, It.IsAny <RdfFormat>(), out format)) .Returns(new MemoryStream(Encoding.UTF8.GetBytes("Mock Results"))) .Verifiable(); if (brightstarSetup != null) { brightstarSetup(brightstar); } var app = new Browser(new FakeNancyBootstrapper(brightstar.Object)); // Execute var response = app.Post("/" + storeName + "/sparql", with => { with.Body(query); with.Header("Content-Type", "application/sparql-query"); if (defaultGraphUris != null) { foreach (var defaultGraphUri in defaultGraphUris) { with.Query("default-graph-uri", defaultGraphUri); } } if (namedGraphUris != null) { foreach (var namedGraphUri in namedGraphUris) { with.Query("named-graph-uri", namedGraphUri); } } with.Accept(accept); }); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); Assert.That(accept.Matches(new MediaRange(response.ContentType))); Assert.That(response.Body.AsString(), Is.EqualTo("Mock Results")); brightstar.Verify(); }
/// <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> /// <returns>A stream containing XML SPARQL result XML</returns> public Stream ExecuteQuery(string storeName, string queryExpression, string defaultGraphUri, DateTime? ifNotModifiedSince = null, SparqlResultsFormat resultsFormat = null) { if (defaultGraphUri == null) { throw new ArgumentNullException("defaultGraphUri", Strings.BrightstarServiceClient_QueryDefaultGraphUriMustNotBeNull); } return ExecuteQuery(storeName, queryExpression, new string[] {defaultGraphUri}, ifNotModifiedSince, resultsFormat); }
/// <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 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> /// <returns>A stream containing XML SPARQL results</returns> public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression, SparqlResultsFormat resultsFormat = null) { return ExecuteQuery(commitPoint, queryExpression, (IEnumerable<string>) null, resultsFormat); }
/// <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); } }
public Stream ExecuteQuery(ICommitPointInfo commitPoint, string queryExpression, IEnumerable<string> defaultGraphUris, SparqlResultsFormat resultsFormat = null) { if (commitPoint == null) throw new ArgumentNullException("commitPoint"); if (queryExpression == null) throw new ArgumentNullException("queryExpression", Strings.BrightstarServiceClient_QueryMustNotBeNull); if (String.Empty.Equals(queryExpression)) throw new ArgumentException(Strings.BrightstarServiceClient_QueryMustNotBeEmptyString, "queryExpression"); try { return _service.ExecuteQueryOnCommitPoint( new CommitPointInfo { Id = commitPoint.Id, StoreName = commitPoint.StoreName, CommitTime = commitPoint.CommitTime, JobId = commitPoint.JobId }, queryExpression, defaultGraphUris == null ? null : defaultGraphUris.ToArray(), resultsFormat == null ? SparqlResultsFormat.Xml.MediaTypes.First() + "; charset=utf-8" : resultsFormat.ToString()); } catch (FaultException<ExceptionDetail> fault) { throw new BrightstarClientException(fault); } }
/// <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); }
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; }
/// <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); } }
private static void TestSparqlPostSucceeds(string storeName, string query, IEnumerable<string> defaultGraphUris, IEnumerable<string> namedGraphUris, MediaRange accept, SparqlResultsFormat expectedQueryFormat, Action<Mock<IBrightstarService>> brightstarSetup) { // Setup var brightstar = new Mock<IBrightstarService>(); brightstar.Setup(s => s.ExecuteQuery(storeName, query, defaultGraphUris, null, expectedQueryFormat)) .Returns(new MemoryStream(Encoding.UTF8.GetBytes("Mock Results"))) .Verifiable(); if (brightstarSetup != null) brightstarSetup(brightstar); var app = new Browser(new FakeNancyBootstrapper(brightstar.Object)); // Execute var response = app.Post("/" + storeName + "/sparql", with => { with.Body(query); with.Header("Content-Type", "application/sparql-query"); if (defaultGraphUris != null) { foreach (var defaultGraphUri in defaultGraphUris) { with.Query("default-graph-uri", defaultGraphUri); } } if (namedGraphUris != null) { foreach (var namedGraphUri in namedGraphUris) { with.Query("named-graph-uri", namedGraphUri); } } with.Accept(accept); }); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); Assert.That(response.ContentType, Is.EqualTo(accept.ToString())); Assert.That(response.Body.AsString(), Is.EqualTo("Mock Results")); brightstar.Verify(); }
private static string MakeQueryCacheKey(string storeName, long commitTime, int queryHashcode, SparqlResultsFormat format) { return(storeName + "_" + commitTime + "_" + queryHashcode + "." + format.DefaultExtension); }