Skip to content

Commit

Permalink
Merge pull request #5 from barthanssens/master
Browse files Browse the repository at this point in the history
Upgrades and splitting up some methods
  • Loading branch information
mielvds committed Dec 2, 2015
2 parents 0fa4d74 + efb5da2 commit 9cfd6cb
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 304 deletions.
13 changes: 6 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>LDF-Server</groupId>
<artifactId>LDF-Server</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>war</packaging>

<properties>
<jettyVersion>9.2.5.v20141112</jettyVersion>
<jettyVersion>9.3.6.v20151106</jettyVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.rdfhdt</groupId>
Expand All @@ -31,10 +30,10 @@
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
Expand All @@ -55,7 +54,7 @@
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -116,4 +115,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
13 changes: 6 additions & 7 deletions src/org/linkeddatafragments/config/ConfigReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@
* @author Ruben Verborgh
*/
public class ConfigReader {

private final Map<String, JsonObject> dataSources = new HashMap<String, JsonObject>();
private final Map<String, String> prefixes = new HashMap<String, String>();
private final Map<String, JsonObject> dataSources = new HashMap<>();
private final Map<String, String> prefixes = new HashMap<>();

/**
* Creates a new configuration reader.
*
* @param configReader the configuration
*/
public ConfigReader(Reader configReader) {
final JsonObject root = new JsonParser().parse(configReader).getAsJsonObject();
for (final Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
final JsonObject dataSource = entry.getValue().getAsJsonObject();
JsonObject root = new JsonParser().parse(configReader).getAsJsonObject();
for (Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
JsonObject dataSource = entry.getValue().getAsJsonObject();
this.dataSources.put(entry.getKey(), dataSource);
}
for (final Entry<String, JsonElement> entry : root.getAsJsonObject("prefixes").entrySet()) {
for (Entry<String, JsonElement> entry : root.getAsJsonObject("prefixes").entrySet()) {
this.prefixes.put(entry.getKey(), entry.getValue().getAsString());
}
}
Expand Down
92 changes: 50 additions & 42 deletions src/org/linkeddatafragments/datasource/HdtDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,61 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
throw new IllegalArgumentException("limit");
}

// look up the result from the HDT datasource
final int subjectId = subject == null ? 0 : dictionary.getIntID(subject.asNode(), TripleComponentRole.SUBJECT);
final int predicateId = predicate == null ? 0 : dictionary.getIntID(predicate.asNode(), TripleComponentRole.PREDICATE);
final int objectId = object == null ? 0 : dictionary.getIntID(object.asNode(), TripleComponentRole.OBJECT);
// look up the result from the HDT datasource)
int subjectId = subject == null ? 0 : dictionary.getIntID(subject.asNode(), TripleComponentRole.SUBJECT);
int predicateId = predicate == null ? 0 : dictionary.getIntID(predicate.asNode(), TripleComponentRole.PREDICATE);
int objectId = object == null ? 0 : dictionary.getIntID(object.asNode(), TripleComponentRole.OBJECT);

if (subjectId < 0 || predicateId < 0 || objectId < 0) {
return new TriplePatternFragmentBase();
}

final Model triples = ModelFactory.createDefaultModel();
final IteratorTripleID matches = datasource.getTriples().search(new TripleID(subjectId, predicateId, objectId));
final boolean hasMatches = matches.hasNext();
IteratorTripleID matches = datasource.getTriples().search(new TripleID(subjectId, predicateId, objectId));
boolean hasMatches = matches.hasNext();

if (hasMatches) {
// try to jump directly to the offset
boolean atOffset;
if (matches.canGoTo()) {
try {
matches.goTo(offset);
atOffset = true;
}
// if the offset is outside the bounds, this page has no matches
catch (IndexOutOfBoundsException exception) { atOffset = false; }
}
// if not possible, advance to the offset iteratively
else {
matches.goToStart();
for (int i = 0; !(atOffset = i == offset) && matches.hasNext(); i++)
matches.next();
}
// try to add `limit` triples to the result model
if (atOffset) {
for (int i = 0; i < limit && matches.hasNext(); i++)
triples.add(triples.asStatement(toTriple(matches.next())));
}
}

// estimates can be wrong; ensure 0 is returned if there are no results, and always more than actual results
final long estimatedTotal = triples.size() > 0 ? Math.max(offset + triples.size() + 1, matches.estimatedNumResults())
: hasMatches ? Math.max(matches.estimatedNumResults(), 1) : 0;
if (hasMatches) {
// try to jump directly to the offset
boolean atOffset;
if (matches.canGoTo()) {
try {
matches.goTo(offset);
atOffset = true;
} // if the offset is outside the bounds, this page has no matches
catch (IndexOutOfBoundsException exception) {
atOffset = false;
}
} // if not possible, advance to the offset iteratively
else {
matches.goToStart();
for (int i = 0; !(atOffset = i == offset) && matches.hasNext(); i++) {
matches.next();
}
}
// try to add `limit` triples to the result model
if (atOffset) {
for (int i = 0; i < limit && matches.hasNext(); i++) {
triples.add(triples.asStatement(toTriple(matches.next())));
}
}
}

// estimates can be wrong; ensure 0 is returned if there are no results, and always more than actual results
final long estimatedTotal = triples.size() > 0 ? Math.max(offset + triples.size() + 1, matches.estimatedNumResults())
: hasMatches ? Math.max(matches.estimatedNumResults(), 1) : 0;

// create the fragment
return new TriplePatternFragment() {
@Override
public Model getTriples() { return triples; }

@Override
public long getTotalSize() { return estimatedTotal; }
};
@Override
public Model getTriples() {
return triples;
}

@Override
public long getTotalSize() {
return estimatedTotal;
}
};
}

/**
Expand All @@ -106,9 +114,9 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
*/
private Triple toTriple(TripleID tripleId) {
return new Triple(
dictionary.getNode(tripleId.getSubject(), TripleComponentRole.SUBJECT),
dictionary.getNode(tripleId.getPredicate(), TripleComponentRole.PREDICATE),
dictionary.getNode(tripleId.getObject(), TripleComponentRole.OBJECT)
dictionary.getNode(tripleId.getSubject(), TripleComponentRole.SUBJECT),
dictionary.getNode(tripleId.getPredicate(), TripleComponentRole.PREDICATE),
dictionary.getNode(tripleId.getObject(), TripleComponentRole.OBJECT)
);
}
}
26 changes: 13 additions & 13 deletions src/org/linkeddatafragments/datasource/IDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
* @author Ruben Verborgh
*/
public interface IDataSource {
/**
* Gets a page of the Basic Linked Data Fragment matching the specified triple pattern.
* @param subject the subject (null to match any subject)
* @param predicate the predicate (null to match any predicate)
* @param object the object (null to match any object)
* @param offset the triple index at which to start the page
* @param limit the number of triples on the page
* @return the first page of the fragment
*/
public TriplePatternFragment getFragment(Resource subject, Property predicate, RDFNode object,
long offset, long limit);
public String getTitle();
/**
* Gets a page of the Basic Linked Data Fragment matching the specified triple pattern.
* @param subject the subject (null to match any subject)
* @param predicate the predicate (null to match any predicate)
* @param object the object (null to match any object)
* @param offset the triple index at which to start the page
* @param limit the number of triples on the page
* @return the first page of the fragment
*/
public TriplePatternFragment getFragment(Resource subject, Property predicate,
RDFNode object, long offset, long limit);
public String getTitle();

public String getDescription();
public String getDescription();
}
22 changes: 11 additions & 11 deletions src/org/linkeddatafragments/datasource/TriplePatternFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* @author Ruben Verborgh
*/
public interface TriplePatternFragment {
/**
* Gets the data of this fragment (possibly only partial).
* @return the data as triples
*/
public Model getTriples();
/**
* Gets the total number of triples in the fragment (can be an estimate).
* @return the total number of triples
*/
public long getTotalSize();
/**
* Gets the data of this fragment (possibly only partial).
* @return the data as triples
*/
public Model getTriples();

/**
* Gets the total number of triples in the fragment (can be an estimate).
* @return the total number of triples
*/
public long getTotalSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@
* @author Ruben Verborgh
*/
public class TriplePatternFragmentBase implements TriplePatternFragment {
private final Model triples;
private final long totalSize;

/**
* Creates an empty Basic Linked Data Fragment.
*/
public TriplePatternFragmentBase() {
this(null, 0);
}

/**
* Creates a new Basic Linked Data Fragment.
* @param triples the triples (possibly partial)
* @param totalSize the total size
*/
public TriplePatternFragmentBase(Model triples, long totalSize) {
this.triples = triples == null ? ModelFactory.createDefaultModel() : triples;
this.totalSize = totalSize < 0 ? 0 : totalSize;
}
private final Model triples;
private final long totalSize;

@Override
public Model getTriples() {
return triples;
}
/**
* Creates an empty Basic Linked Data Fragment.
*/
public TriplePatternFragmentBase() {
this(null, 0);
}

@Override
public long getTotalSize() {
return totalSize;
}
/**
* Creates a new Basic Linked Data Fragment.
* @param triples the triples (possibly partial)
* @param totalSize the total size
*/
public TriplePatternFragmentBase(Model triples, long totalSize) {
this.triples = triples == null ? ModelFactory.createDefaultModel() : triples;
this.totalSize = totalSize < 0 ? 0 : totalSize;
}

@Override
public Model getTriples() {
return triples;
}

@Override
public long getTotalSize() {
return totalSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* @author mielvandersande
*/
public class DataSourceException extends Exception {
private static final long serialVersionUID = 1L;

public DataSourceException(Throwable cause) {
super(cause.getMessage());
}

public DataSourceException(String message) {
super("Could not create DataSource: " + message);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @author mielvandersande
*/
public class UnknownDataSourceTypeException extends DataSourceException {
private static final long serialVersionUID = 1L;

public UnknownDataSourceTypeException(String type) {
super("Type " + type + " does not exist.");
}

}
}
Loading

0 comments on commit 9cfd6cb

Please sign in to comment.