Skip to content

Commit 52480b3

Browse files
committed
Corrections for query and better estimate
1 parent 91754b7 commit 52480b3

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/org/linkeddatafragments/datasource/JenaTDBDataSource.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.linkeddatafragments.datasource;
22

3+
import com.hp.hpl.jena.graph.GraphStatisticsHandler;
4+
import com.hp.hpl.jena.graph.Node;
35
import com.hp.hpl.jena.query.Dataset;
46
import com.hp.hpl.jena.query.Query;
57
import com.hp.hpl.jena.query.QueryExecution;
@@ -23,8 +25,10 @@
2325
*/
2426
public class JenaTDBDataSource extends DataSource {
2527
private final Dataset tdb;
26-
private final String sparql = "CONSTRUCT ?s ?p ?o " +
27-
"ORDER BY ?s ?p ?o";
28+
private final String sparql = "CONSTRUCT { ?s ?p ?o } " +
29+
"WHERE { ?s ?p ?o } " +
30+
"ORDER BY ?s ?p ?o";
31+
2832
private final Query query = QueryFactory.create(sparql, Syntax.syntaxSPARQL_11);
2933

3034
@Override
@@ -41,20 +45,25 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
4145

4246
query.setOffset(offset);
4347
query.setLimit(limit);
44-
45-
QueryExecution qexec = QueryExecutionFactory.create(query, model);
46-
final Model triples = ModelFactory.createDefaultModel();
48+
49+
QueryExecution qexec = QueryExecutionFactory.create(query, model, map);
50+
Model triples = ModelFactory.createDefaultModel();
4751
qexec.execConstruct(triples);
52+
53+
// For now, fake the estimate
54+
long size = triples.size();
55+
long estimate = (size == limit) ? offset + limit + 1 : offset + size;
56+
// Try to get a better estimate
57+
GraphStatisticsHandler stats = model.getGraph().getStatisticsHandler();
58+
if (stats != null) {
59+
estimate = stats.getStatistic(subject.asNode(), predicate.asNode(), object.asNode());
60+
}
4861

4962
tdb.end();
5063

5164
if (triples.isEmpty()) {
5265
return new TriplePatternFragmentBase();
5366
} else {
54-
// For now, fake the estimate
55-
long size = triples.size();
56-
long estimate = (size == limit) ? offset + limit + 1 : offset + size;
57-
5867
return new TriplePatternFragmentBase(triples, estimate);
5968
}
6069
}

0 commit comments

Comments
 (0)