49
49
import static com .google .common .collect .ImmutableList .toImmutableList ;
50
50
import static com .google .common .collect .ImmutableMap .toImmutableMap ;
51
51
import static com .google .common .collect .ImmutableSet .toImmutableSet ;
52
+ import static com .google .common .collect .ImmutableSetMultimap .flatteningToImmutableSetMultimap ;
53
+ import static com .google .common .collect .Lists .partition ;
52
54
import static com .google .common .net .HttpHeaders .CONTENT_TYPE ;
53
55
import static com .google .common .net .MediaType .JSON_UTF_8 ;
54
56
import static io .airlift .http .client .FullJsonResponseHandler .createFullJsonResponseHandler ;
@@ -66,6 +68,7 @@ public class OpaHttpClient
66
68
private final boolean logRequests ;
67
69
private final boolean logResponses ;
68
70
private static final Logger log = Logger .get (OpaHttpClient .class );
71
+ private final Optional <Integer > opaBatchSize ;
69
72
70
73
@ Inject
71
74
public OpaHttpClient (
@@ -79,6 +82,7 @@ public OpaHttpClient(
79
82
this .executor = requireNonNull (executor , "executor is null" );
80
83
this .logRequests = config .getLogRequests ();
81
84
this .logResponses = config .getLogResponses ();
85
+ this .opaBatchSize = config .getOpaBatchSize ();
82
86
}
83
87
84
88
public <T > FluentFuture <T > submitOpaRequest (OpaQueryInput input , URI uri , JsonCodec <T > deserializer )
@@ -158,11 +162,14 @@ public <T> Set<T> batchFilterFromOpa(Collection<T> items, Function<List<T>, OpaQ
158
162
159
163
public <K , V > Map <K , Set <V >> parallelBatchFilterFromOpa (Map <K , ? extends Collection <V >> items , BiFunction <K , List <V >, OpaQueryInput > requestBuilder , URI uri , JsonCodec <? extends OpaBatchQueryResult > deserializer )
160
164
{
161
- List <Map .Entry <K , ImmutableList <V >>> parallelRequestItems = items .entrySet ()
165
+ List <Map .Entry <K , List <V >>> parallelRequestItems = items .entrySet ()
162
166
.stream ()
163
167
.filter (entry -> !entry .getValue ().isEmpty ())
164
- .map (entry -> Map .entry (entry .getKey (), ImmutableList .copyOf (entry .getValue ())))
168
+ .flatMap (entry -> partition (ImmutableList .copyOf (entry .getValue ()), this .opaBatchSize .orElse (entry .getValue ().size ()))
169
+ .stream ()
170
+ .map (partition -> Map .entry (entry .getKey (), partition )))
165
171
.collect (toImmutableList ());
172
+
166
173
return parallelRequest (
167
174
parallelRequestItems ,
168
175
entry -> requestBuilder .apply (entry .getKey (), entry .getValue ()),
@@ -176,7 +183,11 @@ public <K, V> Map<K, Set<V>> parallelBatchFilterFromOpa(Map<K, ? extends Collect
176
183
uri ,
177
184
deserializer )
178
185
.stream ()
179
- .collect (toImmutableMap (Map .Entry ::getKey , Map .Entry ::getValue ));
186
+ .collect (flatteningToImmutableSetMultimap (Map .Entry ::getKey , entry -> entry .getValue ().stream ()))
187
+ .asMap ()
188
+ .entrySet ()
189
+ .stream ()
190
+ .collect (toImmutableMap (Map .Entry ::getKey , entry -> ImmutableSet .copyOf (entry .getValue ())));
180
191
}
181
192
182
193
private <T > T parseOpaResponse (FullJsonResponseHandler .JsonResponse <T > response , URI uri )
0 commit comments