use of com.palantir.atlasdb.keyvalue.api.RangeRequest in project atlasdb by palantir.
the class DbKvs method getRangeOfTimestamps.
/**
* @param tableRef the name of the table to read from.
* @param rangeRequest the range to load.
* @param timestamp the maximum timestamp to load.
*
* @return Each row that has fewer than maxRangeOfTimestampsBatchSize entries is guaranteed to be returned in a
* single RowResult. If a row has more than maxRangeOfTimestampsBatchSize results, it will potentially be split
* into multiple RowResults, by finishing the current column; see example below. Note that:
* 1) this may cause a RowResult to have more than maxRangeOfTimestampsBatchSize entries
* 2) this may still finish a row, in which case there is going to be only one RowResult for that row.
* It is, furthermore, guaranteed that the columns will be read in ascending order
*
* E.g., for the following table, rangeRequest taking all rows in ascending order,
* maxRangeOfTimestampsBatchSize == 5, and timestamp 10:
*
* a | b | c | d
* ------------------------------------------------
* a | (1, 2, 3) | (1, 2, 3) | (4, 5, 6) | (4, 5, 6)|
* ------------------------------------------------
* b | (1, 3, 5) | - | (1) | - |
* ------------------------------------------------
* c | (1, 2) | (1, 2) | (4, 5, 6) | (4, 5, 6)|
* ------------------------------------------------
* d | (1, 3, 5) | - | (1, 2, 3) | - |
* ------------------------------------------------
* e | (1, 3) | - | - | - |
* ------------------------------------------------
*
* The RowResults will be:
* 1. (a, (a -> 1, 2, 3; b -> 1, 2, 3))
* 2. (a, (c -> 4, 5, 6; d -> 4, 5, 6))
*
* 3. (b, (a -> 1, 3, 5; b -> 1))
*
* 4. (c, (a -> 1, 2, b -> 1, 2; c -> 4, 5, 6))
* 5. (c, (d -> 4, 5, 6))
*
* 6. (d, (a -> 1, 3, 5, c -> 1, 2, 3))
*
* 7. (e, (a -> 1, 3))
*/
@Override
public ClosableIterator<RowResult<Set<Long>>> getRangeOfTimestamps(TableReference tableRef, RangeRequest rangeRequest, long timestamp) {
Iterable<RowResult<Set<Long>>> rows = new AbstractPagingIterable<RowResult<Set<Long>>, TokenBackedBasicResultsPage<RowResult<Set<Long>>, Token>>() {
@Override
protected TokenBackedBasicResultsPage<RowResult<Set<Long>>, Token> getFirstPage() {
return getTimestampsPage(tableRef, rangeRequest, timestamp, maxRangeOfTimestampsBatchSize, Token.INITIAL);
}
@Override
protected TokenBackedBasicResultsPage<RowResult<Set<Long>>, Token> getNextPage(TokenBackedBasicResultsPage<RowResult<Set<Long>>, Token> previous) {
Token token = previous.getTokenForNextPage();
RangeRequest newRange = rangeRequest.getBuilder().startRowInclusive(token.row()).build();
return getTimestampsPage(tableRef, newRange, timestamp, maxRangeOfTimestampsBatchSize, token);
}
};
return ClosableIterators.wrap(rows.iterator());
}
use of com.palantir.atlasdb.keyvalue.api.RangeRequest in project atlasdb by palantir.
the class DbKvsGetRanges method breakUpByBatch.
/**
* This tablehod expects the input to be sorted by rowname ASC for both rowsForBatches and
* cellsByRow.
*/
private Map<RangeRequest, TokenBackedBasicResultsPage<RowResult<Value>, byte[]>> breakUpByBatch(List<RangeRequest> requests, SortedSetMultimap<Integer, byte[]> rowsForBatches, NavigableMap<byte[], SortedMap<byte[], Value>> cellsByRow) {
Map<RangeRequest, TokenBackedBasicResultsPage<RowResult<Value>, byte[]>> ret = Maps.newHashMap();
for (int i = 0; i < requests.size(); i++) {
RangeRequest request = requests.get(i);
if (ret.containsKey(request)) {
continue;
}
SortedSet<byte[]> rowNames = rowsForBatches.get(i);
SortedMap<byte[], SortedMap<byte[], Value>> cellsForBatch = Maps.filterKeys(request.isReverse() ? cellsByRow.descendingMap() : cellsByRow, Predicates.in(rowNames));
validateRowNames(cellsForBatch.keySet(), request.getStartInclusive(), request.getEndExclusive(), request.isReverse());
IterableView<RowResult<Value>> rows = RowResults.viewOfMap(cellsForBatch);
if (!request.getColumnNames().isEmpty()) {
rows = filterColumnSelection(rows, request);
}
if (rowNames.isEmpty()) {
assert rows.isEmpty();
ret.put(request, SimpleTokenBackedResultsPage.create(request.getEndExclusive(), rows, false));
} else {
byte[] last = rowNames.last();
if (request.isReverse()) {
last = rowNames.first();
}
if (RangeRequests.isTerminalRow(request.isReverse(), last)) {
ret.put(request, SimpleTokenBackedResultsPage.create(last, rows, false));
} else {
// If rowNames isn't a whole batch we know we don't have any more results.
boolean hasMore = request.getBatchHint() == null || request.getBatchHint() <= rowNames.size();
byte[] nextStartRow = RangeRequests.getNextStartRow(request.isReverse(), last);
ret.put(request, SimpleTokenBackedResultsPage.create(nextStartRow, rows, hasMore));
}
}
}
return ret;
}
use of com.palantir.atlasdb.keyvalue.api.RangeRequest in project atlasdb by palantir.
the class TableClassRendererV2 method renderNamedGetRangeStartEnd.
private MethodSpec renderNamedGetRangeStartEnd(NamedColumnDescription col) {
Preconditions.checkArgument(tableMetadata.getRowMetadata().getRowParts().size() == 1);
NameComponentDescription rowComponent = tableMetadata.getRowMetadata().getRowParts().get(0);
MethodSpec.Builder getterBuilder = MethodSpec.methodBuilder("getSmallRowRange" + VarName(col)).addModifiers(Modifier.PUBLIC).addJavadoc("Returns a mapping from all the row keys in a range to their value at column $L\n" + "(if that column exists for the row-key). As the $L values are all loaded in memory,\n" + "do not use for large amounts of data. The order of results is preserved in the map.", VarName(col), VarName(col)).addParameter(rowComponent.getType().getJavaClass(), "startInclusive").addParameter(rowComponent.getType().getJavaClass(), "endExclusive").returns(ParameterizedTypeName.get(ClassName.get(LinkedHashMap.class), ClassName.get(rowComponent.getType().getJavaClass()), ClassName.get(getColumnClassForGenericTypeParameter(col))));
getterBuilder.addStatement("$T rangeRequest = $T.builder()\n" + ".startRowInclusive($T.of(startInclusive).persistToBytes())\n" + ".endRowExclusive($T.of(endExclusive).persistToBytes())\n" + ".build()", RangeRequest.class, RangeRequest.class, rowType, rowType).addStatement("return getSmallRowRange$L(rangeRequest)", VarName(col));
return getterBuilder.build();
}
use of com.palantir.atlasdb.keyvalue.api.RangeRequest in project atlasdb by palantir.
the class TableClassRendererV2 method renderNamedGetRangeColumn.
private MethodSpec renderNamedGetRangeColumn(NamedColumnDescription col) {
Preconditions.checkArgument(tableMetadata.getRowMetadata().getRowParts().size() == 1);
NameComponentDescription rowComponent = tableMetadata.getRowMetadata().getRowParts().get(0);
MethodSpec.Builder getterBuilder = MethodSpec.methodBuilder("getSmallRowRange" + VarName(col)).addModifiers(Modifier.PUBLIC).addJavadoc("Returns a mapping from all the row keys in a rangeRequest to their value at column $L\n" + "(if that column exists for the row-key). As the $L values are all loaded in memory,\n" + "do not use for large amounts of data. The order of results is preserved in the map.", VarName(col), VarName(col)).addParameter(RangeRequest.class, "rangeRequest").returns(ParameterizedTypeName.get(ClassName.get(LinkedHashMap.class), ClassName.get(rowComponent.getType().getJavaClass()), ClassName.get(getColumnClassForGenericTypeParameter(col))));
getterBuilder.addStatement("$T colSelection =\n" + "$T.create($T.of($T.toCachedBytes($L)))", ColumnSelection.class, ColumnSelection.class, ImmutableList.class, PtBytes.class, ColumnRenderers.short_name(col)).addStatement("rangeRequest = rangeRequest.getBuilder().retainColumns(colSelection).build()").addStatement("$T.checkArgument(rangeRequest.getColumnNames().size() <= 1,\n$S)", Preconditions.class, "Must not request columns other than " + VarName(col) + ".").addCode("\n").addStatement("$T<$T, $T> resultsMap = new $T<>()", LinkedHashMap.class, rowComponent.getType().getJavaClass(), getColumnClassForGenericTypeParameter(col), LinkedHashMap.class).addStatement("$T.of(t.getRange(tableRef, rangeRequest))\n" + ".immutableCopy().forEach(entry -> {\n" + " $T resultEntry =\n " + " $T.of(entry);\n" + " resultsMap.put(resultEntry.getRowName().get$L(), resultEntry.get$L());\n" + "})", BatchingVisitableView.class, rowResultType, rowResultType, CamelCase(rowComponent.getComponentName()), VarName(col)).addStatement("return resultsMap");
return getterBuilder.build();
}
use of com.palantir.atlasdb.keyvalue.api.RangeRequest in project atlasdb by palantir.
the class WhereClausesTest method whereClausesNoColumns.
@Test
public void whereClausesNoColumns() {
RangeRequest request = RangeRequest.builder().startRowInclusive(START).endRowExclusive(END).build();
WhereClauses whereClauses = WhereClauses.create("i", request);
List<String> expectedClauses = ImmutableList.of("i.row_name >= ?", "i.row_name < ?");
assertEquals(whereClauses.getClauses(), expectedClauses);
checkWhereArguments(whereClauses, ImmutableList.of(START, END));
}
Aggregations