1818 */
1919package org .neo4j .driver ;
2020
21+ import java .util .List ;
2122import java .util .Map ;
2223import java .util .function .BiFunction ;
2324import java .util .function .Consumer ;
2425import java .util .stream .Collector ;
26+ import java .util .stream .Collectors ;
27+ import org .neo4j .driver .internal .EagerResultValue ;
2528import org .neo4j .driver .summary .ResultSummary ;
2629import org .neo4j .driver .util .Experimental ;
2730
7982 * .execute(mapping(record -> record.get("N").asLong(), maxBy(Long::compare)));
8083 * }
8184 * </pre>
82- * If there is a need to access {@link ResultSummary} value, another method option is available:
85+ * If there is a need to access {@link Result#keys()} and/or {@link ResultSummary} value, another method option is
86+ * available:
8387 * <pre>
8488 * {@code
8589 * import static java.util.stream.Collectors.*;
8690 *
87- * private record ResultValue(Set<Long> values, ResultSummary summary) {}
91+ * private record ResultValue(List<String> keys, Set<Long> values, ResultSummary summary) {}
8892 *
8993 * var result = driver.queryTask("UNWIND range(0, 5) as N RETURN N")
9094 * .execute(Collectors.mapping(record -> record.get("N").asLong(), toSet()), ResultValue::new);
@@ -118,7 +122,9 @@ public interface QueryTask {
118122 *
119123 * @return an instance of result containing all records, keys and result summary
120124 */
121- EagerResult execute ();
125+ default EagerResult execute () {
126+ return execute (Collectors .toList (), EagerResultValue ::new );
127+ }
122128
123129 /**
124130 * Executes query, collects {@link Record} values using the provided {@link Collector} and produces a final result.
@@ -129,22 +135,49 @@ public interface QueryTask {
129135 * @return the final result value
130136 */
131137 default <T > T execute (Collector <Record , ?, T > recordCollector ) {
132- return execute (recordCollector , (collectorResult , ignored ) -> collectorResult );
138+ return execute (recordCollector , (ignoredKeys , collectorResult , ignoredSummary ) -> collectorResult );
133139 }
134140
135141 /**
136142 * Executes query, collects {@link Record} values using the provided {@link Collector} and produces a final result
137143 * by invoking the provided {@link BiFunction} with the collected result and {@link ResultSummary} values.
144+ * <p>
145+ * If any of the arguments throws an exception implementing the
146+ * {@link org.neo4j.driver.exceptions.RetryableException} marker interface, the query is retried automatically in
147+ * the same way as in the transaction functions. Exceptions not implementing the interface trigger transaction
148+ * rollback and are then propagated to the user.
138149 *
139150 * @param recordCollector collector instance responsible for processing {@link Record} values and producing a
140151 * collected result, the collector may be used multiple times if query is retried
141- * @param finisherWithSummary function accepting both the collected result and {@link ResultSummary} values to
142- * output the final result, the function may be invoked multiple times if query is
143- * retried
152+ * @param resultFinisher function accepting the {@link Result#keys()}, collected result and {@link ResultSummary}
153+ * values to output the final result value , the function may be invoked multiple times if
154+ * query is retried
144155 * @param <A> the mutable accumulation type of the collector's reduction operation
145156 * @param <R> the collector's result type
146157 * @param <T> the final result type
147158 * @return the final result value
148159 */
149- <A , R , T > T execute (Collector <Record , A , R > recordCollector , BiFunction <R , ResultSummary , T > finisherWithSummary );
160+ <A , R , T > T execute (Collector <Record , A , R > recordCollector , ResultFinisher <R , T > resultFinisher );
161+
162+ /**
163+ * A function accepting the {@link Result#keys()}, collected result and {@link ResultSummary} values to produce a
164+ * final result value.
165+ *
166+ * @param <S> the collected value type
167+ * @param <T> the final value type
168+ * @since 5.5
169+ */
170+ @ Experimental
171+ @ FunctionalInterface
172+ interface ResultFinisher <S , T > {
173+ /**
174+ * Accepts the {@link Result#keys()}, collected result and {@link ResultSummary} values to produce the final
175+ * result value.
176+ * @param value the collected value
177+ * @param keys the {@link Result#keys()} value
178+ * @param summary the {@link ResultSummary} value
179+ * @return the final value
180+ */
181+ T finish (List <String > keys , S value , ResultSummary summary );
182+ }
150183}
0 commit comments