--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.component.index;
+
+import java.util.Arrays;
+import java.util.List;
+import org.elasticsearch.search.SearchHit;
+import org.sonar.core.util.stream.MoreCollectors;
+
+public class ComponentHit {
+
+ private final String uuid;
+
+ private ComponentHit(String uuid) {
+ this.uuid = uuid;
+ }
+
+ public String getUuid() {
+ return uuid;
+ }
+
+ public static List<ComponentHit> fromSearchHits(SearchHit... hits) {
+ return Arrays.stream(hits).map(ComponentHit::fromSearchHit)
+ .collect(MoreCollectors.toList(hits.length));
+ }
+
+ public static ComponentHit fromSearchHit(SearchHit hit) {
+ return new ComponentHit(hit.getId());
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.component.index;
+
+import java.util.List;
+import org.sonar.core.util.stream.MoreCollectors;
+
+public class ComponentHitsPerQualifier {
+
+ private final String qualifier;
+ private final List<ComponentHit> hits;
+ private final long totalHits;
+
+ public ComponentHitsPerQualifier(String qualifier, List<ComponentHit> hits, long totalHits) {
+ this.qualifier = qualifier;
+ this.hits = hits;
+ this.totalHits = totalHits;
+ }
+
+ public String getQualifier() {
+ return qualifier;
+ }
+
+ public List<String> getComponentUuids() {
+ return hits.stream().map(ComponentHit::getUuid).collect(MoreCollectors.toList(hits.size()));
+ }
+
+ public long getTotalHits() {
+ return totalHits;
+ }
+
+ public long getNumberOfFurtherResults() {
+ return Math.max(totalHits - hits.size(), 0L);
+ }
+}
package org.sonar.server.component.index;
import com.google.common.annotations.VisibleForTesting;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
this.authorizationTypeSupport = authorizationTypeSupport;
}
- public List<ComponentsPerQualifier> search(ComponentIndexQuery query) {
+ public List<ComponentHitsPerQualifier> search(ComponentIndexQuery query) {
return search(query, ComponentTextSearchFeature.values());
}
@VisibleForTesting
- List<ComponentsPerQualifier> search(ComponentIndexQuery query, ComponentTextSearchFeature... features) {
+ List<ComponentHitsPerQualifier> search(ComponentIndexQuery query, ComponentTextSearchFeature... features) {
Collection<String> qualifiers = query.getQualifiers();
if (qualifiers.isEmpty()) {
return Collections.emptyList();
return esQuery.must(ComponentTextSearchQueryFactory.createQuery(componentTextSearchQuery, features));
}
- private static List<ComponentsPerQualifier> aggregationsToQualifiers(SearchResponse response) {
+ private static List<ComponentHitsPerQualifier> aggregationsToQualifiers(SearchResponse response) {
InternalFilters filtersAgg = response.getAggregations().get(FILTERS_AGGREGATION_NAME);
List<Bucket> buckets = filtersAgg.getBuckets();
return buckets.stream()
.collect(MoreCollectors.toList(buckets.size()));
}
- private static ComponentsPerQualifier bucketToQualifier(Bucket bucket) {
+ private static ComponentHitsPerQualifier bucketToQualifier(Bucket bucket) {
InternalTopHits docs = bucket.getAggregations().get(DOCS_AGGREGATION_NAME);
SearchHits hitList = docs.getHits();
SearchHit[] hits = hitList.getHits();
- List<String> componentUuids = Arrays.stream(hits).map(SearchHit::getId)
- .collect(MoreCollectors.toList(hits.length));
-
- return new ComponentsPerQualifier(bucket.getKey(), componentUuids, hitList.totalHits());
+ return new ComponentHitsPerQualifier(bucket.getKey(), ComponentHit.fromSearchHits(hits), hitList.totalHits());
}
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.component.index;
-
-import java.util.List;
-
-public class ComponentsPerQualifier {
-
- private final String qualifier;
- private final List<String> componentUuids;
- private final long totalHits;
-
- public ComponentsPerQualifier(String qualifier, List<String> componentUuids, long totalHits) {
- this.qualifier = qualifier;
- this.componentUuids = componentUuids;
- this.totalHits = totalHits;
- }
-
- public String getQualifier() {
- return qualifier;
- }
-
- public List<String> getComponentUuids() {
- return componentUuids;
- }
-
- public long getTotalHits() {
- return totalHits;
- }
-
- public long getNumberOfFurtherResults() {
- return Math.max(getTotalHits() - componentUuids.size(), 0L);
- }
-}
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
+import org.sonar.server.component.index.ComponentHitsPerQualifier;
import org.sonar.server.component.index.ComponentIndex;
import org.sonar.server.component.index.ComponentIndexQuery;
-import org.sonar.server.component.index.ComponentsPerQualifier;
import org.sonar.server.es.textsearch.ComponentTextSearchFeature;
import org.sonarqube.ws.WsComponents.Component;
import org.sonarqube.ws.WsComponents.SuggestionsWsResponse;
ComponentIndexQuery.Builder queryBuilder = ComponentIndexQuery.builder().setQuery(query);
- List<ComponentsPerQualifier> componentsPerQualifiers = getComponentsPerQualifiers(more, queryBuilder);
+ List<ComponentHitsPerQualifier> componentsPerQualifiers = getComponentsPerQualifiers(more, queryBuilder);
String warning = getWarning(query);
SuggestionsWsResponse searchWsResponse = toResponse(componentsPerQualifiers, warning);
return null;
}
- private List<ComponentsPerQualifier> getComponentsPerQualifiers(String more, ComponentIndexQuery.Builder queryBuilder) {
- List<ComponentsPerQualifier> componentsPerQualifiers;
+ private List<ComponentHitsPerQualifier> getComponentsPerQualifiers(String more, ComponentIndexQuery.Builder queryBuilder) {
+ List<ComponentHitsPerQualifier> componentsPerQualifiers;
if (more == null) {
queryBuilder.setQualifiers(stream(SuggestionCategory.values()).map(SuggestionCategory::getQualifier).collect(Collectors.toList()))
.setLimit(DEFAULT_LIMIT);
return componentsPerQualifiers;
}
- private List<ComponentsPerQualifier> searchInIndex(ComponentIndexQuery componentIndexQuery) {
+ private List<ComponentHitsPerQualifier> searchInIndex(ComponentIndexQuery componentIndexQuery) {
return index.search(componentIndexQuery);
}
- private SuggestionsWsResponse toResponse(List<ComponentsPerQualifier> componentsPerQualifiers, @Nullable String warning) {
+ private SuggestionsWsResponse toResponse(List<ComponentHitsPerQualifier> componentsPerQualifiers, @Nullable String warning) {
SuggestionsWsResponse.Builder builder = newBuilder();
if (!componentsPerQualifiers.isEmpty()) {
Map<String, OrganizationDto> organizationsByUuids;
Map<String, ComponentDto> projectsByUuids;
try (DbSession dbSession = dbClient.openSession(false)) {
Set<String> componentUuids = componentsPerQualifiers.stream()
- .map(ComponentsPerQualifier::getComponentUuids)
+ .map(ComponentHitsPerQualifier::getComponentUuids)
.flatMap(Collection::stream)
.collect(toSet());
componentsByUuids = dbClient.componentDao().selectByUuids(dbSession, componentUuids).stream()
return builder.build();
}
- private static List<Category> toCategoryResponses(List<ComponentsPerQualifier> componentsPerQualifiers, Map<String, ComponentDto> componentsByUuids,
+ private static List<Category> toCategoryResponses(List<ComponentHitsPerQualifier> componentsPerQualifiers, Map<String, ComponentDto> componentsByUuids,
Map<String, OrganizationDto> organizationByUuids, Map<String, ComponentDto> projectsByUuids) {
return componentsPerQualifiers.stream().map(qualifier -> {
protected AbstractListAssert<?, ? extends List<? extends String>, String> assertSearch(ComponentIndexQuery query) {
return assertThat(index.search(query, features.get()))
- .flatExtracting(ComponentsPerQualifier::getComponentUuids);
+ .flatExtracting(ComponentHitsPerQualifier::getComponentUuids);
}
protected void assertSearchResults(String query, ComponentDto... expectedComponents) {