use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class MetadataDataset method getMetadata.
/**
* Retrieves the metadata for the specified {@link NamespacedEntityId}.
*
* @param targetId the specified {@link NamespacedEntityId}
* @return a Map representing the metadata for the specified {@link NamespacedEntityId}
*/
private Map<String, String> getMetadata(NamespacedEntityId targetId) {
String targetType = EntityIdKeyHelper.getTargetType(targetId);
MDSKey mdsKey = MdsKey.getMDSValueKey(targetId, null);
byte[] startKey = mdsKey.getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
Map<String, String> metadata = new HashMap<>();
try (Scanner scan = indexedTable.scan(startKey, stopKey)) {
Row next;
while ((next = scan.next()) != null) {
String key = MdsKey.getMetadataKey(targetType, next.getRow());
byte[] value = next.get(VALUE_COLUMN);
if (key == null || value == null) {
continue;
}
metadata.put(key, Bytes.toString(value));
}
return metadata;
}
}
use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class MetadataDataset method removeMetadata.
/**
* Removes all keys that satisfy a given predicate from the metadata of the specified {@link NamespacedEntityId}.
*
* @param targetId the {@link NamespacedEntityId} for which keys are to be removed
* @param filter the {@link Predicate} that should be satisfied to remove a key
*/
private void removeMetadata(NamespacedEntityId targetId, Predicate<String> filter) {
String targetType = EntityIdKeyHelper.getTargetType(targetId);
MDSKey mdsKey = MdsKey.getMDSValueKey(targetId, null);
byte[] prefix = mdsKey.getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(prefix);
List<String> deletedMetadataKeys = new LinkedList<>();
try (Scanner scan = indexedTable.scan(prefix, stopKey)) {
Row next;
while ((next = scan.next()) != null) {
String value = next.getString(VALUE_COLUMN);
if (value == null) {
continue;
}
String metadataKey = MdsKey.getMetadataKey(targetType, next.getRow());
if (filter.apply(metadataKey)) {
indexedTable.delete(new Delete(next.getRow()));
// store the key to delete its indexes later
deletedMetadataKeys.add(metadataKey);
}
}
}
// delete all the indexes for all deleted metadata key
for (String deletedMetadataKey : deletedMetadataKeys) {
deleteIndexes(targetId, deletedMetadataKey);
}
writeHistory(targetId);
}
use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class LevelDBTable method scanPersisted.
@ReadOnly
@Override
protected Scanner scanPersisted(Scan scan) throws Exception {
FuzzyRowFilter filter = null;
if (scan.getFilter() != null) {
// todo: currently we support only FuzzyRowFilter as an experimental feature
if (scan.getFilter() instanceof FuzzyRowFilter) {
filter = (FuzzyRowFilter) scan.getFilter();
} else {
throw new DataSetException("Unknown filter type: " + scan.getFilter());
}
}
final Scanner scanner = core.scan(scan.getStartRow(), scan.getStopRow(), filter, null, tx);
return new Scanner() {
@Nullable
@Override
public Row next() {
return LevelDBTable.this.next(scanner);
}
@Override
public void close() {
scanner.close();
}
};
}
use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class LineageDataset method getEntitiesForRun.
/**
* @return a set of entities (program and data it accesses) associated with a program run.
*/
public Set<NamespacedEntityId> getEntitiesForRun(ProgramRunId run) {
ImmutableSet.Builder<NamespacedEntityId> recordBuilder = ImmutableSet.builder();
byte[] startKey = getRunScanStartKey(run);
try (Scanner scanner = accessRegistryTable.scan(startKey, Bytes.stopKeyForPrefix(startKey))) {
Row row;
while ((row = scanner.next()) != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Got row key = {}", Bytes.toString(row.getRow()));
}
RowKey rowKey = parseRow(row);
if (run.getEntityName().equals(rowKey.getRunId().getId())) {
recordBuilder.add(rowKey.getProgram());
recordBuilder.add(rowKey.getData());
}
}
}
return recordBuilder.build();
}
use of co.cask.cdap.api.dataset.table.Scanner in project cdap by caskdata.
the class MetadataStoreDataset method deleteAll.
public void deleteAll(MDSKey id, @Nullable Predicate<MDSKey> filter) {
byte[] prefix = id.getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(prefix);
try {
try (Scanner scan = table.scan(prefix, stopKey)) {
Row next;
while ((next = scan.next()) != null) {
String columnValue = next.getString(COLUMN);
if (columnValue == null) {
continue;
}
MDSKey key = new MDSKey(next.getRow());
if (filter != null && !filter.test(key)) {
continue;
}
table.delete(new Delete(next.getRow()).add(COLUMN));
}
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
Aggregations