Search in sources :

Example 1 with ClosableIterator

use of com.palantir.common.base.ClosableIterator in project atlasdb by palantir.

the class GetCandidateCellsForSweepingShim method getCandidateCellsForSweeping.

public ClosableIterator<List<CandidateCellForSweeping>> getCandidateCellsForSweeping(TableReference tableRef, CandidateCellForSweepingRequest request) {
    RangeRequest range = RangeRequest.builder().startRowInclusive(request.startRowInclusive()).batchHint(request.batchSizeHint().orElse(AtlasDbConstants.DEFAULT_SWEEP_CANDIDATE_BATCH_HINT)).build();
    try (ReleasableCloseable<ClosableIterator<RowResult<Value>>> valueResults = new ReleasableCloseable<>(getValues(tableRef, range, request.maxTimestampExclusive(), request.shouldCheckIfLatestValueIsEmpty()));
        ReleasableCloseable<ClosableIterator<RowResult<Set<Long>>>> tsResults = new ReleasableCloseable<>(keyValueService.getRangeOfTimestamps(tableRef, range, request.maxTimestampExclusive()))) {
        PeekingIterator<RowResult<Value>> peekingValues = Iterators.peekingIterator(valueResults.get());
        Iterator<List<RowResult<Set<Long>>>> tsBatches = Iterators.partition(tsResults.get(), range.getBatchHint());
        Iterator<List<CandidateCellForSweeping>> candidates = Iterators.transform(tsBatches, tsBatch -> {
            List<CandidateCellForSweeping> candidateBatch = Lists.newArrayList();
            for (RowResult<Set<Long>> rr : tsBatch) {
                for (Map.Entry<byte[], Set<Long>> e : rr.getColumns().entrySet()) {
                    byte[] colName = e.getKey();
                    List<Long> sortedTimestamps = e.getValue().stream().filter(request::shouldSweep).sorted().collect(Collectors.toList());
                    Cell cell = Cell.create(rr.getRowName(), colName);
                    boolean latestValEmpty = isLatestValueEmpty(cell, peekingValues);
                    candidateBatch.add(ImmutableCandidateCellForSweeping.builder().cell(cell).sortedTimestamps(sortedTimestamps).isLatestValueEmpty(latestValEmpty).build());
                }
            }
            return candidateBatch;
        });
        Closer closer = createCloserAndRelease(valueResults, tsResults);
        return ClosableIterators.wrap(candidates, closer);
    }
}
Also used : Closer(com.google.common.io.Closer) Set(java.util.Set) ClosableIterator(com.palantir.common.base.ClosableIterator) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) CandidateCellForSweeping(com.palantir.atlasdb.keyvalue.api.CandidateCellForSweeping) ImmutableCandidateCellForSweeping(com.palantir.atlasdb.keyvalue.api.ImmutableCandidateCellForSweeping) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) Value(com.palantir.atlasdb.keyvalue.api.Value) List(java.util.List) Map(java.util.Map) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 2 with ClosableIterator

use of com.palantir.common.base.ClosableIterator in project atlasdb by palantir.

the class DbKvs method getTimestampsPageInternal.

private TokenBackedBasicResultsPage<RowResult<Set<Long>>, Token> getTimestampsPageInternal(DbReadTable table, RangeRequest range, long timestamp, long batchSize, Token token) {
    Set<byte[]> rows = Sets.newHashSet();
    int maxRows = getMaxRowsFromBatchHint(range.getBatchHint());
    try (ClosableIterator<AgnosticLightResultRow> rangeResults = table.getRange(range, timestamp, maxRows)) {
        while (rows.size() < maxRows && rangeResults.hasNext()) {
            byte[] rowName = rangeResults.next().getBytes(ROW);
            if (rowName != null) {
                rows.add(rowName);
            }
        }
        if (rows.isEmpty()) {
            return SimpleTokenBackedResultsPage.create(null, ImmutableList.<RowResult<Set<Long>>>of(), false);
        }
    }
    ColumnSelection cols = range.getColumnNames().isEmpty() ? ColumnSelection.all() : ColumnSelection.create(range.getColumnNames());
    TimestampsByCellResultWithToken result = getTimestampsByCell(table, rows, cols, timestamp, batchSize, range.isReverse(), token);
    NavigableMap<byte[], SortedMap<byte[], Set<Long>>> cellsByRow = Cells.breakCellsUpByRow(Multimaps.asMap(result.entries));
    if (range.isReverse()) {
        cellsByRow = cellsByRow.descendingMap();
    }
    List<RowResult<Set<Long>>> finalResults = cellsByRow.entrySet().stream().map(entry -> RowResult.create(entry.getKey(), entry.getValue())).collect(Collectors.toList());
    return SimpleTokenBackedResultsPage.create(result.getToken(), finalResults, result.mayHaveMoreResults());
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ImmutablePostgresDdlConfig(com.palantir.atlasdb.keyvalue.dbkvs.ImmutablePostgresDdlConfig) Arrays(java.util.Arrays) PalantirSqlException(com.palantir.exception.PalantirSqlException) Throwables(com.palantir.common.base.Throwables) CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) OracleDdlConfig(com.palantir.atlasdb.keyvalue.dbkvs.OracleDdlConfig) Collections2(com.google.common.collect.Collections2) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) CandidateCellForSweeping(com.palantir.atlasdb.keyvalue.api.CandidateCellForSweeping) CellTsPairLoader(com.palantir.atlasdb.keyvalue.dbkvs.impl.sweep.CellTsPairLoader) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) DdlConfig(com.palantir.atlasdb.keyvalue.dbkvs.DdlConfig) AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) NamedThreadFactory(com.palantir.common.concurrent.NamedThreadFactory) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Set(java.util.Set) AgnosticLightResultRow(com.palantir.nexus.db.sql.AgnosticLightResultRow) OracleGetRange(com.palantir.atlasdb.keyvalue.dbkvs.impl.oracle.OracleGetRange) CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) Cells(com.palantir.atlasdb.keyvalue.impl.Cells) AbstractKeyValueService(com.palantir.atlasdb.keyvalue.impl.AbstractKeyValueService) ColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.ColumnRangeSelection) Iterables(com.google.common.collect.Iterables) ParallelTaskRunner(com.palantir.atlasdb.keyvalue.dbkvs.impl.batch.ParallelTaskRunner) BatchingTaskRunner(com.palantir.atlasdb.keyvalue.dbkvs.impl.batch.BatchingTaskRunner) DbKvsPartitioners(com.palantir.atlasdb.keyvalue.dbkvs.util.DbKvsPartitioners) DbkvsVersionException(com.palantir.atlasdb.keyvalue.dbkvs.impl.postgres.DbkvsVersionException) PostgresPrefixedTableNames(com.palantir.atlasdb.keyvalue.dbkvs.impl.postgres.PostgresPrefixedTableNames) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Multimaps(com.google.common.collect.Multimaps) LinkedHashMap(java.util.LinkedHashMap) ClusterAvailabilityStatus(com.palantir.atlasdb.keyvalue.api.ClusterAvailabilityStatus) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) Atomics(com.google.common.util.concurrent.Atomics) PostgresCellTsPageLoader(com.palantir.atlasdb.keyvalue.dbkvs.impl.postgres.PostgresCellTsPageLoader) Sha256Hash(com.palantir.util.crypto.Sha256Hash) Nullable(javax.annotation.Nullable) Functions(com.google.common.base.Functions) AbstractIterator(com.google.common.collect.AbstractIterator) DbKvsGetRange(com.palantir.atlasdb.keyvalue.dbkvs.impl.ranges.DbKvsGetRange) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) DbKvsGetCandidateCellsForSweeping(com.palantir.atlasdb.keyvalue.dbkvs.impl.sweep.DbKvsGetCandidateCellsForSweeping) PostgresDdlConfig(com.palantir.atlasdb.keyvalue.dbkvs.PostgresDdlConfig) Preconditions(com.google.common.base.Preconditions) LocalRowColumnRangeIterator(com.palantir.atlasdb.keyvalue.impl.LocalRowColumnRangeIterator) RangeRequests(com.palantir.atlasdb.keyvalue.api.RangeRequests) OracleCellTsPageLoader(com.palantir.atlasdb.keyvalue.dbkvs.impl.oracle.OracleCellTsPageLoader) Connection(java.sql.Connection) ImmediateSingleBatchTaskRunner(com.palantir.atlasdb.keyvalue.dbkvs.impl.batch.ImmediateSingleBatchTaskRunner) ListIterator(java.util.ListIterator) BatchColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection) ClosableIterator(com.palantir.common.base.ClosableIterator) LoggerFactory(org.slf4j.LoggerFactory) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) BatchingStrategies(com.palantir.atlasdb.keyvalue.dbkvs.impl.batch.BatchingStrategies) PTExecutors(com.palantir.common.concurrent.PTExecutors) DbKvsGetRanges(com.palantir.atlasdb.keyvalue.dbkvs.impl.ranges.DbKvsGetRanges) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractPagingIterable(com.palantir.util.paging.AbstractPagingIterable) Collection(java.util.Collection) AgnosticResultRow(com.palantir.nexus.db.sql.AgnosticResultRow) NavigableMap(java.util.NavigableMap) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) Objects(java.util.Objects) List(java.util.List) CandidateCellForSweepingRequest(com.palantir.atlasdb.keyvalue.api.CandidateCellForSweepingRequest) DbKeyValueServiceConfig(com.palantir.atlasdb.keyvalue.dbkvs.DbKeyValueServiceConfig) Entry(java.util.Map.Entry) Optional(java.util.Optional) OracleOverflowValueLoader(com.palantir.atlasdb.keyvalue.dbkvs.impl.oracle.OracleOverflowValueLoader) SortedMap(java.util.SortedMap) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Stopwatch(com.google.common.base.Stopwatch) Output(com.palantir.common.annotation.Output) AccumulatorStrategies(com.palantir.atlasdb.keyvalue.dbkvs.impl.batch.AccumulatorStrategies) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) AtomicReference(java.util.concurrent.atomic.AtomicReference) OracleTableNameGetter(com.palantir.atlasdb.keyvalue.dbkvs.OracleTableNameGetter) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) Iterators(com.google.common.collect.Iterators) ImmutableList(com.google.common.collect.ImmutableList) Suppliers(com.google.common.base.Suppliers) Nonnull(javax.annotation.Nonnull) ClosableIterators(com.palantir.common.base.ClosableIterators) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) H2DdlConfig(com.palantir.atlasdb.keyvalue.dbkvs.H2DdlConfig) Iterator(java.util.Iterator) Value(com.palantir.atlasdb.keyvalue.api.Value) IterablePartitioner(com.palantir.atlasdb.keyvalue.impl.IterablePartitioner) TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) PostgresGetRange(com.palantir.atlasdb.keyvalue.dbkvs.impl.postgres.PostgresGetRange) Ordering(com.google.common.collect.Ordering) SimpleTokenBackedResultsPage(com.palantir.util.paging.SimpleTokenBackedResultsPage) Collections(java.util.Collections) AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) Set(java.util.Set) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) SortedMap(java.util.SortedMap) AgnosticLightResultRow(com.palantir.nexus.db.sql.AgnosticLightResultRow)

Example 3 with ClosableIterator

use of com.palantir.common.base.ClosableIterator in project atlasdb by palantir.

the class InMemoryKeyValueService method getRangeInternal.

private <T> ClosableIterator<RowResult<T>> getRangeInternal(TableReference tableRef, final RangeRequest range, final ResultProducer<T> resultProducer) {
    ConcurrentNavigableMap<Key, byte[]> tableMap = getTableMap(tableRef).entries;
    if (range.isReverse()) {
        tableMap = tableMap.descendingMap();
    }
    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);
                }
            }
        }
    });
}
Also used : RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Entry(java.util.Map.Entry) SortedMap(java.util.SortedMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ClosableIterator(com.palantir.common.base.ClosableIterator) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) PeekingIterator(com.google.common.collect.PeekingIterator) Iterator(java.util.Iterator) AbstractIterator(com.google.common.collect.AbstractIterator) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Aggregations

Cell (com.palantir.atlasdb.keyvalue.api.Cell)3 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)3 ClosableIterator (com.palantir.common.base.ClosableIterator)3 AbstractIterator (com.google.common.collect.AbstractIterator)2 CandidateCellForSweeping (com.palantir.atlasdb.keyvalue.api.CandidateCellForSweeping)2 RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)2 RowColumnRangeIterator (com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator)2 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2 SortedMap (java.util.SortedMap)2 Function (com.google.common.base.Function)1 Functions (com.google.common.base.Functions)1 Preconditions (com.google.common.base.Preconditions)1 Stopwatch (com.google.common.base.Stopwatch)1 Suppliers (com.google.common.base.Suppliers)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Collections2 (com.google.common.collect.Collections2)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1