use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.
the class ConnectionStore method clear.
// clean up all connections
@VisibleForTesting
void clear() {
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
table.deleteAll(Range.all());
});
}
use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.
the class ConnectionStore method saveConnection.
/**
* Save the connection in the store.
*
* @param connectionId the connection id
* @param connection the connection information
* @param overWrite flag indicating whether the store should overwrite an existing connection with same connection id
* but different connection name, i.e, a b and a.b both convert to id a_b
*/
public void saveConnection(ConnectionId connectionId, Connection connection, boolean overWrite) {
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
Connection oldConnection = getConnectionInternal(table, connectionId, false);
Connection newConnection = connection;
if (oldConnection != null) {
if (oldConnection.isPreConfigured()) {
throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s and is pre-configured. " + "Preconfigured connections cannot be updated or overwritten.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
}
if (!oldConnection.getName().equals(newConnection.getName()) && !overWrite) {
throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s. Please choose a different connection name.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
}
newConnection = new Connection(connection.getName(), oldConnection.getConnectionId(), connection.getConnectionType(), connection.getDescription(), connection.isPreConfigured(), connection.isDefault(), oldConnection.getCreatedTimeMillis(), connection.getUpdatedTimeMillis(), connection.getPlugin());
}
Collection<Field<?>> fields = getConnectionKeys(connectionId);
fields.add(Fields.longField(CREATED_COL, newConnection.getCreatedTimeMillis()));
fields.add(Fields.longField(UPDATED_COL, newConnection.getUpdatedTimeMillis()));
fields.add(Fields.stringField(CONNECTION_DATA_FIELD, GSON.toJson(newConnection)));
table.upsert(fields);
});
}
use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.
the class DraftStore method deleteDraft.
/**
* Delete the given draft. This is a no-op if the draft does not exist
*
* @param id {@link DraftId} that is used to uniquely identify a draft
* @throws TableNotFoundException if the draft store table is not found
* @throws InvalidFieldException if the fields in the {@link Draft} object do not match the fields in the
* StructuredTable
*/
public void deleteDraft(DraftId id) throws TableNotFoundException, InvalidFieldException {
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
table.delete(getKey(id));
}, TableNotFoundException.class, InvalidFieldException.class);
}
use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.
the class DatasetBasedTimeScheduleStore method persistChangeOfState.
private void persistChangeOfState(final TriggerKey triggerKey, final Trigger.TriggerState newTriggerState) {
try {
Preconditions.checkNotNull(triggerKey);
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = getTimeScheduleStructuredTable(context);
TriggerStatusV2 storedTriggerStatus = readTrigger(table, triggerKey);
if (storedTriggerStatus != null) {
// its okay to persist the same trigger back again since during pause/resume
// operation the trigger does not change. We persist it here with just the new trigger state
persistTrigger(table, storedTriggerStatus.trigger, newTriggerState);
} else {
LOG.warn("Trigger key {} was not found while trying to persist its state to {}.", triggerKey, newTriggerState);
}
});
} catch (Throwable th) {
throw Throwables.propagate(th);
}
}
use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.
the class SystemAppTestBaseTest method testTableOperations.
@Test
public void testTableOperations() throws Exception {
StructuredTableAdmin tableAdmin = getStructuredTableAdmin();
StructuredTableId id = new StructuredTableId("t0");
Assert.assertFalse(tableAdmin.exists(id));
String keyCol = "key";
String valCol = "val";
tableAdmin.create(new StructuredTableSpecification.Builder().withId(id).withFields(new FieldType(keyCol, FieldType.Type.STRING), new FieldType(valCol, FieldType.Type.STRING)).withPrimaryKeys(keyCol).build());
try {
TransactionRunner transactionRunner = getTransactionRunner();
String key = "k0";
String val = "v0";
transactionRunner.run(context -> {
StructuredTable table = context.getTable(id);
List<Field<?>> fields = new ArrayList<>();
fields.add(Fields.stringField(keyCol, key));
Optional<StructuredRow> row = table.read(fields);
Assert.assertFalse(row.isPresent());
fields.add(Fields.stringField(valCol, val));
table.upsert(fields);
});
transactionRunner.run(context -> {
StructuredTable table = context.getTable(id);
List<Field<?>> keyField = Collections.singletonList(Fields.stringField(keyCol, key));
Optional<StructuredRow> row = table.read(keyField);
Assert.assertTrue(row.isPresent());
Assert.assertEquals(val, row.get().getString(valCol));
});
} finally {
tableAdmin.drop(id);
}
}
Aggregations