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);
}
}
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());
}
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);
}
}
}
});
}
Aggregations