From c9ae6d687f6b230225abf09b9ce7b81c818ed8d7 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Wed, 14 May 2014 11:52:57 +0200 Subject: [PATCH] Added Index synchronization for Nested Dtos --- .../java/org/sonar/server/db/BaseDao.java | 16 +++++ .../persistence/ActiveRuleDao.java | 61 +++++++++++++------ .../server/rule2/persistence/RuleDao.java | 39 ++++++------ 3 files changed, 77 insertions(+), 39 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java index 27d48bee6f0..741bdf7db74 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ b/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java @@ -26,6 +26,7 @@ import org.sonar.core.db.Dto; import org.sonar.core.persistence.DbSession; import org.sonar.server.search.IndexDefinition; import org.sonar.server.search.action.DtoIndexAction; +import org.sonar.server.search.action.EmbeddedIndexAction; import org.sonar.server.search.action.IndexAction; import org.sonar.server.search.action.KeyIndexAction; @@ -195,4 +196,19 @@ public abstract class BaseDao, K extends Serializable> imple doDeleteByKey(key, session); session.enqueue(new KeyIndexAction(this.getIndexType(), IndexAction.Method.DELETE, key)); } + + protected void enqueueUpdate(Object nestedItem, K key, DbSession session) { + session.enqueue(new EmbeddedIndexAction( + this.getIndexType(), IndexAction.Method.UPDATE, nestedItem, key)); + } + + public void enqueueDelete(Object nestedItem, K key, DbSession session) { + session.enqueue(new EmbeddedIndexAction( + this.getIndexType(), IndexAction.Method.DELETE, nestedItem, key)); + } + + public void enqueueInsert(Object nestedItem, K key, DbSession session) { + session.enqueue(new EmbeddedIndexAction( + this.getIndexType(), IndexAction.Method.INSERT, nestedItem, key)); + } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/persistence/ActiveRuleDao.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/persistence/ActiveRuleDao.java index 6495596473f..f29d341c2f4 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/persistence/ActiveRuleDao.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/persistence/ActiveRuleDao.java @@ -20,7 +20,9 @@ package org.sonar.server.qualityprofile.persistence; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; +import org.sonar.api.utils.System2; import org.sonar.core.persistence.DbSession; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleKey; @@ -33,13 +35,12 @@ import org.sonar.server.db.BaseDao; import org.sonar.server.qualityprofile.QProfile; import org.sonar.server.qualityprofile.index.ActiveRuleIndexDefinition; import org.sonar.server.rule2.persistence.RuleDao; -import org.sonar.server.search.action.EmbeddedIndexAction; -import org.sonar.server.search.action.IndexAction; import java.util.List; public class ActiveRuleDao extends BaseDao { + //TODO remove once key is finalized (used only to get id for SQL statement) private final RuleDao ruleDao; private final QualityProfileDao profileDao; @@ -49,6 +50,13 @@ public class ActiveRuleDao extends BaseDao keysOfRowsUpdatedAfter(long timestamp, DbSession session) { throw new UnsupportedOperationException("Need to implement ActiveRuleDto.doGetByKey() method"); @@ -91,33 +99,39 @@ public class ActiveRuleDao extends BaseDao findParamsByActiveRule(ActiveRuleDto dto, DbSession session) { - Preconditions.checkArgument(dto.getId() != null, "ActiveRule is not persisted"); - return mapper(session).selectParamsByActiveRuleId(dto.getId()); - } - - public ActiveRuleParamDto addParam(ActiveRuleDto activeRule, ActiveRuleParamDto paramDto, DbSession session) { - Preconditions.checkState(activeRule.getId() != null, "ActiveRule id is not yet persisted"); - Preconditions.checkState(paramDto.getId() == null, "ActiveRuleParam is already persisted"); - Preconditions.checkState(paramDto.getRulesParameterId() != null, "Rule param is not persisted"); - - paramDto.setActiveRuleId(activeRule.getId()); - mapper(session).insertParameter(paramDto); - session.enqueue(new EmbeddedIndexAction(this.getIndexType(), IndexAction.Method.INSERT, paramDto, activeRule.getKey())); - return paramDto; - } + /** + * Finder methods for Rules + */ public List findByRule(RuleDto rule, DbSession dbSession) { Preconditions.checkArgument(rule.getId()!=null, "Rule is not persisted"); return mapper(dbSession).selectByRuleId(rule.getId()); } + /** + * Nested DTO ActiveRuleParams + */ + + public ActiveRuleParamDto addParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, DbSession session) { + Preconditions.checkState(activeRule.getId() != null, "ActiveRule id is not yet persisted"); + Preconditions.checkState(activeRuleParam.getId() == null, "ActiveRuleParam is already persisted"); + Preconditions.checkState(activeRuleParam.getRulesParameterId() != null, "Rule param is not persisted"); + + activeRuleParam.setActiveRuleId(activeRule.getId()); + mapper(session).insertParameter(activeRuleParam); + this.enqueueInsert(activeRuleParam, activeRule.getKey(), session); + return activeRuleParam; + } + public void removeAllParam(ActiveRuleDto activeRule, DbSession session) { Preconditions.checkArgument(activeRule.getId()!=null, "ActiveRule is not persisted"); + //TODO Optimize this + for(ActiveRuleParamDto activeRuleParam:this.findParamsByActiveRule(activeRule,session)){ + this.enqueueDelete(activeRuleParam, activeRule.getKey(), session); + } mapper(session).deleteParameters(activeRule.getId()); } @@ -125,12 +139,14 @@ public class ActiveRuleDao extends BaseDao findParamsByActiveRule(ActiveRuleDto dto, DbSession session) { + Preconditions.checkArgument(dto.getId() != null, "ActiveRule is not persisted"); + return mapper(session).selectParamsByActiveRuleId(dto.getId()); + } } diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/persistence/RuleDao.java b/sonar-server/src/main/java/org/sonar/server/rule2/persistence/RuleDao.java index e13b10f7877..0f9fea1a8c3 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/persistence/RuleDao.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/persistence/RuleDao.java @@ -32,8 +32,6 @@ import org.sonar.core.rule.RuleMapper; import org.sonar.core.rule.RuleParamDto; import org.sonar.server.db.BaseDao; import org.sonar.server.rule2.index.RuleIndexDefinition; -import org.sonar.server.search.action.EmbeddedIndexAction; -import org.sonar.server.search.action.IndexAction; import javax.annotation.CheckForNull; import java.sql.Timestamp; @@ -59,6 +57,10 @@ public class RuleDao extends BaseDao { return mapper(session).selectByKey(key); } + public RuleDto getByName(String name, DbSession session) { + return mapper(session).selectByName(name); + } + @Override protected RuleDto doInsert(RuleDto item, DbSession session) { mapper(session).insert(item); @@ -117,35 +119,30 @@ public class RuleDao extends BaseDao { return mapper(session).selectEnablesAndNonManual(); } - public RuleDto getByName(String name, DbSession session) { - //TODO change selectByName to return a list - return mapper(session).selectByName(name); - } - /** * Nested DTO RuleParams */ - public void addRuleParam(RuleDto rule, RuleParamDto paramDto, DbSession session) { + public void addRuleParam(RuleDto rule, RuleParamDto ruleParam, DbSession session) { Preconditions.checkNotNull(rule.getId(), "Rule id must be set"); - paramDto.setRuleId(rule.getId()); - mapper(session).insertParameter(paramDto); - session.enqueue(new EmbeddedIndexAction(this.getIndexType(), IndexAction.Method.INSERT, paramDto, rule.getKey())); + ruleParam.setRuleId(rule.getId()); + mapper(session).insertParameter(ruleParam); + this.enqueueInsert(ruleParam, rule.getKey(), session); } - public RuleParamDto updateRuleParam(RuleDto rule, RuleParamDto paramDto, DbSession session) { + public RuleParamDto updateRuleParam(RuleDto rule, RuleParamDto ruleParam, DbSession session) { Preconditions.checkNotNull(rule.getId(), "Rule id must be set"); - Preconditions.checkNotNull(paramDto.getId(), "Param is not yet persisted must be set"); - paramDto.setRuleId(rule.getId()); - session.enqueue(new EmbeddedIndexAction(this.getIndexType(), IndexAction.Method.UPDATE, paramDto, rule.getKey())); - mapper(session).updateParameter(paramDto); - return paramDto; + Preconditions.checkNotNull(ruleParam.getId(), "Param is not yet persisted must be set"); + ruleParam.setRuleId(rule.getId()); + mapper(session).updateParameter(ruleParam); + this.enqueueUpdate(ruleParam, rule.getKey(), session); + return ruleParam; } - public void removeRuleParam(RuleDto rule, RuleParamDto param, DbSession session) { - Preconditions.checkNotNull(param.getId(), "Param is not persisted"); - mapper(session).deleteParameter(param.getId()); - session.enqueue(new EmbeddedIndexAction(this.getIndexType(), IndexAction.Method.DELETE, param, rule.getKey())); + public void removeRuleParam(RuleDto rule, RuleParamDto ruleParam, DbSession session) { + Preconditions.checkNotNull(ruleParam.getId(), "Param is not persisted"); + mapper(session).deleteParameter(ruleParam.getId()); + this.enqueueDelete(ruleParam, rule.getKey(), session); } /** -- 2.39.5