8
8
import com .hp .hpl .jena .query .QueryExecution ;
9
9
import com .hp .hpl .jena .query .QueryExecutionFactory ;
10
10
import com .hp .hpl .jena .query .QueryFactory ;
11
+ import com .hp .hpl .jena .query .QuerySolution ;
11
12
import com .hp .hpl .jena .query .QuerySolutionMap ;
13
+ import com .hp .hpl .jena .query .ResultSet ;
12
14
import com .hp .hpl .jena .query .Syntax ;
15
+ import com .hp .hpl .jena .rdf .model .Literal ;
13
16
import com .hp .hpl .jena .rdf .model .Model ;
14
17
import com .hp .hpl .jena .rdf .model .ModelFactory ;
15
18
import com .hp .hpl .jena .rdf .model .Property ;
20
23
21
24
/**
22
25
* Experimental Jena TDB-backed data source of Basic Linked Data Fragments.
23
- *
26
+ *
24
27
* @author Bart Hanssens <[email protected] >
25
28
*/
26
29
public class JenaTDBDataSource extends DataSource {
27
30
private final Dataset tdb ;
28
- private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " +
31
+ private final String sparql = "CONSTRUCT WHERE { ?s ?p ?o } " +
29
32
"ORDER BY ?s ?p ?o" ;
30
-
33
+
34
+ private final String count = "SELECT (COUNT(?s) AS ?count) WHERE { ?s ?p ?o }" ;
35
+
31
36
private final Query query = QueryFactory .create (sparql , Syntax .syntaxSPARQL_11 );
32
-
37
+ private final Query countQuery = QueryFactory . create ( count , Syntax . syntaxSPARQL_11 );
33
38
34
39
@ Override
35
40
public TriplePatternFragment getFragment (Resource subject , Property predicate , RDFNode object , long offset , long limit ) {
36
41
checkBoundaries (offset , limit );
37
-
42
+
38
43
Model model = tdb .getDefaultModel ();
39
44
QuerySolutionMap map = new QuerySolutionMap ();
40
45
if (subject != null ) {
@@ -46,44 +51,52 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
46
51
if (object != null ) {
47
52
map .add ("o" , object );
48
53
}
49
-
54
+
50
55
query .setOffset (offset );
51
56
query .setLimit (limit );
52
-
57
+
53
58
Model triples = ModelFactory .createDefaultModel ();
54
-
59
+
55
60
try (QueryExecution qexec = QueryExecutionFactory .create (query , model , map )) {
56
61
qexec .execConstruct (triples );
57
62
}
58
63
59
64
if (triples .isEmpty ()) {
60
65
return new TriplePatternFragmentBase ();
61
66
}
62
-
67
+
63
68
// Try to get an estimate
64
69
long size = triples .size ();
65
70
long estimate = -1 ;
66
71
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();
69
82
if (stats != null) {
70
83
Node s = (subject != null) ? subject.asNode() : null;
71
84
Node p = (predicate != null) ? predicate.asNode() : null;
72
85
Node o = (object != null) ? object.asNode() : null;
73
86
estimate = stats.getStatistic(s, p, o);
74
- }
75
-
87
+ }*/
88
+
76
89
// No estimate or incorrect
77
90
if (estimate < offset + size ) {
78
91
estimate = (size == limit ) ? offset + size + 1 : offset + size ;
79
92
}
80
93
return new TriplePatternFragmentBase (triples , estimate );
81
94
}
82
-
83
-
95
+
96
+
84
97
/**
85
98
* Constructor
86
- *
99
+ *
87
100
* @param title
88
101
* @param description
89
102
* @param tdbdir directory used for TDB backing
0 commit comments