Search in sources :

Example 1 with RowResult

use of com.palantir.atlasdb.keyvalue.api.RowResult 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 RowResult

use of com.palantir.atlasdb.keyvalue.api.RowResult 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 RowResult

use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.

the class CachingTransactionTest method testGetRows.

@Test
public void testGetRows() {
    final Set<byte[]> oneRow = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).add(ROW_BYTES).build();
    final ColumnSelection oneColumn = ColumnSelection.create(ImmutableList.of(COL_BYTES));
    final Set<byte[]> noRows = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).build();
    final SortedMap<byte[], RowResult<byte[]>> emptyResults = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR).build();
    final RowResult<byte[]> rowResult = RowResult.of(Cell.create(ROW_BYTES, COL_BYTES), VALUE_BYTES);
    final SortedMap<byte[], RowResult<byte[]>> oneResult = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR).put(ROW_BYTES, rowResult).build();
    mockery.checking(new Expectations() {

        {
            // row result is cached after first call, so second call requests no rows
            oneOf(txn).getRows(table, oneRow, oneColumn);
            will(returnValue(oneResult));
            oneOf(txn).getRows(table, noRows, oneColumn);
            will(returnValue(emptyResults));
        }
    });
    Assert.assertEquals(oneResult, ct.getRows(table, oneRow, oneColumn));
    Assert.assertEquals(oneResult, ct.getRows(table, oneRow, oneColumn));
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Test(org.junit.Test)

Example 4 with RowResult

use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.

the class CachingTransactionTest method testCacheEmptyGets.

@Test
public void testCacheEmptyGets() {
    final Set<byte[]> oneRow = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).add(ROW_BYTES).build();
    final ColumnSelection oneColumn = ColumnSelection.create(ImmutableList.of(COL_BYTES));
    final SortedMap<byte[], RowResult<byte[]>> emptyResults = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR).build();
    mockery.checking(new Expectations() {

        {
            // the cache doesn't actually cache empty results in this case
            // this is probably an oversight, but this has been the behavior for a long time
            oneOf(txn).getRows(table, oneRow, oneColumn);
            will(returnValue(emptyResults));
            oneOf(txn).getRows(table, oneRow, oneColumn);
            will(returnValue(emptyResults));
        }
    });
    Assert.assertEquals(emptyResults, ct.getRows(table, oneRow, oneColumn));
    Assert.assertEquals(emptyResults, ct.getRows(table, oneRow, oneColumn));
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Test(org.junit.Test)

Example 5 with RowResult

use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.

the class SnapshotTransactionTest method readRow.

private RowResult<byte[]> readRow(byte[] defaultRow) {
    Transaction readTransaction = txManager.createNewTransaction();
    SortedMap<byte[], RowResult<byte[]>> allRows = readTransaction.getRows(TABLE, ImmutableSet.of(defaultRow), ColumnSelection.all());
    return allRows.get(defaultRow);
}
Also used : RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Transaction(com.palantir.atlasdb.transaction.api.Transaction)

Aggregations

RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)57 RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)40 Test (org.junit.Test)23 Cell (com.palantir.atlasdb.keyvalue.api.Cell)17 Value (com.palantir.atlasdb.keyvalue.api.Value)16 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)10 TokenBackedBasicResultsPage (com.palantir.util.paging.TokenBackedBasicResultsPage)10 Set (java.util.Set)8 ColumnSelection (com.palantir.atlasdb.keyvalue.api.ColumnSelection)7 Transaction (com.palantir.atlasdb.transaction.api.Transaction)7 SortedMap (java.util.SortedMap)7 Map (java.util.Map)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 Lists (com.google.common.collect.Lists)5 AbstractPagingIterable (com.palantir.util.paging.AbstractPagingIterable)5 Entry (java.util.Map.Entry)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)4 PtBytes (com.palantir.atlasdb.encoding.PtBytes)4 ClosableIterator (com.palantir.common.base.ClosableIterator)4