34
34
import java .util .concurrent .atomic .AtomicInteger ;
35
35
import java .util .function .Function ;
36
36
import java .util .function .UnaryOperator ;
37
+ import java .util .stream .Stream ;
37
38
38
39
import static me .bechberger .ebpf .bcc .PanamaUtil .*;
39
40
@@ -191,18 +192,39 @@ public void rawRemove(Object key) {
191
192
}
192
193
}
193
194
195
+ /**
196
+ * Only use this method if batch functions are not available.
197
+ */
198
+ private Stream <K > slowKeyStream () {
199
+ assert !PanamaUtil .hasBCCBatchFunctions ();
200
+ return keySet ().stream ();
201
+ }
202
+
194
203
@ NotNull
195
204
public Set <K > keySet () {
205
+ if (!PanamaUtil .hasBCCBatchFunctions ()) {
206
+ // very inefficient, but some bcc versions do not support batch functions
207
+ return slowKeyStream ().collect (java .util .stream .Collectors .toSet ());
208
+ }
196
209
return items_lookup_batch ().stream ().map (Map .Entry ::getKey ).collect (java .util .stream .Collectors .toSet ());
197
210
}
198
211
199
212
@ NotNull
200
213
public Collection <V > values () {
214
+ if (!PanamaUtil .hasBCCBatchFunctions ()) {
215
+ // very inefficient, but some bcc versions do not support batch functions
216
+ return slowKeyStream ().map (this ::get ).collect (java .util .stream .Collectors .toList ());
217
+ }
201
218
return items_lookup_batch ().stream ().map (Map .Entry ::getValue ).collect (java .util .stream .Collectors .toList ());
202
219
}
203
220
204
221
@ NotNull
205
222
public Set <Map .Entry <K , V >> entrySet () {
223
+ if (!PanamaUtil .hasBCCBatchFunctions ()) {
224
+ // very inefficient, but some bcc versions do not support batch functions
225
+ return slowKeyStream ().map (k -> new AbstractMap .SimpleEntry <>(k , get (k )))
226
+ .collect (java .util .stream .Collectors .toSet ());
227
+ }
206
228
return new HashSet <>(items_lookup_batch ());
207
229
}
208
230
@@ -297,6 +319,11 @@ private int sanity_check_keys_values(@Nullable MemorySegment keys, @Nullable Mem
297
319
* @return key-value pairs
298
320
*/
299
321
public List <Map .Entry <K , V >> items_lookup_batch () {
322
+ if (!PanamaUtil .hasBCCBatchFunctions ()) {
323
+ // very inefficient, but some bcc versions do not support batch functions
324
+ return slowKeyStream ().map (k -> new AbstractMap .SimpleEntry <>(k , get (k )))
325
+ .collect (java .util .stream .Collectors .toList ());
326
+ }
300
327
return items_lookup_and_optionally_delete_batch (false );
301
328
}
302
329
@@ -317,7 +344,8 @@ private ResultAndErr<Integer> bpf_delete_batch(Arena arena, int map_fd, @Nullabl
317
344
* deletes all the related keys-values.
318
345
* If keys is None (default) then it deletes all entries.
319
346
*/
320
- public void items_delete_batch (Arena arena , @ Nullable MemorySegment keys ) {
347
+ private void items_delete_batch (Arena arena , @ Nullable MemorySegment keys ) {
348
+ assert PanamaUtil .hasBCCBatchFunctions ();
321
349
if (keys != null ) {
322
350
var count = sanity_check_keys_values (keys , null );
323
351
var countRef = arena .allocate (ValueLayout .JAVA_LONG );
@@ -338,6 +366,15 @@ public void items_delete_batch(Arena arena, @Nullable MemorySegment keys) {
338
366
* @param keys keys array to delete. If an array of keys is given then it deletes all
339
367
*/
340
368
public void delete_keys (@ Nullable List <K > keys ) {
369
+ if (!PanamaUtil .hasBCCBatchFunctions ()) {
370
+ // very inefficient, but some bcc versions do not support batch functions
371
+ if (keys == null ) {
372
+ zero ();
373
+ return ;
374
+ }
375
+ keys .forEach (this ::rawRemove );
376
+ return ;
377
+ }
341
378
try (var arena = Arena .ofConfined ()) {
342
379
if (keys != null ) {
343
380
var allocated = alloc_key_values (arena , true , false , keys .size ());
@@ -367,7 +404,7 @@ private static ResultAndErr<Integer> bpf_update_batch(Arena arena, int map_fd, @
367
404
* @param keys keys array to update
368
405
* @param values values array to update
369
406
*/
370
- public void items_update_batch (Arena arena , @ Nullable MemorySegment keys , @ Nullable MemorySegment values ) {
407
+ private void items_update_batch (Arena arena , @ Nullable MemorySegment keys , @ Nullable MemorySegment values ) {
371
408
var count = sanity_check_keys_values (keys , values );
372
409
var countRef = arena .allocate (ValueLayout .JAVA_LONG );
373
410
countRef .set (ValueLayout .JAVA_LONG , 0 , count );
@@ -380,7 +417,7 @@ public void items_update_batch(Arena arena, @Nullable MemorySegment keys, @Nulla
380
417
/**
381
418
* Look up and delete all the key-value pairs in the map.
382
419
*/
383
- public List <Map .Entry <K , V >> items_lookup_and_delete_batch () {
420
+ private List <Map .Entry <K , V >> items_lookup_and_delete_batch () {
384
421
return items_lookup_and_optionally_delete_batch (true );
385
422
}
386
423
0 commit comments