Search in sources :

Example 46 with StructuredRow

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));
}
Also used : IntStream(java.util.stream.IntStream) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) LoggerFactory(org.slf4j.LoggerFactory) FieldValidator(io.cdap.cdap.spi.data.table.field.FieldValidator) Fields(io.cdap.cdap.spi.data.table.field.Fields) HashMap(java.util.HashMap) Value(com.google.cloud.spanner.Value) Function(java.util.function.Function) KeyRange(com.google.cloud.spanner.KeyRange) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) Map(java.util.Map) Field(io.cdap.cdap.spi.data.table.field.Field) ByteArray(com.google.cloud.ByteArray) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) TransactionContext(com.google.cloud.spanner.TransactionContext) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Collectors(java.util.stream.Collectors) KeySet(com.google.cloud.spanner.KeySet) Statement(com.google.cloud.spanner.Statement) Objects(java.util.Objects) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) List(java.util.List) Struct(com.google.cloud.spanner.Struct) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) Optional(java.util.Optional) StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) Collections(java.util.Collections) Struct(com.google.cloud.spanner.Struct)

Example 47 with StructuredRow

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;
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArrayList(java.util.ArrayList)

Example 48 with StructuredRow

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);
    }
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArrayList(java.util.ArrayList) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) FieldType(io.cdap.cdap.spi.data.table.field.FieldType)

Example 49 with StructuredRow

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);
}
Also used : HashMap(java.util.HashMap) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 50 with StructuredRow

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;
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) IOException(java.io.IOException)

Aggregations

StructuredRow (io.cdap.cdap.spi.data.StructuredRow)142 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)68 Field (io.cdap.cdap.spi.data.table.field.Field)66 ArrayList (java.util.ArrayList)54 Range (io.cdap.cdap.spi.data.table.field.Range)36 HashSet (java.util.HashSet)28 IOException (java.io.IOException)22 HashMap (java.util.HashMap)22 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)20 LinkedHashMap (java.util.LinkedHashMap)18 Map (java.util.Map)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)16 Collection (java.util.Collection)16 Set (java.util.Set)16 Nullable (javax.annotation.Nullable)16 ImmutableList (com.google.common.collect.ImmutableList)14 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)14 Fields (io.cdap.cdap.spi.data.table.field.Fields)14