Search in sources :

Example 1 with ColumnSelection

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

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

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

the class CqlKeyValueService method getRangeWithPageCreator.

public <T> ClosableIterator<RowResult<T>> getRangeWithPageCreator(TableReference tableRef, RangeRequest rangeRequest, long timestamp, com.datastax.driver.core.ConsistencyLevel consistency, Supplier<ResultsExtractor<T>> resultsExtractor) {
    if (rangeRequest.isReverse()) {
        throw new UnsupportedOperationException();
    }
    if (rangeRequest.isEmptyRange()) {
        return ClosableIterators.wrap(ImmutableList.<RowResult<T>>of().iterator());
    }
    final int batchHint = rangeRequest.getBatchHint() == null ? 100 : rangeRequest.getBatchHint();
    final ColumnSelection selection = rangeRequest.getColumnNames().isEmpty() ? ColumnSelection.all() : ColumnSelection.create(rangeRequest.getColumnNames());
    final byte[] endExclusive = rangeRequest.getEndExclusive();
    final StringBuilder bindQuery = new StringBuilder();
    bindQuery.append("SELECT * FROM " + getFullTableName(tableRef) + " WHERE token(" + fieldNameProvider.row() + ") >= token(?) ");
    if (endExclusive.length > 0) {
        bindQuery.append("AND token(" + fieldNameProvider.row() + ") < token(?) ");
    }
    bindQuery.append("LIMIT " + batchHint);
    final String getLastRowQuery = "SELECT * FROM " + getFullTableName(tableRef) + " WHERE " + fieldNameProvider.row() + " = ?";
    return ClosableIterators.wrap(new AbstractPagingIterable<RowResult<T>, TokenBackedBasicResultsPage<RowResult<T>, byte[]>>() {

        @Override
        protected TokenBackedBasicResultsPage<RowResult<T>, byte[]> getFirstPage() throws Exception {
            return getPage(rangeRequest.getStartInclusive());
        }

        @Override
        protected TokenBackedBasicResultsPage<RowResult<T>, byte[]> getNextPage(TokenBackedBasicResultsPage<RowResult<T>, byte[]> previous) throws Exception {
            return getPage(previous.getTokenForNextPage());
        }

        TokenBackedBasicResultsPage<RowResult<T>, byte[]> getPage(final byte[] startKey) throws Exception {
            BoundStatement boundStatement = getPreparedStatement(tableRef, bindQuery.toString(), session).setConsistencyLevel(consistency).bind();
            boundStatement.setBytes(0, ByteBuffer.wrap(startKey));
            if (endExclusive.length > 0) {
                boundStatement.setBytes(1, ByteBuffer.wrap(endExclusive));
            }
            ResultSet resultSet = session.execute(boundStatement);
            List<Row> rows = Lists.newArrayList(resultSet.all());
            cqlKeyValueServices.logTracedQuery(bindQuery.toString(), resultSet, session, cqlStatementCache.normalQuery);
            byte[] maxRow = null;
            ResultsExtractor<T> extractor = resultsExtractor.get();
            for (Row row : rows) {
                byte[] rowName = getRowName(row);
                if (maxRow == null) {
                    maxRow = rowName;
                } else {
                    maxRow = PtBytes.BYTES_COMPARATOR.max(maxRow, rowName);
                }
            }
            if (maxRow == null) {
                return new SimpleTokenBackedResultsPage<>(endExclusive, ImmutableList.of(), false);
            }
            // get the rest of the last row
            BoundStatement boundLastRow = getPreparedStatement(tableRef, getLastRowQuery, session).bind();
            boundLastRow.setBytes(fieldNameProvider.row(), ByteBuffer.wrap(maxRow));
            try {
                resultSet = session.execute(boundLastRow);
            } catch (com.datastax.driver.core.exceptions.UnavailableException e) {
                throw new InsufficientConsistencyException("This operation requires all Cassandra" + " nodes to be up and available.", e);
            }
            rows.addAll(resultSet.all());
            cqlKeyValueServices.logTracedQuery(getLastRowQuery, resultSet, session, cqlStatementCache.normalQuery);
            for (Row row : rows) {
                extractor.internalExtractResult(timestamp, selection, getRowName(row), getColName(row), getValue(row), getTs(row));
            }
            SortedMap<byte[], SortedMap<byte[], T>> resultsByRow = Cells.breakCellsUpByRow(extractor.asMap());
            return ResultsExtractor.getRowResults(endExclusive, maxRow, resultsByRow);
        }
    }.iterator());
}
Also used : TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) SimpleTokenBackedResultsPage(com.palantir.util.paging.SimpleTokenBackedResultsPage) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap) ResultSet(com.datastax.driver.core.ResultSet) AbstractPagingIterable(com.palantir.util.paging.AbstractPagingIterable) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 4 with ColumnSelection

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

the class SchemaApiTestV2Table method getSmallRowRangeColumn1.

/**
 * Returns a mapping from all the row keys in a rangeRequest to their value at column Column1
 * (if that column exists for the row-key). As the Column1 values are all loaded in memory,
 * do not use for large amounts of data. The order of results is preserved in the map.
 */
public LinkedHashMap<String, Long> getSmallRowRangeColumn1(RangeRequest rangeRequest) {
    ColumnSelection colSelection = ColumnSelection.create(ImmutableList.of(PtBytes.toCachedBytes("c")));
    rangeRequest = rangeRequest.getBuilder().retainColumns(colSelection).build();
    Preconditions.checkArgument(rangeRequest.getColumnNames().size() <= 1, "Must not request columns other than Column1.");
    LinkedHashMap<String, Long> resultsMap = new LinkedHashMap<>();
    BatchingVisitableView.of(t.getRange(tableRef, rangeRequest)).immutableCopy().forEach(entry -> {
        SchemaApiTestTable.SchemaApiTestRowResult resultEntry = SchemaApiTestTable.SchemaApiTestRowResult.of(entry);
        resultsMap.put(resultEntry.getRowName().getComponent1(), resultEntry.getColumn1());
    });
    return resultsMap;
}
Also used : ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) Long(java.lang.Long) String(java.lang.String) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ColumnSelection

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

the class SchemaApiTestV2Table method getColumn2.

/**
 * Returns a mapping from the specified row keys to their value at column Column2.
 * As the Column2 values are all loaded in memory, do not use for large amounts of data.
 * If the column does not exist for a key, the entry will be omitted from the map.
 */
public Map<String, StringValue> getColumn2(Iterable<String> rowKeys) {
    ColumnSelection colSelection = ColumnSelection.create(ImmutableList.of(PtBytes.toCachedBytes("d")));
    List<SchemaApiTestTable.SchemaApiTestRow> rows = Lists.newArrayList(rowKeys).stream().map(SchemaApiTestTable.SchemaApiTestRow::of).collect(Collectors.toList());
    SortedMap<byte[], RowResult<byte[]>> results = t.getRows(tableRef, Persistables.persistAll(rows), colSelection);
    return results.values().stream().map(entry -> SchemaApiTestTable.SchemaApiTestRowResult.of(entry)).collect(Collectors.toMap(entry -> entry.getRowName().getComponent1(), SchemaApiTestTable.SchemaApiTestRowResult::getColumn2));
}
Also used : StringValue(com.palantir.atlasdb.table.description.test.StringValue) Persistables(com.palantir.common.persist.Persistables) Function(java.util.function.Function) PtBytes(com.palantir.atlasdb.encoding.PtBytes) Iterable(java.lang.Iterable) LinkedHashMap(java.util.LinkedHashMap) Generated(javax.annotation.Generated) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ColumnValues(com.palantir.atlasdb.table.generation.ColumnValues) Long(java.lang.Long) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) String(java.lang.String) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) Namespace(com.palantir.atlasdb.keyvalue.api.Namespace) SuppressWarnings(java.lang.SuppressWarnings) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Set(java.util.Set) 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) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Objects(java.util.Objects) List(java.util.List) Transaction(com.palantir.atlasdb.transaction.api.Transaction) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) SortedMap(java.util.SortedMap) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult)

Aggregations

ColumnSelection (com.palantir.atlasdb.keyvalue.api.ColumnSelection)19 StringValue (com.palantir.atlasdb.table.description.test.StringValue)7 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 Test (org.junit.Test)7 Cell (com.palantir.atlasdb.keyvalue.api.Cell)6 RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)6 Transaction (com.palantir.atlasdb.transaction.api.Transaction)6 String (java.lang.String)6 List (java.util.List)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 PtBytes (com.palantir.atlasdb.encoding.PtBytes)5 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)5 BatchingVisitableView (com.palantir.common.base.BatchingVisitableView)5 Long (java.lang.Long)4 SortedMap (java.util.SortedMap)4 Preconditions (com.google.common.base.Preconditions)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3