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();
}
}
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();
}
}
Aggregations