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,7 +135,7 @@ 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
/**
@@ -138,13 +144,35 @@ default <T> T execute(Collector<Record, ?, T> recordCollector) {
138
144
*
139
145
* @param recordCollector collector instance responsible for processing {@link Record} values and producing a
140
146
* 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
147
+ * @param resultFinisher function accepting the {@link Result#keys()}, collected result and {@link ResultSummary}
148
+ * values to output the final result value , the function may be invoked multiple times if
149
+ * query is retried
144
150
* @param <A> the mutable accumulation type of the collector's reduction operation
145
151
* @param <R> the collector's result type
146
152
* @param <T> the final result type
147
153
* @return the final result value
148
154
*/
149
- <A , R , T > T execute (Collector <Record , A , R > recordCollector , BiFunction <R , ResultSummary , T > finisherWithSummary );
155
+ <A , R , T > T execute (Collector <Record , A , R > recordCollector , ResultFinisher <R , T > resultFinisher );
156
+
157
+ /**
158
+ * A function accepting the {@link Result#keys()}, collected result and {@link ResultSummary} values to produce a
159
+ * final result value.
160
+ *
161
+ * @param <S> the collected value type
162
+ * @param <T> the final value type
163
+ * @since 5.5
164
+ */
165
+ @ Experimental
166
+ @ FunctionalInterface
167
+ interface ResultFinisher <S , T > {
168
+ /**
169
+ * Accepts the {@link Result#keys()}, collected result and {@link ResultSummary} values to produce the final
170
+ * result value.
171
+ * @param value the collected value
172
+ * @param keys the {@link Result#keys()} value
173
+ * @param summary the {@link ResultSummary} value
174
+ * @return the final value
175
+ */
176
+ T finish (List <String > keys , S value , ResultSummary summary );
177
+ }
150
178
}
0 commit comments