common/src/main/java/org/flowant/website/repository/RelationshipService.java
public static Mono<Collection<ReputationCounter>> findPopularSubItems(int cntPopular, UUID containerId, Class<?> reputationCls) {

        // prevent out of memory
        final int MAX_HEAP_SIZE = 1000;

        log.warn("Be careful, it's very heavy IO bound function. do not use in production.");

        ReputationCounterRepository<?> repo = repoCounter.get(reputationCls);

        int maxSize = Math.min(cntPopular + 1, MAX_HEAP_SIZE);

        PriorityQueue<ReputationCounter> minHeap = new PriorityQueue<>(maxSize + 1,
                Comparator.comparing(ReputationCounter::getLiked));

        // TODO restrict amount of IO
        return repo.findAllByIdCidContainerId(containerId)
                .doOnNext(r -> {
                    minHeap.add(r); // log n
                    // This way is faster than using MinMaxPriorityQueue with max size although remove() is used.
                    if (minHeap.size() == maxSize) {
                        minHeap.remove(); // log n
                    }
                }).then(Mono.just(minHeap));
    }
// Be careful, it's very heavy IO bound function.
Be careful, it's very heavy IO bound function.
Be careful it ' s very heavy IO bound function .
public static Mono<Collection<ReputationCounter>> findPopularSubItems(int cntPopular, UUID containerId, Class<?> reputationCls) {
        final int MAX_HEAP_SIZE = 1000;
        log.warn("Be careful, it's very heavy IO bound function. do not use in production.");
        ReputationCounterRepository<?> repo = repoCounter.get(reputationCls);
        int maxSize = Math.min(cntPopular + 1, MAX_HEAP_SIZE);
        PriorityQueue<ReputationCounter> minHeap = new PriorityQueue<>(maxSize + 1,
                Comparator.comparing(ReputationCounter::getLiked));
        return repo.findAllByIdCidContainerId(containerId)
                .doOnNext(r -> {
                    minHeap.add(r); 
                    if (minHeap.size() == maxSize) {
                        minHeap.remove(); 
                    }
                }).then(Mono.just(minHeap));
    }
public static Mono < Collection < ReputationCounter > > findPopularSubItems ( int cntPopular , UUID containerId , Class < ? > reputationCls ) { final int MAX_HEAP_SIZE = 1000 ; log . warn ( "Be careful, it's very heavy IO bound function. do not use in production." ) ; ReputationCounterRepository < ? > repo = repoCounter . get ( reputationCls ) ; int maxSize = Math . min ( cntPopular + 1 , MAX_HEAP_SIZE ) ; PriorityQueue < ReputationCounter > minHeap = new PriorityQueue < > ( maxSize + 1 , Comparator . comparing ( ReputationCounter :: getLiked ) ) ; return repo . findAllByIdCidContainerId ( containerId ) . doOnNext ( r -> { minHeap . add ( r ) ; if ( minHeap . size ( ) == maxSize ) { minHeap . remove ( ) ; } } ) . then ( Mono . just ( minHeap ) ) ; }
Be careful, it's very heavy IO bound function.
Be careful it ' s very heavy IO bound function .
// prevent out of memory // TODO restrict amount of IO // log n // This way is faster than using MinMaxPriorityQueue with max size although remove() is used. // log n