Search in sources :

Example 11 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException 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 12 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.

the class OAuthStore method writeProvider.

/**
 * Create/update an OAuth provider.
 *
 * @param oauthProvider {@link OAuthProvider} to write
 * @throws OAuthStoreException if the write fails
 */
public void writeProvider(OAuthProvider oauthProvider) throws OAuthStoreException {
    try {
        TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(TABLE_ID);
            table.upsert(getRow(oauthProvider));
        }, TableNotFoundException.class, InvalidFieldException.class);
    } catch (TableNotFoundException e) {
        throw new OAuthStoreException("OAuth provider table not found", e);
    } catch (InvalidFieldException e) {
        throw new OAuthStoreException("OAuth provider object fields do not match table", e);
    }
    String namespace = NamespaceId.SYSTEM.getNamespace();
    try {
        secureStoreManager.put(namespace, getClientCredsKey(oauthProvider.getName()), GSON.toJson(oauthProvider.getClientCredentials()), "OAuth client creds", Collections.emptyMap());
    } catch (IOException e) {
        throw new OAuthStoreException("Failed to write to OAuth provider secure storage", e);
    } catch (Exception e) {
        throw new OAuthStoreException("Namespace \"" + namespace + "\" does not exist", e);
    }
}
Also used : TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException)

Example 13 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.

the class OAuthStore method getProvider.

/**
 * Get an OAuth provider.
 *
 * @param name name of {@link OAuthProvider} to read
 * @throws OAuthStoreException if the read fails
 */
public Optional<OAuthProvider> getProvider(String name) throws OAuthStoreException {
    OAuthClientCredentials clientCreds;
    try {
        String clientCredsJson = new String(secureStore.get(NamespaceId.SYSTEM.getNamespace(), getClientCredsKey(name)).get(), StandardCharsets.UTF_8);
        clientCreds = GSON.fromJson(clientCredsJson, OAuthClientCredentials.class);
    } catch (IOException e) {
        throw new OAuthStoreException("Failed to read from OAuth provider secure storage", e);
    } catch (Exception e) {
        return Optional.empty();
    }
    try {
        return TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(TABLE_ID);
            Optional<StructuredRow> row = table.read(getKey(name));
            return row.map(structuredRow -> fromRow(structuredRow, clientCreds));
        }, TableNotFoundException.class, InvalidFieldException.class);
    } catch (TableNotFoundException e) {
        throw new OAuthStoreException("OAuth provider table not found", e);
    } catch (InvalidFieldException e) {
        throw new OAuthStoreException("OAuth provider object fields do not match table", e);
    }
}
Also used : TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException)

Example 14 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.

the class OAuthStore method writeProvider.

/**
 * Create/update an OAuth provider.
 *
 * @param oauthProvider {@link OAuthProvider} to write
 * @throws OAuthStoreException if the write fails
 */
public void writeProvider(OAuthProvider oauthProvider) throws OAuthStoreException {
    try {
        TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(TABLE_ID);
            table.upsert(getRow(oauthProvider));
        }, TableNotFoundException.class, InvalidFieldException.class);
    } catch (TableNotFoundException e) {
        throw new OAuthStoreException("OAuth provider table not found", e);
    } catch (InvalidFieldException e) {
        throw new OAuthStoreException("OAuth provider object fields do not match table", e);
    }
    String namespace = NamespaceId.SYSTEM.getNamespace();
    try {
        secureStoreManager.put(namespace, getClientCredsKey(oauthProvider.getName()), GSON.toJson(oauthProvider.getClientCredentials()), "OAuth client creds", Collections.emptyMap());
    } catch (IOException e) {
        throw new OAuthStoreException("Failed to write to OAuth provider secure storage", e);
    } catch (Exception e) {
        throw new OAuthStoreException("Namespace \"" + namespace + "\" does not exist", e);
    }
}
Also used : TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException)

Example 15 with InvalidFieldException

use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.

the class OAuthStore method getProvider.

/**
 * Get an OAuth provider.
 *
 * @param name name of {@link OAuthProvider} to read
 * @throws OAuthStoreException if the read fails
 */
public Optional<OAuthProvider> getProvider(String name) throws OAuthStoreException {
    OAuthClientCredentials clientCreds;
    try {
        String clientCredsJson = new String(secureStore.get(NamespaceId.SYSTEM.getNamespace(), getClientCredsKey(name)).get(), StandardCharsets.UTF_8);
        clientCreds = GSON.fromJson(clientCredsJson, OAuthClientCredentials.class);
    } catch (IOException e) {
        throw new OAuthStoreException("Failed to read from OAuth provider secure storage", e);
    } catch (Exception e) {
        return Optional.empty();
    }
    try {
        return TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(TABLE_ID);
            Optional<StructuredRow> row = table.read(getKey(name));
            return row.map(structuredRow -> fromRow(structuredRow, clientCreds));
        }, TableNotFoundException.class, InvalidFieldException.class);
    } catch (TableNotFoundException e) {
        throw new OAuthStoreException("OAuth provider table not found", e);
    } catch (InvalidFieldException e) {
        throw new OAuthStoreException("OAuth provider object fields do not match table", e);
    }
}
Also used : TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException)

Aggregations

InvalidFieldException (io.cdap.cdap.spi.data.InvalidFieldException)26 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)14 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)14 Field (io.cdap.cdap.spi.data.table.field.Field)14 FieldType (io.cdap.cdap.spi.data.table.field.FieldType)14 ArrayList (java.util.ArrayList)12 KeySet (com.google.cloud.spanner.KeySet)10 ResultSet (com.google.cloud.spanner.ResultSet)10 AbstractCloseableIterator (io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator)10 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)10 StructuredTableSchema (io.cdap.cdap.spi.data.table.StructuredTableSchema)10 FieldValidator (io.cdap.cdap.spi.data.table.field.FieldValidator)10 Range (io.cdap.cdap.spi.data.table.field.Range)10 IOException (java.io.IOException)10 Collection (java.util.Collection)10 Collections (java.util.Collections)10 HashSet (java.util.HashSet)10 List (java.util.List)10 Map (java.util.Map)10 Optional (java.util.Optional)10