use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class ArtifactStore method clear.
/**
* Clear all data in the given namespace. Used only in unit tests.
*
* @param namespace the namespace to delete data in
* @throws IOException if there was some problem deleting the data
*/
@VisibleForTesting
void clear(final NamespaceId namespace) throws IOException {
final Id.Namespace namespaceId = Id.Namespace.fromEntityId(namespace);
namespacedLocationFactory.get(namespace).append(ARTIFACTS_PATH).delete(true);
Transactionals.execute(transactional, context -> {
// delete all rows about artifacts in the namespace
Table metaTable = getMetaTable(context);
Row row;
try (Scanner scanner = metaTable.scan(scanArtifacts(namespace))) {
while ((row = scanner.next()) != null) {
metaTable.delete(row.getRow());
}
}
// delete all rows about artifacts in the namespace and the plugins they have access to
Scan pluginsScan = new Scan(Bytes.toBytes(String.format("%s:%s:", PLUGIN_PREFIX, namespace.getNamespace())), Bytes.toBytes(String.format("%s:%s;", PLUGIN_PREFIX, namespace.getNamespace())));
try (Scanner scanner = metaTable.scan(pluginsScan)) {
while ((row = scanner.next()) != null) {
metaTable.delete(row.getRow());
}
}
// delete all rows about universal plugins
try (Scanner scanner = metaTable.scan(scanUniversalPlugin(namespace.getNamespace(), null))) {
while ((row = scanner.next()) != null) {
metaTable.delete(row.getRow());
}
}
// delete app classes in this namespace
try (Scanner scanner = metaTable.scan(scanAppClasses(namespace))) {
while ((row = scanner.next()) != null) {
metaTable.delete(row.getRow());
}
}
// delete plugins in this namespace from system artifacts
// for example, if there was an artifact in this namespace that extends a system artifact
Scan systemPluginsScan = new Scan(Bytes.toBytes(String.format("%s:%s:", PLUGIN_PREFIX, Id.Namespace.SYSTEM.getId())), Bytes.toBytes(String.format("%s:%s;", PLUGIN_PREFIX, Id.Namespace.SYSTEM.getId())));
try (Scanner scanner = metaTable.scan(systemPluginsScan)) {
while ((row = scanner.next()) != null) {
for (Map.Entry<byte[], byte[]> columnVal : row.getColumns().entrySet()) {
// the column is the id of the artifact the plugin is from
ArtifactColumn column = ArtifactColumn.parse(columnVal.getKey());
// if the plugin artifact is in the namespace we're deleting, delete this column.
if (column.artifactId.getNamespace().equals(namespaceId)) {
metaTable.delete(row.getRow(), column.getColumn());
}
}
}
}
}, IOException.class);
}
use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class ProgramScheduleStoreDataset method listSchedules.
/*------------------- private helpers ---------------------*/
/**
* List schedules in a given application and if the programId is not null, only return the schedules
* which can launch the given program
*/
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.Scanner 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.Scanner in project cdap by caskdata.
the class ProgramScheduleStoreDataset method findSchedules.
/**
* Find all schedules that have a trigger with a given trigger key.
*
* @param triggerKey the trigger key to look up
* @return a list of all schedules that are triggered by this key; never null
*/
public Collection<ProgramScheduleRecord> findSchedules(String triggerKey) {
Map<ScheduleId, ProgramScheduleRecord> schedulesFound = new HashMap<>();
try (Scanner scanner = store.readByIndex(TRIGGER_KEY_COLUMN_BYTES, Bytes.toBytes(triggerKey))) {
Row triggerRow;
while ((triggerRow = scanner.next()) != null) {
String triggerRowKey = Bytes.toString(triggerRow.getRow());
try {
ScheduleId scheduleId = extractScheduleIdFromTriggerKey(triggerRowKey);
if (schedulesFound.containsKey(scheduleId)) {
continue;
}
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);
ProgramScheduleRecord record = new ProgramScheduleRecord(schedule, meta);
schedulesFound.put(scheduleId, record);
} catch (IllegalArgumentException | NotFoundException e) {
// the only exceptions we know to be thrown here are IllegalArgumentException (ill-formed key) or
// NotFoundException (if the schedule does not exist). Both should never happen, so we warn and ignore.
// we will let any other exception propagate up, because it would be a DataSetException or similarly serious.
LOG.warn("Problem with trigger id '{}' found for trigger key '{}': {}. Skipping entry.", triggerRowKey, triggerKey, e.getMessage());
}
}
}
return schedulesFound.values();
}
use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class ProgramScheduleStoreDataset method listScheduleRecords.
/**
* List schedule records in a given application and if the programId is not null, only return the schedules
* which can launch the given program
*/
private List<ProgramScheduleRecord> listScheduleRecords(ApplicationId appId, @Nullable ProgramId programId) {
List<ProgramScheduleRecord> 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(new ProgramScheduleRecord(schedule, extractMetaFromRow(schedule.getScheduleId(), row)));
}
}
}
}
return result;
}
Aggregations