Skip to content

Commit

Permalink
#1808 simplified QueryResults slightly
Browse files Browse the repository at this point in the history
Signed-off-by: Håvard Ottestad <hmottestad@gmail.com>
  • Loading branch information
hmottestad committed Jan 28, 2020
1 parent 2777daf commit bab4d4e
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions core/query/src/main/java/org/eclipse/rdf4j/query/QueryResults.java
Expand Up @@ -17,6 +17,7 @@
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Stream;

import javax.xml.datatype.XMLGregorianCalendar;

Expand Down Expand Up @@ -50,14 +51,14 @@

/**
* Utility methods related to query results.
*
*
* @author Jeen Broekstra
*/
public class QueryResults extends Iterations {

/**
* Get a {@link Model} containing all elements obtained from the specified query result.
*
*
* @param iteration the source iteration to get the statements from. This can be a {@link GraphQueryResult}, a
* {@link RepositoryResult&lt;Statement&gt;}, or any other instance of {@link CloseableIteration
* &lt;Statement&gt;}
Expand All @@ -72,45 +73,33 @@ public static Model asModel(CloseableIteration<? extends Statement, ? extends RD

/**
* Returns a single element from the query result.The QueryResult is automatically closed by this method.
*
*
* @param result
* @return a single query result element.
* @return a single query result element or null
* @throws QueryEvaluationException
*/
public static Statement singleResult(GraphQueryResult result) throws QueryEvaluationException {
Statement singleResult = null;
try {
if (result.hasNext()) {
singleResult = result.next();
}
} finally {
result.close();
try (Stream<Statement> stream = result.stream()) {
return stream.findFirst().orElse(null);
}
return singleResult;
}

/**
* Returns a single element from the query result.The QueryResult is automatically closed by this method.
*
*
* @param result
* @return a single query result element.
* @return a single query result element or null
* @throws QueryEvaluationException
*/
public static BindingSet singleResult(TupleQueryResult result) throws QueryEvaluationException {
BindingSet singleResult = null;
try {
if (result.hasNext()) {
singleResult = result.next();
}
} finally {
result.close();
try (Stream<BindingSet> stream = result.stream()) {
return stream.findFirst().orElse(null);
}
return singleResult;
}

/**
* Returns a {@link GraphQueryResult} that filters out any duplicate solutions from the supplied queryResult.
*
*
* @param queryResult a queryResult containing possible duplicate statements.
* @return a {@link GraphQueryResult} with any duplicates filtered out.
*/
Expand All @@ -120,7 +109,7 @@ public static GraphQueryResult distinctResults(GraphQueryResult queryResult) {

/**
* Returns a {@link TupleQueryResult} that filters out any duplicate solutions from the supplied queryResult.
*
*
* @param queryResult a queryResult containing possible duplicate solutions.
* @return a {@link TupleQueryResult} with any duplicates filtered out.
*/
Expand All @@ -131,7 +120,7 @@ public static TupleQueryResult distinctResults(TupleQueryResult queryResult) {
/**
* Returns a {@link TupleQueryResult} that returns at most the specified maximum number of solutions, starting at
* the supplied offset.
*
*
* @param queryResult a query result possibly containing more solutions than the specified maximum.
* @param limit the maximum number of solutions to return. If set to 0 or lower, no limit will be applied.
* @param offset the number of solutions to skip at the beginning. If set to 0 or lower, no offset will be
Expand All @@ -157,7 +146,7 @@ public static TupleQueryResult limitResults(TupleQueryResult queryResult, long l
/**
* Returns a {@link GraphQueryResult} that returns at most the specified maximum number of solutions, starting at
* the supplied offset.
*
*
* @param queryResult a query result possibly containing more solutions than the specified maximum.
* @param limit the maximum number of solutions to return. If set to 0 or lower, no limit will be applied.
* @param offset the number of solutions to skip at the beginning. If set to 0 or lower, no offset will be
Expand Down Expand Up @@ -185,7 +174,7 @@ public static GraphQueryResult limitResults(GraphQueryResult queryResult, long l
* background.<br>
* IMPORTANT: As this method will spawn a new thread in the background, it is vitally important that the resulting
* GraphQueryResult be closed consistently when it is no longer required, to prevent resource leaks.
*
*
* @param in The {@link InputStream} containing the RDF document.
* @param baseURI The base URI for the RDF document.
* @param format The {@link RDFFormat} of the RDF document.
Expand All @@ -201,7 +190,7 @@ public static GraphQueryResult parseGraphBackground(InputStream in, String baseU
* background.<br>
* IMPORTANT: As this method will spawn a new thread in the background, it is vitally important that the resulting
* GraphQueryResult be closed consistently when it is no longer required, to prevent resource leaks.
*
*
* @param in The {@link InputStream} containing the RDF document.
* @param baseURI The base URI for the RDF document.
* @param parser The {@link RDFParser}.
Expand All @@ -228,13 +217,14 @@ public static GraphQueryResult parseGraphBackground(InputStream in, String baseU
* The {@link TupleQueryResult#close()} method will always be called before this method returns. <br>
* If there is an exception generated by the TupleQueryResult, {@link QueryResultHandler#endQueryResult()} will not
* be called.
*
*
* @param tqr The query result to report.
* @param handler The handler to report the query result to.
* @throws TupleQueryResultHandlerException If such an exception is thrown by the used query result writer.
*/
public static void report(TupleQueryResult tqr, QueryResultHandler handler)
throws TupleQueryResultHandlerException, QueryEvaluationException {

try {
handler.startQueryResult(tqr.getBindingNames());

Expand All @@ -252,7 +242,7 @@ public static void report(TupleQueryResult tqr, QueryResultHandler handler)
* Reports a graph query result to an {@link RDFHandler}. <br>
* The {@link GraphQueryResult#close()} method will always be called before this method returns.<br>
* If there is an exception generated by the GraphQueryResult, {@link RDFHandler#endRDF()} will not be called.
*
*
* @param gqr The query result to report.
* @param rdfHandler The handler to report the query result to.
* @throws RDFHandlerException If such an exception is thrown by the used RDF writer.
Expand Down Expand Up @@ -284,7 +274,7 @@ public static void report(GraphQueryResult gqr, RDFHandler rdfHandler)
* contain the same set of {@link BindingSet}s and have the same headers. Blank nodes identifiers are not relevant
* for equality, they are matched by trying to find compatible mappings between BindingSets. Note that the method
* consumes both query results fully.
*
*
* @param tqr1 the first {@link TupleQueryResult} to compare.
* @param tqr2 the second {@link TupleQueryResult} to compare.
* @return true if equal
Expand Down Expand Up @@ -317,7 +307,7 @@ public static boolean isSubset(TupleQueryResult tqr1, TupleQueryResult tqr2) thr
/**
* Compares two graph query results and returns {@code true} if they are equal. Two graph query results are
* considered equal if they are isomorphic graphs. Note that the method consumes both query results fully.
*
*
* @param result1 the first query result to compare
* @param result2 the second query result to compare.
* @return {@code true} if the supplied graph query results are isomorphic graphs, {@code false} otherwise.
Expand All @@ -340,7 +330,7 @@ private static boolean matchBindingSets(List<? extends BindingSet> queryResult1,
* A recursive method for finding a complete mapping between blank nodes in queryResult1 and blank nodes in
* queryResult2. The algorithm does a depth-first search trying to establish a mapping for each blank node occurring
* in queryResult1.
*
*
* @return true if a complete mapping has been found, false otherwise.
*/
private static boolean matchBindingSets(List<? extends BindingSet> queryResult1,
Expand Down Expand Up @@ -477,7 +467,7 @@ private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNod
/**
* Check whether two {@link BindingSet}s are compatible.Two binding sets are compatible if they have equal values
* for each binding name that occurs in both binding sets.
*
*
* @param bs1
* @param bs2
* @return true if compatible
Expand Down

0 comments on commit bab4d4e

Please sign in to comment.