diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-06-17 13:52:59 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-06-17 14:26:04 +0200 |
commit | 7e3b9ca7aeada4e763d69a7f82d11ca426e4757d (patch) | |
tree | f091f962a808361895b9c2363f2c784a35d1b976 /sonar-batch/src | |
parent | 0683bf8abce8e902a59599018315d5aa12ea2f73 (diff) | |
download | sonarqube-7e3b9ca7aeada4e763d69a7f82d11ca426e4757d.tar.gz sonarqube-7e3b9ca7aeada4e763d69a7f82d11ca426e4757d.zip |
SONAR-5233 Speed up migration
Diffstat (limited to 'sonar-batch/src')
3 files changed, 78 insertions, 27 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java index 4e169391d11..6e61e07d723 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java @@ -103,7 +103,8 @@ public class ResourceKeyMigration implements BatchComponent { // Find all FIL or CLA resources for this module StringBuilder hql = newResourceQuery() .append(" and scope = '").append(Scopes.FILE).append("' order by qualifier, key"); - List<ResourceModel> resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList(); + Map<String, ResourceModel> disabledResourceByKey = loadDisabledResources(moduleId, hql); + List<ResourceModel> resources = loadEnabledResources(moduleId, hql); for (ResourceModel resourceModel : resources) { String oldEffectiveKey = resourceModel.getKey(); boolean isTest = Qualifiers.UNIT_TEST_FILE.equals(resourceModel.getQualifier()); @@ -127,7 +128,7 @@ public class ResourceKeyMigration implements BatchComponent { logger.warn("Directory with key " + parentOldKey + " matches both " + deprecatedDirectoryKeyMapper.get(parentOldKey) + " and " + parentNewKey + ". First match is arbitrary chosen."); } - updateKey(resourceModel, newEffectiveKey); + updateKey(resourceModel, newEffectiveKey, disabledResourceByKey); resourceModel.setDeprecatedKey(oldEffectiveKey); logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey); } else { @@ -136,17 +137,10 @@ public class ResourceKeyMigration implements BatchComponent { } } - private void updateKey(ResourceModel resourceModel, String newEffectiveKey) { + private void updateKey(ResourceModel resourceModel, String newEffectiveKey, Map<String, ResourceModel> disabledResourceByKey) { // Look for disabled resource with conflicting key - List<ResourceModel> duplicateDisabledResources = session.createQuery(new StringBuilder().append("from ") - .append(ResourceModel.class.getSimpleName()) - .append(" where enabled = false ") - .append(" and kee = :kee ") - .append(" and qualifier = :qualifier ").toString()) - .setParameter("kee", newEffectiveKey) - .setParameter("qualifier", resourceModel.getQualifier()).getResultList(); - if (duplicateDisabledResources.size() > 0) { - ResourceModel duplicateDisabledResource = duplicateDisabledResources.get(0); + if (disabledResourceByKey.containsKey(newEffectiveKey)) { + ResourceModel duplicateDisabledResource = disabledResourceByKey.get(newEffectiveKey); String disabledKey = newEffectiveKey + "_renamed_by_resource_key_migration"; duplicateDisabledResource.setKey(disabledKey); logger.info(COMPONENT_CHANGED_TO, newEffectiveKey, disabledKey); @@ -157,7 +151,7 @@ public class ResourceKeyMigration implements BatchComponent { private StringBuilder newResourceQuery() { return new StringBuilder().append("from ") .append(ResourceModel.class.getSimpleName()) - .append(" where enabled = true ") + .append(" where enabled = :enabled") .append(" and rootId = :rootId "); } @@ -173,12 +167,13 @@ public class ResourceKeyMigration implements BatchComponent { // Find all DIR resources for this module StringBuilder hql = newResourceQuery() .append(" and qualifier = '").append(Qualifiers.DIRECTORY).append("'"); - List<ResourceModel> resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList(); + Map<String, ResourceModel> disabledResourceByKey = loadDisabledResources(moduleId, hql); + List<ResourceModel> resources = loadEnabledResources(moduleId, hql); for (ResourceModel resourceModel : resources) { String oldEffectiveKey = resourceModel.getKey(); if (deprecatedDirectoryKeyMapper.containsKey(oldEffectiveKey)) { String newEffectiveKey = deprecatedDirectoryKeyMapper.get(oldEffectiveKey); - updateKey(resourceModel, newEffectiveKey); + updateKey(resourceModel, newEffectiveKey, disabledResourceByKey); resourceModel.setDeprecatedKey(oldEffectiveKey); logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey); } else { @@ -187,6 +182,26 @@ public class ResourceKeyMigration implements BatchComponent { } } + private List<ResourceModel> loadEnabledResources(int moduleId, StringBuilder hql) { + List<ResourceModel> resources = session.createQuery(hql.toString()) + .setParameter("rootId", moduleId) + .setParameter("enabled", true) + .getResultList(); + return resources; + } + + private Map<String, ResourceModel> loadDisabledResources(int moduleId, StringBuilder hql) { + List<ResourceModel> disabledResources = session.createQuery(hql.toString()) + .setParameter("rootId", moduleId) + .setParameter("enabled", false) + .getResultList(); + Map<String, ResourceModel> disabledResourceByKey = new HashMap<String, ResourceModel>(); + for (ResourceModel disabledResourceModel : disabledResources) { + disabledResourceByKey.put(disabledResourceModel.getKey(), disabledResourceModel); + } + return disabledResourceByKey; + } + private String getParentKey(InputFile matchedFile) { String filePath = PathUtils.sanitize(matchedFile.relativePath()); String parentFolderPath; diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys-result.xml index 3dacd66ea57..262a6fdb2d3 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys-result.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys-result.xml @@ -41,25 +41,43 @@ name="RootBar" long_name="RootBar" description="[null]" enabled="true" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="b:[default].RootBar" /> + <!-- Disabled Java resources --> + <projects id="1009" scope="DIR" qualifier="DIR" kee="b:src/main/java/org/foo_renamed_by_resource_key_migration" root_id="1003" + name="src/main/java/org/foo" long_name="src/main/java/org/foo" description="[null]" + enabled="false" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + <projects id="1010" scope="FIL" qualifier="CLA" kee="b:src.main.java.org.foo.Bar" root_id="1003" + name="Bar" long_name="src.main.java.org.foo.Bar" description="[null]" + enabled="false" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + <!-- PHP resources --> - <projects id="1009" scope="DIR" qualifier="DIR" kee="a:org/foo" root_id="1002" + <projects id="1020" scope="DIR" qualifier="DIR" kee="a:org/foo" root_id="1002" name="org/foo" long_name="org/foo" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="a:org/foo" /> - <projects id="1010" scope="FIL" qualifier="FIL" kee="a:org/foo/Bar.php" root_id="1002" + <projects id="1021" scope="FIL" qualifier="FIL" kee="a:org/foo/Bar.php" root_id="1002" name="Bar.php" long_name="Bar.php" description="[null]" enabled="true" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="a:org/foo/Bar.php" /> - - <projects id="1011" scope="FIL" qualifier="UTS" kee="a:test/org/foo/BarTest.php" root_id="1002" + + <projects id="1022" scope="FIL" qualifier="UTS" kee="a:test/org/foo/BarTest.php" root_id="1002" name="BarTest.php" long_name="BarTest.php" description="[null]" enabled="true" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="a:org/foo/BarTest.php" /> - <projects id="1012" scope="DIR" qualifier="DIR" kee="a:/" root_id="1002" + <projects id="1023" scope="DIR" qualifier="DIR" kee="a:/" root_id="1002" name="[root]" long_name="[root]" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="a:[root]" /> - <projects id="1013" scope="FIL" qualifier="FIL" kee="a:RootBar.php" root_id="1002" + <projects id="1024" scope="FIL" qualifier="FIL" kee="a:RootBar.php" root_id="1002" name="RootBar.php" long_name="RootBar.php" description="[null]" enabled="true" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="a:RootBar.php" /> + + <!-- Disabled PHP resources --> + <projects id="1025" scope="DIR" qualifier="DIR" kee="a:test/org/foo" root_id="1002" + name="test/org/foo" long_name="test/org/foo" description="[null]" + enabled="false" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + + <projects id="1026" scope="FIL" qualifier="UTS" kee="a:test/org/foo/BarTest.php_renamed_by_resource_key_migration" root_id="1002" + name="BarTest.php" long_name="BarTest.php" description="[null]" + enabled="false" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + </dataset> diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys.xml index 4cfae50cb46..03a02da501b 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourceKeyMigrationTest/shouldMigrateResourceKeys.xml @@ -21,6 +21,7 @@ enabled="true" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> <!-- Java resources --> + <projects id="1004" scope="DIR" qualifier="DIR" kee="b:org/foo" root_id="1003" name="org/foo" long_name="org/foo" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> @@ -41,25 +42,42 @@ name="RootBar" long_name="RootBar" description="[null]" enabled="true" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + <!-- Disabled Java resources --> + <projects id="1009" scope="DIR" qualifier="DIR" kee="b:src/main/java/org/foo" root_id="1003" + name="src/main/java/org/foo" long_name="src/main/java/org/foo" description="[null]" + enabled="false" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + <projects id="1010" scope="FIL" qualifier="CLA" kee="b:src.main.java.org.foo.Bar" root_id="1003" + name="Bar" long_name="src.main.java.org.foo.Bar" description="[null]" + enabled="false" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + <!-- PHP resources --> - <projects id="1009" scope="DIR" qualifier="DIR" kee="a:org/foo" root_id="1002" + <projects id="1020" scope="DIR" qualifier="DIR" kee="a:org/foo" root_id="1002" name="org/foo" long_name="org/foo" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> - <projects id="1010" scope="FIL" qualifier="FIL" kee="a:org/foo/Bar.php" root_id="1002" + <projects id="1021" scope="FIL" qualifier="FIL" kee="a:org/foo/Bar.php" root_id="1002" name="Bar.php" long_name="Bar.php" description="[null]" enabled="true" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> - <projects id="1011" scope="FIL" qualifier="UTS" kee="a:org/foo/BarTest.php" root_id="1002" + <projects id="1022" scope="FIL" qualifier="UTS" kee="a:org/foo/BarTest.php" root_id="1002" name="BarTest.php" long_name="BarTest.php" description="[null]" enabled="true" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> - - <projects id="1012" scope="DIR" qualifier="DIR" kee="a:[root]" root_id="1002" + + <projects id="1023" scope="DIR" qualifier="DIR" kee="a:[root]" root_id="1002" name="[root]" long_name="[root]" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> - <projects id="1013" scope="FIL" qualifier="FIL" kee="a:RootBar.php" root_id="1002" + <projects id="1024" scope="FIL" qualifier="FIL" kee="a:RootBar.php" root_id="1002" name="RootBar.php" long_name="RootBar.php" description="[null]" enabled="true" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + + <!-- Disabled PHP resources --> + <projects id="1025" scope="DIR" qualifier="DIR" kee="a:test/org/foo" root_id="1002" + name="test/org/foo" long_name="test/org/foo" description="[null]" + enabled="false" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> + + <projects id="1026" scope="FIL" qualifier="UTS" kee="a:test/org/foo/BarTest.php" root_id="1002" + name="BarTest.php" long_name="BarTest.php" description="[null]" + enabled="false" language="php" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]" /> </dataset> |