Search in sources :

Example 16 with MetadataDataset

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

the class DefaultMetadataStore method removeTags.

/**
 * Removes the specified tags from the {@link NamespacedEntityId}
 */
@Override
public void removeTags(final MetadataScope scope, final NamespacedEntityId namespacedEntityId, final String... tagsToRemove) {
    final AtomicReference<MetadataRecord> previousRef = new AtomicReference<>();
    execute(new TransactionExecutor.Procedure<MetadataDataset>() {

        @Override
        public void apply(MetadataDataset input) throws Exception {
            previousRef.set(new MetadataRecord(namespacedEntityId, scope, input.getProperties(namespacedEntityId), input.getTags(namespacedEntityId)));
            input.removeTags(namespacedEntityId, tagsToRemove);
        }
    }, scope);
    publishAudit(previousRef.get(), new MetadataRecord(namespacedEntityId, scope), new MetadataRecord(namespacedEntityId, scope, EMPTY_PROPERTIES, Sets.newHashSet(tagsToRemove)));
}
Also used : MetadataDataset(co.cask.cdap.data2.metadata.dataset.MetadataDataset) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransactionExecutor(org.apache.tephra.TransactionExecutor) MetadataRecord(co.cask.cdap.common.metadata.MetadataRecord) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 17 with MetadataDataset

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

the class DefaultMetadataStore method removeProperties.

/**
 * Removes all properties for the specified {@link NamespacedEntityId}.
 */
@Override
public void removeProperties(final MetadataScope scope, final NamespacedEntityId namespacedEntityId) {
    final AtomicReference<MetadataRecord> previousRef = new AtomicReference<>();
    execute(new TransactionExecutor.Procedure<MetadataDataset>() {

        @Override
        public void apply(MetadataDataset input) throws Exception {
            previousRef.set(new MetadataRecord(namespacedEntityId, scope, input.getProperties(namespacedEntityId), input.getTags(namespacedEntityId)));
            input.removeProperties(namespacedEntityId);
        }
    }, scope);
    publishAudit(previousRef.get(), new MetadataRecord(namespacedEntityId, scope), new MetadataRecord(namespacedEntityId, scope, previousRef.get().getProperties(), EMPTY_TAGS));
}
Also used : MetadataDataset(co.cask.cdap.data2.metadata.dataset.MetadataDataset) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransactionExecutor(org.apache.tephra.TransactionExecutor) MetadataRecord(co.cask.cdap.common.metadata.MetadataRecord) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 18 with MetadataDataset

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

the class DefaultMetadataStore method setProperties.

/**
 * Adds/updates metadata for the specified {@link NamespacedEntityId}.
 */
@Override
public void setProperties(final MetadataScope scope, final NamespacedEntityId namespacedEntityId, final Map<String, String> properties) {
    final AtomicReference<MetadataRecord> previousRef = new AtomicReference<>();
    execute(new TransactionExecutor.Procedure<MetadataDataset>() {

        @Override
        public void apply(MetadataDataset input) throws Exception {
            Map<String, String> existingProperties = input.getProperties(namespacedEntityId);
            Set<String> existingTags = input.getTags(namespacedEntityId);
            previousRef.set(new MetadataRecord(namespacedEntityId, scope, existingProperties, existingTags));
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                input.setProperty(namespacedEntityId, entry.getKey(), entry.getValue());
            }
        }
    }, scope);
    final ImmutableMap.Builder<String, String> propAdditions = ImmutableMap.builder();
    final ImmutableMap.Builder<String, String> propDeletions = ImmutableMap.builder();
    MetadataRecord previousRecord = previousRef.get();
    // Iterating over properties all over again, because we want to move the diff calculation outside the transaction.
    for (Map.Entry<String, String> entry : properties.entrySet()) {
        String existingValue = previousRecord.getProperties().get(entry.getKey());
        if (existingValue != null && existingValue.equals(entry.getValue())) {
            // Value already exists and is the same as the value being passed. No update necessary.
            continue;
        }
        // If it is an update, then mark a single deletion.
        if (existingValue != null) {
            propDeletions.put(entry.getKey(), existingValue);
        }
        // In both update or new cases, mark a single addition.
        propAdditions.put(entry.getKey(), entry.getValue());
    }
    publishAudit(previousRecord, new MetadataRecord(namespacedEntityId, scope, propAdditions.build(), EMPTY_TAGS), new MetadataRecord(namespacedEntityId, scope, propDeletions.build(), EMPTY_TAGS));
}
Also used : MetadataDataset(co.cask.cdap.data2.metadata.dataset.MetadataDataset) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransactionExecutor(org.apache.tephra.TransactionExecutor) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) MetadataEntry(co.cask.cdap.data2.metadata.dataset.MetadataEntry) MetadataRecord(co.cask.cdap.common.metadata.MetadataRecord) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 19 with MetadataDataset

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

the class DefaultMetadataStore method removeMetadata.

/**
 * Removes all metadata (including properties and tags) for the specified {@link NamespacedEntityId}.
 */
@Override
public void removeMetadata(final MetadataScope scope, final NamespacedEntityId namespacedEntityId) {
    final AtomicReference<MetadataRecord> previousRef = new AtomicReference<>();
    execute(new TransactionExecutor.Procedure<MetadataDataset>() {

        @Override
        public void apply(MetadataDataset input) throws Exception {
            previousRef.set(new MetadataRecord(namespacedEntityId, scope, input.getProperties(namespacedEntityId), input.getTags(namespacedEntityId)));
            input.removeProperties(namespacedEntityId);
            input.removeTags(namespacedEntityId);
        }
    }, scope);
    MetadataRecord previous = previousRef.get();
    publishAudit(previous, new MetadataRecord(namespacedEntityId, scope), new MetadataRecord(previous));
}
Also used : MetadataDataset(co.cask.cdap.data2.metadata.dataset.MetadataDataset) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransactionExecutor(org.apache.tephra.TransactionExecutor) MetadataRecord(co.cask.cdap.common.metadata.MetadataRecord) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 20 with MetadataDataset

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

the class DatasetMetadataStorage method addInScope.

private MetadataDataset.Change addInScope(MetadataDatasetContext context, MetadataScope scope, MetadataEntity entity, Set<String> tagsToAdd, Map<String, String> propertiesToAdd) {
    MetadataDataset dataset = context.getDataset(scope);
    MetadataDataset.Record before = null, after = null;
    if (tagsToAdd.isEmpty() && propertiesToAdd.isEmpty()) {
        before = dataset.getMetadata(entity);
        after = before;
    } else {
        if (!tagsToAdd.isEmpty()) {
            MetadataDataset.Change change = dataset.addTags(entity, tagsToAdd);
            before = change.getExisting();
            after = change.getLatest();
        }
        if (!propertiesToAdd.isEmpty()) {
            MetadataDataset.Change change = dataset.addProperties(entity, propertiesToAdd);
            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)

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