use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class DatasetInstanceHandlerTest method testCreateDelete.
@Test
public void testCreateDelete() throws Exception {
try {
deployModule("default-table", InMemoryTableModule.class);
deployModule("default-core", CoreDatasetsModule.class);
// cannot create instance with same name again
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable1", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable2", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(2, getInstances().getResponseObject().size());
// we want to verify that data is also gone, so we write smth to tables first
final Table table1 = dsFramework.getDataset(NamespaceId.DEFAULT.dataset("myTable1"), DatasetDefinition.NO_ARGUMENTS, null);
final Table table2 = dsFramework.getDataset(NamespaceId.DEFAULT.dataset("myTable2"), DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table1);
Assert.assertNotNull(table2);
TransactionExecutor txExecutor = new DefaultTransactionExecutor(new InMemoryTxSystemClient(txManager), ImmutableList.of((TransactionAware) table1, (TransactionAware) table2));
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table1.put(new Put("key1", "col1", "val1"));
table2.put(new Put("key2", "col2", "val2"));
}
});
// verify that we can read the data
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("val1", table1.get(new Get("key1", "col1")).getString("col1"));
Assert.assertEquals("val2", table2.get(new Get("key2", "col2")).getString("col2"));
}
});
// delete table, check that it is deleted, create again and verify that it is empty
Assert.assertEquals(HttpStatus.SC_OK, deleteInstance("myTable1").getResponseCode());
ObjectResponse<List<DatasetSpecificationSummary>> instances = getInstances();
Assert.assertEquals(1, instances.getResponseObject().size());
Assert.assertEquals("myTable2", instances.getResponseObject().get(0).getName());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable1", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(2, getInstances().getResponseObject().size());
// verify that table1 is empty. Note: it is ok for test purpose to re-use the table clients
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertTrue(table1.get(new Get("key1", "col1")).isEmpty());
Assert.assertEquals("val2", table2.get(new Get("key2", "col2")).getString("col2"));
// writing smth to table1 for subsequent test
table1.put(new Put("key3", "col3", "val3"));
}
});
// delete all tables, check that they deleted, create again and verify that they are empty
deleteInstances();
Assert.assertEquals(0, getInstances().getResponseObject().size());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable1", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable2", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(2, getInstances().getResponseObject().size());
// verify that tables are empty. Note: it is ok for test purpose to re-use the table clients
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertTrue(table1.get(new Get("key3", "col3")).isEmpty());
Assert.assertTrue(table2.get(new Get("key2", "col2")).isEmpty());
}
});
} finally {
// cleanup
deleteInstances();
Assert.assertEquals(HttpStatus.SC_OK, deleteModules().getResponseCode());
}
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class IndexedTableTest method testIndexedOperations.
@Test
public void testIndexedOperations() throws Exception {
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(table);
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// add a value c with idx = 1, and b with idx = 2
table.put(new Put(keyC).add(idxCol, idx1).add(valCol, valC));
table.put(new Put(keyB).add(idxCol, idx2).add(valCol, valB));
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// read by key c
Row row = table.get(new Get(keyC, colIdxVal));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valC });
// read by key b
row = table.get(new Get(keyB, colIdxVal));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valB });
// read by idx 1 -> c
row = readFirst(table.readByIndex(idxCol, idx1));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valC });
// read by idx 2 -> b
row = readFirst(table.readByIndex(idxCol, idx2));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valB });
// test read over empty index (idx 3)
assertEmpty(table.readByIndex(idxCol, idx3));
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// add a value a with idx = 1
table.put(new Put(keyA).add(idxCol, idx1).add(valCol, valA));
}
});
// read by idx 1 -> a
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Row row = readFirst(table.readByIndex(idxCol, idx1));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valA });
}
});
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// delete value a
table.delete(new Delete(keyA, colIdxVal));
}
});
// read by idx 1 -> c
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Row row = readFirst(table.readByIndex(idxCol, idx1));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valC });
}
});
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// add a value aa with idx 2
table.put(new Put(keyAA).add(idxCol, idx2).add(valCol, valAA));
}
});
// read by idx 2 -> aa
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Row row = readFirst(table.readByIndex(idxCol, idx2));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valAA });
}
});
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// swap value for aa to ab
Assert.assertTrue(table.compareAndSwap(keyAA, valCol, valAA, valAB));
}
});
// read by idx 2 -> ab
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Row row = readFirst(table.readByIndex(idxCol, idx2));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valAB });
}
});
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// swap value for aa to bb
Assert.assertTrue(table.compareAndSwap(keyAA, valCol, valAB, valBB));
}
});
// read by idx 2 -> bb (value of key aa)
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Row row = readFirst(table.readByIndex(idxCol, idx2));
TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valBB });
}
});
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// swap value for aa to null
Assert.assertTrue(table.compareAndSwap(keyAA, valCol, valBB, null));
}
});
// read by idx 2 -> null (value of b)
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Row row = readFirst(table.readByIndex(idxCol, idx2));
TableAssert.assertColumn(row, idxCol, idx2);
}
});
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// swap idx for c to 3
Assert.assertTrue(table.compareAndSwap(keyC, idxCol, idx1, idx3));
}
});
// read by idx 1 -> null (no row has that any more)
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
assertEmpty(table.readByIndex(idxCol, idx1));
// read by idx 3 > c
Row row = readFirst(table.readByIndex(idxCol, idx3));
TableAssert.assertColumns(row, new byte[][] { idxCol, valCol }, new byte[][] { idx3, valC });
}
});
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class IndexedTableTest method testIndexedRangeLookups.
@Test
public void testIndexedRangeLookups() throws Exception {
DatasetId indexRangedLookupDs = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("rangeLookup");
dsFrameworkUtil.createInstance("indexedTable", indexRangedLookupDs, DatasetProperties.builder().add(IndexedTable.INDEX_COLUMNS_CONF_KEY, idxColString).build());
final IndexedTable iTable = dsFrameworkUtil.getInstance(indexRangedLookupDs);
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(iTable);
try {
// start a new transaction
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// perform 5 puts, using idx values 1,2,3,4,5
iTable.put(new Put(keyE).add(idxCol, idx4).add(valCol, valE));
iTable.put(new Put(keyC).add(idxCol, idx1).add(valCol, valC));
iTable.put(new Put(keyD).add(idxCol, idx5).add(valCol, valA));
iTable.put(new Put(keyB).add(idxCol, idx2).add(valCol, valB));
iTable.put(new Put(keyA).add(idxCol, idx3).add(valCol, valD));
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// do a scan using idx value range [idx2, idx5). Assert that we retrieve idx2, idx3, idx4.
Scanner scanner = iTable.scanByIndex(idxCol, idx2, idx5);
Row next = scanner.next();
Assert.assertNotNull(next);
Assert.assertTrue(Bytes.equals(keyB, next.getRow()));
Assert.assertTrue(Bytes.equals(valB, next.get(valCol)));
next = scanner.next();
Assert.assertNotNull(next);
Assert.assertTrue(Bytes.equals(keyA, next.getRow()));
Assert.assertTrue(Bytes.equals(valD, next.get(valCol)));
next = scanner.next();
Assert.assertNotNull(next);
Assert.assertTrue(Bytes.equals(keyE, next.getRow()));
Assert.assertTrue(Bytes.equals(valE, next.get(valCol)));
assertEmpty(scanner);
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// do a scan using idx value range [null (first row), idx3). Assert that we retrieve the values corresponding
// to idx1, idx2.
Scanner scanner = iTable.scanByIndex(idxCol, null, idx3);
Row next = scanner.next();
Assert.assertNotNull(next);
Assert.assertTrue(Bytes.equals(keyC, next.getRow()));
Assert.assertTrue(Bytes.equals(valC, next.get(valCol)));
next = scanner.next();
Assert.assertNotNull(next);
Assert.assertTrue(Bytes.equals(keyB, next.getRow()));
Assert.assertTrue(Bytes.equals(valB, next.get(valCol)));
assertEmpty(scanner);
}
});
} finally {
dsFrameworkUtil.deleteInstance(indexRangedLookupDs);
}
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class ReflectionTableTest method testStructuredRecordRepresentation.
@Test
public void testStructuredRecordRepresentation() throws Exception {
dsFrameworkUtil.createInstance("table", users, DatasetProperties.builder().build());
try {
final Table usersTable = dsFrameworkUtil.getInstance(users);
final byte[] rowKey = Bytes.toBytes(123);
final Schema schema = new ReflectionSchemaGenerator().generate(User.class);
// TableDataset is not accessible here, but we know that's the underlying implementation...
TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor((TransactionAware) usersTable);
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Put put = new Put(rowKey);
ReflectionPutWriter<User> putWriter = new ReflectionPutWriter<>(schema);
putWriter.write(SAMUEL, put);
usersTable.put(put);
Row row = usersTable.get(rowKey);
ReflectionRowRecordReader rowReader = new ReflectionRowRecordReader(schema, null);
StructuredRecord actual = rowReader.read(row, schema);
assertRecordEqualsUser(SAMUEL, actual);
}
});
} finally {
dsFrameworkUtil.deleteInstance(users);
}
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class AppWithCustomTx method recordTransaction.
/**
* If in a transaction, records the timeout that the current transaction was given, or "default" if no explicit
* timeout was given. Otherwise does nothing.
*
* Note: we know whether and what explicit timeout was given, because we inject a {@link RevealingTxSystemClient},
* which returns a {@link RevealingTransaction} for {@link TransactionSystemClient#startShort(int)} only.
*/
static void recordTransaction(DatasetContext context, String row, String column) {
TransactionCapturingTable capture = context.getDataset(CAPTURE);
Transaction tx = capture.getTx();
// we cannot cast because the RevealingTransaction is not visible in the program class loader
String value = DEFAULT;
if (tx == null) {
try {
capture.getTable().put(new Put(row, column, value));
throw new RuntimeException("put to table without transaction should have failed.");
} catch (DataSetException e) {
// expected
}
return;
}
if ("RevealingTransaction".equals(tx.getClass().getSimpleName())) {
int txTimeout;
try {
txTimeout = (int) tx.getClass().getField("timeout").get(tx);
} catch (Exception e) {
throw Throwables.propagate(e);
}
value = String.valueOf(txTimeout);
}
capture.getTable().put(new Put(row, column, value));
}
Aggregations