use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class TableTest method testMultiGetWithTx.
@Test
public void testMultiGetWithTx() throws Exception {
String testMultiGet = "testMultiGet";
DatasetAdmin admin = getTableAdmin(CONTEXT1, testMultiGet);
admin.create();
try {
Transaction tx = txClient.startShort();
Table table = getTable(CONTEXT1, testMultiGet);
((TransactionAware) table).startTx(tx);
for (int i = 0; i < 100; i++) {
table.put(new Put(Bytes.toBytes("r" + i)).add(C1, V1).add(C2, V2));
}
Assert.assertTrue(txClient.canCommit(tx, ((TransactionAware) table).getTxChanges()));
Assert.assertTrue(((TransactionAware) table).commitTx());
Assert.assertTrue(txClient.commit(tx));
Transaction tx2 = txClient.startShort();
((TransactionAware) table).startTx(tx2);
List<Get> gets = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < 100; i++) {
gets.add(new Get(Bytes.toBytes("r" + i)));
}
List<Row> results = table.get(gets);
Assert.assertTrue(txClient.commit(tx2));
for (int i = 0; i < 100; i++) {
Row row = results.get(i);
Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
byte[] val = row.get(C1);
Assert.assertNotNull(val);
Assert.assertArrayEquals(V1, val);
byte[] val2 = row.get(C2);
Assert.assertNotNull(val2);
Assert.assertArrayEquals(V2, val2);
}
Transaction tx3 = txClient.startShort();
((TransactionAware) table).startTx(tx3);
gets = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < 100; i++) {
gets.add(new Get("r" + i).add(C1));
}
results = table.get(gets);
Assert.assertTrue(txClient.commit(tx3));
for (int i = 0; i < 100; i++) {
Row row = results.get(i);
Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
byte[] val = row.get(C1);
Assert.assertNotNull(val);
Assert.assertArrayEquals(V1, val);
// should have only returned column 1
byte[] val2 = row.get(C2);
Assert.assertNull(val2);
}
// retrieve different columns per row
Transaction tx4 = txClient.startShort();
((TransactionAware) table).startTx(tx4);
gets = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < 100; i++) {
Get get = new Get("r" + i);
// evens get C1, odds get C2
get.add(i % 2 == 0 ? C1 : C2);
gets.add(get);
}
results = table.get(gets);
Assert.assertTrue(txClient.commit(tx4));
for (int i = 0; i < 100; i++) {
Row row = results.get(i);
Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
byte[] val1 = row.get(C1);
byte[] val2 = row.get(C2);
if (i % 2 == 0) {
Assert.assertNotNull(val1);
Assert.assertArrayEquals(V1, val1);
Assert.assertNull(val2);
} else {
Assert.assertNull(val1);
Assert.assertNotNull(val2);
Assert.assertArrayEquals(V2, val2);
}
}
} finally {
admin.drop();
}
}
use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class TableTest method testBasicColumnRangeWithTx.
@Test
public void testBasicColumnRangeWithTx() throws Exception {
// todo: test more tx logic (or add to get/put unit-test)
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try {
// write test data and commit
Transaction tx1 = txClient.startShort();
Table myTable1 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable1).startTx(tx1);
myTable1.put(R1, a(C1, C2, C3, C4, C5), a(V1, V2, V3, V4, V5));
myTable1.put(R2, a(C1), a(V2));
Assert.assertTrue(txClient.canCommit(tx1, ((TransactionAware) myTable1).getTxChanges()));
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
Assert.assertTrue(txClient.commit(tx1));
// Now, we will test column range get
Transaction tx2 = txClient.startShort();
Table myTable2 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable2).startTx(tx2);
// bounded range
TableAssert.assertRow(a(C2, V2, C3, V3, C4, V4), myTable2.get(R1, C2, C5, Integer.MAX_VALUE));
// open start range
TableAssert.assertRow(a(C1, V1, C2, V2, C3, V3), myTable2.get(R1, null, C4, Integer.MAX_VALUE));
// open end range
TableAssert.assertRow(a(C3, V3, C4, V4, C5, V5), myTable2.get(R1, C3, null, Integer.MAX_VALUE));
// open ends range
TableAssert.assertRow(a(C1, V1, C2, V2, C3, V3, C4, V4, C5, V5), myTable2.get(R1, null, null, Integer.MAX_VALUE));
// same with limit
// bounded range with limit
TableAssert.assertRow(a(C2, V2), myTable2.get(R1, C2, C5, 1));
// open start range with limit
TableAssert.assertRow(a(C1, V1, C2, V2), myTable2.get(R1, null, C4, 2));
// open end range with limit
TableAssert.assertRow(a(C3, V3, C4, V4), myTable2.get(R1, C3, null, 2));
// open ends range with limit
TableAssert.assertRow(a(C1, V1, C2, V2, C3, V3, C4, V4), myTable2.get(R1, null, null, 4));
// adding/changing/removing some columns
myTable2.put(R1, a(C1, C2, C3), a(V4, V3, V2));
myTable2.delete(R1, a(C4));
Assert.assertTrue(txClient.canCommit(tx2, ((TransactionAware) myTable2).getTxChanges()));
Assert.assertTrue(((TransactionAware) myTable2).commitTx());
Assert.assertTrue(txClient.commit(tx2));
// Checking that changes are reflected in new scans in new tx
Transaction tx3 = txClient.startShort();
// NOTE: table can be re-used betweet tx
((TransactionAware) myTable1).startTx(tx3);
TableAssert.assertRow(a(C2, V3, C3, V2), myTable1.get(R1, C2, C5, Integer.MAX_VALUE));
Assert.assertTrue(txClient.canCommit(tx3, ((TransactionAware) myTable1).getTxChanges()));
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
Assert.assertTrue(txClient.commit(tx3));
} finally {
admin.drop();
}
}
use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class CubeDatasetAdmin method update.
@Override
public void update(DatasetSpecification oldSpec) throws IOException {
// update all existing resolution tables, create all new resolutions
for (Map.Entry<String, DatasetSpecification> entry : spec.getSpecifications().entrySet()) {
DatasetSpecification oldSubSpec = spec.getSpecification(entry.getKey());
DatasetAdmin subAdmin = delegates.get(entry.getKey());
if (oldSubSpec != null && subAdmin instanceof Updatable) {
((Updatable) subAdmin).update(oldSubSpec);
} else if (oldSubSpec == null) {
subAdmin.create();
}
}
// TODO (CDAP-6342) delete all resolutions that were removed as part of the update
}
use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class InMemoryDatasetFramework method updateInstance.
@Override
public void updateInstance(DatasetId datasetInstanceId, DatasetProperties props) throws DatasetManagementException, IOException {
writeLock.lock();
try {
DatasetSpecification oldSpec = instances.get(datasetInstanceId.getParent(), datasetInstanceId);
if (oldSpec == null) {
throw new InstanceNotFoundException(datasetInstanceId.getEntityName());
}
DatasetDefinition def = getDefinitionForType(datasetInstanceId.getParent(), oldSpec.getType());
if (def == null) {
throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", oldSpec.getType(), datasetInstanceId.getParent()));
}
DatasetSpecification spec = AbstractDatasetDefinition.reconfigure(def, datasetInstanceId.getEntityName(), props, oldSpec).setOriginalProperties(props);
if (props.getDescription() != null) {
spec = spec.setDescription(props.getDescription());
}
instances.put(datasetInstanceId.getParent(), datasetInstanceId, spec);
DatasetAdmin admin = def.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec, null);
if (admin instanceof Updatable) {
((Updatable) admin).update(oldSpec);
} else {
admin.upgrade();
}
publishAudit(datasetInstanceId, AuditType.UPDATE);
} catch (IncompatibleUpdateException e) {
throw new InstanceConflictException("Update failed for dataset instance " + datasetInstanceId, e);
} finally {
writeLock.unlock();
}
}
use of co.cask.cdap.api.dataset.DatasetAdmin 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