Search in sources :

Example 21 with MetadataDataset

use of io.cdap.cdap.data2.metadata.dataset.MetadataDataset in project cdap by caskdata.

the class DatasetMetadataStorage method removeInScope.

private MetadataDataset.Change removeInScope(MetadataDatasetContext context, MetadataScope scope, MetadataEntity entity, Set<String> tagsToRemove, Set<String> propertiesToRemove) {
    MetadataDataset dataset = context.getDataset(scope);
    MetadataDataset.Record before = null;
    MetadataDataset.Record after = null;
    if (tagsToRemove.isEmpty() && propertiesToRemove.isEmpty()) {
        before = dataset.getMetadata(entity);
        after = before;
    } else {
        if (!tagsToRemove.isEmpty()) {
            MetadataDataset.Change change = dataset.removeTags(entity, tagsToRemove);
            before = change.getExisting();
            after = change.getLatest();
        }
        if (!propertiesToRemove.isEmpty()) {
            MetadataDataset.Change change = dataset.removeProperties(entity, propertiesToRemove);
            before = before != null ? before : change.getExisting();
            after = change.getLatest();
        }
    }
    return new MetadataDataset.Change(before, after);
}
Also used : MetadataDataset(io.cdap.cdap.data2.metadata.dataset.MetadataDataset) MetadataChange(io.cdap.cdap.spi.metadata.MetadataChange)

Example 22 with MetadataDataset

use of io.cdap.cdap.data2.metadata.dataset.MetadataDataset in project cdap by cdapio.

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

IOException (java.io.IOException)14 MetadataDataset (co.cask.cdap.data2.metadata.dataset.MetadataDataset)12 TransactionExecutor (org.apache.tephra.TransactionExecutor)12 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)10 BadRequestException (co.cask.cdap.common.BadRequestException)10 MetadataDataset (io.cdap.cdap.data2.metadata.dataset.MetadataDataset)10 MetadataChange (io.cdap.cdap.spi.metadata.MetadataChange)10 MetadataRecord (co.cask.cdap.common.metadata.MetadataRecord)9 EnumSet (java.util.EnumSet)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 HashMap (java.util.HashMap)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Map (java.util.Map)7 ImmutableSet (com.google.common.collect.ImmutableSet)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Maps (com.google.common.collect.Maps)4 Sets (com.google.common.collect.Sets)4 Inject (com.google.inject.Inject)4