Skip to content

Commit

Permalink
Merge pull request #213 from Swirrl/rdf4j-and-updates
Browse files Browse the repository at this point in the history
Update drafter to RDF4j and latest grafter
  • Loading branch information
RickMoynihan authored Oct 19, 2017
2 parents bd78be4 + 9224497 commit 352d5d3
Show file tree
Hide file tree
Showing 33 changed files with 320 additions and 259 deletions.
5 changes: 3 additions & 2 deletions env/dev/clj/drafter/routes/dump.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[drafter.rdf.drafter-ontology :refer :all]
[grafter.rdf :refer [add statements]]
[grafter.rdf.formats :refer [rdf-trig]]
[grafter.rdf.io :refer [rdf-serializer default-prefixes]]
[grafter.rdf.io :refer [default-prefixes]]
[grafter.rdf4j.io :refer [rdf-writer]]
[ring.util.io :as rio]
[ring.util.response :refer [not-found]]
[grafter.url :as url]))
Expand All @@ -20,7 +21,7 @@
the RAW database as a Trig String for debugging. Don't use on large
databases as it will be loaded into memory."
[db ostream]
(add (rdf-serializer ostream :format rdf-trig :prefixes drafter-prefixes)
(add (rdf-writer ostream :format rdf-trig :prefixes drafter-prefixes)
(statements db)))

(defn build-dump-route [backend]
Expand Down
12 changes: 3 additions & 9 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject drafter "2.1.6-SNAPSHOT"
(defproject drafter "2.1.7-SNAPSHOT"
:description "Backend PMD service"
:url "http://github.com/Swirrl/drafter"
:license {:name "Proprietary & Commercially Licensed Only"
Expand All @@ -23,10 +23,6 @@
[org.clojure/clojure "1.9.0-beta2"]
[org.clojure/math.combinatorics "0.1.4"]

[org.openrdf.sesame/sesame-queryrender "2.8.11"]
[org.openrdf.sesame/sesame-runtime "2.8.11"]
[org.openrdf.sesame/sesame-queryresultio-sparqlxml "2.8.11"]

[cognician/dogstatsd-clj "0.1.2"]

[clj-yaml "0.4.0"] ;; for loading our Swagger schemas
Expand All @@ -49,7 +45,7 @@

[com.sun.mail/javax.mail "1.6.0"]
;;[com.taoensso/tower "2.0.2"]
[grafter "0.9.0"]
[grafter "0.10.0"]
[grafter/url "0.2.5"]
;[grafter/vocabularies "0.1.3"]
[lib-noir "0.9.9" :exclusions [compojure org.clojure/java.classpath org.clojure/tools.reader org.clojure/java.classpath]]
Expand All @@ -66,9 +62,7 @@
[org.apache.jena/jena-iri "3.4.0" :exclusions [org.slf4j/slf4j-api]]

[org.mindrot/jbcrypt "0.4"]
[org.openrdf.sesame/sesame-queryrender "2.8.11"]
[org.openrdf.sesame/sesame-queryresultio-sparqlxml "2.8.11"]
[org.openrdf.sesame/sesame-runtime "2.8.11"]

[org.slf4j/slf4j-log4j12 "1.7.25" :exclusions [log4j org.slf4j/slf4j-api]]
[prismatic/schema "1.1.7"]

Expand Down
144 changes: 76 additions & 68 deletions src-java/drafter_sparql_repository/drafter-sparql-repository.iml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src-java/drafter_sparql_repository/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
NOTE: this version of sesame should be kept in sync with the version in the parent
leiningen project.clj.
-->
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-runtime</artifactId>
<version>2.8.11</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-runtime</artifactId>
<version>2.2.2</version>
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>org.openrdf.sesame</groupId>-->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package drafter.rdf;

import org.openrdf.query.*;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sparql.SPARQLConnection;
import org.openrdf.repository.sparql.SPARQLRepository;
import org.openrdf.http.client.SparqlSession;
import org.eclipse.rdf4j.http.client.SPARQLProtocolSession;
import org.eclipse.rdf4j.query.*;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sparql.SPARQLConnection;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;
import org.eclipse.rdf4j.http.client.SPARQLProtocolSession;


public class DrafterSPARQLConnection extends SPARQLConnection {

public DrafterSPARQLConnection(SPARQLRepository repository, SparqlSession sparqlSession) {
public DrafterSPARQLConnection(SPARQLRepository repository, SPARQLProtocolSession sparqlSession) {
super(repository, sparqlSession);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,57 @@

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openrdf.http.client.SesameClient;
import org.openrdf.model.vocabulary.SESAME;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sparql.SPARQLRepository;
import org.eclipse.rdf4j.http.client.HttpClientSessionManager;
import org.eclipse.rdf4j.http.client.SesameClient;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;

public class DrafterSPARQLRepository extends SPARQLRepository {

private SesameClient sesameClient;
private HttpClientSessionManager httpClientManager;
private Integer maxConcurrentHttpConnections;

public DrafterSPARQLRepository(String queryEndpoint) { super(queryEndpoint); }
public DrafterSPARQLRepository(String queryEndpoint, String updateEndpoint) {
super(queryEndpoint, updateEndpoint);
}

@Override public RepositoryConnection getConnection() throws RepositoryException {
if(!this.isInitialized()) {
throw new RepositoryException("SPARQLRepository not initialized.");
} else {
return new DrafterSPARQLConnection(this, createHTTPClient());
}
}
@Override public RepositoryConnection getConnection() throws RepositoryException {
if(!this.isInitialized()) {
throw new RepositoryException("SPARQLRepository not initialized.");
} else {
return new DrafterSPARQLConnection(this, createHTTPClient());
}
}

@Override public synchronized SesameClient getSesameClient() {
if (this.sesameClient == null) {
HttpClient httpClient = newHttpClient();
this.sesameClient = new DrafterSesameClientImpl(httpClient);
}
return this.sesameClient;
}
public synchronized HttpClientSessionManager getHttpClientSessionManager() {
if (this.httpClientManager == null) {
HttpClient httpClient = newHttpClient();
this.httpClientManager = new DrafterSesameClientImpl(httpClient);
}
return this.httpClientManager;
}

@Override public synchronized void setSesameClient(SesameClient client) {
this.sesameClient = client;
// @Override public synchronized SesameClient getSesameClient() {
// if (this.httpClientManager == null) {
// HttpClient httpClient = newHttpClient();
// this.httpClientManager = new DrafterSesameClientImpl(httpClient);
// }
// return this.httpClientManager;
// }


@Override public synchronized void setHttpClientSessionManager(HttpClientSessionManager httpMan) {
this.httpClientManager = httpMan;
}


//
// @Override public synchronized void setSesameClient(SesameClient client) {
// this.httpClientManager = client;
// }

/**
* Gets the maximum number of concurrent TCP connections created per route
* in the HTTP client used by this repository.
Expand All @@ -60,6 +75,7 @@ public synchronized void setMaxConcurrentHttpConnections(Integer maxConcurrentHt
this.setSesameClient(null);
}

// Fix for: https://github.com/eclipse/rdf4j/issues/367
private synchronized HttpClient newHttpClient() {
//the 'system' HTTP client uses the http.maxConnections
//system property to define the size of its connection pool (5 is the default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
package drafter.rdf;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openrdf.http.client.SesameClientImpl;
import org.openrdf.http.client.SparqlSession;
import org.eclipse.rdf4j.http.client.SPARQLProtocolSession;
import org.eclipse.rdf4j.http.client.SharedHttpClientSessionManager;
import org.eclipse.rdf4j.http.client.SparqlSession;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DrafterSesameClientImpl extends SesameClientImpl {
//public class DrafterSesameClientImpl extends SesameClientImpl {
public class DrafterSesameClientImpl extends SharedHttpClientSessionManager {

private static final ExecutorService QUERY_EXECUTOR = Executors.newCachedThreadPool();

public DrafterSesameClientImpl(HttpClient httpClient) {
this.setHttpClient(httpClient);
}

@Override public synchronized SparqlSession createSparqlSession(String queryEndpointUrl, String updateEndpointUrl) {
//NOTE: The two-argument constructor (HttpClient, ExecutorService) of SesameClientImpl does not seem to be
//used and the executor is created by the initialize() method called from the default constructor. initialize()
//sets the executor to Executors.newCachedThreadPool()
//ExecutorService executor = Executors.newCachedThreadPool();
@Override public SPARQLProtocolSession createSPARQLProtocolSession(String queryEndpointUrl, String updateEndpointUrl) {
DrafterSparqlSession session = new DrafterSparqlSession(queryEndpointUrl, updateEndpointUrl, this.getHttpClient(), QUERY_EXECUTOR);

return new DrafterSparqlSession(queryEndpointUrl, updateEndpointUrl, this.getHttpClient(), QUERY_EXECUTOR);
//session.setQueryURL(queryEndpointUrl);
//session.setUpdateURL(updateEndpointUrl);
return session;
}

// @Override public synchronized SparqlSession createSparqlSession(String queryEndpointUrl, String updateEndpointUrl) {
// //NOTE: The two-argument constructor (HttpClient, ExecutorService) of SesameClientImpl does not seem to be
// //used and the executor is created by the initialize() method called from the default constructor. initialize()
// //sets the executor to Executors.newCachedThreadPool()
// //ExecutorService executor = Executors.newCachedThreadPool();
//
// return new DrafterSparqlSession(queryEndpointUrl, updateEndpointUrl, this.getHttpClient(), QUERY_EXECUTOR);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.openrdf.OpenRDFException;
import org.openrdf.http.client.SparqlSession;
import org.openrdf.http.protocol.UnauthorizedException;
import org.openrdf.http.protocol.error.ErrorInfo;
import org.openrdf.http.protocol.error.ErrorType;
import org.openrdf.query.*;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.UnsupportedRDFormatException;
import org.eclipse.rdf4j.OpenRDFException;
import org.eclipse.rdf4j.http.client.SPARQLProtocolSession;
import org.eclipse.rdf4j.http.client.SparqlSession;
import org.eclipse.rdf4j.http.protocol.UnauthorizedException;
import org.eclipse.rdf4j.http.protocol.error.ErrorInfo;
import org.eclipse.rdf4j.http.protocol.error.ErrorType;
import org.eclipse.rdf4j.query.*;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.UnsupportedRDFormatException;

import java.io.IOException;
import java.lang.reflect.Field;
Expand All @@ -30,12 +31,12 @@
import java.util.List;
import java.util.concurrent.ExecutorService;

public class DrafterSparqlSession extends SparqlSession {
public class DrafterSparqlSession extends SPARQLProtocolSession /*SparqlSession*/ {
/**
* The longest URL length accepted by stardog. SPARQL queries which result in a URL longer than this length
* should be sent as POST requests instead.
*/
public static final int STARDOG_MAXIMUM_URL_LENGTH = 4083;
//public static final int STARDOG_MAXIMUM_URL_LENGTH = 4083;

public DrafterSparqlSession(String queryEndpointUrl, String updateEndpointUrl, HttpClient client, ExecutorService executor) {
super(client, executor);
Expand Down Expand Up @@ -79,7 +80,7 @@ private HttpParams getHttpParams() {
}

private HttpClientContext getHttpContext() {
return readField(SparqlSession.class, this, "httpContext");
return readField(SPARQLProtocolSession.class, this, "httpContext");
}

/**
Expand Down Expand Up @@ -144,9 +145,9 @@ private static void removeTimeoutQueryParams(List<NameValuePair> queryPairs) {
* @param query The SPARQL query
* @return Whether to use a POST request to submit the query
*/
@Override protected boolean shouldUsePost(String query) {
return query.length() > STARDOG_MAXIMUM_URL_LENGTH;
}
// @Override protected boolean shouldUsePost(String query) {
// return query.length() > STARDOG_MAXIMUM_URL_LENGTH;
// }

@SuppressWarnings("deprecation")
@Override protected HttpResponse execute(HttpUriRequest method) throws IOException, OpenRDFException {
Expand Down
2 changes: 1 addition & 1 deletion src/drafter/backend/endpoints.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[protocols :as proto]
[repository :as repo]]
[schema.core :as s])
(:import org.openrdf.model.URI))
(:import org.eclipse.rdf4j.model.URI))

(require 'drafter.backend.repository)

Expand Down
2 changes: 1 addition & 1 deletion src/drafter/backend/repository.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
type."
(:require [drafter.backend.protocols :refer :all]
[drafter.rdf.sesame :as ses])
(:import org.openrdf.repository.Repository))
(:import org.eclipse.rdf4j.repository.Repository))

(extend-type Repository
SparqlExecutor
Expand Down
10 changes: 5 additions & 5 deletions src/drafter/backend/sesame/remote.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
[clojure.tools.logging :as log]
[grafter.rdf.repository.registry :as reg])
(:import drafter.rdf.DrafterSPARQLRepository
[org.openrdf.query.resultio.sparqljson SPARQLBooleanJSONParserFactory SPARQLResultsJSONParserFactory]
[org.openrdf.query.resultio.sparqlxml SPARQLBooleanXMLParserFactory SPARQLResultsXMLParserFactory]
org.openrdf.query.resultio.text.BooleanTextParserFactory
org.openrdf.rio.nquads.NQuadsParserFactory
org.openrdf.rio.ntriples.NTriplesParserFactory))
[org.eclipse.rdf4j.query.resultio.sparqljson SPARQLBooleanJSONParserFactory SPARQLResultsJSONParserFactory]
[org.eclipse.rdf4j.query.resultio.sparqlxml SPARQLBooleanXMLParserFactory SPARQLResultsXMLParserFactory]
org.eclipse.rdf4j.query.resultio.text.BooleanTextParserFactory
org.eclipse.rdf4j.rio.nquads.NQuadsParserFactory
org.eclipse.rdf4j.rio.ntriples.NTriplesParserFactory))

(defn get-required-configuration-setting [var-key config]
(if-let [ev (var-key config)]
Expand Down
2 changes: 1 addition & 1 deletion src/drafter/errors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ overly-specific in your interpretation of the error."
[errors :refer [encode-error]]
[responses :as r]]))

(defmethod encode-error org.openrdf.query.QueryEvaluationException [ex]
(defmethod encode-error org.eclipse.rdf4j.query.QueryEvaluationException [ex]
;; This exception should also be caught in specific routes concerned with
;; SPARQL endpoints, here we use this to unpack our ConnectionPoolTimeoutException.

Expand Down
2 changes: 1 addition & 1 deletion src/drafter/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[drafter.user :as user]
[drafter.user.repository :as user-repo]
[drafter.rdf.sesame :refer [read-statements] :as ses]
[grafter.rdf.io :refer [mimetype->rdf-format]]
[grafter.rdf.formats :refer [mimetype->rdf-format]]
[buddy.auth :as auth]
[buddy.auth.protocols :as authproto]
[buddy.auth.backends.httpbasic :refer [http-basic-backend]]
Expand Down
4 changes: 2 additions & 2 deletions src/drafter/rdf/content_negotiation.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns drafter.rdf.content-negotiation
(:require [ring.middleware.accept :refer [wrap-accept]]
[drafter.rdf.formats :refer [csv-rdf-format tsv-rdf-format]])
(:import [org.openrdf.query.resultio BooleanQueryResultFormat TupleQueryResultFormat]
[org.openrdf.rio RDFFormat]))
(:import [org.eclipse.rdf4j.query.resultio BooleanQueryResultFormat TupleQueryResultFormat]
[org.eclipse.rdf4j.rio RDFFormat]))

(defn- format-preferences->mime-spec [format-prefs]
(into {} (mapcat (fn [[f q]] (map (fn [m] [m [f q]]) (.getMIMETypes f))) format-prefs)))
Expand Down
Loading

0 comments on commit 352d5d3

Please sign in to comment.