use of co.cask.cdap.data2.dataset2.SimpleKVTable in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testSimpleDataset.
@Test
public void testSimpleDataset() throws Exception {
// Configuring Dataset types
DatasetFramework framework = getFramework();
// system namespace has a module orderedTable-inMemory
Assert.assertTrue(framework.hasSystemType("table"));
// myspace namespace has no modules
Assert.assertFalse(framework.hasType(IN_MEMORY_TYPE));
Assert.assertFalse(framework.hasType(SIMPLE_KV_TYPE));
// add module to namespace 'myspace'
framework.addModule(KEY_VALUE, new SingleTypeModule(SimpleKVTable.class));
// make sure it got added to 'myspace'
Assert.assertTrue(framework.hasType(SIMPLE_KV_TYPE));
// but not to 'system'
Assert.assertFalse(framework.hasSystemType(SimpleKVTable.class.getName()));
Assert.assertFalse(framework.hasInstance(MY_TABLE));
// Creating instance using a type from own namespace
framework.addInstance(SimpleKVTable.class.getName(), MY_TABLE, DatasetProperties.EMPTY);
// verify it got added to the right namespace
Assert.assertTrue(framework.hasInstance(MY_TABLE));
// and not to the system namespace
Assert.assertFalse(framework.hasInstance(NamespaceId.SYSTEM.dataset("my_table")));
// Doing some admin and data ops
DatasetAdmin admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final SimpleKVTable kvTable = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(kvTable);
TransactionExecutor txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) kvTable);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
kvTable.put("key1", "value1");
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", kvTable.get("key1"));
}
});
admin.truncate();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertTrue(kvTable.get("key1") == null);
}
});
// cleanup
framework.deleteInstance(MY_TABLE);
framework.deleteModule(KEY_VALUE);
// recreate instance without adding a module in 'myspace'. This should use types from default namespace
framework.addInstance("table", MY_TABLE, DatasetProperties.EMPTY);
// verify it got added to the right namespace
Assert.assertTrue(framework.hasInstance(MY_TABLE));
admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final Table table = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) table);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(Bytes.toBytes("key1"), Bytes.toBytes("column1"), Bytes.toBytes("value1"));
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", Bytes.toString(table.get(Bytes.toBytes("key1"), Bytes.toBytes("column1"))));
}
});
// cleanup
framework.deleteInstance(MY_TABLE);
}
Aggregations