use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class BufferingTable method get.
@ReadOnly
@Override
public List<Row> get(List<Get> gets) {
ensureTransactionIsStarted();
try {
// get persisted, then overwrite with whats buffered
List<Map<byte[], byte[]>> persistedRows = getPersisted(gets);
// gets and rows lists are always of the same size
Preconditions.checkArgument(gets.size() == persistedRows.size(), "Invalid number of rows fetched when performing multi-get. There must be one row for each get.");
List<Row> result = Lists.newArrayListWithCapacity(persistedRows.size());
Iterator<Map<byte[], byte[]>> persistedRowsIter = persistedRows.iterator();
Iterator<Get> getIter = gets.iterator();
while (persistedRowsIter.hasNext() && getIter.hasNext()) {
Get get = getIter.next();
Map<byte[], byte[]> persistedRow = persistedRowsIter.next();
// navigable copy of the persisted data. Implementation may return immutable or unmodifiable maps,
// so we make a copy here.
NavigableMap<byte[], byte[]> rowColumns = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
rowColumns.putAll(persistedRow);
byte[] row = get.getRow();
NavigableMap<byte[], Update> buffCols = buff.get(row);
// merge what was in the buffer and what was persisted
if (buffCols != null) {
List<byte[]> getColumns = get.getColumns();
byte[][] columns = getColumns == null ? null : getColumns.toArray(new byte[getColumns.size()][]);
mergeToPersisted(rowColumns, buffCols, columns);
}
result.add(new Result(row, unwrapDeletes(rowColumns)));
}
return result;
} catch (Exception e) {
LOG.debug("multi-get failed for table: " + getTransactionAwareName(), e);
throw new DataSetException("multi-get failed", e);
}
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class NoSqlStructuredTable method read.
@Override
public Optional<StructuredRow> read(Collection<Field<?>> keys) throws InvalidFieldException {
LOG.trace("Table {}: Read with keys {}", schema.getTableId(), keys);
Row row = table.get(convertKeyToBytes(keys, false));
return row.isEmpty() ? Optional.empty() : Optional.of(new NoSqlStructuredRow(row, schema));
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class NoSqlStructuredTable method read.
@Override
public Optional<StructuredRow> read(Collection<Field<?>> keys, Collection<String> columns) throws InvalidFieldException {
LOG.trace("Table {}: Read with keys {} and columns {}", schema.getTableId(), keys, columns);
if (columns == null || columns.isEmpty()) {
throw new IllegalArgumentException("No columns are specified to read");
}
Row row = table.get(convertKeyToBytes(keys, false), convertColumnsToBytes(columns));
return row.isEmpty() ? Optional.empty() : Optional.of(new NoSqlStructuredRow(row, schema));
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class NoSqlStructuredTable method updateFieldsToBytes.
/**
* Updates fields in the row and converts to a {@link Put} to write to table. The primary key must be provided.
*
* @param fields the fields to update
* @return a PUT object
* @throws InvalidFieldException if primary keys are missing or the column is not in schema
*/
private Put updateFieldsToBytes(Collection<Field<?>> fields) throws InvalidFieldException {
Set<String> fieldNames = fields.stream().map(Field::getName).collect(Collectors.toSet());
if (!fieldNames.containsAll(schema.getPrimaryKeys())) {
throw new InvalidFieldException(schema.getTableId(), fields, String.format("Given fields %s does not contain all the " + "primary keys %s", fieldNames, schema.getPrimaryKeys()));
}
Set<String> primaryKeys = new HashSet<>(schema.getPrimaryKeys());
Collection<Field<?>> keyFields = fields.stream().filter(field -> primaryKeys.contains(field.getName())).collect(Collectors.toList());
byte[] primaryKey = convertKeyToBytes(keyFields, false);
Put put = new Put(primaryKey);
Row row = table.get(primaryKey);
Map<String, Field<?>> fieldMap = fields.stream().collect(Collectors.toMap(Field::getName, Function.identity()));
for (Map.Entry<byte[], byte[]> entry : row.getColumns().entrySet()) {
String columnName = Bytes.toString(entry.getKey());
if (!fieldMap.containsKey(columnName) || primaryKeys.contains(columnName)) {
put.add(entry.getKey(), entry.getValue());
} else {
put.add(entry.getKey(), fieldToBytes(fieldMap.get(columnName)));
}
}
return put;
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class ReflectionTableTest method assertGetAndPut.
private void assertGetAndPut(final Table table, final byte[] rowKey, final User obj, final Schema schema) throws Exception {
// TableDataset is not accessible here, but we know that's the underlying implementation...
TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor((TransactionAware) table);
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Put put = new Put(rowKey);
ReflectionPutWriter<User> putWriter = new ReflectionPutWriter<>(schema);
putWriter.write(obj, put);
table.put(put);
Row row = table.get(rowKey);
ReflectionRowReader<User> rowReader = new ReflectionRowReader<>(schema, TypeToken.of(User.class));
User actual = rowReader.read(row, schema);
Assert.assertEquals(obj, actual);
}
});
}
Aggregations