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())));
}
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());
}
}
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());
}
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);
}
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());
}
Aggregations