88import com .hp .hpl .jena .query .QueryExecution ;
99import com .hp .hpl .jena .query .QueryExecutionFactory ;
1010import com .hp .hpl .jena .query .QueryFactory ;
11+ import com .hp .hpl .jena .query .QuerySolution ;
1112import com .hp .hpl .jena .query .QuerySolutionMap ;
13+ import com .hp .hpl .jena .query .ResultSet ;
1214import com .hp .hpl .jena .query .Syntax ;
15+ import com .hp .hpl .jena .rdf .model .Literal ;
1316import com .hp .hpl .jena .rdf .model .Model ;
1417import com .hp .hpl .jena .rdf .model .ModelFactory ;
1518import com .hp .hpl .jena .rdf .model .Property ;
2023
2124/**
2225 * Experimental Jena TDB-backed data source of Basic Linked Data Fragments.
23- *
26+ *
2427 * @author Bart Hanssens <[email protected] > 2528 */
2629public class JenaTDBDataSource extends DataSource {
2730 private final Dataset tdb ;
28- private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " +
31+ private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " +
2932 "ORDER BY ?s ?p ?o" ;
30-
33+
34+ private final String count = "SELECT (COUNT(?s) AS ?count) WHERE { ?s ?p ?o }" ;
35+
3136 private final Query query = QueryFactory .create (sparql , Syntax .syntaxSPARQL_11 );
32-
37+ private final Query countQuery = QueryFactory . create ( count , Syntax . syntaxSPARQL_11 );
3338
3439 @ Override
3540 public TriplePatternFragment getFragment (Resource subject , Property predicate , RDFNode object , long offset , long limit ) {
3641 checkBoundaries (offset , limit );
37-
42+
3843 Model model = tdb .getDefaultModel ();
3944 QuerySolutionMap map = new QuerySolutionMap ();
4045 if (subject != null ) {
@@ -46,44 +51,52 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
4651 if (object != null ) {
4752 map .add ("o" , object );
4853 }
49-
54+
5055 query .setOffset (offset );
5156 query .setLimit (limit );
52-
57+
5358 Model triples = ModelFactory .createDefaultModel ();
54-
59+
5560 try (QueryExecution qexec = QueryExecutionFactory .create (query , model , map )) {
5661 qexec .execConstruct (triples );
5762 }
5863
5964 if (triples .isEmpty ()) {
6065 return new TriplePatternFragmentBase ();
6166 }
62-
67+
6368 // Try to get an estimate
6469 long size = triples .size ();
6570 long estimate = -1 ;
6671
67-
68- GraphStatisticsHandler stats = model .getGraph ().getStatisticsHandler ();
72+ try (QueryExecution qexec = QueryExecutionFactory .create (countQuery , model , map )) {
73+ ResultSet results = qexec .execSelect ();
74+ if (results .hasNext ()) {
75+ QuerySolution soln = results .nextSolution () ;
76+ Literal literal = soln .getLiteral ("count" );
77+ estimate = literal .getLong ();
78+ }
79+ }
80+
81+ /*GraphStatisticsHandler stats = model.getGraph().getStatisticsHandler();
6982 if (stats != null) {
7083 Node s = (subject != null) ? subject.asNode() : null;
7184 Node p = (predicate != null) ? predicate.asNode() : null;
7285 Node o = (object != null) ? object.asNode() : null;
7386 estimate = stats.getStatistic(s, p, o);
74- }
75-
87+ }*/
88+
7689 // No estimate or incorrect
7790 if (estimate < offset + size ) {
7891 estimate = (size == limit ) ? offset + size + 1 : offset + size ;
7992 }
8093 return new TriplePatternFragmentBase (triples , estimate );
8194 }
82-
83-
95+
96+
8497 /**
8598 * Constructor
86- *
99+ *
87100 * @param title
88101 * @param description
89102 * @param tdbdir directory used for TDB backing
0 commit comments