Search in sources :

Example 1 with ComponentHitsPerQualifier

use of org.sonar.server.component.index.ComponentHitsPerQualifier 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)

Aggregations

HashSet (java.util.HashSet)1 DbSession (org.sonar.db.DbSession)1 ComponentDto (org.sonar.db.component.ComponentDto)1 ComponentHit (org.sonar.server.component.index.ComponentHit)1 ComponentHitsPerQualifier (org.sonar.server.component.index.ComponentHitsPerQualifier)1 ComponentIndexResults (org.sonar.server.component.index.ComponentIndexResults)1