use of co.cask.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class MdsKey method getMetadataKey.
static String getMetadataKey(String type, byte[] rowKey) {
MDSKey.Splitter keySplitter = new MDSKey(rowKey).split();
// The rowkey is
// [rowPrefix][targetType][targetId][key] for value rows and
// [rowPrefix][targetType][targetId][key][index] for value index rows
// so skip the first few strings.
// Skip rowType
keySplitter.skipBytes();
// Skip targetType
keySplitter.skipString();
// Skip targetId
if (type.equals(EntityIdKeyHelper.TYPE_MAP.get(ProgramId.class))) {
keySplitter.skipString();
keySplitter.skipString();
keySplitter.skipString();
keySplitter.skipString();
} else if (type.equals(EntityIdKeyHelper.TYPE_MAP.get(ApplicationId.class))) {
keySplitter.skipString();
keySplitter.skipString();
} else if (type.equals(EntityIdKeyHelper.TYPE_MAP.get(DatasetId.class))) {
keySplitter.skipString();
keySplitter.skipString();
} else if (type.equals(EntityIdKeyHelper.TYPE_MAP.get(StreamId.class))) {
keySplitter.skipString();
keySplitter.skipString();
} else if (type.equals(EntityIdKeyHelper.TYPE_MAP.get(StreamViewId.class))) {
// skip namespace, stream, view
keySplitter.skipString();
keySplitter.skipString();
keySplitter.skipString();
} else if (type.equals(EntityIdKeyHelper.TYPE_MAP.get(ArtifactId.class))) {
// skip namespace, name, version
keySplitter.skipString();
keySplitter.skipString();
keySplitter.skipString();
} else {
throw new IllegalArgumentException("Illegal Type " + type + " of metadata source.");
}
return keySplitter.getString();
}
use of co.cask.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class MetadataDataset method getMetadata.
/**
* Return metadata based on target id, and key.
*
* @param targetId 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(NamespacedEntityId targetId, String key) {
MDSKey mdsKey = MdsKey.getMDSValueKey(targetId, 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 moved one by one. The row still exists, but the value is null.
return null;
}
return new MetadataEntry(targetId, key, Bytes.toString(value));
}
use of co.cask.cdap.data2.dataset2.lib.table.MDSKey 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.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 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.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class MetadataDataset method getFuzzyKeyFor.
private ImmutablePair<byte[], byte[]> getFuzzyKeyFor(NamespacedEntityId targetId) {
// We need to create fuzzy pairs to match the first part of the key containing targetId
MDSKey mdsKey = MdsKey.getMDSValueKey(targetId, null);
byte[] keyBytes = mdsKey.getKey();
// byte array is automatically initialized to 0, which implies fixed match in fuzzy info
// the row key after targetId 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);
}
Aggregations