Search in sources :

Example 6 with DatasetContext

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());
}
Also used : DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) DatasetContext(io.cdap.cdap.api.dataset.DatasetContext)

Example 7 with DatasetContext

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));
}
Also used : DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) DatasetContext(io.cdap.cdap.api.dataset.DatasetContext) Test(org.junit.Test)

Aggregations

DatasetContext (io.cdap.cdap.api.dataset.DatasetContext)7 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)5 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)3 IOException (java.io.IOException)3 DatasetDefinition (io.cdap.cdap.api.dataset.DatasetDefinition)2 IncompatibleUpdateException (io.cdap.cdap.api.dataset.IncompatibleUpdateException)2 Test (org.junit.Test)2 Transactional (io.cdap.cdap.api.Transactional)1 DatasetInstantiationException (io.cdap.cdap.api.data.DatasetInstantiationException)1 Dataset (io.cdap.cdap.api.dataset.Dataset)1 Updatable (io.cdap.cdap.api.dataset.Updatable)1 CompositeDatasetDefinition (io.cdap.cdap.api.dataset.lib.CompositeDatasetDefinition)1 IndexedTableDefinition (io.cdap.cdap.api.dataset.lib.IndexedTableDefinition)1 DatasetType (io.cdap.cdap.api.dataset.module.DatasetType)1 EmbeddedDataset (io.cdap.cdap.api.dataset.module.EmbeddedDataset)1 AccessException (io.cdap.cdap.api.security.AccessException)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 DatasetType (io.cdap.cdap.data2.datafabric.dataset.DatasetType)1 DatasetClassLoaderProvider (io.cdap.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider)1