18
18
*/
19
19
package org .neo4j .driver ;
20
20
21
+ import java .util .List ;
21
22
import java .util .Map ;
22
23
import java .util .function .BiFunction ;
23
24
import java .util .function .Consumer ;
24
25
import java .util .stream .Collector ;
26
+ import java .util .stream .Collectors ;
27
+ import org .neo4j .driver .internal .EagerResultValue ;
25
28
import org .neo4j .driver .summary .ResultSummary ;
26
29
import org .neo4j .driver .util .Experimental ;
27
30
79
82
* .execute(mapping(record -> record.get("N").asLong(), maxBy(Long::compare)));
80
83
* }
81
84
* </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:
83
87
* <pre>
84
88
* {@code
85
89
* import static java.util.stream.Collectors.*;
86
90
*
87
- * private record ResultValue(Set<Long> values, ResultSummary summary) {}
91
+ * private record ResultValue(List<String> keys, Set<Long> values, ResultSummary summary) {}
88
92
*
89
93
* var result = driver.queryTask("UNWIND range(0, 5) as N RETURN N")
90
94
* .execute(Collectors.mapping(record -> record.get("N").asLong(), toSet()), ResultValue::new);
@@ -118,7 +122,9 @@ public interface QueryTask {
118
122
*
119
123
* @return an instance of result containing all records, keys and result summary
120
124
*/
121
- EagerResult execute ();
125
+ default EagerResult execute () {
126
+ return execute (Collectors .toList (), EagerResultValue ::new );
127
+ }
122
128
123
129
/**
124
130
* Executes query, collects {@link Record} values using the provided {@link Collector} and produces a final result.
@@ -129,22 +135,49 @@ public interface QueryTask {
129
135
* @return the final result value
130
136
*/
131
137
default <T > T execute (Collector <Record , ?, T > recordCollector ) {
132
- return execute (recordCollector , (collectorResult , ignored ) -> collectorResult );
138
+ return execute (recordCollector , (ignoredKeys , collectorResult , ignoredSummary ) -> collectorResult );
133
139
}
134
140
135
141
/**
136
142
* Executes query, collects {@link Record} values using the provided {@link Collector} and produces a final result
137
143
* 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.
138
149
*
139
150
* @param recordCollector collector instance responsible for processing {@link Record} values and producing a
140
151
* 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
144
155
* @param <A> the mutable accumulation type of the collector's reduction operation
145
156
* @param <R> the collector's result type
146
157
* @param <T> the final result type
147
158
* @return the final result value
148
159
*/
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
+ }
150
183
}
0 commit comments