Skip to content

Commit 9cfd6cb

Browse files
author
mielvds
committed
Merge pull request #5 from barthanssens/master
Upgrades and splitting up some methods
2 parents 0fa4d74 + efb5da2 commit 9cfd6cb

File tree

11 files changed

+426
-304
lines changed

11 files changed

+426
-304
lines changed

pom.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>LDF-Server</groupId>
55
<artifactId>LDF-Server</artifactId>
6-
<version>0.0.1</version>
6+
<version>0.0.2</version>
77
<packaging>war</packaging>
88

99
<properties>
10-
<jettyVersion>9.2.5.v20141112</jettyVersion>
10+
<jettyVersion>9.3.6.v20151106</jettyVersion>
1111
</properties>
12-
1312
<dependencies>
1413
<dependency>
1514
<groupId>org.rdfhdt</groupId>
@@ -31,10 +30,10 @@
3130
<artifactId>httpclient</artifactId>
3231
<version>4.3.5</version>
3332
</dependency>
34-
<dependency>
33+
<dependency>
3534
<groupId>com.google.code.gson</groupId>
3635
<artifactId>gson</artifactId>
37-
<version>2.3</version>
36+
<version>2.5</version>
3837
</dependency>
3938
<dependency>
4039
<groupId>javax.servlet</groupId>
@@ -55,7 +54,7 @@
5554
<dependency>
5655
<groupId>commons-cli</groupId>
5756
<artifactId>commons-cli</artifactId>
58-
<version>1.2</version>
57+
<version>1.3.1</version>
5958
</dependency>
6059
<dependency>
6160
<groupId>org.apache.commons</groupId>
@@ -116,4 +115,4 @@
116115
</plugin>
117116
</plugins>
118117
</build>
119-
</project>
118+
</project>

src/org/linkeddatafragments/config/ConfigReader.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
* @author Ruben Verborgh
1616
*/
1717
public class ConfigReader {
18-
19-
private final Map<String, JsonObject> dataSources = new HashMap<String, JsonObject>();
20-
private final Map<String, String> prefixes = new HashMap<String, String>();
18+
private final Map<String, JsonObject> dataSources = new HashMap<>();
19+
private final Map<String, String> prefixes = new HashMap<>();
2120

2221
/**
2322
* Creates a new configuration reader.
2423
*
2524
* @param configReader the configuration
2625
*/
2726
public ConfigReader(Reader configReader) {
28-
final JsonObject root = new JsonParser().parse(configReader).getAsJsonObject();
29-
for (final Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
30-
final JsonObject dataSource = entry.getValue().getAsJsonObject();
27+
JsonObject root = new JsonParser().parse(configReader).getAsJsonObject();
28+
for (Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
29+
JsonObject dataSource = entry.getValue().getAsJsonObject();
3130
this.dataSources.put(entry.getKey(), dataSource);
3231
}
33-
for (final Entry<String, JsonElement> entry : root.getAsJsonObject("prefixes").entrySet()) {
32+
for (Entry<String, JsonElement> entry : root.getAsJsonObject("prefixes").entrySet()) {
3433
this.prefixes.put(entry.getKey(), entry.getValue().getAsString());
3534
}
3635
}

src/org/linkeddatafragments/datasource/HdtDataSource.java

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,53 +49,61 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
4949
throw new IllegalArgumentException("limit");
5050
}
5151

52-
// look up the result from the HDT datasource
53-
final int subjectId = subject == null ? 0 : dictionary.getIntID(subject.asNode(), TripleComponentRole.SUBJECT);
54-
final int predicateId = predicate == null ? 0 : dictionary.getIntID(predicate.asNode(), TripleComponentRole.PREDICATE);
55-
final int objectId = object == null ? 0 : dictionary.getIntID(object.asNode(), TripleComponentRole.OBJECT);
52+
// look up the result from the HDT datasource)
53+
int subjectId = subject == null ? 0 : dictionary.getIntID(subject.asNode(), TripleComponentRole.SUBJECT);
54+
int predicateId = predicate == null ? 0 : dictionary.getIntID(predicate.asNode(), TripleComponentRole.PREDICATE);
55+
int objectId = object == null ? 0 : dictionary.getIntID(object.asNode(), TripleComponentRole.OBJECT);
56+
5657
if (subjectId < 0 || predicateId < 0 || objectId < 0) {
5758
return new TriplePatternFragmentBase();
5859
}
60+
5961
final Model triples = ModelFactory.createDefaultModel();
60-
final IteratorTripleID matches = datasource.getTriples().search(new TripleID(subjectId, predicateId, objectId));
61-
final boolean hasMatches = matches.hasNext();
62+
IteratorTripleID matches = datasource.getTriples().search(new TripleID(subjectId, predicateId, objectId));
63+
boolean hasMatches = matches.hasNext();
6264

63-
if (hasMatches) {
64-
// try to jump directly to the offset
65-
boolean atOffset;
66-
if (matches.canGoTo()) {
67-
try {
68-
matches.goTo(offset);
69-
atOffset = true;
70-
}
71-
// if the offset is outside the bounds, this page has no matches
72-
catch (IndexOutOfBoundsException exception) { atOffset = false; }
73-
}
74-
// if not possible, advance to the offset iteratively
75-
else {
76-
matches.goToStart();
77-
for (int i = 0; !(atOffset = i == offset) && matches.hasNext(); i++)
78-
matches.next();
79-
}
80-
// try to add `limit` triples to the result model
81-
if (atOffset) {
82-
for (int i = 0; i < limit && matches.hasNext(); i++)
83-
triples.add(triples.asStatement(toTriple(matches.next())));
84-
}
85-
}
86-
87-
// estimates can be wrong; ensure 0 is returned if there are no results, and always more than actual results
88-
final long estimatedTotal = triples.size() > 0 ? Math.max(offset + triples.size() + 1, matches.estimatedNumResults())
89-
: hasMatches ? Math.max(matches.estimatedNumResults(), 1) : 0;
65+
if (hasMatches) {
66+
// try to jump directly to the offset
67+
boolean atOffset;
68+
if (matches.canGoTo()) {
69+
try {
70+
matches.goTo(offset);
71+
atOffset = true;
72+
} // if the offset is outside the bounds, this page has no matches
73+
catch (IndexOutOfBoundsException exception) {
74+
atOffset = false;
75+
}
76+
} // if not possible, advance to the offset iteratively
77+
else {
78+
matches.goToStart();
79+
for (int i = 0; !(atOffset = i == offset) && matches.hasNext(); i++) {
80+
matches.next();
81+
}
82+
}
83+
// try to add `limit` triples to the result model
84+
if (atOffset) {
85+
for (int i = 0; i < limit && matches.hasNext(); i++) {
86+
triples.add(triples.asStatement(toTriple(matches.next())));
87+
}
88+
}
89+
}
90+
91+
// estimates can be wrong; ensure 0 is returned if there are no results, and always more than actual results
92+
final long estimatedTotal = triples.size() > 0 ? Math.max(offset + triples.size() + 1, matches.estimatedNumResults())
93+
: hasMatches ? Math.max(matches.estimatedNumResults(), 1) : 0;
9094

9195
// create the fragment
9296
return new TriplePatternFragment() {
93-
@Override
94-
public Model getTriples() { return triples; }
95-
96-
@Override
97-
public long getTotalSize() { return estimatedTotal; }
98-
};
97+
@Override
98+
public Model getTriples() {
99+
return triples;
100+
}
101+
102+
@Override
103+
public long getTotalSize() {
104+
return estimatedTotal;
105+
}
106+
};
99107
}
100108

101109
/**
@@ -106,9 +114,9 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
106114
*/
107115
private Triple toTriple(TripleID tripleId) {
108116
return new Triple(
109-
dictionary.getNode(tripleId.getSubject(), TripleComponentRole.SUBJECT),
110-
dictionary.getNode(tripleId.getPredicate(), TripleComponentRole.PREDICATE),
111-
dictionary.getNode(tripleId.getObject(), TripleComponentRole.OBJECT)
117+
dictionary.getNode(tripleId.getSubject(), TripleComponentRole.SUBJECT),
118+
dictionary.getNode(tripleId.getPredicate(), TripleComponentRole.PREDICATE),
119+
dictionary.getNode(tripleId.getObject(), TripleComponentRole.OBJECT)
112120
);
113121
}
114122
}

src/org/linkeddatafragments/datasource/IDataSource.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
* @author Ruben Verborgh
1010
*/
1111
public interface IDataSource {
12-
/**
13-
* Gets a page of the Basic Linked Data Fragment matching the specified triple pattern.
14-
* @param subject the subject (null to match any subject)
15-
* @param predicate the predicate (null to match any predicate)
16-
* @param object the object (null to match any object)
17-
* @param offset the triple index at which to start the page
18-
* @param limit the number of triples on the page
19-
* @return the first page of the fragment
20-
*/
21-
public TriplePatternFragment getFragment(Resource subject, Property predicate, RDFNode object,
22-
long offset, long limit);
23-
public String getTitle();
12+
/**
13+
* Gets a page of the Basic Linked Data Fragment matching the specified triple pattern.
14+
* @param subject the subject (null to match any subject)
15+
* @param predicate the predicate (null to match any predicate)
16+
* @param object the object (null to match any object)
17+
* @param offset the triple index at which to start the page
18+
* @param limit the number of triples on the page
19+
* @return the first page of the fragment
20+
*/
21+
public TriplePatternFragment getFragment(Resource subject, Property predicate,
22+
RDFNode object, long offset, long limit);
23+
public String getTitle();
2424

25-
public String getDescription();
25+
public String getDescription();
2626
}

src/org/linkeddatafragments/datasource/TriplePatternFragment.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
* @author Ruben Verborgh
88
*/
99
public interface TriplePatternFragment {
10-
/**
11-
* Gets the data of this fragment (possibly only partial).
12-
* @return the data as triples
13-
*/
14-
public Model getTriples();
15-
16-
/**
17-
* Gets the total number of triples in the fragment (can be an estimate).
18-
* @return the total number of triples
19-
*/
20-
public long getTotalSize();
10+
/**
11+
* Gets the data of this fragment (possibly only partial).
12+
* @return the data as triples
13+
*/
14+
public Model getTriples();
15+
16+
/**
17+
* Gets the total number of triples in the fragment (can be an estimate).
18+
* @return the total number of triples
19+
*/
20+
public long getTotalSize();
2121
}

src/org/linkeddatafragments/datasource/TriplePatternFragmentBase.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@
88
* @author Ruben Verborgh
99
*/
1010
public class TriplePatternFragmentBase implements TriplePatternFragment {
11-
private final Model triples;
12-
private final long totalSize;
13-
14-
/**
15-
* Creates an empty Basic Linked Data Fragment.
16-
*/
17-
public TriplePatternFragmentBase() {
18-
this(null, 0);
19-
}
20-
21-
/**
22-
* Creates a new Basic Linked Data Fragment.
23-
* @param triples the triples (possibly partial)
24-
* @param totalSize the total size
25-
*/
26-
public TriplePatternFragmentBase(Model triples, long totalSize) {
27-
this.triples = triples == null ? ModelFactory.createDefaultModel() : triples;
28-
this.totalSize = totalSize < 0 ? 0 : totalSize;
29-
}
11+
private final Model triples;
12+
private final long totalSize;
3013

31-
@Override
32-
public Model getTriples() {
33-
return triples;
34-
}
14+
/**
15+
* Creates an empty Basic Linked Data Fragment.
16+
*/
17+
public TriplePatternFragmentBase() {
18+
this(null, 0);
19+
}
3520

36-
@Override
37-
public long getTotalSize() {
38-
return totalSize;
39-
}
21+
/**
22+
* Creates a new Basic Linked Data Fragment.
23+
* @param triples the triples (possibly partial)
24+
* @param totalSize the total size
25+
*/
26+
public TriplePatternFragmentBase(Model triples, long totalSize) {
27+
this.triples = triples == null ? ModelFactory.createDefaultModel() : triples;
28+
this.totalSize = totalSize < 0 ? 0 : totalSize;
29+
}
30+
31+
@Override
32+
public Model getTriples() {
33+
return triples;
34+
}
35+
36+
@Override
37+
public long getTotalSize() {
38+
return totalSize;
39+
}
4040
}

src/org/linkeddatafragments/exceptions/DataSourceException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* @author mielvandersande
66
*/
77
public class DataSourceException extends Exception {
8+
private static final long serialVersionUID = 1L;
89

910
public DataSourceException(Throwable cause) {
1011
super(cause.getMessage());
1112
}
1213

1314
public DataSourceException(String message) {
1415
super("Could not create DataSource: " + message);
15-
}
16-
16+
}
1717
}

src/org/linkeddatafragments/exceptions/UnknownDataSourceTypeException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* @author mielvandersande
66
*/
77
public class UnknownDataSourceTypeException extends DataSourceException {
8+
private static final long serialVersionUID = 1L;
89

910
public UnknownDataSourceTypeException(String type) {
1011
super("Type " + type + " does not exist.");
11-
}
12-
12+
}
1313
}

0 commit comments

Comments
 (0)