Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-6315 use new RegisterQualityProfile code only if property is set
- Loading branch information
Showing
10 changed files
with
493 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...-server/src/main/java/org/sonar/server/qualityprofile/CachingDefinedQProfileCreation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.qualityprofile; | ||
|
||
/** | ||
* Marker interface of any implementation of {@link DefinedQProfileCreation} which supports caching. | ||
*/ | ||
public interface CachingDefinedQProfileCreation extends DefinedQProfileCreation { | ||
} |
28 changes: 28 additions & 0 deletions
28
...ver/src/main/java/org/sonar/server/qualityprofile/CachingDefinedQProfileCreationImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.qualityprofile; | ||
|
||
import org.sonar.db.DbClient; | ||
|
||
public class CachingDefinedQProfileCreationImpl extends DefinedQProfileCreationImpl implements CachingDefinedQProfileCreation { | ||
public CachingDefinedQProfileCreationImpl(DbClient dbClient, QProfileFactory profileFactory, CachingRuleActivator ruleActivator) { | ||
super(dbClient, profileFactory, ruleActivator); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/CachingRuleActivator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.qualityprofile; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import java.util.List; | ||
import org.sonar.api.utils.System2; | ||
import org.sonar.db.DbClient; | ||
import org.sonar.db.DbSession; | ||
import org.sonar.db.qualityprofile.QualityProfileDto; | ||
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; | ||
import org.sonar.server.rule.index.RuleIndex; | ||
import org.sonar.server.user.UserSession; | ||
import org.sonar.server.util.TypeValidations; | ||
|
||
public class CachingRuleActivator extends RuleActivator { | ||
private final Cache<String, List<QualityProfileDto>> childrenByParentKey = CacheBuilder.newBuilder() | ||
.maximumSize(10_000) | ||
.build(); | ||
|
||
public CachingRuleActivator(System2 system2, DbClient db, RuleIndex ruleIndex, CachingRuleActivatorContextFactory contextFactory, TypeValidations typeValidations, | ||
ActiveRuleIndexer activeRuleIndexer, UserSession userSession) { | ||
super(system2, db, ruleIndex, contextFactory, typeValidations, activeRuleIndexer, userSession); | ||
} | ||
|
||
@Override | ||
protected List<QualityProfileDto> getChildren(DbSession session, String qualityProfileKey) { | ||
List<QualityProfileDto> res = childrenByParentKey.getIfPresent(qualityProfileKey); | ||
if (res != null) { | ||
return res; | ||
} | ||
res = super.getChildren(session, qualityProfileKey); | ||
childrenByParentKey.put(qualityProfileKey, res); | ||
return res; | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...ver/src/main/java/org/sonar/server/qualityprofile/CachingRuleActivatorContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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.qualityprofile; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import org.picocontainer.Startable; | ||
import org.sonar.api.rule.RuleKey; | ||
import org.sonar.core.util.stream.MoreCollectors; | ||
import org.sonar.db.DbClient; | ||
import org.sonar.db.DbSession; | ||
import org.sonar.db.qualityprofile.ActiveRuleDto; | ||
import org.sonar.db.qualityprofile.ActiveRuleKey; | ||
import org.sonar.db.rule.RuleDefinitionDto; | ||
|
||
public class CachingRuleActivatorContextFactory extends RuleActivatorContextFactory implements Startable { | ||
private final DbClient dbClient; | ||
private final Map<RuleKey, RuleDefinitionDto> rulesByRuleKey = new HashMap<>(); | ||
private final Cache<String, Map<RuleKey, ActiveRuleDto>> childrenByParentKey = CacheBuilder.newBuilder() | ||
.maximumSize(10) | ||
.build(); | ||
|
||
public CachingRuleActivatorContextFactory(DbClient db) { | ||
super(db); | ||
this.dbClient = db; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
try (DbSession dbSession = dbClient.openSession(false)) { | ||
dbClient.ruleDao().selectAllDefinitions(dbSession).forEach(rule -> rulesByRuleKey.put(rule.getKey(), rule)); | ||
} | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
Optional<RuleDefinitionDto> getRule(DbSession dbSession, RuleKey ruleKey) { | ||
return Optional.ofNullable(rulesByRuleKey.get(ruleKey)); | ||
} | ||
|
||
@Override | ||
Optional<ActiveRuleDto> getActiveRule(DbSession session, ActiveRuleKey key) { | ||
try { | ||
String profileKey = key.qProfile(); | ||
Map<RuleKey, ActiveRuleDto> profileActiveRulesByRuleKey = childrenByParentKey.get( | ||
profileKey, | ||
() -> loadActiveRulesOfQualityProfile(session, profileKey)); | ||
return Optional.ofNullable(profileActiveRulesByRuleKey.get(key.ruleKey())); | ||
} catch (ExecutionException e) { | ||
throw new IllegalStateException(e.getCause()); | ||
} | ||
} | ||
|
||
private Map<RuleKey, ActiveRuleDto> loadActiveRulesOfQualityProfile(DbSession session, String profileKey) { | ||
return dbClient.activeRuleDao().selectByProfileKey(session, profileKey).stream() | ||
.collect(MoreCollectors.uniqueIndex(dto -> dto.getKey().ruleKey())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.