Search in sources :

Example 1 with RangeRequest

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());
}
Also used : RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) Set(java.util.Set) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) AbstractPagingIterable(com.palantir.util.paging.AbstractPagingIterable)

Example 2 with RangeRequest

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;
}
Also used : TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) SortedMap(java.util.SortedMap) Value(com.palantir.atlasdb.keyvalue.api.Value)

Example 3 with RangeRequest

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();
}
Also used : NameComponentDescription(com.palantir.atlasdb.table.description.NameComponentDescription) MethodSpec(com.squareup.javapoet.MethodSpec) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest)

Example 4 with RangeRequest

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();
}
Also used : NameComponentDescription(com.palantir.atlasdb.table.description.NameComponentDescription) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) MethodSpec(com.squareup.javapoet.MethodSpec) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest)

Example 5 with RangeRequest

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));
}
Also used : RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) Test(org.junit.Test)

Aggregations

RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)68 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)36 Test (org.junit.Test)35 Value (com.palantir.atlasdb.keyvalue.api.Value)17 TokenBackedBasicResultsPage (com.palantir.util.paging.TokenBackedBasicResultsPage)14 Cell (com.palantir.atlasdb.keyvalue.api.Cell)12 Transaction (com.palantir.atlasdb.transaction.api.Transaction)7 List (java.util.List)5 Set (java.util.Set)5 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)4 StringValue (com.palantir.atlasdb.table.description.test.StringValue)4 AbstractPagingIterable (com.palantir.util.paging.AbstractPagingIterable)4 Map (java.util.Map)4 ColumnSelection (com.palantir.atlasdb.keyvalue.api.ColumnSelection)3 AbstractIterator (com.google.common.collect.AbstractIterator)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Hashing (com.google.common.hash.Hashing)2 NameComponentDescription (com.palantir.atlasdb.table.description.NameComponentDescription)2 BatchingVisitableView (com.palantir.common.base.BatchingVisitableView)2 ClosableIterator (com.palantir.common.base.ClosableIterator)2