use of io.cdap.cdap.spi.data.TableNotFoundException in project cdap by caskdata.
the class TransactionRunnersTest method testSingleExceptionCheckedPropagation.
@Test
public void testSingleExceptionCheckedPropagation() {
try {
TransactionRunners.run(MOCK, (TxRunnable) context -> {
throw new TableNotFoundException(new StructuredTableId("id"));
}, TableNotFoundException.class);
Assert.fail("runnable should have thrown an exception");
} catch (TableNotFoundException e) {
// expected
}
try {
TransactionRunners.run(MOCK, (TxCallable<Object>) context -> {
throw new TableNotFoundException(new StructuredTableId("id"));
}, TableNotFoundException.class);
Assert.fail("runnable should have thrown an exception");
} catch (TableNotFoundException e) {
// expected
}
}
use of io.cdap.cdap.spi.data.TableNotFoundException 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);
}
}
use of io.cdap.cdap.spi.data.TableNotFoundException 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);
}
}
use of io.cdap.cdap.spi.data.TableNotFoundException in project cdap by caskdata.
the class SpannerStructuredTableAdmin method loadSchema.
private StructuredTableSchema loadSchema(StructuredTableId tableId) throws TableNotFoundException {
// Query the information_schema to reconstruct the StructuredTableSchema
// See https://cloud.google.com/spanner/docs/information-schema for details
Statement schemaStatement = Statement.newBuilder("SELECT C.column_name, C.spanner_type, I.index_type, I.ordinal_position FROM information_schema.columns C " + "LEFT JOIN information_schema.index_columns I " + "ON C.column_name = I.column_name AND C.table_name = I.table_name " + "WHERE C.table_name = @table_name ORDER BY C.ordinal_position").bind("table_name").to(tableId.getName()).build();
List<FieldType> fields = new ArrayList<>();
List<String> primaryKeys = new ArrayList<>();
List<String> indexes = new ArrayList<>();
try (ReadOnlyTransaction tx = databaseClient.readOnlyTransaction()) {
try (ResultSet resultSet = tx.executeQuery(schemaStatement)) {
while (resultSet.next()) {
Struct row = resultSet.getCurrentRowAsStruct();
String columnName = row.getString("column_name");
String indexType = row.isNull("index_type") ? null : row.getString("index_type");
// If a field is not a primary nor an index, the ordinal_position will be NULL in the index_columns table.
boolean isIndex = !row.isNull("ordinal_position");
fields.add(new FieldType(columnName, fromSpannerType(row.getString("spanner_type"))));
if ("PRIMARY_KEY".equalsIgnoreCase(indexType)) {
primaryKeys.add(columnName);
} else if ("INDEX".equalsIgnoreCase(indexType) && isIndex) {
indexes.add(columnName);
}
}
}
}
if (fields.isEmpty()) {
throw new TableNotFoundException(tableId);
}
return new StructuredTableSchema(tableId, fields, primaryKeys, indexes);
}
Aggregations