Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering resources by their subject IDs when doing a query to store. #239

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- 🧨 Lyo is now built using JDK 11
- 🧨 Jena is upgraded to 4.1.0
- Jena renamed `RDFReader/RDFWriter` to `RDFReaderI/RDFWriterI`
- LyoStore: Ordering resources by their subject IDs when doing a query to store.


### Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.jena.arq.querybuilder.SelectBuilder;
import org.apache.jena.arq.querybuilder.DescribeBuilder;
import org.apache.jena.arq.querybuilder.ExprFactory;
import org.apache.jena.arq.querybuilder.Order;
import org.apache.jena.riot.RiotException;

import java.lang.reflect.Array;
Expand All @@ -42,13 +43,15 @@
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;
import javax.xml.datatype.DatatypeConfigurationException;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -411,7 +414,10 @@ private <T extends IResource> List<T> getResourcesFromModel(final Model model,
for (int i = 0; i < obj.length; i++) {
castObjects[i] = clazz.cast(obj[i]);
}
return Arrays.asList(castObjects);
//The Model is most likely obtained via Select query that is orded by the subject (ascending)
//See sparql construction in constructSparqlWhere()
//Order the list below accordingly.
return Arrays.asList(castObjects).stream().sorted(Comparator.comparing(IResource::getAbout)).collect(Collectors.toList());
} catch (InvocationTargetException | OslcCoreApplicationException | NoSuchMethodException
| URISyntaxException | DatatypeConfigurationException | InstantiationException e) {
throw new ModelUnmarshallingException(e);
Expand Down Expand Up @@ -494,6 +500,7 @@ private Model modelFromQueryFlatPaged(final URI namedGraph, final URI type, fina
+ " ?s ?p ?o .\n"
+ " ?s rdf:type ?t.\n"
+ " }\n"
+ " ORDER BY ASC(?s)\n"
+ " LIMIT ?l\n"
+ " OFFSET "
+ "?f\n"
Expand Down Expand Up @@ -603,6 +610,9 @@ private SelectBuilder constructSparqlWhere(final String prefixes, final String w
distinctResourcesQuery.addFilter(regex);
}

//Order the response by the subject, to ensure stable responses when using limit and offset below.
distinctResourcesQuery.addOrderBy("?s", Order.ASCENDING);

if (limit > 0) {
distinctResourcesQuery.setLimit(limit);
}
Expand Down