Search in sources :

Example 6 with MetadataRecord

use of io.cdap.cdap.spi.metadata.MetadataRecord in project cdap by cdapio.

the class MetadataCompatibility method toV5Results.

/**
 * Convert a list of {@link MetadataRecord}s to an ordered set of 5.x {@link MetadataSearchResultRecord}s.
 *
 * The 5.x convention was that the results only contain non-empty records.
 */
private static Set<MetadataSearchResultRecord> toV5Results(List<MetadataRecord> results) {
    Set<MetadataSearchResultRecord> records = new LinkedHashSet<>();
    for (MetadataRecord record : results) {
        Map<MetadataScope, io.cdap.cdap.api.metadata.Metadata> map = toV5Metadata(record.getMetadata());
        records.add(new MetadataSearchResultRecord(record.getEntity(), Maps.filterValues(map, meta -> meta != null && !(meta.getProperties().isEmpty() && meta.getTags().isEmpty()))));
    }
    return records;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MetadataSearchResultRecord(io.cdap.cdap.proto.metadata.MetadataSearchResultRecord) Metadata(io.cdap.cdap.spi.metadata.Metadata) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope)

Example 7 with MetadataRecord

use of io.cdap.cdap.spi.metadata.MetadataRecord in project cdap by caskdata.

the class ElasticsearchMetadataStorage method createSearchResponse.

private io.cdap.cdap.spi.metadata.SearchResponse createSearchResponse(SearchRequest request, SearchResponse response, String cursor, int offset, int limit) {
    SearchHits hits = response.getHits();
    List<MetadataRecord> results = fromHits(hits);
    return new io.cdap.cdap.spi.metadata.SearchResponse(request, cursor, offset, limit, (int) hits.getTotalHits(), results);
}
Also used : SearchHits(org.elasticsearch.search.SearchHits) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with MetadataRecord

use of io.cdap.cdap.spi.metadata.MetadataRecord in project cdap by caskdata.

the class DatasetMetadataStorageTest method testCrossNamespacePagination.

// this test is specific to teh DatasetMetadataStorage, because of the specific way it tests pagination:
// it requests offsets that are not a multiple of the page size, which is not supported in all implementations.
@Test
public void testCrossNamespacePagination() throws IOException {
    MetadataStorage mds = getMetadataStorage();
    NamespaceId ns1Id = new NamespaceId("ns1");
    NamespaceId ns2Id = new NamespaceId("ns2");
    MetadataEntity ns1app1 = ns1Id.app("a1").toMetadataEntity();
    MetadataEntity ns1app2 = ns1Id.app("a2").toMetadataEntity();
    MetadataEntity ns1app3 = ns1Id.app("a3").toMetadataEntity();
    MetadataEntity ns2app1 = ns2Id.app("a1").toMetadataEntity();
    MetadataEntity ns2app2 = ns2Id.app("a2").toMetadataEntity();
    mds.batch(ImmutableList.of(new Update(ns1app1, new Metadata(USER, tags("v1"))), new Update(ns1app2, new Metadata(USER, tags("v1"))), new Update(ns1app3, new Metadata(USER, tags("v1"))), new Update(ns2app1, new Metadata(USER, tags("v1"))), new Update(ns2app2, new Metadata(USER, tags("v1")))), MutationOptions.DEFAULT);
    MetadataRecord record11 = new MetadataRecord(ns1app1, new Metadata(USER, tags("v1")));
    MetadataRecord record12 = new MetadataRecord(ns1app2, new Metadata(USER, tags("v1")));
    MetadataRecord record13 = new MetadataRecord(ns1app3, new Metadata(USER, tags("v1")));
    MetadataRecord record21 = new MetadataRecord(ns2app1, new Metadata(USER, tags("v1")));
    MetadataRecord record22 = new MetadataRecord(ns2app2, new Metadata(USER, tags("v1")));
    SearchResponse response = assertResults(mds, SearchRequest.of("*").setLimit(Integer.MAX_VALUE).setCursorRequested(true).build(), record11, record12, record13, record21, record22);
    // iterate over results to find the order in which they are returned
    Iterator<MetadataRecord> resultIter = response.getResults().iterator();
    MetadataRecord[] results = { resultIter.next(), resultIter.next(), resultIter.next(), resultIter.next(), resultIter.next() };
    // get 4 results (guaranteed to have at least one from each namespace), offset 1
    assertResults(mds, SearchRequest.of("*").setCursorRequested(true).setOffset(1).setLimit(4).build(), results[1], results[2], results[3], results[4]);
    // get the first four
    assertResults(mds, SearchRequest.of("*").setCursorRequested(true).setOffset(0).setLimit(4).build(), results[0], results[1], results[2], results[3]);
    // get middle 3
    assertResults(mds, SearchRequest.of("*").setCursorRequested(true).setOffset(1).setLimit(3).build(), results[1], results[2], results[3], results[3]);
    // clean up
    mds.batch(ImmutableList.of(new Drop(ns1app1), new Drop(ns1app2), new Drop(ns1app3), new Drop(ns2app1), new Drop(ns2app2)), MutationOptions.DEFAULT);
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) Metadata(io.cdap.cdap.spi.metadata.Metadata) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) SearchResponse(io.cdap.cdap.spi.metadata.SearchResponse) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) MetadataStorageTest(io.cdap.cdap.spi.metadata.MetadataStorageTest) Test(org.junit.Test)

Example 9 with MetadataRecord

use of io.cdap.cdap.spi.metadata.MetadataRecord in project cdap by caskdata.

the class DatasetMetadataStorageTest method testSearchWeight.

// this tests is not in MetadataStorageTest,
// because it tests result scoring and sorting specific to the dataset-based implementation
@Test
public void testSearchWeight() throws IOException {
    MetadataStorage mds = getMetadataStorage();
    String ns = "ns1";
    NamespaceId nsId = new NamespaceId(ns);
    MetadataEntity service1 = nsId.app("app1").service("service1").toMetadataEntity();
    MetadataEntity dataset1 = nsId.dataset("ds1").toMetadataEntity();
    MetadataEntity dataset2 = nsId.dataset("ds2").toMetadataEntity();
    // Add metadata
    String multiWordValue = "aV1 av2 ,  -  ,  av3 - av4_av5 av6";
    Map<String, String> userProps = ImmutableMap.of("key1", "value1", "key2", "value2", "multiword", multiWordValue);
    Map<String, String> systemProps = ImmutableMap.of("sysKey1", "sysValue1");
    Set<String> userTags = ImmutableSet.of("tag1", "tag2");
    Set<String> temporaryUserTags = ImmutableSet.of("tag3", "tag4");
    Map<String, String> dataset1UserProps = ImmutableMap.of("sKey1", "sValuee1 sValuee2");
    Map<String, String> dataset2UserProps = ImmutableMap.of("sKey1", "sValue1 sValue2", "Key1", "Value1");
    Set<String> sysTags = ImmutableSet.of("sysTag1");
    MetadataRecord service1Record = new MetadataRecord(service1, union(new Metadata(USER, userTags, userProps), new Metadata(SYSTEM, sysTags, systemProps)));
    mds.apply(new Update(service1Record.getEntity(), service1Record.getMetadata()), MutationOptions.DEFAULT);
    // dd and then remove some metadata for dataset2
    mds.apply(new Update(dataset2, new Metadata(USER, temporaryUserTags, userProps)), MutationOptions.DEFAULT);
    mds.apply(new Remove(dataset2, temporaryUserTags.stream().map(tag -> new ScopedNameOfKind(TAG, USER, tag)).collect(Collectors.toSet())), MutationOptions.DEFAULT);
    mds.apply(new Remove(dataset2, userProps.keySet().stream().map(tag -> new ScopedNameOfKind(PROPERTY, USER, tag)).collect(Collectors.toSet())), MutationOptions.DEFAULT);
    MetadataRecord dataset1Record = new MetadataRecord(dataset1, new Metadata(USER, tags(), dataset1UserProps));
    MetadataRecord dataset2Record = new MetadataRecord(dataset2, new Metadata(USER, tags(), dataset2UserProps));
    mds.batch(ImmutableList.of(new Update(dataset1Record.getEntity(), dataset1Record.getMetadata()), new Update(dataset2Record.getEntity(), dataset2Record.getMetadata())), MutationOptions.DEFAULT);
    // Test score and metadata match
    assertInOrder(mds, SearchRequest.of("value1 multiword:av2").addNamespace(ns).build(), service1Record, dataset2Record);
    assertInOrder(mds, SearchRequest.of("value1 sValue*").addNamespace(ns).setLimit(Integer.MAX_VALUE).build(), dataset2Record, dataset1Record, service1Record);
    assertResults(mds, SearchRequest.of("*").addNamespace(ns).setLimit(Integer.MAX_VALUE).build(), dataset2Record, dataset1Record, service1Record);
    // clean up
    mds.batch(ImmutableList.of(new Drop(service1), new Drop(dataset1), new Drop(dataset2)), MutationOptions.DEFAULT);
}
Also used : Module(com.google.inject.Module) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) SystemDatasetRuntimeModule(io.cdap.cdap.data.runtime.SystemDatasetRuntimeModule) Metadata(io.cdap.cdap.spi.metadata.Metadata) Map(java.util.Map) Closeables(com.google.common.io.Closeables) SearchRequest(io.cdap.cdap.spi.metadata.SearchRequest) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) EnumSet(java.util.EnumSet) PROPERTY(io.cdap.cdap.spi.metadata.MetadataKind.PROPERTY) AfterClass(org.junit.AfterClass) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) SYSTEM(io.cdap.cdap.api.metadata.MetadataScope.SYSTEM) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) BeforeClass(org.junit.BeforeClass) LocalLocationModule(io.cdap.cdap.common.guice.LocalLocationModule) AuthorizationTestModule(io.cdap.cdap.security.authorization.AuthorizationTestModule) USER(io.cdap.cdap.api.metadata.MetadataScope.USER) NamespaceAdminTestModule(io.cdap.cdap.common.guice.NamespaceAdminTestModule) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) TAG(io.cdap.cdap.spi.metadata.MetadataKind.TAG) AuthorizationEnforcementModule(io.cdap.cdap.security.authorization.AuthorizationEnforcementModule) ImmutableList(com.google.common.collect.ImmutableList) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) TransactionInMemoryModule(org.apache.tephra.runtime.TransactionInMemoryModule) MetadataStorageTest(io.cdap.cdap.spi.metadata.MetadataStorageTest) StorageModule(io.cdap.cdap.data.runtime.StorageModule) TransactionManager(org.apache.tephra.TransactionManager) Iterator(java.util.Iterator) Cursor(io.cdap.cdap.common.metadata.Cursor) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) SearchResponse(io.cdap.cdap.spi.metadata.SearchResponse) Test(org.junit.Test) IOException(java.io.IOException) Scopes(com.google.inject.Scopes) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) Injector(com.google.inject.Injector) EntityScope(io.cdap.cdap.proto.EntityScope) Guice(com.google.inject.Guice) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MutationOptions(io.cdap.cdap.spi.metadata.MutationOptions) Assert(org.junit.Assert) Remove(io.cdap.cdap.spi.metadata.MetadataMutation.Remove) Collections(java.util.Collections) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) AbstractModule(com.google.inject.AbstractModule) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) Metadata(io.cdap.cdap.spi.metadata.Metadata) Remove(io.cdap.cdap.spi.metadata.MetadataMutation.Remove) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MetadataStorageTest(io.cdap.cdap.spi.metadata.MetadataStorageTest) Test(org.junit.Test)

Example 10 with MetadataRecord

use of io.cdap.cdap.spi.metadata.MetadataRecord in project cdap by cdapio.

the class DatasetMetadataStorageTest method testSearchWeight.

// this tests is not in MetadataStorageTest,
// because it tests result scoring and sorting specific to the dataset-based implementation
@Test
public void testSearchWeight() throws IOException {
    MetadataStorage mds = getMetadataStorage();
    String ns = "ns1";
    NamespaceId nsId = new NamespaceId(ns);
    MetadataEntity service1 = nsId.app("app1").service("service1").toMetadataEntity();
    MetadataEntity dataset1 = nsId.dataset("ds1").toMetadataEntity();
    MetadataEntity dataset2 = nsId.dataset("ds2").toMetadataEntity();
    // Add metadata
    String multiWordValue = "aV1 av2 ,  -  ,  av3 - av4_av5 av6";
    Map<String, String> userProps = ImmutableMap.of("key1", "value1", "key2", "value2", "multiword", multiWordValue);
    Map<String, String> systemProps = ImmutableMap.of("sysKey1", "sysValue1");
    Set<String> userTags = ImmutableSet.of("tag1", "tag2");
    Set<String> temporaryUserTags = ImmutableSet.of("tag3", "tag4");
    Map<String, String> dataset1UserProps = ImmutableMap.of("sKey1", "sValuee1 sValuee2");
    Map<String, String> dataset2UserProps = ImmutableMap.of("sKey1", "sValue1 sValue2", "Key1", "Value1");
    Set<String> sysTags = ImmutableSet.of("sysTag1");
    MetadataRecord service1Record = new MetadataRecord(service1, union(new Metadata(USER, userTags, userProps), new Metadata(SYSTEM, sysTags, systemProps)));
    mds.apply(new Update(service1Record.getEntity(), service1Record.getMetadata()), MutationOptions.DEFAULT);
    // dd and then remove some metadata for dataset2
    mds.apply(new Update(dataset2, new Metadata(USER, temporaryUserTags, userProps)), MutationOptions.DEFAULT);
    mds.apply(new Remove(dataset2, temporaryUserTags.stream().map(tag -> new ScopedNameOfKind(TAG, USER, tag)).collect(Collectors.toSet())), MutationOptions.DEFAULT);
    mds.apply(new Remove(dataset2, userProps.keySet().stream().map(tag -> new ScopedNameOfKind(PROPERTY, USER, tag)).collect(Collectors.toSet())), MutationOptions.DEFAULT);
    MetadataRecord dataset1Record = new MetadataRecord(dataset1, new Metadata(USER, tags(), dataset1UserProps));
    MetadataRecord dataset2Record = new MetadataRecord(dataset2, new Metadata(USER, tags(), dataset2UserProps));
    mds.batch(ImmutableList.of(new Update(dataset1Record.getEntity(), dataset1Record.getMetadata()), new Update(dataset2Record.getEntity(), dataset2Record.getMetadata())), MutationOptions.DEFAULT);
    // Test score and metadata match
    assertInOrder(mds, SearchRequest.of("value1 multiword:av2").addNamespace(ns).build(), service1Record, dataset2Record);
    assertInOrder(mds, SearchRequest.of("value1 sValue*").addNamespace(ns).setLimit(Integer.MAX_VALUE).build(), dataset2Record, dataset1Record, service1Record);
    assertResults(mds, SearchRequest.of("*").addNamespace(ns).setLimit(Integer.MAX_VALUE).build(), dataset2Record, dataset1Record, service1Record);
    // clean up
    mds.batch(ImmutableList.of(new Drop(service1), new Drop(dataset1), new Drop(dataset2)), MutationOptions.DEFAULT);
}
Also used : Module(com.google.inject.Module) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) SystemDatasetRuntimeModule(io.cdap.cdap.data.runtime.SystemDatasetRuntimeModule) Metadata(io.cdap.cdap.spi.metadata.Metadata) Map(java.util.Map) Closeables(com.google.common.io.Closeables) SearchRequest(io.cdap.cdap.spi.metadata.SearchRequest) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) EnumSet(java.util.EnumSet) PROPERTY(io.cdap.cdap.spi.metadata.MetadataKind.PROPERTY) AfterClass(org.junit.AfterClass) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) SYSTEM(io.cdap.cdap.api.metadata.MetadataScope.SYSTEM) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) BeforeClass(org.junit.BeforeClass) LocalLocationModule(io.cdap.cdap.common.guice.LocalLocationModule) AuthorizationTestModule(io.cdap.cdap.security.authorization.AuthorizationTestModule) USER(io.cdap.cdap.api.metadata.MetadataScope.USER) NamespaceAdminTestModule(io.cdap.cdap.common.guice.NamespaceAdminTestModule) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) TAG(io.cdap.cdap.spi.metadata.MetadataKind.TAG) AuthorizationEnforcementModule(io.cdap.cdap.security.authorization.AuthorizationEnforcementModule) ImmutableList(com.google.common.collect.ImmutableList) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) TransactionInMemoryModule(org.apache.tephra.runtime.TransactionInMemoryModule) MetadataStorageTest(io.cdap.cdap.spi.metadata.MetadataStorageTest) StorageModule(io.cdap.cdap.data.runtime.StorageModule) TransactionManager(org.apache.tephra.TransactionManager) Iterator(java.util.Iterator) Cursor(io.cdap.cdap.common.metadata.Cursor) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) SearchResponse(io.cdap.cdap.spi.metadata.SearchResponse) Test(org.junit.Test) IOException(java.io.IOException) Scopes(com.google.inject.Scopes) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) Injector(com.google.inject.Injector) EntityScope(io.cdap.cdap.proto.EntityScope) Guice(com.google.inject.Guice) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MutationOptions(io.cdap.cdap.spi.metadata.MutationOptions) Assert(org.junit.Assert) Remove(io.cdap.cdap.spi.metadata.MetadataMutation.Remove) Collections(java.util.Collections) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) AbstractModule(com.google.inject.AbstractModule) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) Metadata(io.cdap.cdap.spi.metadata.Metadata) Remove(io.cdap.cdap.spi.metadata.MetadataMutation.Remove) Update(io.cdap.cdap.spi.metadata.MetadataMutation.Update) Drop(io.cdap.cdap.spi.metadata.MetadataMutation.Drop) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MetadataStorageTest(io.cdap.cdap.spi.metadata.MetadataStorageTest) Test(org.junit.Test)

Aggregations

MetadataRecord (io.cdap.cdap.spi.metadata.MetadataRecord)12 Metadata (io.cdap.cdap.spi.metadata.Metadata)10 MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)8 MetadataStorage (io.cdap.cdap.spi.metadata.MetadataStorage)8 SearchResponse (io.cdap.cdap.spi.metadata.SearchResponse)8 Cursor (io.cdap.cdap.common.metadata.Cursor)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 MutationOptions (io.cdap.cdap.spi.metadata.MutationOptions)6 ScopedNameOfKind (io.cdap.cdap.spi.metadata.ScopedNameOfKind)6 SearchRequest (io.cdap.cdap.spi.metadata.SearchRequest)6 IOException (java.io.IOException)6 Collections (java.util.Collections)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 MetadataScope (io.cdap.cdap.api.metadata.MetadataScope)5 Drop (io.cdap.cdap.spi.metadata.MetadataMutation.Drop)5 Update (io.cdap.cdap.spi.metadata.MetadataMutation.Update)5 MetadataStorageTest (io.cdap.cdap.spi.metadata.MetadataStorageTest)5 Test (org.junit.Test)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4