]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7594 fix indexing of component names > 400 characters
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 9 May 2016 12:25:08 +0000 (14:25 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 9 May 2016 21:11:07 +0000 (23:11 +0200)
sonar-db/src/main/java/org/sonar/db/component/ResourceIndexDao.java
sonar-db/src/test/java/org/sonar/db/component/ResourceIndexDaoTest.java

index 8bc4876dd0971a53a05fbfc8e34a3167a30f78b3..58bcac6ead65c6ba20aa49b3c28b6d49c1e1b328 100644 (file)
@@ -31,12 +31,19 @@ import org.sonar.db.AbstractDao;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 
+import static org.apache.commons.lang.StringUtils.substring;
+
 public class ResourceIndexDao extends AbstractDao {
 
   private static final String SELECT_RESOURCES = "org.sonar.db.component.ResourceIndexMapper.selectResources";
   public static final int MINIMUM_KEY_SIZE = 3;
   public static final int SINGLE_INDEX_SIZE = 2;
 
+  /**
+   * Length of db column RESOURCE_INDEX.KEE
+   */
+  private static final int MAXIMUM_KEY_SIZE = 400;
+
   // The scopes and qualifiers that are not in the following constants are not indexed at all.
   // Directories and packages are explicitly excluded.
   private static final String[] RENAMABLE_QUALIFIERS = {Qualifiers.PROJECT, Qualifiers.MODULE, Qualifiers.VIEW, Qualifiers.SUBVIEW};
@@ -195,7 +202,7 @@ public class ResourceIndexDao extends AbstractDao {
     int maxPosition = key.length() == SINGLE_INDEX_SIZE ? 0 : key.length() - MINIMUM_KEY_SIZE;
     for (int position = 0; position <= maxPosition; position++) {
       dto.setPosition(position);
-      dto.setKey(StringUtils.substring(key, position));
+      dto.setKey(substring(key, position, MAXIMUM_KEY_SIZE));
       mapper.insert(dto);
     }
   }
index 9c33e3e3d4decdcfc1c223e143722c09012fd06a..d54e39fa7b791f82a8bf556a55fe4a2c38cffba0 100644 (file)
@@ -23,19 +23,20 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 
+import static org.apache.commons.lang.StringUtils.repeat;
 import static org.assertj.core.api.Assertions.assertThat;
 
-
 public class ResourceIndexDaoTest {
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
-  ResourceIndexDao dao = dbTester.getDbClient().componentIndexDao();
+  ResourceIndexDao underTest = dbTester.getDbClient().componentIndexDao();
 
   @Before
   public void setUp() throws Exception {
@@ -46,43 +47,43 @@ public class ResourceIndexDaoTest {
   public void shouldIndexResource() {
     dbTester.prepareDbUnit(getClass(), "shouldIndexResource.xml");
 
-    dao.indexResource(10, "ZipUtils", "FIL", 8);
+    underTest.indexResource(10, "ZipUtils", "FIL", 8);
 
-    dbTester.assertDbUnit(getClass(), "shouldIndexResource-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldIndexResource-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldIndexProjects() {
     dbTester.prepareDbUnit(getClass(), "shouldIndexProjects.xml");
 
-    dao.indexProjects();
+    underTest.indexProjects();
 
-    dbTester.assertDbUnit(getClass(), "shouldIndexProjects-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldIndexProjects-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldIndexMultiModulesProject() {
     dbTester.prepareDbUnit(getClass(), "shouldIndexMultiModulesProject.xml");
 
-    dao.indexProject(1);
+    underTest.indexProject(1);
 
-    dbTester.assertDbUnit(getClass(), "shouldIndexMultiModulesProject-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldIndexMultiModulesProject-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldReindexProjectAfterRenaming() {
     dbTester.prepareDbUnit(getClass(), "shouldReindexProjectAfterRenaming.xml");
 
-    dao.indexProject(1);
+    underTest.indexProject(1);
 
-    dbTester.assertDbUnit(getClass(), "shouldReindexProjectAfterRenaming-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldReindexProjectAfterRenaming-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldNotIndexPackages() {
     dbTester.prepareDbUnit(getClass(), "shouldNotIndexPackages.xml");
 
-    dao.indexProject(1);
+    underTest.indexProject(1);
     // project
     assertThat(dbTester.countSql("select count(1) from resource_index where resource_id=1")).isGreaterThan(0);
     // directory
@@ -95,45 +96,45 @@ public class ResourceIndexDaoTest {
   public void shouldIndexTwoLettersLongResources() {
     dbTester.prepareDbUnit(getClass(), "shouldIndexTwoLettersLongResource.xml");
 
-    dao.indexResource(10, "AB", Qualifiers.PROJECT, 3);
+    underTest.indexResource(10, "AB", Qualifiers.PROJECT, 3);
 
-    dbTester.assertDbUnit(getClass(), "shouldIndexTwoLettersLongResource-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldIndexTwoLettersLongResource-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldReIndexTwoLettersLongResources() {
     dbTester.prepareDbUnit(getClass(), "shouldReIndexTwoLettersLongResource.xml");
 
-    dao.indexResource(1, "AS", Qualifiers.PROJECT, 1);
+    underTest.indexResource(1, "AS", Qualifiers.PROJECT, 1);
 
-    dbTester.assertDbUnit(getClass(), "shouldReIndexTwoLettersLongResource-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldReIndexTwoLettersLongResource-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldReIndexNewTwoLettersLongResource() {
     dbTester.prepareDbUnit(getClass(), "shouldReIndexNewTwoLettersLongResource.xml");
 
-    dao.indexResource(1, "AS", Qualifiers.PROJECT, 1);
+    underTest.indexResource(1, "AS", Qualifiers.PROJECT, 1);
 
-    dbTester.assertDbUnit(getClass(), "shouldReIndexNewTwoLettersLongResource-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldReIndexNewTwoLettersLongResource-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldReindexResource() {
     dbTester.prepareDbUnit(getClass(), "shouldReindexResource.xml");
 
-    dao.indexResource(1, "New Struts", Qualifiers.PROJECT, 1);
+    underTest.indexResource(1, "New Struts", Qualifiers.PROJECT, 1);
 
-    dbTester.assertDbUnit(getClass(), "shouldReindexResource-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldReindexResource-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
   public void shouldNotReindexUnchangedResource() {
     dbTester.prepareDbUnit(getClass(), "shouldNotReindexUnchangedResource.xml");
 
-    dao.indexResource(1, "Struts", Qualifiers.PROJECT, 1);
+    underTest.indexResource(1, "Struts", Qualifiers.PROJECT, 1);
 
-    dbTester.assertDbUnit(getClass(), "shouldNotReindexUnchangedResource-result.xml", new String[]{"id"}, "resource_index");
+    dbTester.assertDbUnit(getClass(), "shouldNotReindexUnchangedResource-result.xml", new String[] {"id"}, "resource_index");
   }
 
   @Test
@@ -143,9 +144,28 @@ public class ResourceIndexDaoTest {
     String viewUuid = "EFGH";
 
     DbSession session = dbTester.getSession();
-    assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "project", viewUuid)).containsOnly(1L, 2L);
-    assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "one", viewUuid)).containsOnly(1L);
-    assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "two", viewUuid)).containsOnly(2L);
-    assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "unknown", viewUuid)).isEmpty();
+    assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "project", viewUuid)).containsOnly(1L, 2L);
+    assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "one", viewUuid)).containsOnly(1L);
+    assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "two", viewUuid)).containsOnly(2L);
+    assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "unknown", viewUuid)).isEmpty();
+  }
+
+  /**
+   * SONAR-7594
+   * PROJECTS.NAME is 2'000 characters long whereas RESOURCE_INDEX.KEE is only 400 characters. As there's no functional need
+   * to increase size of the latter column, indexed values must be truncated.
+   */
+  @Test
+  public void restrict_indexed_combinations_to_400_characters() {
+    String longName = repeat("a", 2_000);
+    ComponentDto project = new ComponentDto().setId(1L).setKey("the_key").setName(longName).setScope(Scopes.PROJECT).setQualifier(Qualifiers.PROJECT);
+    DbSession session = dbTester.getSession();
+    dbTester.getDbClient().componentDao().insert(session, project);
+    dbTester.getDbClient().snapshotDao().insert(session, new SnapshotDto().setComponentId(project.getId()).setRootProjectId(project.getId()).setLast(true));
+
+    underTest.indexProject(project.getId(), session);
+    session.commit();
+
+    assertThat(dbTester.countRowsOfTable("resource_index")).isEqualTo(longName.length() - ResourceIndexDao.MINIMUM_KEY_SIZE + 1);
   }
 }