use of io.cdap.cdap.spi.metadata.MetadataKind in project cdap by caskdata.
the class DatasetMetadataStorage method remove.
private MetadataChange remove(MetadataDatasetContext context, MetadataMutation.Remove remove) {
MetadataEntity entity = remove.getEntity();
MetadataDataset.Change userChange, systemChange;
if (remove.getRemovals() != null) {
Set<String> userTagsToRemove = new HashSet<>();
Set<String> systemTagsToRemove = new HashSet<>();
Set<String> userPropertiesToRemove = new HashSet<>();
Set<String> systemPropertiesToRemove = new HashSet<>();
remove.getRemovals().forEach(removal -> (TAG == removal.getKind() ? USER == removal.getScope() ? userTagsToRemove : systemTagsToRemove : USER == removal.getScope() ? userPropertiesToRemove : systemPropertiesToRemove).add(removal.getName()));
userChange = removeInScope(context, USER, entity, userTagsToRemove, userPropertiesToRemove);
systemChange = removeInScope(context, SYSTEM, entity, systemTagsToRemove, systemPropertiesToRemove);
} else {
Set<MetadataScope> scopes = remove.getScopes();
Set<MetadataKind> kinds = remove.getKinds();
userChange = removeScope(context, USER, entity, scopes, kinds);
systemChange = removeScope(context, SYSTEM, entity, scopes, kinds);
}
return combineChanges(entity, userChange, systemChange);
}
use of io.cdap.cdap.spi.metadata.MetadataKind in project cdap by caskdata.
the class DatasetMetadataStorage method readScope.
private MetadataDataset.Record readScope(MetadataDatasetContext context, MetadataScope scope, Read read) {
MetadataEntity entity = read.getEntity();
if (read.getSelection() == null && (!read.getScopes().contains(scope) || read.getKinds().isEmpty())) {
return new MetadataDataset.Record(entity);
}
Set<ScopedNameOfKind> selectionForScope = null;
if (read.getSelection() != null) {
// noinspection ConstantConditions
selectionForScope = Sets.filter(read.getSelection(), entry -> entry.getScope() == scope);
if (selectionForScope.isEmpty()) {
return new MetadataDataset.Record(entity);
}
}
// now we know we must read from the dataset
MetadataDataset dataset = context.getDataset(scope);
if (selectionForScope != null) {
// request is for a specific set of tags and properties
Set<String> tagsToRead = selectionForScope.stream().filter(entry -> TAG == entry.getKind()).map(ScopedName::getName).collect(Collectors.toSet());
Set<String> propertiesToRead = selectionForScope.stream().filter(entry -> PROPERTY == entry.getKind()).map(ScopedName::getName).collect(Collectors.toSet());
Set<String> tags = tagsToRead.isEmpty() ? Collections.emptySet() : Sets.intersection(tagsToRead, dataset.getTags(entity));
Map<String, String> properties = propertiesToRead.isEmpty() ? Collections.emptyMap() : Maps.filterKeys(dataset.getProperties(entity), propertiesToRead::contains);
return new MetadataDataset.Record(entity, properties, tags);
}
if (MetadataKind.ALL.equals(read.getKinds())) {
// all metadata kinds requested
return dataset.getMetadata(entity);
}
// exactly one kind is requested
MetadataKind requestKind = read.getKinds().iterator().next();
if (requestKind == TAG) {
return new MetadataDataset.Record(entity, Collections.emptyMap(), dataset.getTags(entity));
}
if (requestKind == PROPERTY) {
return new MetadataDataset.Record(entity, dataset.getProperties(entity), Collections.emptySet());
}
throw new IllegalStateException("Encountered metadata read request for unknown kind " + requestKind);
}
Aggregations