use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class SpannerStructuredTable method read.
@Override
public Optional<StructuredRow> read(Collection<Field<?>> keys, Collection<String> columns) throws InvalidFieldException {
if (columns.isEmpty()) {
throw new IllegalArgumentException("No column is specified to read");
}
fieldValidator.validatePrimaryKeys(keys, false);
Set<String> missingColumns = columns.stream().filter(f -> !schema.getFieldNames().contains(f)).collect(Collectors.toSet());
if (!missingColumns.isEmpty()) {
throw new IllegalArgumentException("Some columns do not exists in the table schema " + missingColumns);
}
// Adds all the keys to the result column as well. This is mirroring the PostgreSQL implementation.
Set<String> queryColumns = keys.stream().map(Field::getName).collect(Collectors.toSet());
queryColumns.addAll(columns);
Struct row = transactionContext.readRow(schema.getTableId().getName(), createKey(keys), queryColumns);
return Optional.ofNullable(row).map(r -> new SpannerStructuredRow(schema, r));
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class SpannerStructuredTable method compareAndSwap.
@Override
public boolean compareAndSwap(Collection<Field<?>> keys, Field<?> oldValue, Field<?> newValue) throws InvalidFieldException, IllegalArgumentException {
if (oldValue.getFieldType() != newValue.getFieldType()) {
throw new IllegalArgumentException(String.format("Field types of oldValue (%s) and newValue (%s) are not the same", oldValue.getFieldType(), newValue.getFieldType()));
}
if (!oldValue.getName().equals(newValue.getName())) {
throw new IllegalArgumentException(String.format("Trying to compare and swap different fields. Old Value = %s, New Value = %s", oldValue, newValue));
}
if (schema.isPrimaryKeyColumn(oldValue.getName())) {
throw new IllegalArgumentException("Cannot use compare and swap on a primary key field");
}
StructuredRow existing = read(keys, Collections.singleton(oldValue.getName())).orElse(null);
// Check if the existing value is as expected in the oldValue
if (!isFieldEquals(oldValue, existing)) {
return false;
}
List<Field<?>> updateFields = new ArrayList<>(keys);
updateFields.add(newValue);
if (existing == null) {
insert(updateFields);
} else {
update(updateFields);
}
return true;
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class SpannerStructuredTable method increment.
@Override
public void increment(Collection<Field<?>> keys, String column, long amount) throws InvalidFieldException, IllegalArgumentException {
if (schema.isPrimaryKeyColumn(column)) {
throw new IllegalArgumentException("Cannot use increment on a primary key field");
}
FieldType.Type type = schema.getType(column);
if (type == null) {
throw new InvalidFieldException(schema.getTableId(), column, "Column " + column + " does not exist");
}
if (type != FieldType.Type.LONG) {
throw new IllegalArgumentException(String.format("Trying to increment a column of type %s. Only %s column type can be incremented", type, FieldType.Type.LONG));
}
fieldValidator.validatePrimaryKeys(keys, false);
StructuredRow existing = read(keys, Collections.singleton(column)).orElse(null);
List<Field<?>> fields = new ArrayList<>(keys);
fields.add(Fields.longField(column, amount + (existing == null ? 0L : Objects.requireNonNull(existing.getLong(column)))));
if (existing == null) {
// Insert a new row if there is no existing row
insert(fields);
} else {
// Update the row by incrementing the amount
update(fields);
}
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class AbstractCheckpointManager method getCheckpoint.
@Override
public Map<Integer, Checkpoint<T>> getCheckpoint(Set<Integer> partitions) throws IOException {
return TransactionRunners.run(transactionRunner, context -> {
Map<Integer, Checkpoint<T>> checkpoints = new HashMap<>();
StructuredTable table = context.getTable(StoreDefinition.LogCheckpointStore.LOG_CHECKPOINT_TABLE);
for (int partition : partitions) {
Optional<StructuredRow> optionalRow = table.read(getKeyFields(rowKeyPrefix, partition));
StructuredRow row = optionalRow.orElse(null);
checkpoints.put(partition, fromRow(row));
}
return checkpoints;
}, IOException.class);
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class FileMetadataCleanerTest method deleteAllMetaEntries.
private boolean deleteAllMetaEntries(TransactionRunner transactionRunner) {
final List<DeletedEntry> deletedEntries = new ArrayList<>();
try {
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(StoreDefinition.LogFileMetaStore.LOG_FILE_META);
try (CloseableIterator<StructuredRow> iter = table.scan(Range.all(), Integer.MAX_VALUE)) {
while (iter.hasNext()) {
StructuredRow row = iter.next();
String loggingContext = row.getString(StoreDefinition.LogFileMetaStore.LOGGING_CONTEXT_FIELD);
long eventTime = row.getLong(StoreDefinition.LogFileMetaStore.EVENT_TIME_FIELD);
long creationTime = row.getLong(StoreDefinition.LogFileMetaStore.CREATION_TIME_FIELD);
table.delete(Arrays.asList(Fields.stringField(StoreDefinition.LogFileMetaStore.LOGGING_CONTEXT_FIELD, loggingContext), Fields.longField(StoreDefinition.LogFileMetaStore.EVENT_TIME_FIELD, eventTime), Fields.longField(StoreDefinition.LogFileMetaStore.CREATION_TIME_FIELD, creationTime)));
deletedEntries.add(new DeletedEntry(loggingContext, eventTime, creationTime));
}
}
}, IOException.class);
} catch (IOException e) {
return false;
}
return true;
}
Aggregations