use of io.cdap.cdap.api.dataset.table.Row 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 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 caskdata.
the class MetadataDataset method searchByDefaultIndex.
private SearchResults searchByDefaultIndex(SearchRequest request) {
List<MetadataEntry> results = new LinkedList<>();
String column = request.isNamespaced() ? DEFAULT_INDEX_COLUMN.getColumn() : DEFAULT_INDEX_COLUMN.getCrossNamespaceColumn();
for (SearchTerm searchTerm : getSearchTerms(request)) {
Scanner scanner;
if (searchTerm.isPrefix()) {
// if prefixed search get start and stop key
byte[] startKey = Bytes.toBytes(searchTerm.getTerm());
@SuppressWarnings("ConstantConditions") byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
scanner = indexedTable.scanByIndex(Bytes.toBytes(column), startKey, stopKey);
} else {
byte[] value = Bytes.toBytes(searchTerm.getTerm());
scanner = indexedTable.readByIndex(Bytes.toBytes(column), value);
}
try {
Row next;
while ((next = scanner.next()) != null) {
Optional<MetadataEntry> metadataEntry = parseRow(next, column, request.getTypes(), request.shouldShowHidden());
metadataEntry.ifPresent(results::add);
}
} finally {
scanner.close();
}
}
// cursors are currently not supported for default indexes
return new SearchResults(results, Collections.emptyList());
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by caskdata.
the class MetadataDataset method deleteIndexes.
/**
* Deletes all indexes associated with a metadata key
*
* @param metadataEntity the {@link MetadataEntity} for which keys are to be removed
* @param metadataKey the key to remove from the metadata of the specified {@link MetadataEntity}
*/
private void deleteIndexes(MetadataEntity metadataEntity, String metadataKey) {
MDSKey mdsKey = MetadataKey.createIndexRowKey(metadataEntity, metadataKey, null);
byte[] startKey = mdsKey.getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
try (Scanner scan = indexedTable.scan(startKey, stopKey)) {
Row next;
while ((next = scan.next()) != null) {
deleteIndexRow(next);
}
}
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by caskdata.
the class MetadataDataset method getSnapshotBeforeTime.
private Record getSnapshotBeforeTime(MetadataEntity metadataEntity, long timeMillis) {
byte[] scanStartKey = MetadataHistoryKey.getMDSScanStartKey(metadataEntity, timeMillis).getKey();
byte[] scanEndKey = MetadataHistoryKey.getMDSScanStopKey(metadataEntity).getKey();
// TODO: add limit to scan, we need only one row
try (Scanner scanner = indexedTable.scan(scanStartKey, scanEndKey)) {
Row next = scanner.next();
if (next != null) {
return GSON.fromJson(next.getString(HISTORY_COLUMN), Record.class);
} else {
return new Record(metadataEntity);
}
}
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by caskdata.
the class MetadataStoreDataset method exists.
/**
* Check whether the value is there in the row and default COLUMN
*
* @param id the mds key for the row
* @return a boolean which indicates the value exists or not
*/
public boolean exists(MDSKey id) {
Row row = table.get(id.getKey());
if (row.isEmpty()) {
return false;
}
byte[] value = row.get(COLUMN);
return value != null;
}
Aggregations