Search in sources :

Example 1 with ComponentIndexResults

use of org.sonar.server.component.index.ComponentIndexResults in project sonarqube by SonarSource.

the class SuggestionsAction method loadSuggestionsWithoutSearch.

/**
 * we are generating suggestions, by using (1) favorites and (2) recently browsed components (without searchin in Elasticsearch)
 */
private SuggestionsWsResponse loadSuggestionsWithoutSearch(int skip, int limit, Set<String> recentlyBrowsedKeys, List<String> qualifiers) {
    List<ComponentDto> favoriteDtos = favoriteFinder.list();
    if (favoriteDtos.isEmpty() && recentlyBrowsedKeys.isEmpty()) {
        return newBuilder().build();
    }
    try (DbSession dbSession = dbClient.openSession(false)) {
        Set<ComponentDto> componentDtos = new HashSet<>(favoriteDtos);
        if (!recentlyBrowsedKeys.isEmpty()) {
            componentDtos.addAll(dbClient.componentDao().selectByKeys(dbSession, recentlyBrowsedKeys));
        }
        List<ComponentDto> authorizedComponents = userSession.keepAuthorizedComponents(USER, componentDtos);
        ListMultimap<String, ComponentDto> componentsPerQualifier = authorizedComponents.stream().collect(MoreCollectors.index(ComponentDto::qualifier));
        if (componentsPerQualifier.isEmpty()) {
            return newBuilder().build();
        }
        Set<String> favoriteUuids = favoriteDtos.stream().map(ComponentDto::uuid).collect(MoreCollectors.toSet(favoriteDtos.size()));
        Comparator<ComponentDto> favoriteComparator = Comparator.comparing(c -> favoriteUuids.contains(c.uuid()) ? -1 : +1);
        Comparator<ComponentDto> comparator = favoriteComparator.thenComparing(ComponentDto::name);
        ComponentIndexResults componentsPerQualifiers = ComponentIndexResults.newBuilder().setQualifiers(qualifiers.stream().map(q -> {
            List<ComponentDto> componentsOfThisQualifier = componentsPerQualifier.get(q);
            List<ComponentHit> hits = componentsOfThisQualifier.stream().sorted(comparator).skip(skip).limit(limit).map(ComponentDto::uuid).map(ComponentHit::new).collect(MoreCollectors.toList(limit));
            int totalHits = componentsOfThisQualifier.size();
            return new ComponentHitsPerQualifier(q, hits, totalHits);
        })).build();
        return buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, authorizedComponents, skip + limit).build();
    }
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) ComponentIndexResults(org.sonar.server.component.index.ComponentIndexResults) DbSession(org.sonar.db.DbSession) ComponentHitsPerQualifier(org.sonar.server.component.index.ComponentHitsPerQualifier) ComponentHit(org.sonar.server.component.index.ComponentHit) HashSet(java.util.HashSet)

Example 2 with ComponentIndexResults

use of org.sonar.server.component.index.ComponentIndexResults in project sonarqube by SonarSource.

the class SuggestionsAction method loadSuggestionsWithSearch.

private SuggestionsWsResponse loadSuggestionsWithSearch(String query, int skip, int limit, Set<String> recentlyBrowsedKeys, List<String> qualifiers) {
    if (split(query).noneMatch(token -> token.length() >= MINIMUM_NGRAM_LENGTH)) {
        SuggestionsWsResponse.Builder queryBuilder = newBuilder();
        getWarning(query).ifPresent(queryBuilder::setWarning);
        return queryBuilder.build();
    }
    List<ComponentDto> favorites = favoriteFinder.list();
    Set<String> favoriteKeys = favorites.stream().map(ComponentDto::getDbKey).collect(MoreCollectors.toSet(favorites.size()));
    SuggestionQuery.Builder queryBuilder = SuggestionQuery.builder().setQuery(query).setRecentlyBrowsedKeys(recentlyBrowsedKeys).setFavoriteKeys(favoriteKeys).setQualifiers(qualifiers).setSkip(skip).setLimit(limit);
    ComponentIndexResults componentsPerQualifiers = searchInIndex(queryBuilder.build());
    if (componentsPerQualifiers.isEmpty()) {
        return newBuilder().build();
    }
    try (DbSession dbSession = dbClient.openSession(false)) {
        Set<String> componentUuids = componentsPerQualifiers.getQualifiers().map(ComponentHitsPerQualifier::getHits).flatMap(Collection::stream).map(ComponentHit::getUuid).collect(toSet());
        List<ComponentDto> componentDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids);
        Set<String> favoriteUuids = favorites.stream().map(ComponentDto::uuid).collect(MoreCollectors.toSet(favorites.size()));
        SuggestionsWsResponse.Builder searchWsResponse = buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, componentDtos, skip + limit);
        getWarning(query).ifPresent(searchWsResponse::setWarning);
        return searchWsResponse.build();
    }
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) ComponentIndexResults(org.sonar.server.component.index.ComponentIndexResults) DbSession(org.sonar.db.DbSession) SuggestionQuery(org.sonar.server.component.index.SuggestionQuery) Collection(java.util.Collection)

Aggregations

DbSession (org.sonar.db.DbSession)2 ComponentDto (org.sonar.db.component.ComponentDto)2 ComponentIndexResults (org.sonar.server.component.index.ComponentIndexResults)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 ComponentHit (org.sonar.server.component.index.ComponentHit)1 ComponentHitsPerQualifier (org.sonar.server.component.index.ComponentHitsPerQualifier)1 SuggestionQuery (org.sonar.server.component.index.SuggestionQuery)1 SuggestionsWsResponse (org.sonarqube.ws.Components.SuggestionsWsResponse)1