Search in sources :

Example 11 with RowResult

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

the class AbstractTransactionTest method testReadMyWritesRange.

@Test
public void testReadMyWritesRange() {
    Transaction t = startTransaction();
    put(t, "row1", "col1", "v1");
    put(t, "row1", "col2", "v2");
    put(t, "row2", "col1", "v3");
    put(t, "row4", "col1", "v4");
    t.commit();
    t = startTransaction();
    assertEquals("v1", get(t, "row1", "col1"));
    assertEquals("v2", get(t, "row1", "col2"));
    assertEquals("v3", get(t, "row2", "col1"));
    BatchingVisitable<RowResult<byte[]>> visitable = t.getRange(TEST_TABLE, RangeRequest.builder().build());
    put(t, "row0", "col1", "v5");
    put(t, "row1", "col1", "v5");
    put(t, "row1", "col3", "v6");
    put(t, "row3", "col1", "v7");
    delete(t, "row2", "col1");
    put(t, "row2", "col2", "v8");
    final Map<Cell, byte[]> vals = Maps.newHashMap();
    visitable.batchAccept(100, AbortingVisitors.batching((RowVisitor) item -> {
        MapEntries.putAll(vals, item.getCells());
        if (Arrays.equals(item.getRowName(), "row1".getBytes())) {
            assertEquals(3, IterableView.of(item.getCells()).size());
            assertEquals("v5", new String(item.getColumns().get("col1".getBytes())));
        }
        return true;
    }));
    assertTrue(vals.containsKey(Cell.create("row1".getBytes(), "col1".getBytes())));
    assertTrue(Arrays.equals("v5".getBytes(), vals.get(Cell.create("row1".getBytes(), "col1".getBytes()))));
    assertFalse(vals.containsKey(Cell.create("row2".getBytes(), "col1".getBytes())));
}
Also used : RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 12 with RowResult

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

the class AbstractTransactionTest method testKeyValueRangesMany2.

@Test
public void testKeyValueRangesMany2() {
    putDirect("row1", "col1", "", 0);
    putDirect("row2", "col1", "", 0);
    putDirect("row2", "col2", "", 0);
    RangeRequest allRange = RangeRequest.builder().batchHint(3).build();
    for (int i = 0; i < 1000; i++) {
        ClosableIterator<RowResult<Value>> range = keyValueService.getRange(TEST_TABLE, allRange, 1);
        ImmutableList<RowResult<Value>> list = ImmutableList.copyOf(range);
        assertEquals(2, list.size());
    }
}
Also used : RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) Test(org.junit.Test)

Example 13 with RowResult

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

the class AbstractTransactionTest method testRangeAfterTimestampReverse.

@Test
public void testRangeAfterTimestampReverse() {
    if (!supportsReverse()) {
        return;
    }
    putDirect("row1", "col2", "", 0);
    putDirect("row2", "col2", "", 0);
    putDirect("row3", "col2", "", 5);
    RangeRequest rangeRequest = RangeRequest.reverseBuilder().batchHint(1).build();
    Map<RangeRequest, TokenBackedBasicResultsPage<RowResult<Value>, byte[]>> ranges = keyValueService.getFirstBatchForRanges(TEST_TABLE, Collections.singleton(rangeRequest), 1);
    assertEquals(1, ranges.keySet().size());
    TokenBackedBasicResultsPage<RowResult<Value>, byte[]> page = ranges.values().iterator().next();
    assertTrue(page.moreResultsAvailable());
}
Also used : TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) Value(com.palantir.atlasdb.keyvalue.api.Value) Test(org.junit.Test)

Example 14 with RowResult

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

the class TableRowResultDeserializer method deserialize.

@Override
public TableRowResult deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.readValueAsTree();
    String tableName = node.get("table").textValue();
    Collection<RowResult<byte[]>> rowResults = Lists.newArrayList();
    TableMetadata metadata = metadataCache.getMetadata(tableName);
    for (JsonNode rowResult : node.get("data")) {
        byte[] row = AtlasDeserializers.deserializeRow(metadata.getRowMetadata(), rowResult.get("row"));
        ImmutableSortedMap.Builder<byte[], byte[]> cols = ImmutableSortedMap.orderedBy(UnsignedBytes.lexicographicalComparator());
        if (metadata.getColumns().hasDynamicColumns()) {
            for (JsonNode colVal : rowResult.get("cols")) {
                byte[] col = AtlasDeserializers.deserializeCol(metadata.getColumns(), colVal.get("col"));
                byte[] val = AtlasDeserializers.deserializeVal(metadata.getColumns().getDynamicColumn().getValue(), colVal.get("val"));
                cols.put(col, val);
            }
        } else {
            JsonNode namedCols = rowResult.get("cols");
            for (NamedColumnDescription namedCol : metadata.getColumns().getNamedColumns()) {
                JsonNode valNode = namedCols.get(namedCol.getLongName());
                if (valNode != null) {
                    byte[] col = namedCol.getShortName().getBytes(StandardCharsets.UTF_8);
                    byte[] val = AtlasDeserializers.deserializeVal(namedCol.getValue(), valNode);
                    cols.put(col, val);
                }
            }
        }
        rowResults.add(RowResult.create(row, cols.build()));
    }
    return new TableRowResult(tableName, rowResults);
}
Also used : TableMetadata(com.palantir.atlasdb.table.description.TableMetadata) TableRowResult(com.palantir.atlasdb.api.TableRowResult) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) TableRowResult(com.palantir.atlasdb.api.TableRowResult) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) NamedColumnDescription(com.palantir.atlasdb.table.description.NamedColumnDescription)

Example 15 with RowResult

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

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