Search in sources :

Example 1 with EntityScope

use of io.cdap.cdap.proto.EntityScope in project cdap by caskdata.

the class DatasetMetadataStorage method determineNamespaceAndScopes.

@VisibleForTesting
static ImmutablePair<NamespaceId, Set<EntityScope>> determineNamespaceAndScopes(Set<String> namespaces) {
    // if the request does not specify namespaces at all, then it searches all, including system
    if (namespaces == null || namespaces.isEmpty()) {
        return ImmutablePair.of(null, EnumSet.allOf(EntityScope.class));
    }
    boolean hasSystem = false;
    Set<String> userNamespaces = namespaces;
    if (namespaces.contains(NamespaceId.SYSTEM.getNamespace())) {
        userNamespaces = new HashSet<>(namespaces);
        userNamespaces.remove(NamespaceId.SYSTEM.getNamespace());
        // if the request only specifies the system namespace, search that namespace and system scope
        if (userNamespaces.isEmpty()) {
            return ImmutablePair.of(NamespaceId.SYSTEM, EnumSet.of(EntityScope.SYSTEM));
        }
        hasSystem = true;
    }
    // we have at least one non-system namespace
    if (userNamespaces.size() > 1) {
        throw new UnsupportedOperationException(String.format("This implementation supports at most one non-system namespace, but %s were requested", userNamespaces));
    }
    // we have exactly one non-system namespace
    NamespaceId namespace = new NamespaceId(userNamespaces.iterator().next());
    return hasSystem ? ImmutablePair.of(namespace, EnumSet.allOf(EntityScope.class)) : ImmutablePair.of(namespace, EnumSet.of(EntityScope.USER));
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) EntityScope(io.cdap.cdap.proto.EntityScope) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with EntityScope

use of io.cdap.cdap.proto.EntityScope in project cdap by caskdata.

the class DatasetMetadataStorage method search.

@Override
public SearchResponse search(SearchRequest request) {
    Cursor cursor = request.getCursor() != null && !request.getCursor().isEmpty() ? Cursor.fromString(request.getCursor()) : null;
    Set<String> namespaces = cursor == null ? request.getNamespaces() : cursor.getNamespaces();
    ImmutablePair<NamespaceId, Set<EntityScope>> namespaceAndScopes = determineNamespaceAndScopes(namespaces);
    CursorAndOffsetInfo cursorOffsetAndLimits = determineCursorOffsetAndLimits(request, cursor);
    String query = cursor != null ? cursor.getQuery() : request.getQuery() == null || request.getQuery().isEmpty() ? "*" : request.getQuery();
    Set<String> types = cursor != null ? cursor.getTypes() : request.getTypes();
    types = types == null ? Collections.emptySet() : types;
    Sorting sorting = cursor == null ? request.getSorting() : cursor.getSorting() == null ? null : Sorting.of(cursor.getSorting());
    SortInfo sortInfo = sorting == null ? SortInfo.DEFAULT : new SortInfo(sorting.getKey(), SortInfo.SortOrder.valueOf(sorting.getOrder().name()));
    boolean showHidden = cursor != null ? cursor.isShowHidden() : request.isShowHidden();
    MetadataScope scope = cursor != null ? cursor.getScope() : request.getScope();
    MetadataSearchResponse response = search(new io.cdap.cdap.data2.metadata.dataset.SearchRequest(namespaceAndScopes.getFirst(), query, types, sortInfo, cursorOffsetAndLimits.getOffsetToRequest(), cursorOffsetAndLimits.getLimitToRequest(), request.isCursorRequested() ? 1 : 0, cursorOffsetAndLimits.getCursor(), showHidden, namespaceAndScopes.getSecond()), scope);
    // translate results back and limit them to at most what was requested (see above where we add 1)
    int limitToRespond = cursorOffsetAndLimits.getLimitToRespond();
    int offsetToRespond = cursorOffsetAndLimits.getOffsetToRespond();
    List<MetadataRecord> results = response.getResults().stream().limit(limitToRespond).map(record -> {
        Metadata metadata = Metadata.EMPTY;
        for (Map.Entry<MetadataScope, io.cdap.cdap.api.metadata.Metadata> entry : record.getMetadata().entrySet()) {
            Metadata toAdd = new Metadata(entry.getKey(), entry.getValue().getTags(), entry.getValue().getProperties());
            metadata = mergeDisjointMetadata(metadata, toAdd);
        }
        return new MetadataRecord(record.getMetadataEntity(), metadata);
    }).collect(Collectors.toList());
    Cursor newCursor = null;
    if (response.getCursors() != null && !response.getCursors().isEmpty()) {
        String actualCursor = response.getCursors().get(0);
        if (cursor != null) {
            // the new cursor's offset is the previous cursor's offset plus the number of results
            newCursor = new Cursor(cursor, cursor.getOffset() + results.size(), actualCursor);
        } else {
            newCursor = new Cursor(offsetToRespond + results.size(), limitToRespond, showHidden, scope, namespaces, types, sorting == null ? null : sorting.toString(), actualCursor, query);
        }
    }
    // adjust the total results by the difference of requested offset and the true offset that we respond back
    int totalResults = offsetToRespond - cursorOffsetAndLimits.getOffsetToRequest() + response.getTotal();
    return new SearchResponse(request, newCursor == null ? null : newCursor.toString(), offsetToRespond, limitToRespond, totalResults, results);
}
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) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) Metadata(io.cdap.cdap.spi.metadata.Metadata) Cursor(io.cdap.cdap.common.metadata.Cursor) MetadataRecord(io.cdap.cdap.spi.metadata.MetadataRecord) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope) MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) Sorting(io.cdap.cdap.spi.metadata.Sorting) SortInfo(io.cdap.cdap.data2.metadata.dataset.SortInfo) MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) SearchResponse(io.cdap.cdap.spi.metadata.SearchResponse) NamespaceId(io.cdap.cdap.proto.id.NamespaceId)

Example 3 with EntityScope

use of io.cdap.cdap.proto.EntityScope in project cdap by caskdata.

the class MetadataDatasetTest method testDetermineSearchFields.

@Test
public void testDetermineSearchFields() {
    String term = "a";
    String myns = "myns";
    Optional<NamespaceId> noNs = Optional.empty();
    Optional<NamespaceId> userNs = Optional.of(new NamespaceId(myns));
    Optional<NamespaceId> systemNs = Optional.of(NamespaceId.SYSTEM);
    Set<EntityScope> noScopes = Collections.emptySet();
    Set<EntityScope> userScope = Collections.singleton(EntityScope.USER);
    Set<EntityScope> systemScope = Collections.singleton(EntityScope.SYSTEM);
    Set<EntityScope> allScopes = EnumSet.allOf(EntityScope.class);
    MetadataDataset.SearchTerm a = MetadataDataset.SearchTerm.from(term);
    MetadataDataset.SearchTerm userA = MetadataDataset.SearchTerm.from(userNs.get(), term);
    MetadataDataset.SearchTerm systemA = MetadataDataset.SearchTerm.from(NamespaceId.SYSTEM, term);
    List<Triple<Optional<NamespaceId>, Set<EntityScope>, Set<MetadataDataset.SearchTerm>>> cases = ImmutableList.<Triple<Optional<NamespaceId>, Set<EntityScope>, Set<MetadataDataset.SearchTerm>>>builder().add(Triple.of(noNs, noScopes, ImmutableSet.of())).add(// this should really be "any ns other than system"
    Triple.of(noNs, userScope, ImmutableSet.of(a))).add(Triple.of(noNs, systemScope, ImmutableSet.of(systemA))).add(Triple.of(noNs, allScopes, ImmutableSet.of(a))).add(Triple.of(userNs, noScopes, ImmutableSet.of())).add(Triple.of(userNs, userScope, ImmutableSet.of(userA))).add(Triple.of(userNs, systemScope, ImmutableSet.of(systemA))).add(Triple.of(userNs, allScopes, ImmutableSet.of(userA, systemA))).add(Triple.of(systemNs, noScopes, ImmutableSet.of())).add(Triple.of(systemNs, userScope, ImmutableSet.of())).add(Triple.of(systemNs, systemScope, ImmutableSet.of(systemA))).add(Triple.of(systemNs, allScopes, ImmutableSet.of(systemA))).build();
    for (Triple<Optional<NamespaceId>, Set<EntityScope>, Set<MetadataDataset.SearchTerm>> case1 : cases) {
        List<MetadataDataset.SearchTerm> terms = new ArrayList<>();
        Set<MetadataDataset.SearchTerm> expected = case1.getRight();
        MetadataDataset.determineSearchFields(case1.getLeft(), case1.getMiddle(), terms).accept(term);
        Assert.assertEquals(expected.size(), terms.size());
        Assert.assertEquals(expected, ImmutableSet.copyOf(terms));
    }
}
Also used : EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Optional(java.util.Optional) ArrayList(java.util.ArrayList) EntityScope(io.cdap.cdap.proto.EntityScope) Triple(org.apache.commons.lang3.tuple.Triple) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 4 with EntityScope

use of io.cdap.cdap.proto.EntityScope in project cdap by caskdata.

the class ProfileId method fromScopedName.

/**
 * Get the profile id for the given scoped profile name. A scoped name starts with an entity scope, a ':',
 * then the profile name. If there is no ':', it is assumed that it is user scope.
 *
 * @param namespaceId the namespace to use if user scoped
 * @param scopedName the scoped name
 * @return the profile id
 * @throws IllegalArgumentException if the scope is invalid or the profile name is invalid.
 */
public static ProfileId fromScopedName(NamespaceId namespaceId, String scopedName) {
    int idx = scopedName.indexOf(':');
    if (idx < 0) {
        return namespaceId.profile(scopedName);
    }
    if (idx == scopedName.length()) {
        throw new IllegalArgumentException("Invalid scoped profile " + scopedName + ". Cannot end with a ':'.");
    }
    EntityScope scope = EntityScope.valueOf(scopedName.substring(0, idx).toUpperCase());
    String profileName = scopedName.substring(idx + 1);
    return scope == EntityScope.SYSTEM ? NamespaceId.SYSTEM.profile(profileName) : namespaceId.profile(profileName);
}
Also used : EntityScope(io.cdap.cdap.proto.EntityScope)

Aggregations

EntityScope (io.cdap.cdap.proto.EntityScope)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 EnumSet (java.util.EnumSet)2 HashSet (java.util.HashSet)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 DatasetDefinition (io.cdap.cdap.api.dataset.DatasetDefinition)1 MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)1 MetadataScope (io.cdap.cdap.api.metadata.MetadataScope)1 SYSTEM (io.cdap.cdap.api.metadata.MetadataScope.SYSTEM)1 USER (io.cdap.cdap.api.metadata.MetadataScope.USER)1 Constants (io.cdap.cdap.common.conf.Constants)1 Cursor (io.cdap.cdap.common.metadata.Cursor)1 ImmutablePair (io.cdap.cdap.common.utils.ImmutablePair)1 MetadataDataset (io.cdap.cdap.data2.metadata.dataset.MetadataDataset)1