use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataDataset method removeMetadata.
/**
* Removes all keys that satisfy a given predicate from the metadata of the specified {@link MetadataEntity}.
* @param metadataEntity the {@link MetadataEntity} for which keys are to be removed
* @param filter the {@link Predicate} that should be satisfied to remove a key
*/
private Change removeMetadata(MetadataEntity metadataEntity, Predicate<String> filter) {
MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, null);
byte[] prefix = mdsKey.getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(prefix);
Map<String, String> existingMetadata = new HashMap<>();
Map<String, String> deletedMetadata = new HashMap<>();
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 = MetadataKey.extractMetadataKey(next.getRow());
// put all the metadata for this entity as existing
existingMetadata.put(metadataKey, value);
if (filter.test(metadataKey)) {
// if the key matches the key to be deleted delete it and put it in deleted
indexedTable.delete(new Delete(next.getRow()));
// store the key to delete its indexes later
deletedMetadata.put(metadataKey, value);
}
}
}
// current metadata is existing - deleted
Map<String, String> currentMetadata = new HashMap<>(existingMetadata);
// delete all the indexes for all deleted metadata key
for (String deletedMetadataKey : deletedMetadata.keySet()) {
deleteIndexes(metadataEntity, deletedMetadataKey);
currentMetadata.remove(deletedMetadataKey);
}
Record changedMetadata = getMetadata(metadataEntity, currentMetadata);
writeHistory(changedMetadata);
return new Change(getMetadata(metadataEntity, existingMetadata), changedMetadata);
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataDataset method getMetadata.
/**
* Return metadata based on target id, and key.
*
* @param metadataEntity The id of the target
* @param key The metadata key to get
* @return instance of {@link MetadataEntry} for the target type, id, and key
*/
@Nullable
private MetadataEntry getMetadata(MetadataEntity metadataEntity, String key) {
MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, key);
Row row = indexedTable.get(mdsKey.getKey());
if (row.isEmpty()) {
return null;
}
byte[] value = row.get(VALUE_COLUMN);
if (value == null) {
// This can happen when all tags are removed one by one. The row still exists, but the value is null.
return null;
}
return new MetadataEntry(metadataEntity, key, Bytes.toString(value));
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataStoreDataset method listCombinedFilterKV.
private <T> Map<MDSKey, T> listCombinedFilterKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) {
try {
Map<MDSKey, T> map = Maps.newLinkedHashMap();
try (Scanner scan = table.scan(runScan)) {
Row next;
while ((limit > 0) && (next = scan.next()) != null) {
MDSKey key = new MDSKey(next.getRow());
byte[] columnValue = next.get(COLUMN);
if (columnValue == null) {
continue;
}
T value = deserialize(key, columnValue, typeOfT);
KeyValue<T> kv = new KeyValue<>(key, value);
// Combined Filter doesn't pass
if (combinedFilter != null && !combinedFilter.test(kv)) {
continue;
}
map.put(kv.getKey(), kv.getValue());
limit--;
}
return map;
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataStoreDataset method listKV.
private <T> Map<MDSKey, T> listKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<MDSKey> keyFilter, @Nullable Predicate<T> valueFilter) {
try {
Map<MDSKey, T> map = Maps.newLinkedHashMap();
try (Scanner scan = table.scan(runScan)) {
Row next;
while ((limit > 0) && (next = scan.next()) != null) {
MDSKey key = new MDSKey(next.getRow());
byte[] columnValue = next.get(COLUMN);
if (columnValue == null) {
continue;
}
T value = deserialize(key, columnValue, typeOfT);
// Key Filter doesn't pass
if (keyFilter != null && !keyFilter.test(key)) {
continue;
}
// If Value Filter doesn't pass
if (valueFilter != null && !valueFilter.test(value)) {
continue;
}
map.put(key, value);
limit--;
}
return map;
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataStoreDataset method getKV.
/**
* Get all non-null values of type T with the given ids for default COLUMN in a map
*
* @param ids set of the mds keys
* @param typeOfT the type of the result
* @return a map of the deserialized value of the result
*/
protected <T> Map<MDSKey, T> getKV(Set<MDSKey> ids, Type typeOfT) {
Map<MDSKey, T> resultMap = new HashMap<>();
List<Get> getList = new ArrayList<>();
for (MDSKey id : ids) {
getList.add(new Get(id.getKey()));
}
List<Row> rowList = table.get(getList);
for (Row row : rowList) {
if (row.isEmpty()) {
continue;
}
byte[] value = row.get(COLUMN);
if (value == null) {
continue;
}
MDSKey key = new MDSKey(row.getRow());
T result = deserialize(key, value, typeOfT);
resultMap.put(key, result);
}
return resultMap;
}
Aggregations