use of io.cdap.cdap.api.dataset.DatasetContext in project cdap by caskdata.
the class DefaultMetricDatasetFactory method getOrCreateTable.
private MetricsTable getOrCreateTable(DatasetId tableId, DatasetProperties props) throws IOException {
DatasetContext datasetContext = DatasetContext.from(NamespaceId.SYSTEM.getNamespace());
DatasetSpecification spec = metricsTableDefinition.configure(tableId.getDataset(), props);
if (!existingDatasets.contains(tableId)) {
// Check and create if we don't know if the table exists or not
DatasetAdmin admin = metricsTableDefinition.getAdmin(datasetContext, spec, getClass().getClassLoader());
if (!admin.exists()) {
// All admin.create() implementations handled race condition for concurrent create.
// Not sure if that's the API contract or just the implementations since it is not specified in the API
// But from the dataset op executor implementation, it seems it is a required contract.
admin.create();
}
existingDatasets.add(tableId);
}
return metricsTableDefinition.getDataset(datasetContext, spec, Collections.emptyMap(), getClass().getClassLoader());
}
use of io.cdap.cdap.api.dataset.DatasetContext in project cdap by caskdata.
the class NoTxKeyValueTableTest method test.
@Test
public void test() throws IOException {
DatasetDefinition<? extends NoTxKeyValueTable, ? extends DatasetAdmin> def = getDefinition();
DatasetSpecification spec = def.configure("table", DatasetProperties.EMPTY);
ClassLoader cl = NoTxKeyValueTable.class.getClassLoader();
DatasetContext datasetContext = DatasetContext.from(NAMESPACE_ID.getEntityName());
// create & exists
DatasetAdmin admin = def.getAdmin(datasetContext, spec, cl);
Assert.assertFalse(admin.exists());
admin.create();
Assert.assertTrue(admin.exists());
// put/get
NoTxKeyValueTable table = def.getDataset(datasetContext, spec, NO_ARGS, cl);
Assert.assertNull(table.get(KEY1));
table.put(KEY1, VALUE1);
Assert.assertArrayEquals(VALUE1, table.get(KEY1));
Assert.assertNull(table.get(KEY2));
// override
table.put(KEY1, VALUE2);
Assert.assertArrayEquals(VALUE2, table.get(KEY1));
Assert.assertNull(table.get(KEY2));
// delete & truncate
table.put(KEY2, VALUE1);
Assert.assertArrayEquals(VALUE2, table.get(KEY1));
Assert.assertArrayEquals(VALUE1, table.get(KEY2));
table.put(KEY2, null);
Assert.assertNull(table.get(KEY2));
Assert.assertArrayEquals(VALUE2, table.get(KEY1));
admin.truncate();
Assert.assertNull(table.get(KEY1));
Assert.assertNull(table.get(KEY2));
Assert.assertTrue(admin.exists());
admin.drop();
Assert.assertFalse(admin.exists());
// drop should cleanup data
admin.create();
Assert.assertTrue(admin.exists());
Assert.assertNull(table.get(KEY1));
Assert.assertNull(table.get(KEY2));
table.put(KEY1, VALUE1);
Assert.assertArrayEquals(VALUE1, table.get(KEY1));
admin.drop();
Assert.assertFalse(admin.exists());
admin.create();
Assert.assertTrue(admin.exists());
Assert.assertNull(table.get(KEY1));
}
Aggregations