use of co.cask.cdap.api.dataset.table.Scan in project cdap by caskdata.
the class WorkflowDataset method scan.
/**
* This function scans the workflow.stats dataset for a list of workflow runs in a time range.
*
* @param id The workflow id
* @param timeRangeStart Start of the time range that the scan should begin from
* @param timeRangeEnd End of the time range that the scan should end at
* @return List of WorkflowRunRecords
*/
private List<WorkflowRunRecord> scan(WorkflowId id, long timeRangeStart, long timeRangeEnd) {
byte[] startRowKey = getRowKeyBuilder(id, timeRangeStart).build().getKey();
byte[] endRowKey = getRowKeyBuilder(id, timeRangeEnd).build().getKey();
Scan scan = new Scan(startRowKey, endRowKey);
Scanner scanner = table.scan(scan);
Row indexRow;
List<WorkflowRunRecord> workflowRunRecordList = new ArrayList<>();
while ((indexRow = scanner.next()) != null) {
Map<byte[], byte[]> columns = indexRow.getColumns();
String workflowRunId = Bytes.toString(columns.get(RUNID));
long timeTaken = Bytes.toLong(columns.get(TIME_TAKEN));
List<ProgramRun> programRunList = GSON.fromJson(Bytes.toString(columns.get(NODES)), PROGRAM_RUNS_TYPE);
WorkflowRunRecord workflowRunRecord = new WorkflowRunRecord(workflowRunId, timeTaken, programRunList);
workflowRunRecordList.add(workflowRunRecord);
}
return workflowRunRecordList;
}
use of co.cask.cdap.api.dataset.table.Scan in project cdap by caskdata.
the class MetadataStoreDataset method listKV.
public <T> Map<MDSKey, T> listKV(MDSKey startId, @Nullable MDSKey stopId, Type typeOfT, int limit, Predicate<MDSKey> keyFilter, Predicate<T> valueFilter) {
byte[] startKey = startId.getKey();
byte[] stopKey = stopId == null ? Bytes.stopKeyForPrefix(startKey) : stopId.getKey();
Scan scan = new Scan(startKey, stopKey);
return listKV(scan, typeOfT, limit, keyFilter, valueFilter);
}
use of co.cask.cdap.api.dataset.table.Scan in project cdap by caskdata.
the class ProgramScheduleStoreDataset method deleteSchedules.
/**
* Removes all schedules for a specific program from the store.
*
* @param programId the program id for which to delete the schedules
* @return the IDs of the schedules that were deleted
*/
public List<ScheduleId> deleteSchedules(ProgramId programId) {
List<ScheduleId> deleted = new ArrayList<>();
// since all trigger row keys are prefixed by <scheduleRowKey>@,
// a scan for that prefix finds exactly the schedules and all of its triggers
byte[] prefix = keyPrefixForApplicationScan(programId.getParent());
try (Scanner scanner = store.scan(new Scan(prefix, Bytes.stopKeyForPrefix(prefix)))) {
Row row;
while ((row = scanner.next()) != null) {
byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
if (serialized != null) {
ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
if (programId.equals(schedule.getProgramId())) {
store.delete(row.getRow());
deleted.add(schedule.getScheduleId());
}
}
}
}
return deleted;
}
use of co.cask.cdap.api.dataset.table.Scan in project cdap by caskdata.
the class ProgramScheduleStoreDataset method listSchedules.
/*------------------- private helpers ---------------------*/
private List<ProgramSchedule> listSchedules(ApplicationId appId, @Nullable ProgramId programId) {
List<ProgramSchedule> result = new ArrayList<>();
byte[] prefix = keyPrefixForApplicationScan(appId);
try (Scanner scanner = store.scan(new Scan(prefix, Bytes.stopKeyForPrefix(prefix)))) {
Row row;
while ((row = scanner.next()) != null) {
byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
if (serialized != null) {
ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
if (programId == null || programId.equals(schedule.getProgramId())) {
result.add(schedule);
}
}
}
}
return result;
}
use of co.cask.cdap.api.dataset.table.Scan in project cdap by caskdata.
the class ObjectMappedTableDatasetTest method testScan.
@Test
public void testScan() throws Exception {
dsFrameworkUtil.createInstance(ObjectMappedTable.class.getName(), RECORDS_ID, ObjectMappedTableProperties.builder().setType(Record.class).build());
try {
final ObjectMappedTableDataset<Record> records = dsFrameworkUtil.getInstance(RECORDS_ID);
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) records);
Record record1 = new Record(Integer.MAX_VALUE, Long.MAX_VALUE, Float.MAX_VALUE, Double.MAX_VALUE, "foobar", Bytes.toBytes("foobar"), ByteBuffer.wrap(Bytes.toBytes("foobar")), UUID.randomUUID());
Record record2 = new Record(Integer.MIN_VALUE, Long.MIN_VALUE, Float.MIN_VALUE, Double.MIN_VALUE, "baz", Bytes.toBytes("baz"), ByteBuffer.wrap(Bytes.toBytes("baz")), UUID.randomUUID());
Record record3 = new Record(1, 0L, 3.14f, 3.14159265358979323846, "hello", Bytes.toBytes("world"), ByteBuffer.wrap(Bytes.toBytes("yo")), UUID.randomUUID());
final List<KeyValue<byte[], Record>> recordList = Lists.newArrayList();
recordList.add(new KeyValue<>(Bytes.toBytes("123"), record1));
recordList.add(new KeyValue<>(Bytes.toBytes("456"), record2));
recordList.add(new KeyValue<>(Bytes.toBytes("789"), record3));
for (final KeyValue<byte[], Record> record : recordList) {
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
records.write(record.getKey(), record.getValue());
}
});
}
final List<KeyValue<byte[], Record>> actualList = Lists.newArrayList();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
CloseableIterator<KeyValue<byte[], Record>> results = records.scan((String) null, null);
while (results.hasNext()) {
actualList.add(results.next());
}
results.close();
}
});
Assert.assertEquals(recordList.size(), actualList.size());
for (int i = 0; i < actualList.size(); i++) {
KeyValue<byte[], Record> expected = recordList.get(i);
KeyValue<byte[], Record> actual = actualList.get(i);
Assert.assertArrayEquals(expected.getKey(), actual.getKey());
Assert.assertEquals(expected.getValue(), actual.getValue());
}
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
CloseableIterator<KeyValue<byte[], Record>> results = records.scan("789", null);
KeyValue<byte[], Record> actualRecord = results.next();
Assert.assertFalse(results.hasNext());
Assert.assertArrayEquals(actualRecord.getKey(), recordList.get(2).getKey());
Assert.assertEquals(actualRecord.getValue(), recordList.get(2).getValue());
results.close();
results = records.scan(null, "124");
actualRecord = results.next();
Assert.assertFalse(results.hasNext());
Assert.assertArrayEquals(actualRecord.getKey(), recordList.get(0).getKey());
Assert.assertEquals(actualRecord.getValue(), recordList.get(0).getValue());
results.close();
results = records.scan(null, "123");
Assert.assertFalse(results.hasNext());
results.close();
}
});
actualList.clear();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Scan scan = new Scan(null, null);
CloseableIterator<KeyValue<byte[], Record>> results = records.scan(scan);
while (results.hasNext()) {
actualList.add(results.next());
}
}
});
Assert.assertEquals(recordList.size(), actualList.size());
} finally {
dsFrameworkUtil.deleteInstance(RECORDS_ID);
}
}
Aggregations