use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey 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.data2.dataset2.lib.table.MDSKey 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.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class MetadataDataset method getFuzzyKeyFor.
private ImmutablePair<byte[], byte[]> getFuzzyKeyFor(MetadataEntity metadataEntity) {
// We need to create fuzzy pairs to match the first part of the key containing metadataEntity
MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, null);
byte[] keyBytes = mdsKey.getKey();
// byte array is automatically initialized to 0, which implies fixed match in fuzzy info
// the row key after metadataEntity doesn't need to be a match.
// Workaround for HBASE-15676, need to have at least one 1 in the fuzzy filter
byte[] infoBytes = new byte[keyBytes.length + 1];
infoBytes[infoBytes.length - 1] = 1;
// the key array size and mask array size has to be equal so increase the size by 1
return new ImmutablePair<>(Bytes.concat(keyBytes, new byte[1]), infoBytes);
}
use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class DefaultPreviewStore method deleteExpiredData.
@Override
public void deleteExpiredData(long ttlInSeconds) {
Gson gson = new GsonBuilder().registerTypeAdapter(EntityId.class, new EntityIdTypeAdapter()).create();
byte[] startRowKey = new MDSKey.Builder().add(META_ROW_KEY_PREFIX).build().getKey();
byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
long currentTimeInSeconds = System.currentTimeMillis() / 1000;
try (Scanner scanner = previewTable.scan(startRowKey, stopRowKey, null, null, null)) {
Row indexRow;
while ((indexRow = scanner.next()) != null) {
Map<byte[], byte[]> columns = indexRow.getColumns();
String applicationIdGson = Bytes.toString(columns.get(APPID));
if (applicationIdGson == null) {
continue;
}
ApplicationId applicationId = gson.fromJson(applicationIdGson, ApplicationId.class);
long applicationSubmitTime = RunIds.getTime(applicationId.getApplication(), TimeUnit.SECONDS);
if ((currentTimeInSeconds - applicationSubmitTime) > ttlInSeconds) {
remove(applicationId);
}
}
} catch (IOException e) {
throw new RuntimeException("Error while scanning the preview requests for deletion.", e);
}
}
use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class DefaultPreviewStore method setPreviewStatus.
@Override
public void setPreviewStatus(ApplicationId applicationId, PreviewStatus previewStatus) {
// PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
Gson gson = new GsonBuilder().registerTypeAdapter(BasicThrowable.class, new BasicThrowableCodec()).create();
MDSKey mdsKey = getPreviewRowKeyBuilder(META_ROW_KEY_PREFIX, applicationId).build();
try {
previewTable.putDefaultVersion(mdsKey.getKey(), STATUS, Bytes.toBytes(gson.toJson(previewStatus)));
previewTable.putDefaultVersion(mdsKey.getKey(), APPID, Bytes.toBytes(gson.toJson(applicationId)));
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to put preview status %s for preview %s", previewStatus, applicationId), e);
}
}
Aggregations