Example usage for java.util.concurrent ConcurrentNavigableMap entrySet

List of usage examples for java.util.concurrent ConcurrentNavigableMap entrySet

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentNavigableMap entrySet.

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService.java

private <T> ClosableIterator<RowResult<T>> getRangeInternal(String tableName, final RangeRequest range,
        final ResultProducer<T> resultProducer) {
    ConcurrentNavigableMap<Key, byte[]> tableMap = getTableMap(tableName).entries;
    if (range.isReverse()) {
        tableMap = tableMap.descendingMap();
    }//from   w  w w.  ja va 2  s  .  c om
    if (range.getStartInclusive().length != 0) {
        if (range.isReverse()) {
            Cell startCell = Cells.createLargestCellForRow(range.getStartInclusive());
            tableMap = tableMap.tailMap(new Key(startCell, Long.MIN_VALUE));
        } else {
            Cell startCell = Cells.createSmallestCellForRow(range.getStartInclusive());
            tableMap = tableMap.tailMap(new Key(startCell, Long.MIN_VALUE));
        }
    }
    if (range.getEndExclusive().length != 0) {
        if (range.isReverse()) {
            Cell endCell = Cells.createLargestCellForRow(range.getEndExclusive());
            tableMap = tableMap.headMap(new Key(endCell, Long.MAX_VALUE));
        } else {
            Cell endCell = Cells.createSmallestCellForRow(range.getEndExclusive());
            tableMap = tableMap.headMap(new Key(endCell, Long.MAX_VALUE));
        }
    }
    final PeekingIterator<Entry<Key, byte[]>> it = Iterators.peekingIterator(tableMap.entrySet().iterator());
    return ClosableIterators.wrap(new AbstractIterator<RowResult<T>>() {
        @Override
        protected RowResult<T> computeNext() {
            while (true) {
                if (!it.hasNext()) {
                    return endOfData();
                }
                ImmutableSortedMap.Builder<byte[], T> result = ImmutableSortedMap
                        .orderedBy(UnsignedBytes.lexicographicalComparator());
                Key key = it.peek().getKey();
                byte[] row = key.row;
                Iterator<Entry<Key, byte[]>> cellIter = takeCell(it, key);
                collectValueForTimestamp(key.col, cellIter, result, range, resultProducer);

                while (it.hasNext()) {
                    if (!it.peek().getKey().matchesRow(row)) {
                        break;
                    }
                    key = it.peek().getKey();
                    cellIter = takeCell(it, key);
                    collectValueForTimestamp(key.col, cellIter, result, range, resultProducer);
                }
                SortedMap<byte[], T> columns = result.build();
                if (!columns.isEmpty()) {
                    return RowResult.create(row, columns);
                }
            }
        }

    });
}