Search in sources :

Example 1 with MetadataKind

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);
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) MetadataDataset(io.cdap.cdap.data2.metadata.dataset.MetadataDataset) MetadataKind(io.cdap.cdap.spi.metadata.MetadataKind) HashSet(java.util.HashSet) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope)

Example 2 with MetadataKind

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);
}
Also used : MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) MetadataDirective(io.cdap.cdap.spi.metadata.MetadataDirective) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) USER(io.cdap.cdap.api.metadata.MetadataScope.USER) MetadataChange(io.cdap.cdap.spi.metadata.MetadataChange) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) HashSet(java.util.HashSet) TAG(io.cdap.cdap.spi.metadata.MetadataKind.TAG) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) Metadata(io.cdap.cdap.spi.metadata.Metadata) Map(java.util.Map) SearchRequest(io.cdap.cdap.spi.metadata.SearchRequest) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) Read(io.cdap.cdap.spi.metadata.Read) EnumSet(java.util.EnumSet) Sorting(io.cdap.cdap.spi.metadata.Sorting) SortInfo(io.cdap.cdap.data2.metadata.dataset.SortInfo) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) PROPERTY(io.cdap.cdap.spi.metadata.MetadataKind.PROPERTY) ImmutableMap(com.google.common.collect.ImmutableMap) Cursor(io.cdap.cdap.common.metadata.Cursor) SYSTEM(io.cdap.cdap.api.metadata.MetadataScope.SYSTEM) Set(java.util.Set) SearchResponse(io.cdap.cdap.spi.metadata.SearchResponse) IOException(java.io.IOException) MetadataKind(io.cdap.cdap.spi.metadata.MetadataKind) ScopedName(io.cdap.cdap.spi.metadata.ScopedName) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) EntityScope(io.cdap.cdap.proto.EntityScope) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope) Named(com.google.inject.name.Named) Constants(io.cdap.cdap.common.conf.Constants) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DatasetDefinition(io.cdap.cdap.api.dataset.DatasetDefinition) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MutationOptions(io.cdap.cdap.spi.metadata.MutationOptions) MetadataDataset(io.cdap.cdap.data2.metadata.dataset.MetadataDataset) Collections(java.util.Collections) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) MetadataDataset(io.cdap.cdap.data2.metadata.dataset.MetadataDataset) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MetadataKind(io.cdap.cdap.spi.metadata.MetadataKind)

Aggregations

MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)2 MetadataScope (io.cdap.cdap.api.metadata.MetadataScope)2 MetadataDataset (io.cdap.cdap.data2.metadata.dataset.MetadataDataset)2 MetadataKind (io.cdap.cdap.spi.metadata.MetadataKind)2 HashSet (java.util.HashSet)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 DatasetDefinition (io.cdap.cdap.api.dataset.DatasetDefinition)1 SYSTEM (io.cdap.cdap.api.metadata.MetadataScope.SYSTEM)1 USER (io.cdap.cdap.api.metadata.MetadataScope.USER)1 Constants (io.cdap.cdap.common.conf.Constants)1 Cursor (io.cdap.cdap.common.metadata.Cursor)1 ImmutablePair (io.cdap.cdap.common.utils.ImmutablePair)1 SortInfo (io.cdap.cdap.data2.metadata.dataset.SortInfo)1 EntityScope (io.cdap.cdap.proto.EntityScope)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1