|
2 | 2 |
|
3 | 3 | import java.io.IOException;
|
4 | 4 |
|
| 5 | +import org.rdfhdt.hdt.enums.TripleComponentRole; |
5 | 6 | import org.rdfhdt.hdt.hdt.HDT;
|
6 | 7 | import org.rdfhdt.hdt.hdt.HDTManager;
|
7 |
| -import org.rdfhdt.hdtjena.HDTGraph; |
| 8 | +import org.rdfhdt.hdt.triples.IteratorTripleID; |
| 9 | +import org.rdfhdt.hdt.triples.TripleID; |
| 10 | +import org.rdfhdt.hdtjena.NodeDictionary; |
8 | 11 |
|
| 12 | +import com.hp.hpl.jena.graph.Triple; |
9 | 13 | import com.hp.hpl.jena.rdf.model.Model;
|
10 | 14 | import com.hp.hpl.jena.rdf.model.ModelFactory;
|
11 | 15 | import com.hp.hpl.jena.rdf.model.Property;
|
12 | 16 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
13 | 17 | import com.hp.hpl.jena.rdf.model.Resource;
|
14 |
| -import com.hp.hpl.jena.rdf.model.StmtIterator; |
15 | 18 |
|
16 | 19 | /**
|
17 | 20 | * An HDT data source of Basic Linked Data Fragments.
|
18 | 21 | * @author Ruben Verborgh
|
19 | 22 | */
|
20 | 23 | public class HdtDataSource implements DataSource {
|
21 | 24 | private final static int TRIPLES_LIMIT = 100;
|
22 |
| - private Model data; |
| 25 | + private final HDT datasource; |
| 26 | + private final NodeDictionary dictionary; |
23 | 27 |
|
24 | 28 | /**
|
25 | 29 | * Creates a new HdtDataSource.
|
26 | 30 | * @param hdtFile the HDT datafile
|
27 | 31 | * @throws IOException if the file cannot be loaded
|
28 | 32 | */
|
29 | 33 | public HdtDataSource(String hdtFile) throws IOException {
|
30 |
| - final HDT hdt = HDTManager.mapIndexedHDT(hdtFile, null); |
31 |
| - data = ModelFactory.createModelForGraph(new HDTGraph(hdt)); |
| 34 | + datasource = HDTManager.mapIndexedHDT(hdtFile, null); |
| 35 | + dictionary = new NodeDictionary(datasource.getDictionary()); |
32 | 36 | }
|
33 | 37 |
|
34 | 38 | @Override
|
35 |
| - public BasicLinkedDataFragment getFragment(final Resource subject, final Property predicate, final RDFNode object) { |
| 39 | + public BasicLinkedDataFragment getFragment(Resource subject, Property predicate, RDFNode object) { |
| 40 | + // look up the result from the HDT datasource |
| 41 | + final int subjectId = subject == null ? 0 : dictionary.getIntID(subject.asNode(), TripleComponentRole.SUBJECT); |
| 42 | + final int predicateId = predicate == null ? 0 : dictionary.getIntID(predicate.asNode(), TripleComponentRole.PREDICATE); |
| 43 | + final int objectId = object == null ? 0 : dictionary.getIntID(object.asNode(), TripleComponentRole.OBJECT); |
| 44 | + final IteratorTripleID result = datasource.getTriples().search(new TripleID(subjectId, predicateId, objectId)); |
| 45 | + |
| 46 | + // create the fragment |
36 | 47 | return new BasicLinkedDataFragment() {
|
37 | 48 | @Override
|
38 | 49 | public Model getTriples() {
|
39 | 50 | final Model triples = ModelFactory.createDefaultModel();
|
40 |
| - final StmtIterator statements = data.listStatements(subject, predicate, object); |
41 |
| - for (int i = 0; i < TRIPLES_LIMIT && statements.hasNext(); i++) |
42 |
| - triples.add(statements.next()); |
| 51 | + result.goToStart(); |
| 52 | + for (int i = 0; i < TRIPLES_LIMIT && result.hasNext(); i++) |
| 53 | + triples.add(triples.asStatement(toTriple(result.next()))); |
43 | 54 | return triples;
|
44 | 55 | }
|
45 | 56 |
|
46 | 57 | @Override
|
47 |
| - public int getTotalSize() { |
48 |
| - return 0; |
| 58 | + public long getTotalSize() { |
| 59 | + return result.estimatedNumResults(); |
49 | 60 | }
|
50 | 61 | };
|
51 | 62 | }
|
| 63 | + |
| 64 | + /** |
| 65 | + * Converts the HDT triple to a Jena Triple. |
| 66 | + * @param tripleId the HDT triple |
| 67 | + * @return the Jena triple |
| 68 | + */ |
| 69 | + private Triple toTriple(TripleID tripleId) { |
| 70 | + return new Triple( |
| 71 | + dictionary.getNode(tripleId.getSubject(), TripleComponentRole.SUBJECT), |
| 72 | + dictionary.getNode(tripleId.getPredicate(), TripleComponentRole.PREDICATE), |
| 73 | + dictionary.getNode(tripleId.getObject(), TripleComponentRole.OBJECT) |
| 74 | + ); |
| 75 | + } |
52 | 76 | }
|
0 commit comments