use of co.cask.cdap.api.dataset.table.Get 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.table.Get in project cdap by caskdata.
the class ProgramScheduleStoreDataset method addSchedules.
/**
* Add one or more schedules to the store.
*
* @param schedules the schedules to add
* @return the new schedules' last modified timestamp
* @throws AlreadyExistsException if one of the schedules already exists
*/
public long addSchedules(Iterable<? extends ProgramSchedule> schedules) throws AlreadyExistsException {
long currentTime = System.currentTimeMillis();
for (ProgramSchedule schedule : schedules) {
byte[] scheduleKey = rowKeyBytesForSchedule(schedule.getProgramId().getParent().schedule(schedule.getName()));
if (!store.get(new Get(scheduleKey)).isEmpty()) {
throw new AlreadyExistsException(schedule.getProgramId().getParent().schedule(schedule.getName()));
}
Put schedulePut = new Put(scheduleKey);
schedulePut.add(SCHEDULE_COLUMN_BYTES, GSON.toJson(schedule));
schedulePut.add(UPDATED_COLUMN_BYTES, currentTime);
// initially suspended
schedulePut.add(STATUS_COLUMN_BYTES, ProgramScheduleStatus.SUSPENDED.toString());
store.put(schedulePut);
int count = 0;
for (String triggerKey : extractTriggerKeys(schedule)) {
byte[] triggerRowKey = rowKeyBytesForTrigger(scheduleKey, count++);
store.put(new Put(triggerRowKey, TRIGGER_KEY_COLUMN_BYTES, triggerKey));
}
}
return currentTime;
}
use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.
the class ProgramScheduleStoreDataset method getScheduleRecord.
/**
* Read all information about a schedule from the store.
*
* @param scheduleId the id of the schedule to read
* @return the schedule record from the store
* @throws NotFoundException if the schedule does not exist in the store
*/
public ProgramScheduleRecord getScheduleRecord(ScheduleId scheduleId) throws NotFoundException {
Row row = store.get(new Get(rowKeyForSchedule(scheduleId)));
byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
if (serialized == null) {
throw new NotFoundException(scheduleId);
}
ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row);
return new ProgramScheduleRecord(schedule, meta);
}
use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.
the class ProgramScheduleStoreDataset method deleteSchedules.
/**
* Removes one or more schedules from the store. Succeeds whether the schedules exist or not.
*
* @param scheduleIds the schedules to delete
* @throws NotFoundException if one of the schedules does not exist in the store
*/
public void deleteSchedules(Iterable<? extends ScheduleId> scheduleIds) throws NotFoundException {
for (ScheduleId scheduleId : scheduleIds) {
String scheduleKey = rowKeyForSchedule(scheduleId);
if (store.get(new Get(scheduleKey)).isEmpty()) {
throw new NotFoundException(scheduleId);
}
store.delete(new Delete(scheduleKey));
byte[] prefix = keyPrefixForTriggerScan(scheduleKey);
try (Scanner scanner = store.scan(new Scan(prefix, Bytes.stopKeyForPrefix(prefix)))) {
Row row;
while ((row = scanner.next()) != null) {
store.delete(row.getRow());
}
}
}
}
use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.
the class ProgramScheduleStoreDataset method updateScheduleStatus.
/**
* Update the status of a schedule. This also updates the last-updated timestamp.
* @return the updated schedule's last modified timestamp
*/
public long updateScheduleStatus(ScheduleId scheduleId, ProgramScheduleStatus newStatus) throws NotFoundException {
long currentTime = System.currentTimeMillis();
String scheduleKey = rowKeyForSchedule(scheduleId);
Row row = store.get(new Get(scheduleKey));
if (row.isEmpty()) {
throw new NotFoundException(scheduleId);
}
Put updatePut = new Put(scheduleKey);
// record current time
updatePut.add(UPDATED_COLUMN_BYTES, currentTime);
updatePut.add(STATUS_COLUMN_BYTES, newStatus.toString());
store.put(updatePut);
return currentTime;
}
Aggregations