]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8675 remove joins on resource_index in ComponentMapper
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 23 Jan 2017 19:58:25 +0000 (20:58 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 25 Jan 2017 10:41:56 +0000 (11:41 +0100)
sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java
sonar-db/src/main/java/org/sonar/db/component/ComponentTreeQuery.java
sonar-db/src/main/java/org/sonar/db/measure/MeasureTreeQuery.java
sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
sonar-db/src/test/java/org/sonar/db/component/ComponentQueryTest.java
sonar-db/src/test/java/org/sonar/db/component/ComponentTreeQueryTest.java
sonar-db/src/test/java/org/sonar/db/measure/MeasureTreeQueryTest.java

index 58771e1e140043d9a5d27d529778376ad035751c..1d0f82addb855227656090de1917ed78975754a8 100644 (file)
@@ -23,10 +23,10 @@ import java.util.Locale;
 import java.util.Set;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+import org.sonar.db.WildcardPosition;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static org.sonar.db.DatabaseUtils.buildLikeValue;
-import static org.sonar.db.WildcardPosition.AFTER;
 
 public class ComponentQuery {
   private final String nameOrKeyQuery;
@@ -64,14 +64,12 @@ public class ComponentQuery {
     return nameOrKeyQuery;
   }
 
+  /**
+   * Used by MyBatis mapper
+   */
   @CheckForNull
-  public String getNameOrKeyQueryToSqlForResourceIndex() {
-    return buildLikeValue(nameOrKeyQuery, AFTER).toLowerCase(Locale.ENGLISH);
-  }
-
-  @CheckForNull
-  public String getNameOrKeyQueryToSqlForProjectKey() {
-    return buildLikeValue(nameOrKeyQuery, AFTER);
+  public String getNameOrKeyUpperLikeQuery() {
+    return buildLikeValue(nameOrKeyQuery, WildcardPosition.BEFORE_AND_AFTER).toUpperCase(Locale.ENGLISH);
   }
 
   @CheckForNull
index 5405f9cb364cd9dcbd1138c19801e435f6f69bd1..1e9a03a78a4bf7e4de24d07f3d9ee497ba07bd98 100644 (file)
@@ -29,7 +29,7 @@ import org.sonar.db.WildcardPosition;
 import static com.google.common.collect.Lists.newArrayList;
 import static java.util.Objects.requireNonNull;
 import static org.sonar.db.DatabaseUtils.buildLikeValue;
-import static org.sonar.db.WildcardPosition.AFTER;
+import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
 
 public class ComponentTreeQuery {
 
@@ -62,9 +62,12 @@ public class ComponentTreeQuery {
     return nameOrKeyQuery;
   }
 
+  /**
+   * Used by MyBatis mapper
+   */
   @CheckForNull
-  public String getNameOrKeyQueryToSqlForResourceIndex() {
-    return nameOrKeyQuery == null ? null : buildLikeValue(nameOrKeyQuery, AFTER).toLowerCase(Locale.ENGLISH);
+  public String getNameOrKeyUpperLikeQuery() {
+    return nameOrKeyQuery == null ? null : buildLikeValue(nameOrKeyQuery, BEFORE_AND_AFTER).toUpperCase(Locale.ENGLISH);
   }
 
   public String getBaseUuid() {
index 8ccd5a6a85d872e5f79f055ef9e93378a9ad387c..19970bd24dbad9ab775567bb5e48beae304c379f 100644 (file)
@@ -29,7 +29,7 @@ import org.sonar.db.component.ComponentDto;
 import static com.google.common.collect.Lists.newArrayList;
 import static java.util.Objects.requireNonNull;
 import static org.sonar.db.DatabaseUtils.buildLikeValue;
-import static org.sonar.db.WildcardPosition.AFTER;
+import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
 
 public class MeasureTreeQuery {
 
@@ -63,9 +63,12 @@ public class MeasureTreeQuery {
     return nameOrKeyQuery;
   }
 
+  /**
+   * Used by MyBatis mapper
+   */
   @CheckForNull
-  public String getNameOrKeyQueryToSqlForResourceIndex() {
-    return nameOrKeyQuery == null ? null : buildLikeValue(nameOrKeyQuery, AFTER).toLowerCase(Locale.ENGLISH);
+  public String getNameOrKeyUpperLikeQuery() {
+    return nameOrKeyQuery == null ? null : buildLikeValue(nameOrKeyQuery, BEFORE_AND_AFTER).toUpperCase(Locale.ENGLISH);
   }
 
   @CheckForNull
index 3ff8987b017f7444afa7821573952cdf01a2ebbb..0607cbce185759c9bca367fcacf64d1456d4b1b7 100644 (file)
         </foreach>
       </if>
       <if test="query.nameOrKeyQuery!=null">
-        AND (
-          p.kee=#{query.nameOrKeyQuery,jdbcType=VARCHAR}
-          OR
-          p.uuid IN (
-            SELECT ri.component_uuid
-            FROM resource_index ri
-            WHERE ri.kee like #{query.nameOrKeyQueryToSqlForResourceIndex} ESCAPE '/'
-            <if test="query.qualifiers!=null">
-            AND ri.qualifier in
-              <foreach collection="query.qualifiers" item="qualifier" open="(" close=")" separator=",">
-                #{qualifier,jdbcType=VARCHAR}
-              </foreach>
-            </if>
-          )
+        and (
+          p.kee = #{query.nameOrKeyQuery,jdbcType=VARCHAR}
+          or
+          upper(p.name) like #{query.nameOrKeyUpperLikeQuery,jdbcType=VARCHAR} escape '/'
         )
       </if>
   </sql>
     </if>
     <if test="query.nameOrKeyQuery != null">
       and (
-      p.kee=#{query.nameOrKeyQuery}
-      or
-      p.uuid IN (
-      SELECT ri.component_uuid
-      FROM resource_index ri
-      WHERE ri.kee like #{query.nameOrKeyQueryToSqlForResourceIndex} ESCAPE '/'
-      )
-      or
-      p.copy_component_uuid IN (
-      SELECT ri.component_uuid
-      FROM resource_index ri
-      WHERE ri.kee like #{query.nameOrKeyQueryToSqlForResourceIndex} ESCAPE '/'
-      )
+        p.kee = #{query.nameOrKeyQuery,jdbcType=VARCHAR}
+        or
+        upper(p.name) like #{query.nameOrKeyUpperLikeQuery,jdbcType=VARCHAR} escape '/'
       )
     </if>
   </sql>
index ebf181d5847b2cdbbeb3abbf171a40e0fff3bfff..f81cf160dd9c5470ef94ca3459a92eef6644a30d 100644 (file)
@@ -27,14 +27,13 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.api.resources.Qualifiers.PROJECT;
 
 public class ComponentQueryTest {
+
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  ComponentQuery underTest;
-
   @Test
   public void build_query() throws Exception {
-    underTest = ComponentQuery.builder()
+    ComponentQuery underTest = ComponentQuery.builder()
       .setNameOrKeyQuery("key")
       .setLanguage("java")
       .setQualifiers(PROJECT)
@@ -47,7 +46,7 @@ public class ComponentQueryTest {
 
   @Test
   public void build_query_minimal_properties() throws Exception {
-    underTest = ComponentQuery.builder()
+    ComponentQuery underTest = ComponentQuery.builder()
       .setQualifiers(PROJECT)
       .build();
 
@@ -61,26 +60,16 @@ public class ComponentQueryTest {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("At least one qualifier must be provided");
 
-    underTest = ComponentQuery.builder().setLanguage("java").build();
+    ComponentQuery.builder().setLanguage("java").build();
   }
 
   @Test
-  public void test_getNameOrKeyQueryToSqlForResourceIndex() throws Exception {
-    underTest = ComponentQuery.builder()
+  public void test_getNameOrKeyUpperLikeQuery() throws Exception {
+    ComponentQuery underTest = ComponentQuery.builder()
       .setNameOrKeyQuery("NAME/key")
       .setQualifiers(PROJECT)
       .build();
 
-    assertThat(underTest.getNameOrKeyQueryToSqlForResourceIndex()).isEqualTo("name//key%");
-  }
-
-  @Test
-  public void test_getNameOrKeyQueryToSqlForProjectKey() throws Exception {
-    underTest = ComponentQuery.builder()
-      .setNameOrKeyQuery("name/key")
-      .setQualifiers(PROJECT)
-      .build();
-
-    assertThat(underTest.getNameOrKeyQueryToSqlForProjectKey()).isEqualTo("name//key%");
+    assertThat(underTest.getNameOrKeyUpperLikeQuery()).isEqualTo("%NAME//KEY%");
   }
 }
index 817f2f0ecfd23085d8c7d8ba156f43cf4d729fc3..813ccf1502f5a813eeec5a112ea58a899546e815 100644 (file)
@@ -33,6 +33,7 @@ import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
 public class ComponentTreeQueryTest {
 
   private static final String BASE_UUID = "ABCD";
+
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
@@ -49,6 +50,7 @@ public class ComponentTreeQueryTest {
     assertThat(query.getStrategy()).isEqualTo(CHILDREN);
     assertThat(query.getQualifiers()).containsOnly("FIL", "DIR");
     assertThat(query.getNameOrKeyQuery()).isEqualTo("teSt");
+    assertThat(query.getNameOrKeyUpperLikeQuery()).isEqualTo("%TEST%");
   }
 
   @Test
@@ -62,6 +64,7 @@ public class ComponentTreeQueryTest {
     assertThat(query.getStrategy()).isEqualTo(CHILDREN);
     assertThat(query.getQualifiers()).isNull();
     assertThat(query.getNameOrKeyQuery()).isNull();
+    assertThat(query.getNameOrKeyUpperLikeQuery()).isNull();
   }
 
   @Test
index 3620cd3cc7b48df4ee7ba5912e1d9cd6076386dc..90db7f36863723945a5f5e122888ef8ce2323a81 100644 (file)
@@ -68,15 +68,15 @@ public class MeasureTreeQueryTest {
   }
 
   @Test
-  public void test_getNameOrKeyQueryToSqlForResourceIndex() throws Exception {
+  public void test_getNameOrKeyUpperLikeQuery() throws Exception {
     assertThat(MeasureTreeQuery.builder()
       .setNameOrKeyQuery("like-\\_%/-value")
       .setStrategy(CHILDREN)
-      .build().getNameOrKeyQueryToSqlForResourceIndex()).isEqualTo("like-\\/_/%//-value%");
+      .build().getNameOrKeyUpperLikeQuery()).isEqualTo("%LIKE-\\/_/%//-VALUE%");
 
     assertThat(MeasureTreeQuery.builder()
       .setStrategy(CHILDREN)
-      .build().getNameOrKeyQueryToSqlForResourceIndex()).isNull();
+      .build().getNameOrKeyUpperLikeQuery()).isNull();
   }
 
   @Test