Faster Graph query for sprawl interface; add caching as it is still quite slow
This commit is contained in:
parent
cc300bf419
commit
dd76bb73a4
1 changed files with 55 additions and 30 deletions
|
@ -11,6 +11,7 @@ import java.io.StringWriter;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
@ -87,6 +88,9 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
|
|
||||||
protected HttpClient httpClient;
|
protected HttpClient httpClient;
|
||||||
|
|
||||||
|
protected boolean rebuildGraphURICache = true;
|
||||||
|
private List<String> graphURIs = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an RDFService for a remote repository
|
* Returns an RDFService for a remote repository
|
||||||
* @param String - URI of the read SPARQL endpoint for the knowledge base
|
* @param String - URI of the read SPARQL endpoint for the knowledge base
|
||||||
|
@ -215,6 +219,8 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e, e);
|
log.error(e, e);
|
||||||
throw new RDFServiceException(e);
|
throw new RDFServiceException(e);
|
||||||
|
} finally {
|
||||||
|
rebuildGraphURICache = true;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -413,12 +419,16 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<String> getGraphURIs() throws RDFServiceException {
|
public List<String> getGraphURIs() throws RDFServiceException {
|
||||||
return getGraphURIsFromSparqlQuery();
|
if (graphURIs == null || rebuildGraphURICache) {
|
||||||
|
graphURIs = getGraphURIsFromSparqlQuery();
|
||||||
|
rebuildGraphURICache = false;
|
||||||
|
}
|
||||||
|
return graphURIs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getGraphURIsFromSparqlQuery() throws RDFServiceException {
|
private List<String> getGraphURIsFromSparqlQuery() throws RDFServiceException {
|
||||||
String fastJenaQuery = "SELECT DISTINCT ?g WHERE { GRAPH ?g {} } ORDER BY ?g";
|
String fastJenaQuery = "SELECT DISTINCT ?g WHERE { GRAPH ?g {} } ORDER BY ?g";
|
||||||
String standardQuery = "SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o } } ORDER BY ?g";
|
String standardQuery = "SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o } }";
|
||||||
List<String> graphURIs = new ArrayList<String>();
|
List<String> graphURIs = new ArrayList<String>();
|
||||||
try {
|
try {
|
||||||
graphURIs = getGraphURIsFromSparqlQuery(fastJenaQuery);
|
graphURIs = getGraphURIsFromSparqlQuery(fastJenaQuery);
|
||||||
|
@ -427,6 +437,7 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
}
|
}
|
||||||
if (graphURIs.isEmpty()) {
|
if (graphURIs.isEmpty()) {
|
||||||
graphURIs = getGraphURIsFromSparqlQuery(standardQuery);
|
graphURIs = getGraphURIsFromSparqlQuery(standardQuery);
|
||||||
|
Collections.sort(graphURIs);
|
||||||
}
|
}
|
||||||
return graphURIs;
|
return graphURIs;
|
||||||
}
|
}
|
||||||
|
@ -537,11 +548,19 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addModel(Model model, String graphURI) throws RDFServiceException {
|
public void addModel(Model model, String graphURI) throws RDFServiceException {
|
||||||
verbModel(model, graphURI, "INSERT");
|
try {
|
||||||
|
verbModel(model, graphURI, "INSERT");
|
||||||
|
} finally {
|
||||||
|
rebuildGraphURICache = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteModel(Model model, String graphURI) throws RDFServiceException {
|
public void deleteModel(Model model, String graphURI) throws RDFServiceException {
|
||||||
verbModel(model, graphURI, "DELETE");
|
try {
|
||||||
|
verbModel(model, graphURI, "DELETE");
|
||||||
|
} finally {
|
||||||
|
rebuildGraphURICache = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verbModel(Model model, String graphURI, String verb) throws RDFServiceException {
|
private void verbModel(Model model, String graphURI, String verb) throws RDFServiceException {
|
||||||
|
@ -573,37 +592,43 @@ public class RDFServiceSparql extends RDFServiceImpl implements RDFService {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addTriple(Triple t, String graphURI) throws RDFServiceException {
|
protected void addTriple(Triple t, String graphURI) throws RDFServiceException {
|
||||||
|
try {
|
||||||
|
StringBuffer updateString = new StringBuffer();
|
||||||
|
updateString.append("INSERT DATA { ");
|
||||||
|
updateString.append((graphURI != null) ? "GRAPH <" + graphURI + "> { " : "");
|
||||||
|
updateString.append(sparqlNodeUpdate(t.getSubject(), ""));
|
||||||
|
updateString.append(" ");
|
||||||
|
updateString.append(sparqlNodeUpdate(t.getPredicate(), ""));
|
||||||
|
updateString.append(" ");
|
||||||
|
updateString.append(sparqlNodeUpdate(t.getObject(), ""));
|
||||||
|
updateString.append(" }");
|
||||||
|
updateString.append((graphURI != null) ? " } " : "");
|
||||||
|
|
||||||
StringBuffer updateString = new StringBuffer();
|
executeUpdate(updateString.toString());
|
||||||
updateString.append("INSERT DATA { ");
|
notifyListeners(t, ModelChange.Operation.ADD, graphURI);
|
||||||
updateString.append((graphURI != null) ? "GRAPH <" + graphURI + "> { " : "" );
|
} finally {
|
||||||
updateString.append(sparqlNodeUpdate(t.getSubject(), ""));
|
rebuildGraphURICache = true;
|
||||||
updateString.append(" ");
|
}
|
||||||
updateString.append(sparqlNodeUpdate(t.getPredicate(), ""));
|
|
||||||
updateString.append(" ");
|
|
||||||
updateString.append(sparqlNodeUpdate(t.getObject(), ""));
|
|
||||||
updateString.append(" }");
|
|
||||||
updateString.append((graphURI != null) ? " } " : "");
|
|
||||||
|
|
||||||
executeUpdate(updateString.toString());
|
|
||||||
notifyListeners(t, ModelChange.Operation.ADD, graphURI);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void removeTriple(Triple t, String graphURI) throws RDFServiceException {
|
protected void removeTriple(Triple t, String graphURI) throws RDFServiceException {
|
||||||
|
try {
|
||||||
|
StringBuffer updateString = new StringBuffer();
|
||||||
|
updateString.append("DELETE DATA { ");
|
||||||
|
updateString.append((graphURI != null) ? "GRAPH <" + graphURI + "> { " : "");
|
||||||
|
updateString.append(sparqlNodeUpdate(t.getSubject(), ""));
|
||||||
|
updateString.append(" ");
|
||||||
|
updateString.append(sparqlNodeUpdate(t.getPredicate(), ""));
|
||||||
|
updateString.append(" ");
|
||||||
|
updateString.append(sparqlNodeUpdate(t.getObject(), ""));
|
||||||
|
updateString.append(" }");
|
||||||
|
updateString.append((graphURI != null) ? " } " : "");
|
||||||
|
|
||||||
StringBuffer updateString = new StringBuffer();
|
executeUpdate(updateString.toString());
|
||||||
updateString.append("DELETE DATA { ");
|
notifyListeners(t, ModelChange.Operation.REMOVE, graphURI);
|
||||||
updateString.append((graphURI != null) ? "GRAPH <" + graphURI + "> { " : "" );
|
} finally {
|
||||||
updateString.append(sparqlNodeUpdate(t.getSubject(), ""));
|
rebuildGraphURICache = true;
|
||||||
updateString.append(" ");
|
}
|
||||||
updateString.append(sparqlNodeUpdate(t.getPredicate(), ""));
|
|
||||||
updateString.append(" ");
|
|
||||||
updateString.append(sparqlNodeUpdate(t.getObject(), ""));
|
|
||||||
updateString.append(" }");
|
|
||||||
updateString.append((graphURI != null) ? " } " : "");
|
|
||||||
|
|
||||||
executeUpdate(updateString.toString());
|
|
||||||
notifyListeners(t, ModelChange.Operation.REMOVE, graphURI);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Add table
Reference in a new issue