]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9077 hold component search hits in separate class
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Mon, 10 Apr 2017 11:06:35 +0000 (13:06 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Thu, 20 Apr 2017 07:48:52 +0000 (09:48 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentHit.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentHitsPerQualifier.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentHit.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentHit.java
new file mode 100644 (file)
index 0000000..e17eeaa
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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());
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentHitsPerQualifier.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentHitsPerQualifier.java
new file mode 100644 (file)
index 0000000..4aa32e1
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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);
+  }
+}
index 47564f4d88961e65d3eb94b7eac893bee7f1cced..86f5fbb28cb2b65416ae5cd03b07d274ad53b3cd 100644 (file)
@@ -20,7 +20,6 @@
 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;
@@ -63,12 +62,12 @@ public class ComponentIndex {
     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();
@@ -111,7 +110,7 @@ public class ComponentIndex {
     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()
@@ -119,15 +118,12 @@ public class ComponentIndex {
       .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());
   }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java
deleted file mode 100644 (file)
index 03ade35..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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);
-  }
-}
index f256f0ad15b0a55f543d6c1c59bee89a3cab795c..f554345758a63fd91297f98a9ae99140567b3c78 100644 (file)
@@ -35,9 +35,9 @@ import org.sonar.db.DbClient;
 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;
@@ -102,7 +102,7 @@ public class SuggestionsAction implements ComponentsWsAction {
 
     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);
@@ -117,8 +117,8 @@ public class SuggestionsAction implements ComponentsWsAction {
     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);
@@ -130,11 +130,11 @@ public class SuggestionsAction implements ComponentsWsAction {
     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;
@@ -142,7 +142,7 @@ public class SuggestionsAction implements ComponentsWsAction {
       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()
@@ -168,7 +168,7 @@ public class SuggestionsAction implements ComponentsWsAction {
     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 -> {
 
index d519764725ce21c21777dee84569e0dc652181a9..f2bce6bb2483f8d89d3df7f3e2fba3553a8e6e9b 100644 (file)
@@ -104,7 +104,7 @@ public abstract class ComponentIndexTest {
 
   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) {