Search in sources :

Example 6 with MetadataRecord

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

the class DefaultMetadataStore method removeProperties.

/**
   * Removes the specified properties of the {@link NamespacedEntityId}.
   */
@Override
public void removeProperties(final MetadataScope scope, final NamespacedEntityId namespacedEntityId, final String... keys) {
    final AtomicReference<MetadataRecord> previousRef = new AtomicReference<>();
    final ImmutableMap.Builder<String, String> deletesBuilder = ImmutableMap.builder();
    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)));
            for (String key : keys) {
                MetadataEntry record = input.getProperty(namespacedEntityId, key);
                if (record == null) {
                    continue;
                }
                deletesBuilder.put(record.getKey(), record.getValue());
            }
            input.removeProperties(namespacedEntityId, keys);
        }
    }, scope);
    publishAudit(previousRef.get(), new MetadataRecord(namespacedEntityId, scope), new MetadataRecord(namespacedEntityId, scope, deletesBuilder.build(), EMPTY_TAGS));
}
Also used : MetadataDataset(co.cask.cdap.data2.metadata.dataset.MetadataDataset) MetadataEntry(co.cask.cdap.data2.metadata.dataset.MetadataEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransactionExecutor(org.apache.tephra.TransactionExecutor) MetadataRecord(co.cask.cdap.proto.metadata.MetadataRecord) ImmutableMap(com.google.common.collect.ImmutableMap) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 7 with MetadataRecord

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

the class DefaultMetadataStore method addTags.

/**
   * Adds tags for the specified {@link NamespacedEntityId}.
   */
@Override
public void addTags(final MetadataScope scope, final NamespacedEntityId namespacedEntityId, final String... tagsToAdd) {
    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));
            input.addTags(namespacedEntityId, tagsToAdd);
        }
    }, scope);
    publishAudit(previousRef.get(), new MetadataRecord(namespacedEntityId, scope, EMPTY_PROPERTIES, Sets.newHashSet(tagsToAdd)), new MetadataRecord(namespacedEntityId, scope));
}
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) MetadataRecord(co.cask.cdap.proto.metadata.MetadataRecord) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 8 with MetadataRecord

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

the class DefaultMetadataStore method getSnapshotBeforeTime.

@Override
public Set<MetadataRecord> getSnapshotBeforeTime(MetadataScope scope, final Set<NamespacedEntityId> namespacedEntityIds, final long timeMillis) {
    Set<Metadata> metadataHistoryEntries = execute(new TransactionExecutor.Function<MetadataDataset, Set<Metadata>>() {

        @Override
        public Set<Metadata> apply(MetadataDataset input) throws Exception {
            return input.getSnapshotBeforeTime(namespacedEntityIds, timeMillis);
        }
    }, scope);
    ImmutableSet.Builder<MetadataRecord> builder = ImmutableSet.builder();
    for (Metadata metadata : metadataHistoryEntries) {
        builder.add(new MetadataRecord(metadata.getEntityId(), scope, metadata.getProperties(), metadata.getTags()));
    }
    return builder.build();
}
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) ImmutableSet(com.google.common.collect.ImmutableSet) Metadata(co.cask.cdap.data2.metadata.dataset.Metadata) TransactionExecutor(org.apache.tephra.TransactionExecutor) MetadataRecord(co.cask.cdap.proto.metadata.MetadataRecord) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 9 with MetadataRecord

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

the class DefaultMetadataStore method setProperty.

@Override
public void setProperty(final MetadataScope scope, final NamespacedEntityId namespacedEntityId, final String key, final String value) {
    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));
            input.setProperty(namespacedEntityId, key, value);
        }
    }, scope);
    publishAudit(previousRef.get(), new MetadataRecord(namespacedEntityId, scope, ImmutableMap.of(key, value), EMPTY_TAGS), new MetadataRecord(namespacedEntityId, scope));
}
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) MetadataRecord(co.cask.cdap.proto.metadata.MetadataRecord) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 10 with MetadataRecord

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

the class ArtifactHttpHandlerTest method testDeletePropertiesAndArtifacts.

@Test
public void testDeletePropertiesAndArtifacts() throws Exception {
    // add 2 versions of the same app that doesn't use config
    Id.Artifact wordcountId1 = Id.Artifact.from(Id.Namespace.DEFAULT, "wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), addAppArtifact(wordcountId1, WordCountApp.class).getStatusLine().getStatusCode());
    // test get /artifacts endpoint
    Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0"));
    Set<ArtifactSummary> actualArtifacts = getArtifacts(Id.Namespace.DEFAULT);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    addArtifactProperties(wordcountId1, ImmutableMap.of("key1", "value1", "key2", "value2", "key3", "value3"));
    Assert.assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2", "key3", "value3"), getArtifactProperties(wordcountId1));
    // delete a single property
    deleteArtifact(wordcountId1, false, "key1", 200);
    Assert.assertEquals(ImmutableMap.of("key2", "value2", "key3", "value3"), getArtifactProperties(wordcountId1));
    // delete all properties
    deleteArtifact(wordcountId1, false, null, 200);
    Assert.assertEquals(ImmutableMap.of(), getArtifactProperties(wordcountId1));
    Set<MetadataRecord> metadataRecords = metadataClient.getMetadata(wordcountId1, MetadataScope.USER);
    Assert.assertEquals(1, metadataRecords.size());
    Assert.assertEquals(new MetadataRecord(wordcountId1.toEntityId(), MetadataScope.USER), metadataRecords.iterator().next());
    // delete artifact
    deleteArtifact(wordcountId1, true, null, 200);
    try {
        metadataClient.getMetadata(wordcountId1, MetadataScope.USER);
        Assert.fail("Should not reach here");
    } catch (NotFoundException e) {
    // no-op
    }
    actualArtifacts = getArtifacts(Id.Namespace.DEFAULT);
    Assert.assertTrue(actualArtifacts.isEmpty());
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) WordCountApp(co.cask.cdap.WordCountApp) NotFoundException(co.cask.cdap.common.NotFoundException) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) MetadataRecord(co.cask.cdap.proto.metadata.MetadataRecord) Test(org.junit.Test)

Aggregations

MetadataRecord (co.cask.cdap.proto.metadata.MetadataRecord)23 Test (org.junit.Test)11 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 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 LinkedHashSet (java.util.LinkedHashSet)5 Lineage (co.cask.cdap.data2.metadata.lineage.Lineage)4 Relation (co.cask.cdap.data2.metadata.lineage.Relation)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 EnumSet (java.util.EnumSet)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 AllProgramsApp (co.cask.cdap.client.app.AllProgramsApp)3