use of io.cdap.cdap.spi.data.table.StructuredTableId in project cdap by caskdata.
the class SqlStructuredTableRegistry method registerSpecification.
@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
initIfNeeded();
TransactionRunners.run(transactionRunner, context -> {
StructuredTable registry = context.getTable(REGISTRY);
StructuredTableId tableId = specification.getTableId();
Optional<StructuredRow> optional = registry.read(Collections.singleton(Fields.stringField(TABLE_NAME_FIELD, tableId.getName())));
if (optional.isPresent()) {
throw new TableAlreadyExistsException(tableId);
}
LOG.debug("Registering table specification {}", specification);
registry.upsert(Arrays.asList(Fields.stringField(TABLE_NAME_FIELD, tableId.getName()), Fields.stringField(TABLE_SPEC_FIELD, GSON.toJson(specification))));
}, TableAlreadyExistsException.class);
}
use of io.cdap.cdap.spi.data.table.StructuredTableId in project cdap by caskdata.
the class NoSqlStructuredTableRegistry method registerSpecification.
@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
LOG.debug("Registering table specification {}", specification);
StructuredTableId tableId = specification.getTableId();
MetricsTable table = getRegistryTable();
try {
byte[] rowKeyBytes = getRowKeyBytes(tableId);
byte[] serialized = table.get(rowKeyBytes, SCHEMA_COL_BYTES);
if (serialized != null) {
throw new TableAlreadyExistsException(tableId);
}
serialized = Bytes.toBytes(GSON.toJson(specification));
if (!table.swap(rowKeyBytes, SCHEMA_COL_BYTES, null, serialized)) {
throw new TableAlreadyExistsException(tableId);
}
} finally {
closeRegistryTable(table);
}
}
use of io.cdap.cdap.spi.data.table.StructuredTableId 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);
}
}
use of io.cdap.cdap.spi.data.table.StructuredTableId in project cdap by caskdata.
the class TransactionRunnersTest method testTwoExceptionCheckedPropagation.
@Test
public void testTwoExceptionCheckedPropagation() throws Exception {
try {
TransactionRunners.run(MOCK, (TxRunnable) context -> {
throw new TableAlreadyExistsException(new StructuredTableId("id"));
}, TableNotFoundException.class, TableAlreadyExistsException.class);
Assert.fail("runnable should have thrown an exception");
} catch (TableAlreadyExistsException e) {
// expected
}
try {
TransactionRunners.run(MOCK, (TxCallable<Object>) context -> {
throw new TableAlreadyExistsException(new StructuredTableId("id"));
}, TableNotFoundException.class, TableAlreadyExistsException.class);
Assert.fail("runnable should have thrown an exception");
} catch (TableAlreadyExistsException e) {
// expected
}
}
use of io.cdap.cdap.spi.data.table.StructuredTableId 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
}
}
Aggregations