Search in sources :

Example 16 with MetadataRecord

use of co.cask.cdap.common.metadata.MetadataRecord 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 MetadataRecord

use of co.cask.cdap.common.metadata.MetadataRecord 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 MetadataRecord

use of co.cask.cdap.common.metadata.MetadataRecord 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 MetadataRecord

use of co.cask.cdap.common.metadata.MetadataRecord 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 MetadataRecord

use of co.cask.cdap.common.metadata.MetadataRecord in project cdap by caskdata.

the class ArtifactRepositoryTest method testDeletingArtifact.

@Test
public void testDeletingArtifact() throws Exception {
    MetadataRecord record = metadataStore.getMetadata(MetadataScope.SYSTEM, APP_ARTIFACT_ID.toEntityId());
    Assert.assertFalse(record.getProperties().isEmpty());
    artifactRepository.deleteArtifact(APP_ARTIFACT_ID);
    record = metadataStore.getMetadata(MetadataScope.SYSTEM, APP_ARTIFACT_ID.toEntityId());
    Assert.assertTrue(record.getProperties().isEmpty());
}
Also used : MetadataRecord(co.cask.cdap.common.metadata.MetadataRecord) Test(org.junit.Test)

Aggregations

MetadataRecord (co.cask.cdap.common.metadata.MetadataRecord)22 Test (org.junit.Test)10 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)9 BadRequestException (co.cask.cdap.common.BadRequestException)9 MetadataDataset (co.cask.cdap.data2.metadata.dataset.MetadataDataset)9 IOException (java.io.IOException)9 TransactionExecutor (org.apache.tephra.TransactionExecutor)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 DatasetId (co.cask.cdap.proto.id.DatasetId)6 Lineage (co.cask.cdap.data2.metadata.lineage.Lineage)4 Relation (co.cask.cdap.data2.metadata.lineage.Relation)4 NamespaceId (co.cask.cdap.proto.id.NamespaceId)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 EnumSet (java.util.EnumSet)4 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 Set (java.util.Set)4 AllProgramsApp (co.cask.cdap.client.app.AllProgramsApp)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3