From 59bafb00f935b64455e51aac5b035434d0b0debb Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 29 Oct 2014 16:59:51 +0100 Subject: [PATCH] SONAR-5755 Add cursor mode when searching for project --- .../migrations/v42/PackageKeysMigration.java | 5 +- .../PopulateProjectsUuidColumnsMigration.java | 78 ++++++++++--------- .../migration/v50/Migration50Mapper.java | 16 ++++ .../migration/v50/Migration50Mapper.xml | 16 ---- 4 files changed, 63 insertions(+), 52 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java index 284b1893cdf..e0c75510945 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java @@ -43,7 +43,7 @@ public class PackageKeysMigration extends BaseDataChange { } @Override - public void execute(Context context) throws SQLException { + public void execute(final Context context) throws SQLException { MassUpdate massUpdate = context.prepareMassUpdate(); massUpdate.select("SELECT id, kee FROM projects WHERE qualifier='PAC'"); massUpdate.update("UPDATE projects SET qualifier='DIR', kee=? WHERE id=?"); @@ -54,6 +54,9 @@ public class PackageKeysMigration extends BaseDataChange { String key = row.getString(2); update.setString(1, convertKey(key)); update.setLong(2, id); + + context.prepareSelect(""); + return true; } }); diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java index e5ad598c6bb..6bb0228c2c5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java @@ -22,6 +22,8 @@ package org.sonar.server.db.migrations.v50; import com.google.common.base.Splitter; import com.google.common.base.Strings; +import org.apache.ibatis.session.ResultContext; +import org.apache.ibatis.session.ResultHandler; import org.sonar.api.resources.Scopes; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.migration.v50.Component; @@ -59,32 +61,38 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { Timer timer = new Timer("Db Migration Progress"); timer.schedule(progressTask, MassUpdate.ProgressTask.PERIOD_MS, MassUpdate.ProgressTask.PERIOD_MS); - DbSession session = db.openSession(true); + final DbSession readSession = db.openSession(false); + final DbSession writeSession = db.openSession(true); try { - Migration50Mapper mapper = session.getMapper(Migration50Mapper.class); + readSession.select("org.sonar.core.persistence.migration.v50.Migration50Mapper.selectRootProjects", new ResultHandler() { + @Override + public void handleResult(ResultContext context) { + Component project = (Component) context.getResultObject(); + Map uuidByComponentId = newHashMap(); + migrateEnabledComponents(readSession, writeSession, project, uuidByComponentId); + migrateDisabledComponents(readSession, writeSession, project, uuidByComponentId); + } + }); + writeSession.commit(); + readSession.commit(); - for (Component project : mapper.selectRootProjects()) { - Map uuidByComponentId = newHashMap(); - migrateEnabledComponents(session, mapper, project, uuidByComponentId); - migrateDisabledComponents(session, mapper, project, uuidByComponentId); - } - migrateComponentsWithoutUuid(session, mapper); + migrateComponentsWithoutUuid(readSession, writeSession); + writeSession.commit(); - session.commit(); // log the total number of process rows progressTask.log(); - } finally { - session.close(); + readSession.close(); + writeSession.close(); timer.cancel(); timer.purge(); } } - private void migrateEnabledComponents(DbSession session, Migration50Mapper mapper, Component project, Map uuidByComponentId) { + private void migrateEnabledComponents(DbSession readSession, DbSession writeSession, Component project, Map uuidByComponentId) { Map componentsBySnapshotId = newHashMap(); - List components = mapper.selectComponentChildrenForProjects(project.getId()); + List components = readSession.getMapper(Migration50Mapper.class).selectComponentChildrenForProjects(project.getId()); components.add(project); List componentsToMigrate = newArrayList(); for (Component component : components) { @@ -100,7 +108,25 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { for (Component component : componentsToMigrate) { updateComponent(component, project, componentsBySnapshotId, uuidByComponentId); - mapper.updateComponentUuids(component); + writeSession.getMapper(Migration50Mapper.class).updateComponentUuids(component); + counter.getAndIncrement(); + } + } + + private void migrateDisabledComponents(DbSession readSession, DbSession writeSession, Component project, Map uuidByComponentId) { + String projectUuid = getOrCreateUuid(project, uuidByComponentId); + for (Component component : readSession.getMapper(Migration50Mapper.class).selectDisabledDirectComponentChildrenForProjects(project.getId())) { + component.setUuid(getOrCreateUuid(component, uuidByComponentId)); + component.setProjectUuid(projectUuid); + + writeSession.getMapper(Migration50Mapper.class).updateComponentUuids(component); + counter.getAndIncrement(); + } + for (Component component : readSession.getMapper(Migration50Mapper.class).selectDisabledNoneDirectComponentChildrenForProjects(project.getId())) { + component.setUuid(getOrCreateUuid(component, uuidByComponentId)); + component.setProjectUuid(projectUuid); + + writeSession.getMapper(Migration50Mapper.class).updateComponentUuids(component); counter.getAndIncrement(); } } @@ -131,31 +157,13 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { } } - private void migrateDisabledComponents(DbSession session, Migration50Mapper mapper, Component project, Map uuidByComponentId) { - String projectUuid = getOrCreateUuid(project, uuidByComponentId); - for (Component component : mapper.selectDisabledDirectComponentChildrenForProjects(project.getId())) { - component.setUuid(getOrCreateUuid(component, uuidByComponentId)); - component.setProjectUuid(projectUuid); - - mapper.updateComponentUuids(component); - counter.getAndIncrement(); - } - for (Component component : mapper.selectDisabledNoneDirectComponentChildrenForProjects(project.getId())) { - component.setUuid(getOrCreateUuid(component, uuidByComponentId)); - component.setProjectUuid(projectUuid); - - mapper.updateComponentUuids(component); - counter.getAndIncrement(); - } - } - - private void migrateComponentsWithoutUuid(DbSession session, Migration50Mapper mapper) { - for (Component component : mapper.selectComponentsWithoutUuid()) { + private void migrateComponentsWithoutUuid(DbSession readSession, DbSession writeSession) { + for (Component component : readSession.getMapper(Migration50Mapper.class).selectComponentsWithoutUuid()) { String uuid = UUID.randomUUID().toString(); component.setUuid(uuid); component.setProjectUuid(uuid); - mapper.updateComponentUuids(component); + writeSession.getMapper(Migration50Mapper.class).updateComponentUuids(component); counter.getAndIncrement(); } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java b/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java index 8fa97afee1e..e5e9af6a2a7 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java @@ -21,6 +21,7 @@ package org.sonar.core.persistence.migration.v50; import org.apache.ibatis.annotations.*; +import org.apache.ibatis.mapping.ResultSetType; import java.util.List; @@ -29,6 +30,21 @@ public interface Migration50Mapper { /** * Return root projects (Views and Developers are NOT returned) */ + @Select("SELECT " + + " p.id AS \"id\", " + + " p.uuid AS \"uuid\", " + + " p.project_uuid AS \"projectUuid\", " + + " s.root_project_id AS \"projectId\", " + + " s.id AS \"snapshotId\", " + + " s.path AS \"snapshotPath\", " + + " p.scope AS \"scope\" " + + "FROM projects p " + + " LEFT OUTER JOIN snapshots s ON s.project_id = p.id AND s.islast = ${_true} " + + " WHERE " + + " p.scope = 'PRJ' " + + " AND p.root_id IS NULL ") + @Result(javaType = Component.class) + @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 200) List selectRootProjects(); @Select("SELECT " + diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/migration/v50/Migration50Mapper.xml b/sonar-core/src/main/resources/org/sonar/core/persistence/migration/v50/Migration50Mapper.xml index 5d69ead06e5..1db97943867 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/migration/v50/Migration50Mapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/migration/v50/Migration50Mapper.xml @@ -3,21 +3,5 @@ - - -- 2.39.5