Skip to content

Commit

Permalink
Ordering resources by their subject IDs when doing a query to store. (#…
Browse files Browse the repository at this point in the history
…239)

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

Signed-off-by: Jad El-khoury <jad.el.khoury@scania.com>

* change log.

Signed-off-by: Jad El-khoury <jad.el.khoury@scania.com>

Co-authored-by: Jad El-khoury <jad.el.khoury@scania.com>
  • Loading branch information
jadelkhoury and jad-elkhoury committed Feb 3, 2022
1 parent c80e0cd commit e46d315
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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

0 comments on commit e46d315

Please sign in to comment.