use of io.cdap.cdap.spi.metadata.MetadataDirective in project cdap by caskdata.
the class DatasetMetadataStorage method replaceInScope.
private MetadataDataset.Change replaceInScope(MetadataDatasetContext context, MetadataScope scope, MetadataEntity entity, Set<String> newTags, Map<String, String> newProperties, Map<ScopedNameOfKind, MetadataDirective> directives) {
MetadataDataset dataset = context.getDataset(scope);
MetadataDataset.Record before = dataset.getMetadata(entity);
if (newTags.isEmpty() && newProperties.isEmpty()) {
// this scope remains unchanged
return new MetadataDataset.Change(before, before);
}
Set<String> existingTags = before.getTags();
Set<String> tagsToKeepOrPreserve = directives.entrySet().stream().filter(entry -> entry.getKey().getScope() == scope && entry.getKey().getKind() == TAG && (entry.getValue() == MetadataDirective.KEEP || entry.getValue() == MetadataDirective.PRESERVE)).map(Map.Entry::getKey).map(ScopedName::getName).filter(existingTags::contains).collect(Collectors.toSet());
newTags = Sets.union(newTags, tagsToKeepOrPreserve);
Map<String, String> existingProperties = before.getProperties();
Map<String, String> propertiesToKeepOrPreserve = directives.entrySet().stream().filter(entry -> entry.getKey().getScope() == scope && entry.getKey().getKind() == PROPERTY).filter(entry -> existingProperties.containsKey(entry.getKey().getName())).filter(entry -> entry.getValue() == MetadataDirective.PRESERVE || entry.getValue() == MetadataDirective.KEEP && !newProperties.containsKey(entry.getKey().getName())).map(Map.Entry::getKey).map(ScopedName::getName).collect(Collectors.toMap(name -> name, existingProperties::get));
newProperties.putAll(propertiesToKeepOrPreserve);
Set<String> tagsToRemove = Sets.difference(before.getTags(), newTags);
Set<String> tagsToAdd = Sets.difference(newTags, before.getTags());
Set<String> propertiesToRemove = Sets.difference(before.getProperties().keySet(), newProperties.keySet());
@SuppressWarnings("ConstantConditions") Map<String, String> propertiesToAdd = Maps.filterEntries(newProperties, entry -> !entry.getValue().equals(existingProperties.get(entry.getKey())));
MetadataDataset.Record after = before;
if (!tagsToRemove.isEmpty()) {
after = dataset.removeTags(entity, tagsToRemove).getLatest();
}
if (!tagsToAdd.isEmpty()) {
after = dataset.addTags(entity, tagsToAdd).getLatest();
}
if (!propertiesToRemove.isEmpty()) {
after = dataset.removeProperties(entity, propertiesToRemove).getLatest();
}
if (!propertiesToAdd.isEmpty()) {
after = dataset.addProperties(entity, propertiesToAdd).getLatest();
}
return new MetadataDataset.Change(before, after);
}
use of io.cdap.cdap.spi.metadata.MetadataDirective in project cdap by caskdata.
the class ElasticsearchMetadataStorage method create.
/**
* Creates the Elasticsearch index request for an entity creation or update.
* See {@link MetadataMutation.Create} for detailed semantics.
*
* @param before the metadata for the mutation's entity before the change
* @param create the mutation to apply
*
* @return an ElasticSearch request to be executed, and the change caused by the mutation
*/
private RequestAndChange create(VersionedMetadata before, MetadataMutation.Create create) {
// if the entity did not exist before, none of the directives apply and this is equivalent to update()
if (!before.existing()) {
return update(create.getEntity(), before, create.getMetadata());
}
Metadata meta = create.getMetadata();
Map<ScopedNameOfKind, MetadataDirective> directives = create.getDirectives();
// determine the scopes that this mutation applies to (scopes that do not occur in the metadata are no changed)
Set<MetadataScope> scopes = Stream.concat(meta.getTags().stream(), meta.getProperties().keySet().stream()).map(ScopedName::getScope).collect(Collectors.toSet());
// compute what previously existing tags and properties have to be preserved (all others are replaced)
Set<ScopedName> existingTagsToKeep = new HashSet<>();
Map<ScopedName, String> existingPropertiesToKeep = new HashMap<>();
// all tags and properties that are in a scope not affected by this mutation
Sets.difference(MetadataScope.ALL, scopes).forEach(scope -> {
before.getMetadata().getTags().stream().filter(tag -> tag.getScope().equals(scope)).forEach(existingTagsToKeep::add);
before.getMetadata().getProperties().entrySet().stream().filter(entry -> entry.getKey().getScope().equals(scope)).forEach(entry -> existingPropertiesToKeep.put(entry.getKey(), entry.getValue()));
});
// tags and properties in affected scopes that must be kept or preserved
directives.entrySet().stream().filter(entry -> scopes.contains(entry.getKey().getScope())).forEach(entry -> {
ScopedNameOfKind key = entry.getKey();
if (key.getKind() == MetadataKind.TAG && (entry.getValue() == MetadataDirective.PRESERVE || entry.getValue() == MetadataDirective.KEEP)) {
ScopedName tag = new ScopedName(key.getScope(), key.getName());
if (!meta.getTags().contains(tag) && before.getMetadata().getTags().contains(tag)) {
existingTagsToKeep.add(tag);
}
} else if (key.getKind() == MetadataKind.PROPERTY) {
ScopedName property = new ScopedName(key.getScope(), key.getName());
String existingValue = before.getMetadata().getProperties().get(property);
String newValue = meta.getProperties().get(property);
if (existingValue != null && (entry.getValue() == MetadataDirective.PRESERVE && !existingValue.equals(newValue) || entry.getValue() == MetadataDirective.KEEP && newValue == null)) {
existingPropertiesToKeep.put(property, existingValue);
}
}
});
// compute the new tags and properties
Set<ScopedName> newTags = existingTagsToKeep.isEmpty() ? meta.getTags() : Sets.union(meta.getTags(), existingTagsToKeep);
Map<ScopedName, String> newProperties = meta.getProperties();
if (!existingPropertiesToKeep.isEmpty()) {
newProperties = new HashMap<>(newProperties);
newProperties.putAll(existingPropertiesToKeep);
}
Metadata after = new Metadata(newTags, newProperties);
return new RequestAndChange(writeToIndex(create.getEntity(), before.getVersion(), after), new MetadataChange(create.getEntity(), before.getMetadata(), after));
}
Aggregations