Example usage for com.google.common.collect Range openClosed

List of usage examples for com.google.common.collect Range openClosed

Introduction

In this page you can find the example usage for com.google.common.collect Range openClosed.

Prototype

public static <C extends Comparable<?>> Range<C> openClosed(C lower, C upper) 

Source Link

Document

Returns a range that contains all values strictly greater than lower and less than or equal to upper .

Usage

From source file:org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.java

/**
 *
 * @param newMarkDeletePosition/*  w  w w .ja  va  2 s. c  o  m*/
 *            the new acknowledged position
 * @return the previous acknowledged position
 */
PositionImpl setAcknowledgedPosition(PositionImpl newMarkDeletePosition) {
    if (newMarkDeletePosition.compareTo(markDeletePosition) < 0) {
        throw new IllegalArgumentException("Mark deleting an already mark-deleted position");
    }

    if (readPosition.compareTo(newMarkDeletePosition) <= 0) {
        // If the position that is mark-deleted is past the read position, it
        // means that the client has skipped some entries. We need to move
        // read position forward
        PositionImpl oldReadPosition = readPosition;
        readPosition = ledger.getNextValidPosition(newMarkDeletePosition);

        if (log.isDebugEnabled()) {
            log.debug("Moved read position from: {} to: {}", oldReadPosition, readPosition);
        }
    }

    PositionImpl oldMarkDeletePosition = markDeletePosition;

    if (!newMarkDeletePosition.equals(oldMarkDeletePosition)) {
        long skippedEntries = 0;
        if (newMarkDeletePosition.getLedgerId() == oldMarkDeletePosition.getLedgerId()
                && newMarkDeletePosition.getEntryId() == oldMarkDeletePosition.getEntryId() + 1) {
            // Mark-deleting the position next to current one
            skippedEntries = individualDeletedMessages.contains(newMarkDeletePosition) ? 0 : 1;
        } else {
            skippedEntries = getNumberOfEntries(Range.openClosed(oldMarkDeletePosition, newMarkDeletePosition));
        }
        PositionImpl positionAfterNewMarkDelete = ledger.getNextValidPosition(newMarkDeletePosition);
        if (individualDeletedMessages.contains(positionAfterNewMarkDelete)) {
            Range<PositionImpl> rangeToBeMarkDeleted = individualDeletedMessages
                    .rangeContaining(positionAfterNewMarkDelete);
            newMarkDeletePosition = rangeToBeMarkDeleted.upperEndpoint();
        }

        if (log.isDebugEnabled()) {
            log.debug("Moved ack position from: {} to: {} -- skipped: {}", oldMarkDeletePosition,
                    newMarkDeletePosition, skippedEntries);
        }
        messagesConsumedCounter += skippedEntries;
    }

    // markDelete-position and clear out deletedMsgSet
    markDeletePosition = PositionImpl.get(newMarkDeletePosition);
    individualDeletedMessages.remove(Range.atMost(markDeletePosition));

    return newMarkDeletePosition;
}

From source file:com.oodrive.nuage.dtx.DtxManager.java

/**
 * Gets a map of target nodes and last tx IDs for synchronization.
 * /*  www  .j a  v  a 2s  .  c om*/
 * @param resId
 *            the target resource manager's ID
 * @param lowerLimit
 *            the lower limit above which to return last transaction IDs
 * @return a possibly empty {@link Map} with node IDs mapped to last complete transaction IDs
 */
final Map<DtxNode, Long> getClusterMapInfo(final UUID resId, final Long lowerLimit) {
    try {
        mapLock.readLock().lockInterruptibly();
    } catch (final InterruptedException e) {
        throw new IllegalStateException("Interrupted on waiting for lock", e);
    }
    try {
        final Map<DtxNode, Long> resUpdateMap = Maps.filterValues(clusterUpdateMap.row(resId),
                Range.openClosed(lowerLimit, Long.valueOf(Long.MAX_VALUE)));
        // returns a defensive copy to avoid any concurrent modification exceptions
        return new HashMap<DtxNode, Long>(resUpdateMap);
    } finally {
        mapLock.readLock().unlock();
    }
}

From source file:org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.java

@Override
public void asyncDelete(Position pos, final AsyncCallbacks.DeleteCallback callback, Object ctx) {
    checkArgument(pos instanceof PositionImpl);

    if (STATE_UPDATER.get(this) == State.Closed) {
        callback.deleteFailed(new ManagedLedgerException("Cursor was already closed"), ctx);
        return;/*w w  w. ja v  a2  s  .  c  om*/
    }

    PositionImpl position = (PositionImpl) pos;

    PositionImpl previousPosition = ledger.getPreviousPosition(position);
    PositionImpl newMarkDeletePosition = null;

    lock.writeLock().lock();

    try {
        if (log.isDebugEnabled()) {
            log.debug(
                    "[{}] [{}] Deleting single message at {}. Current status: {} - md-position: {}  - previous-position: {}",
                    ledger.getName(), name, pos, individualDeletedMessages, markDeletePosition,
                    previousPosition);
        }

        if (individualDeletedMessages.contains(position) || position.compareTo(markDeletePosition) <= 0) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] Position was already deleted {}", ledger.getName(), name, position);
            }
            callback.deleteComplete(ctx);
            return;
        }

        if (previousPosition.compareTo(markDeletePosition) == 0 && individualDeletedMessages.isEmpty()) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Immediately mark-delete to position {}", ledger.getName(), name, position);
            }

            newMarkDeletePosition = position;
        } else {
            // Add a range (prev, pos] to the set. Adding the previous entry as an open limit to the range will make
            // the RangeSet recognize the "continuity" between adjacent Positions
            individualDeletedMessages.add(Range.openClosed(previousPosition, position));
            ++messagesConsumedCounter;

            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] Individually deleted messages: {}", ledger.getName(), name,
                        individualDeletedMessages);
            }

            // If the lower bound of the range set is the current mark delete position, then we can trigger a new
            // mark
            // delete to the upper bound of the first range segment
            Range<PositionImpl> range = individualDeletedMessages.asRanges().iterator().next();

            // Bug:7062188 - markDeletePosition can sometimes be stuck at the beginning of an empty ledger.
            // If the lowerBound is ahead of MarkDelete, verify if there are any entries in-between
            if (range.lowerEndpoint().compareTo(markDeletePosition) <= 0 || ledger
                    .getNumberOfEntries(Range.openClosed(markDeletePosition, range.lowerEndpoint())) <= 0) {

                if (log.isDebugEnabled()) {
                    log.debug("[{}] Found a position range to mark delete for cursor {}: {} ", ledger.getName(),
                            name, range);
                }

                newMarkDeletePosition = range.upperEndpoint();
            }
        }

        if (newMarkDeletePosition != null) {
            newMarkDeletePosition = setAcknowledgedPosition(newMarkDeletePosition);
        } else {
            newMarkDeletePosition = markDeletePosition;
        }
    } catch (Exception e) {
        log.warn("[{}] [{}] Error while updating individualDeletedMessages [{}]", ledger.getName(), name,
                e.getMessage(), e);
        callback.deleteFailed(new ManagedLedgerException(e), ctx);
        return;
    } finally {
        lock.writeLock().unlock();
    }

    // Apply rate limiting to mark-delete operations
    if (markDeleteLimiter != null && !markDeleteLimiter.tryAcquire()) {
        callback.deleteComplete(ctx);
        return;
    }

    try {
        internalAsyncMarkDelete(newMarkDeletePosition, new MarkDeleteCallback() {
            @Override
            public void markDeleteComplete(Object ctx) {
                callback.deleteComplete(ctx);
            }

            @Override
            public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                callback.deleteFailed(exception, ctx);
            }

        }, ctx);

    } catch (Exception e) {
        log.warn("[{}] [{}] Error doing asyncDelete [{}]", ledger.getName(), name, e.getMessage(), e);
        if (log.isDebugEnabled()) {
            log.debug("[{}] Consumer {} cursor asyncDelete error, counters: consumed {} mdPos {} rdPos {}",
                    ledger.getName(), name, messagesConsumedCounter, markDeletePosition, readPosition);
        }
        callback.deleteFailed(new ManagedLedgerException(e), ctx);
    }
}