Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over Flux and use result from another Mono to filter the Flux

Mono<MyObj1> mono1 = repository.get(id); // data from reactive mongo
Flux<MyObj2> availabilityInfo = getAvailabilityInfo(); // blocking I/O call

What I am trying to achieve is iterate availabilityInfo and need to apply some conditions using mono data and return MyObj2 (which contains delieveryDate and price related information on that date).

I will try to explain in naive coding as below

 foreach(MyObj2 obj : availabilityInfo){
      if(MyObj1.getX() && obj.getY()){
         return or break;
      }
  }

I want to emit the first element from the flux which satisfies the criteria - MyObj1.getX() && obj.getY(), even if there are other elements further in the flux matching the criteria. The criteria is based on the respone from mono which is a reactive mongo db call and few facts from emitted flux element.

like image 561
akreddy.21 Avatar asked Mar 05 '23 09:03

akreddy.21


1 Answers

Way 1:

 Mono<MyObj2> filteredInfo = mono1.flatMapMany(myObj1 -> 
                                        availabilityInfo.filter(myObj1 -> myObj1.getX() && myObj2.getY()).take(1)).single();

Way 2:

Mono<MyObj2> filteredInfo  = availabilityInfo.collectList().zipWith(mono1).flatMap(tuple -> {
            List<MyObj2> list = tuple.getT1();
            MyObj1 myObj1 = tuple.getT2();
            for(MyObj2 myObj2:list){
                if(myObj1.getX() && myObj2.getY()){
                    return Mono.just(myObj2);
                }
            }
            return Mono.empty();
        });

flatMapMany is used to create a flux by processing a mono. filter takes a predicate, which forms the basis of filtering the flux.

Example:

 Mono<Integer> mono = Mono.just(3);
        Flux<Integer> flux = Flux.just(1, 2, 3, 4, 5, 6);
        //Way 1
        Mono<Integer> res = mono.flatMapMany(i -> flux.filter(j -> j % i == 0).take(1)).single();

        res.subscribe(System.out::println);

        //Way 2
       flux.collectList().zipWith(mono).flatMap(tuple -> {
        List<Integer> list = tuple.getT1();
        int x = tuple.getT2();
        for(int y:list){
            if(y%x==0){
                return Mono.just(x);
            }
        }
        return Mono.empty();
    }).subscribe(System.out::println);

Both ways will give me the output:

3
like image 93
uneq95 Avatar answered Mar 09 '23 01:03

uneq95