Skip to content

Commit fa16c19

Browse files
committed
Return count metadata.
1 parent 6228a40 commit fa16c19

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

src/org/linkeddatafragments/datasource/BasicLinkedDataFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public interface BasicLinkedDataFragment {
1414
public Model getTriples();
1515

1616
/**
17-
* Gets the total number of triples in the fragment.
17+
* Gets the total number of triples in the fragment (can be an estimate).
1818
* @return the total number of triples
1919
*/
20-
public int getTotalSize();
20+
public long getTotalSize();
2121
}

src/org/linkeddatafragments/datasource/HdtDataSource.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,75 @@
22

33
import java.io.IOException;
44

5+
import org.rdfhdt.hdt.enums.TripleComponentRole;
56
import org.rdfhdt.hdt.hdt.HDT;
67
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;
811

12+
import com.hp.hpl.jena.graph.Triple;
913
import com.hp.hpl.jena.rdf.model.Model;
1014
import com.hp.hpl.jena.rdf.model.ModelFactory;
1115
import com.hp.hpl.jena.rdf.model.Property;
1216
import com.hp.hpl.jena.rdf.model.RDFNode;
1317
import com.hp.hpl.jena.rdf.model.Resource;
14-
import com.hp.hpl.jena.rdf.model.StmtIterator;
1518

1619
/**
1720
* An HDT data source of Basic Linked Data Fragments.
1821
* @author Ruben Verborgh
1922
*/
2023
public class HdtDataSource implements DataSource {
2124
private final static int TRIPLES_LIMIT = 100;
22-
private Model data;
25+
private final HDT datasource;
26+
private final NodeDictionary dictionary;
2327

2428
/**
2529
* Creates a new HdtDataSource.
2630
* @param hdtFile the HDT datafile
2731
* @throws IOException if the file cannot be loaded
2832
*/
2933
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());
3236
}
3337

3438
@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
3647
return new BasicLinkedDataFragment() {
3748
@Override
3849
public Model getTriples() {
3950
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())));
4354
return triples;
4455
}
4556

4657
@Override
47-
public int getTotalSize() {
48-
return 0;
58+
public long getTotalSize() {
59+
return result.estimatedNumResults();
4960
}
5061
};
5162
}
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+
}
5276
}

src/org/linkeddatafragments/servlet/BasicLdfServlet.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
import org.linkeddatafragments.datasource.DataSource;
1717
import org.linkeddatafragments.datasource.HdtDataSource;
1818

19+
import static org.linkeddatafragments.util.CommonResources.*;
20+
1921
import com.hp.hpl.jena.datatypes.TypeMapper;
22+
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
2023
import com.hp.hpl.jena.rdf.model.Model;
2124
import com.hp.hpl.jena.rdf.model.Property;
2225
import com.hp.hpl.jena.rdf.model.RDFNode;
@@ -52,6 +55,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
5255
try {
5356
// find the data source
5457
final String path = request.getRequestURI().substring(request.getContextPath().length());
58+
final String query = request.getQueryString();
5559
final String dataSourceName = path.substring(1);
5660
final DataSource dataSource = dataSources.get(dataSourceName);
5761
if (dataSource == null)
@@ -67,6 +71,13 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
6771
final Model output = fragment.getTriples();
6872
output.setNsPrefixes(config.getPrefixes());
6973

74+
// add metadata
75+
final String datasetUrl = request.getScheme() + "://" + request.getServerName() + path;
76+
final String fragmentUrl = query == null ? datasetUrl : (datasetUrl + "?" + query);
77+
final Resource fragmentId = output.createResource(fragmentUrl);
78+
output.add(fragmentId, VOID_TRIPLES,
79+
output.createTypedLiteral(fragment.getTotalSize(), XSDDatatype.XSDinteger));
80+
7081
// serialize the output as Turtle
7182
response.setHeader("Server", "Linked Data Fragments Server");
7283
response.setHeader("Content-Type", "text/turtle");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.linkeddatafragments.util;
2+
3+
import com.hp.hpl.jena.rdf.model.Property;
4+
import com.hp.hpl.jena.rdf.model.ResourceFactory;
5+
6+
@SuppressWarnings("javadoc")
7+
public class CommonResources {
8+
public final static String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
9+
public final static Property RDF_TYPE = createProperty(RDF + "type");
10+
11+
public final static String VOID = "http://rdfs.org/ns/void#";
12+
public final static Property VOID_TRIPLES = createProperty(VOID + "triples");
13+
14+
private final static Property createProperty(String uri) {
15+
return ResourceFactory.createProperty(uri);
16+
}
17+
}

0 commit comments

Comments
 (0)