private static void deduplicate()

in lagerta-core/src/main/java/com/epam/lagerta/subscriber/lead/ReadTransactions.java [216:251]


    private static void deduplicate(Set<UUID> lostReaders, List<ReaderTxScope> transactions,
                                    Consumer<ReaderTxScope> onRemove) {
        if (transactions.isEmpty()) {
            return;
        }
        int level = UNKNOWN;
        int count = 0;
        long id = INITIAL_READ_ID;
        for (ListIterator<ReaderTxScope> it = transactions.listIterator(); it.hasNext(); ) {
            ReaderTxScope tx = it.next();
            long thisId = tx.getTransactionId();
            if (thisId > id) {
                id = thisId;
                level = UNKNOWN;
                count = 0;
            }
            int thisLevel = getLevel(tx, lostReaders);
            if (thisLevel < level) {
                it.remove();
                onRemove.accept(tx);
            } else {
                if (thisLevel > level) {
                    level = thisLevel;
                    it.previous();
                    while (count > 0) {
                        ReaderTxScope previous = it.previous();
                        it.remove();
                        onRemove.accept(previous);
                        count--;
                    }
                    it.next();
                }
                count++;
            }
        }
    }