2
2
3
3
import java .io .IOException ;
4
4
5
- import org .linkeddatafragments .datasource .AbstractRequestProcessorForTriplePatterns ;
6
5
import org .linkeddatafragments .datasource .DataSource ;
7
6
import org .linkeddatafragments .datasource .IFragmentRequestProcessor ;
8
- import org .linkeddatafragments .fragments .LinkedDataFragment ;
9
- import org .linkeddatafragments .fragments .LinkedDataFragmentRequest ;
10
- import org .linkeddatafragments .fragments .tpf .TriplePatternFragmentRequest ;
11
- import org .rdfhdt .hdt .enums .TripleComponentRole ;
12
- import org .rdfhdt .hdt .hdt .HDT ;
13
- import org .rdfhdt .hdt .hdt .HDTManager ;
14
- import org .rdfhdt .hdt .triples .IteratorTripleID ;
15
- import org .rdfhdt .hdt .triples .TripleID ;
16
- import org .rdfhdt .hdtjena .NodeDictionary ;
17
-
18
- import com .hp .hpl .jena .graph .Triple ;
19
- import com .hp .hpl .jena .rdf .model .Model ;
20
- import com .hp .hpl .jena .rdf .model .ModelFactory ;
21
- import com .hp .hpl .jena .rdf .model .Property ;
22
- import com .hp .hpl .jena .rdf .model .RDFNode ;
23
- import com .hp .hpl .jena .rdf .model .Resource ;
24
7
25
8
/**
26
9
* An HDT data source of Basic Linked Data Fragments.
30
13
*/
31
14
public class HdtDataSource extends DataSource {
32
15
33
- private final HDT datasource ;
34
- private final NodeDictionary dictionary ;
16
+ protected final HdtBasedRequestProcessorForTPFs requestProcessor ;
35
17
36
18
/**
37
19
* Creates a new HdtDataSource.
@@ -43,98 +25,13 @@ public class HdtDataSource extends DataSource {
43
25
*/
44
26
public HdtDataSource (String title , String description , String hdtFile ) throws IOException {
45
27
super (title , description );
46
- datasource = HDTManager .mapIndexedHDT (hdtFile , null );
47
- dictionary = new NodeDictionary (datasource .getDictionary ());
28
+ requestProcessor = new HdtBasedRequestProcessorForTPFs ( hdtFile );
48
29
}
49
30
50
31
@ Override
51
- public IFragmentRequestProcessor getRequestProcessor (
52
- final LinkedDataFragmentRequest request )
32
+ public IFragmentRequestProcessor getRequestProcessor ()
53
33
{
54
- if ( ! (request instanceof TriplePatternFragmentRequest ) )
55
- throw new IllegalArgumentException ();
56
-
57
- return new MyProcessor ( (TriplePatternFragmentRequest ) request );
58
- }
59
-
60
- protected class MyProcessor extends AbstractRequestProcessorForTriplePatterns
61
- {
62
- public MyProcessor ( final TriplePatternFragmentRequest request ) {
63
- super ( request );
34
+ return requestProcessor ;
64
35
}
65
36
66
- @ Override
67
- protected LinkedDataFragment createFragment ( final Resource subject ,
68
- final Property predicate ,
69
- final RDFNode object ,
70
- final long offset ,
71
- final long limit )
72
- {
73
- // look up the result from the HDT datasource)
74
- int subjectId = subject == null ? 0 : dictionary .getIntID (subject .asNode (), TripleComponentRole .SUBJECT );
75
- int predicateId = predicate == null ? 0 : dictionary .getIntID (predicate .asNode (), TripleComponentRole .PREDICATE );
76
- int objectId = object == null ? 0 : dictionary .getIntID (object .asNode (), TripleComponentRole .OBJECT );
77
-
78
- if (subjectId < 0 || predicateId < 0 || objectId < 0 ) {
79
- return createEmptyTriplePatternFragment ();
80
- }
81
-
82
- final Model triples = ModelFactory .createDefaultModel ();
83
- IteratorTripleID matches = datasource .getTriples ().search (new TripleID (subjectId , predicateId , objectId ));
84
- boolean hasMatches = matches .hasNext ();
85
-
86
- if (hasMatches ) {
87
- // try to jump directly to the offset
88
- boolean atOffset ;
89
- if (matches .canGoTo ()) {
90
- try {
91
- matches .goTo (offset );
92
- atOffset = true ;
93
- } // if the offset is outside the bounds, this page has no matches
94
- catch (IndexOutOfBoundsException exception ) {
95
- atOffset = false ;
96
- }
97
- } // if not possible, advance to the offset iteratively
98
- else {
99
- matches .goToStart ();
100
- for (int i = 0 ; !(atOffset = i == offset ) && matches .hasNext (); i ++) {
101
- matches .next ();
102
- }
103
- }
104
- // try to add `limit` triples to the result model
105
- if (atOffset ) {
106
- for (int i = 0 ; i < limit && matches .hasNext (); i ++) {
107
- triples .add (triples .asStatement (toTriple (matches .next ())));
108
- }
109
- }
110
- }
111
-
112
- // estimates can be wrong; ensure 0 is returned if there are no results,
113
- // and always more than actual results
114
- final long estimatedTotal = triples .size () > 0
115
- ? Math .max (offset + triples .size () + 1 , matches .estimatedNumResults ())
116
- : hasMatches
117
- ? Math .max (matches .estimatedNumResults (), 1 )
118
- : 0 ;
119
-
120
- // create the fragment
121
- final boolean isLastPage = ( estimatedTotal < offset + limit );
122
- return createTriplePatternFragment ( triples , estimatedTotal , isLastPage );
123
- }
124
-
125
- } // end of MyProcessor
126
-
127
- /**
128
- * Converts the HDT triple to a Jena Triple.
129
- *
130
- * @param tripleId the HDT triple
131
- * @return the Jena triple
132
- */
133
- private Triple toTriple (TripleID tripleId ) {
134
- return new Triple (
135
- dictionary .getNode (tripleId .getSubject (), TripleComponentRole .SUBJECT ),
136
- dictionary .getNode (tripleId .getPredicate (), TripleComponentRole .PREDICATE ),
137
- dictionary .getNode (tripleId .getObject (), TripleComponentRole .OBJECT )
138
- );
139
- }
140
37
}
0 commit comments