From dd237c28a020d890e77a275e07ee09adaac39979 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Tue, 19 Aug 2014 18:19:39 +0200 Subject: [PATCH] Updated DAO with findAfterDate method --- .../sonar/server/activity/db/ActivityDao.java | 20 ++--- .../activity/index/ActivityNormalizer.java | 14 ---- .../component/persistence/ComponentDao.java | 21 ----- .../java/org/sonar/server/db/BaseDao.java | 50 ++++++++---- .../main/java/org/sonar/server/db/Dao.java | 26 +++--- .../measure/persistence/MeasureDao.java | 25 ------ .../server/measure/persistence/MetricDao.java | 25 ------ .../qualityprofile/db/ActiveRuleDao.java | 45 +++++------ .../index/ActiveRuleNormalizer.java | 79 +++++++++---------- .../org/sonar/server/rule/db/RuleDao.java | 23 ++---- .../server/rule/index/RuleNormalizer.java | 33 ++++---- .../org/sonar/server/search/BaseIndex.java | 16 +--- .../sonar/server/search/BaseNormalizer.java | 2 - .../java/org/sonar/server/search/Index.java | 4 +- .../server/search/action/DtoIndexAction.java | 2 +- .../server/search/action/KeyIndexAction.java | 2 +- .../org/sonar/server/rule/db/RuleDaoTest.java | 29 +++++++ .../java/org/sonar/core/rule/RuleMapper.java | 38 +++++++++ .../org/sonar/core/rule/RuleMapper.xml | 6 -- 19 files changed, 207 insertions(+), 253 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/activity/db/ActivityDao.java b/server/sonar-server/src/main/java/org/sonar/server/activity/db/ActivityDao.java index 5800f7d698c..df395320c09 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/activity/db/ActivityDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/activity/db/ActivityDao.java @@ -25,9 +25,8 @@ import org.sonar.core.activity.db.ActivityMapper; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.BaseDao; import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.action.DtoIndexAction; -import org.sonar.server.search.action.IndexAction; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -61,15 +60,16 @@ public class ActivityDao extends BaseDao { throw new IllegalStateException("Cannot delete Log!"); } - public List findAll(DbSession session) { - return mapper(session).selectAll(); + @Override + protected Iterable findAfterDate(DbSession session, Date date) { + // for (ActivityDto activity : this.findAll(session)) { +// session.enqueue(new DtoIndexAction(this.getIndexType(), IndexAction.Method.UPSERT, activity)); +// } +// session.commit(); + return Collections.EMPTY_LIST; } - @Override - public void synchronizeAfter(DbSession session, Date time) { - for (ActivityDto activity : this.findAll(session)) { - session.enqueue(new DtoIndexAction(this.getIndexType(), IndexAction.Method.UPSERT, activity)); - } - session.commit(); + public List findAll(DbSession session) { + return mapper(session).selectAll(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/activity/index/ActivityNormalizer.java b/server/sonar-server/src/main/java/org/sonar/server/activity/index/ActivityNormalizer.java index 33361c9493a..f311095e468 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/activity/index/ActivityNormalizer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/activity/index/ActivityNormalizer.java @@ -24,7 +24,6 @@ import org.elasticsearch.action.support.replication.ReplicationType; import org.elasticsearch.action.update.UpdateRequest; import org.sonar.api.utils.KeyValueFormat; import org.sonar.core.activity.db.ActivityDto; -import org.sonar.core.persistence.DbSession; import org.sonar.server.db.DbClient; import org.sonar.server.search.BaseNormalizer; import org.sonar.server.search.IndexDefinition; @@ -33,7 +32,6 @@ import org.sonar.server.search.Indexable; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -78,18 +76,6 @@ public class ActivityNormalizer extends BaseNormalizer { super(IndexDefinition.LOG, db); } - @Override - public List normalize(String activityKey) { - DbSession dbSession = db.openSession(false); - List requests = new ArrayList(); - try { - requests.addAll(normalize(db.activityDao().getByKey(dbSession, activityKey))); - } finally { - dbSession.close(); - } - return requests; - } - @Override public List normalize(ActivityDto dto) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java b/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java index 376e78ec3bf..edcbe529d16 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java @@ -30,8 +30,6 @@ import org.sonar.server.db.BaseDao; import org.sonar.server.exceptions.NotFoundException; import javax.annotation.CheckForNull; - -import java.util.Date; import java.util.List; /** @@ -123,23 +121,4 @@ public class ComponentDao extends BaseDao mapper(session).insert(item); return item; } - - @Override - protected ComponentDto doUpdate(DbSession session, ComponentDto item) { - throw notImplemented(); - } - - @Override - protected void doDeleteByKey(DbSession session, String key) { - throw notImplemented(); - } - - @Override - public void synchronizeAfter(DbSession session, Date timestamp) { - throw notImplemented(); - } - - private static IllegalStateException notImplemented() { - throw new IllegalStateException("Not implemented yet"); - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java index d79460478a0..a14270f1358 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java @@ -33,7 +33,6 @@ import org.sonar.server.search.action.KeyIndexAction; import javax.annotation.CheckForNull; import javax.annotation.Nullable; - import java.io.Serializable; import java.util.Collection; import java.util.Date; @@ -130,15 +129,6 @@ public abstract class BaseDao, K extends Serializable> imple return indexDefinition != null ? this.indexDefinition.getIndexType() : null; } - @CheckForNull - protected abstract E doGetNullableByKey(DbSession session, K key); - - protected abstract E doInsert(DbSession session, E item); - - protected abstract E doUpdate(DbSession session, E item); - - protected abstract void doDeleteByKey(DbSession session, K key); - protected M mapper(DbSession session) { return session.getMapper(mapperClass); } @@ -156,14 +146,14 @@ public abstract class BaseDao, K extends Serializable> imple } @Override - public E update(DbSession session, E item) { + public final E update(DbSession session, E item) { Date now = new Date(system2.now()); update(session, item, now); return item; } @Override - public E update(DbSession session, E item, E... others) { + public final E update(DbSession session, E item, E... others) { Date now = new Date(system2.now()); update(session, item, now); for (E other : others) { @@ -194,13 +184,13 @@ public abstract class BaseDao, K extends Serializable> imple } @Override - public E insert(DbSession session, E item) { + public final E insert(DbSession session, E item) { insert(session, item, new Date(system2.now())); return item; } @Override - public Collection insert(DbSession session, Collection items) { + public final Collection insert(DbSession session, Collection items) { Date now = new Date(system2.now()); for (E item : items) { insert(session, item, now); @@ -287,4 +277,36 @@ public abstract class BaseDao, K extends Serializable> imple this.getIndexType(), IndexAction.Method.UPSERT, key, nestedItem)); } } + + @Override + public final void synchronizeAfter(final DbSession session, Date date) { + for (E dto : this.findAfterDate(session, date)) { + session.enqueue(new DtoIndexAction(getIndexType(), IndexAction.Method.UPSERT, + dto)); + } + session.commit(); + } + + @CheckForNull + protected abstract E doGetNullableByKey(DbSession session, K key); + + protected Iterable findAfterDate(final DbSession session, Date date) { + throw notImplemented(this); + } + + protected E doInsert(DbSession session, E item) { + throw notImplemented(this); + } + + protected E doUpdate(DbSession session, E item) { + throw notImplemented(this); + } + + protected void doDeleteByKey(DbSession session, K key) { + throw notImplemented(this); + } + + private static RuntimeException notImplemented(BaseDao baseDao) { + throw new IllegalStateException("Not implemented yet for class [" + baseDao.getClass().getSimpleName() +"]"); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/Dao.java b/server/sonar-server/src/main/java/org/sonar/server/db/Dao.java index aa32ced7111..11ac312fa83 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/Dao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/Dao.java @@ -28,55 +28,55 @@ import java.io.Serializable; import java.util.Collection; import java.util.Date; -public interface Dao, K extends Serializable> extends ServerComponent { +public interface Dao, KEY extends Serializable> extends ServerComponent { /** * Get a DTO by its key. Return null if the key does not exist. */ @CheckForNull - E getNullableByKey(DbSession session, K key); + DTO getNullableByKey(DbSession session, KEY key); /** * Get a DTO by its key. * * @throws org.sonar.server.exceptions.NotFoundException if the key does not exist */ - E getByKey(DbSession session, K key); + DTO getByKey(DbSession session, KEY key); /** * Update a table row. DTO id must be set. The field updatedAt * is changed by this method. */ - E update(DbSession session, E dto); + DTO update(DbSession session, DTO dto); /** * Update one or more table rows. Note that the returned DTO is only * the first updated one. */ - E update(DbSession session, E dto, E... others); + DTO update(DbSession session, DTO dto, DTO... others); - Collection update(DbSession session, Collection dtos); + Collection update(DbSession session, Collection dtos); - E insert(DbSession session, E dto); + DTO insert(DbSession session, DTO dto); /** * Insert one or more database rows. Note * that the returned DTO is only the first inserted one. */ - E insert(DbSession session, E dto, E... others); + DTO insert(DbSession session, DTO dto, DTO... others); - Collection insert(DbSession session, Collection dtos); + Collection insert(DbSession session, Collection dtos); - void delete(DbSession session, E dto); + void delete(DbSession session, DTO dto); /** * Delete one or more table rows. */ - void delete(DbSession session, E dto, E... others); + void delete(DbSession session, DTO dto, DTO... others); - void delete(DbSession session, Collection dtos); + void delete(DbSession session, Collection dtos); - void deleteByKey(DbSession session, K key); + void deleteByKey(DbSession session, KEY key); void synchronizeAfter(DbSession session, Date date); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java b/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java index 3067f32290f..89790d9524c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java @@ -32,7 +32,6 @@ import org.sonar.core.persistence.DbSession; import org.sonar.server.db.BaseDao; import java.util.Collections; -import java.util.Date; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @@ -72,28 +71,4 @@ public class MeasureDao extends BaseDao i public MeasureDto findByComponentKeyAndMetricKey(String componentKey, String metricKey, DbSession session) { return mapper(session).selectByComponentAndMetric(componentKey, metricKey); } - - @Override - protected MeasureDto doInsert(DbSession session, MeasureDto item) { - throw notImplemented(); - } - - @Override - protected MeasureDto doUpdate(DbSession session, MeasureDto item) { - throw notImplemented(); - } - - @Override - protected void doDeleteByKey(DbSession session, MeasureKey key) { - throw notImplemented(); - } - - @Override - public void synchronizeAfter(DbSession session, Date date) { - throw notImplemented(); - } - - private static RuntimeException notImplemented() { - throw new IllegalStateException("Not implemented yet"); - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MetricDao.java b/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MetricDao.java index 58e20d13557..d561ead931e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MetricDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/persistence/MetricDao.java @@ -29,7 +29,6 @@ import org.sonar.core.persistence.DaoComponent; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.BaseDao; -import java.util.Date; import java.util.List; public class MetricDao extends BaseDao implements ServerComponent, DaoComponent { @@ -51,28 +50,4 @@ public class MetricDao extends BaseDao implemen public List findEnabled(DbSession session) { return mapper(session).selectAllEnabled(); } - - @Override - protected MetricDto doInsert(DbSession session, MetricDto item) { - throw notImplemented(); - } - - @Override - protected MetricDto doUpdate(DbSession session, MetricDto item) { - throw notImplemented(); - } - - @Override - protected void doDeleteByKey(DbSession session, String key) { - throw notImplemented(); - } - - @Override - public void synchronizeAfter(DbSession session, Date date) { - throw notImplemented(); - } - - private static RuntimeException notImplemented() { - throw new IllegalStateException("Not implemented yet"); - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java index 3ecb31a6e4f..682ed69f762 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java @@ -22,9 +22,6 @@ package org.sonar.server.qualityprofile.db; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import org.apache.ibatis.session.ResultContext; -import org.apache.ibatis.session.ResultHandler; -import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.System2; import org.sonar.core.persistence.DbSession; import org.sonar.core.qualityprofile.db.ActiveRuleDto; @@ -37,15 +34,11 @@ import org.sonar.core.rule.RuleDto; import org.sonar.server.db.BaseDao; import org.sonar.server.rule.db.RuleDao; import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.action.IndexAction; -import org.sonar.server.search.action.KeyIndexAction; import javax.annotation.CheckForNull; - -import java.sql.Timestamp; +import java.util.Collections; import java.util.Date; import java.util.List; -import java.util.Map; public class ActiveRuleDao extends BaseDao { @@ -74,22 +67,6 @@ public class ActiveRuleDao extends BaseDao fields = (Map) context.getResultObject(); - // "rule" is a reserved keyword in SQLServer, so "rulefield" is used - ActiveRuleKey key = ActiveRuleKey.of( - (String) fields.get("profileKey"), - RuleKey.of((String) fields.get("repository"), (String) fields.get("rulefield"))); - session.enqueue(new KeyIndexAction(getIndexType(), IndexAction.Method.UPSERT, key)); - } - }); - session.commit(); - } - /** * @deprecated do not use ids but keys */ @@ -140,6 +117,26 @@ public class ActiveRuleDao extends BaseDao findAfterDate(DbSession session, Date date) { +// @Override +// public void synchronizeAfter(final DbSession session, Date date) { +// session.select("selectAllKeysAfterTimestamp", new Timestamp(date.getTime()), new ResultHandler() { +// @Override +// public void handleResult(ResultContext context) { +// Map fields = (Map) context.getResultObject(); +// // "rule" is a reserved keyword in SQLServer, so "rulefield" is used +// ActiveRuleKey key = ActiveRuleKey.of( +// (String) fields.get("profileKey"), +// RuleKey.of((String) fields.get("repository"), (String) fields.get("rulefield"))); +// session.enqueue(new KeyIndexAction(getIndexType(), IndexAction.Method.UPSERT, key)); +// } +// }); +// session.commit(); +// } + return Collections.EMPTY_LIST; + } + /** * Finder methods for Rules */ diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java index a8be590c1dc..8ece6b6eca8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java @@ -102,23 +102,10 @@ public class ActiveRuleNormalizer extends BaseNormalizer normalize(ActiveRuleKey key) { - DbSession dbSession = db.openSession(false); + public List normalize(ActiveRuleDto activeRuleDto) { + List requests = new ArrayList(); - try { - ActiveRuleDto activeRule = db.activeRuleDao().getByKey(dbSession, key); - requests.addAll(normalize(activeRule)); - for (ActiveRuleParamDto param : db.activeRuleDao().findParamsByActiveRuleKey(dbSession, key)) { - requests.addAll(normalizeNested(param, key)); - } - } finally { - dbSession.close(); - } - return requests; - } - @Override - public List normalize(ActiveRuleDto activeRuleDto) { ActiveRuleKey key = activeRuleDto.getKey(); Preconditions.checkArgument(key != null, "Cannot normalize ActiveRuleDto with null key"); @@ -154,21 +141,27 @@ public class ActiveRuleNormalizer extends BaseNormalizer upsert = getUpsertFor(ActiveRuleField.ALL_FIELDS, newRule); - - /* Creating updateRequest */ - return ImmutableList.of(new UpdateRequest() - .replicationType(ReplicationType.ASYNC) - .routing(key.ruleKey().toString()) - .id(activeRuleDto.getKey().toString()) - .parent(activeRuleDto.getKey().ruleKey().toString()) - .doc(newRule) - .upsert(upsert)); + return requests; } @Override @@ -199,27 +192,27 @@ public class ActiveRuleNormalizer extends BaseNormalizer nestedDelete(ActiveRuleParamDto param, ActiveRuleKey key) { return ImmutableList.of(new UpdateRequest() - .replicationType(ReplicationType.ASYNC) - .routing(key.ruleKey().toString()) - .id(key.toString()) - .script(ListUpdate.NAME) - .addScriptParam(ListUpdate.FIELD, ActiveRuleField.PARAMS.field()) - .addScriptParam(ListUpdate.VALUE, null) - .addScriptParam(ListUpdate.ID_FIELD, ActiveRuleParamField.NAME.field()) - .addScriptParam(ListUpdate.ID_VALUE, param.getKey()) - ); + .replicationType(ReplicationType.ASYNC) + .routing(key.ruleKey().toString()) + .id(key.toString()) + .script(ListUpdate.NAME) + .addScriptParam(ListUpdate.FIELD, ActiveRuleField.PARAMS.field()) + .addScriptParam(ListUpdate.VALUE, null) + .addScriptParam(ListUpdate.ID_FIELD, ActiveRuleParamField.NAME.field()) + .addScriptParam(ListUpdate.ID_VALUE, param.getKey()) + ); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java b/server/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java index f1476dafc5b..0e7b19a4ca5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java @@ -21,8 +21,6 @@ package org.sonar.server.rule.db; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import org.apache.ibatis.session.ResultContext; -import org.apache.ibatis.session.ResultHandler; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.System2; import org.sonar.core.persistence.DbSession; @@ -31,14 +29,11 @@ import org.sonar.core.rule.RuleMapper; import org.sonar.core.rule.RuleParamDto; import org.sonar.server.db.BaseDao; import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.action.IndexAction; -import org.sonar.server.search.action.KeyIndexAction; import javax.annotation.CheckForNull; import java.sql.Timestamp; import java.util.Date; import java.util.List; -import java.util.Map; public class RuleDao extends BaseDao { @@ -77,6 +72,11 @@ public class RuleDao extends BaseDao { throw new UnsupportedOperationException("Rules cannot be deleted"); } + @Override + protected Iterable findAfterDate(DbSession session, Date date) { + return mapper(session).selectAfterDate(new Timestamp(date.getTime())); + } + /** * @deprecated use keys. */ @@ -92,19 +92,6 @@ public class RuleDao extends BaseDao { return mapper(session).selectById(rule.getTemplateId()); } - @Override - public void synchronizeAfter(final DbSession session, Date date) { - session.select("selectKeysOfRulesUpdatedSince", new Timestamp(date.getTime()), new ResultHandler() { - @Override - public void handleResult(ResultContext context) { - Map map = (Map) context.getResultObject(); - session.enqueue(new KeyIndexAction(getIndexType(), IndexAction.Method.UPSERT, - RuleKey.of(map.get("repoField"), map.get("ruleField")))); - } - }); - session.commit(); - } - /** * Finder methods for Rules */ diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java index cceea9b0acf..9071d30faf2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java @@ -41,7 +41,12 @@ import org.sonar.server.search.Indexable; import org.sonar.server.search.es.ListUpdate; import java.lang.reflect.Field; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; public class RuleNormalizer extends BaseNormalizer { @@ -144,25 +149,11 @@ public class RuleNormalizer extends BaseNormalizer { super(IndexDefinition.RULE, db); } - @Override - public List normalize(RuleKey key) { - DbSession dbSession = db.openSession(false); - List requests = new ArrayList(); - try { - RuleDto rule = db.ruleDao().getByKey(dbSession, key); - requests.addAll(normalize(rule)); - for (RuleParamDto param : db.ruleDao().findRuleParamsByRuleKey(dbSession, key)) { - requests.addAll(normalizeNested(param, key)); - } - } finally { - dbSession.close(); - } - return requests; - } - @Override public List normalize(RuleDto rule) { + List requests = new ArrayList(); + DbSession session = db.openSession(false); try { @@ -289,16 +280,22 @@ public class RuleNormalizer extends BaseNormalizer { upsert.put(RuleField.KEY.field(), rule.getKey().toString()); /** Creating updateRequest */ - return ImmutableList.of(new UpdateRequest() + requests.add(new UpdateRequest() .replicationType(ReplicationType.ASYNC) .consistencyLevel(WriteConsistencyLevel.QUORUM) .id(rule.getKey().toString()) .doc(update) .upsert(upsert)); + for (RuleParamDto param : db.ruleDao().findRuleParamsByRuleKey(session, rule.getKey())) { + requests.addAll(normalizeNested(param, rule.getKey())); + } + } finally { session.close(); } + + return requests; } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index 5ba63af5ab3..5abddd2db69 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -425,7 +425,7 @@ public abstract class BaseIndex, KEY extends Serial } @Override - public void upsertByDto(DTO item, DTO... items) { + public void upsert(DTO item, DTO... items) { try { long t0 = System.currentTimeMillis(); List requests = normalizer.normalize(item); @@ -443,20 +443,6 @@ public abstract class BaseIndex, KEY extends Serial } } - @Override - public void upsertByKey(KEY key, KEY... keys) { - try { - List requests = normalizer.normalize(key); - for (KEY additionalKey : keys) { - requests.addAll(normalizer.normalize(additionalKey)); - } - this.updateDocument(requests, key); - } catch (Exception e) { - LOG.error("Could not update document for index {}: {}", - this.getIndexName(), e.getMessage(), e); - } - } - private void deleteDocument(KEY key) throws ExecutionException, InterruptedException { LOG.debug("DELETE _id:{} in index {}", key, this.getIndexName()); DeleteRequestBuilder request = client diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java b/server/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java index 5218a9000ad..22114aad423 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java @@ -64,7 +64,5 @@ public abstract class BaseNormalizer, KEY extends Serializa throw new IllegalStateException("Nested Normalize not implemented in current normalizer!"); } - public abstract List normalize(KEY key); - public abstract List normalize(DTO dto); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/Index.java b/server/sonar-server/src/main/java/org/sonar/server/search/Index.java index 0f6838c3994..08e8836c564 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/Index.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/Index.java @@ -42,9 +42,7 @@ public interface Index, KEY extends Serializable> e void upsert(KEY key, Object object, Object... objects) throws Exception; - void upsertByKey(KEY key, KEY... keys); - - void upsertByDto(DTO dto, DTO... dtos); + void upsert(DTO dto, DTO... dtos); void delete(KEY key, Object object, Object... objects) throws Exception; diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/action/DtoIndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/search/action/DtoIndexAction.java index 7215bdd47f5..3f09d708cbd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/action/DtoIndexAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/action/DtoIndexAction.java @@ -48,7 +48,7 @@ public class DtoIndexAction extends IndexAction { if (this.getMethod().equals(Method.DELETE)) { index.deleteByDto(this.item, this.items); } else if (this.getMethod().equals(Method.UPSERT)) { - index.upsertByDto(this.item, this.items); + index.upsert(this.item, this.items); } } catch (Exception e) { throw new IllegalStateException(this.getClass().getSimpleName() + diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/action/KeyIndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/search/action/KeyIndexAction.java index 4a77140df78..3e23d5514e8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/action/KeyIndexAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/action/KeyIndexAction.java @@ -48,7 +48,7 @@ public class KeyIndexAction extends IndexAction { if (this.getMethod().equals(Method.DELETE)) { index.deleteByKey(this.key, this.keys); } else if (this.getMethod().equals(Method.UPSERT)) { - index.upsertByKey(this.key, this.keys); + throw new IllegalStateException("Upsert by Key is not supported anymore"); } } catch (Exception e) { throw new IllegalStateException(this.getClass().getSimpleName() + diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/db/RuleDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/db/RuleDaoTest.java index 013f0a77dec..3c2cc8527fa 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/db/RuleDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/db/RuleDaoTest.java @@ -34,7 +34,9 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleDto.Format; import org.sonar.core.rule.RuleParamDto; +import org.sonar.server.rule.RuleTesting; +import java.util.Date; import java.util.List; import static org.fest.assertions.Assertions.assertThat; @@ -366,4 +368,31 @@ public class RuleDaoTest extends AbstractDaoTestCase { checkTables("update_parameter", "rules_parameters"); } + + @Test + public void findAfterDate() throws Exception { + long t0 = DateUtils.parseDate("2014-01-01").getTime(); + when(system2.now()).thenReturn(t0); + dao.insert(session, RuleTesting.newXooX1()); + session.commit(); + assertThat(dao.getByKey(session, RuleTesting.XOO_X1).getCreatedAt().after(new Date(t0))); + + + long t1 = DateUtils.parseDate("2014-02-01").getTime(); + when(system2.now()).thenReturn(t1); + dao.insert(session, RuleTesting.newXooX2()); + session.commit(); + assertThat(dao.getByKey(session, RuleTesting.XOO_X2).getCreatedAt().after(new Date(t1))); + + long t2 = DateUtils.parseDate("2014-03-01").getTime(); + when(system2.now()).thenReturn(t2); + session.flushStatements(); + + + assertThat(dao.findAll(session)).hasSize(2); + assertThat(dao.findAfterDate(session, new Date(0))).hasSize(2); + assertThat(dao.findAfterDate(session, new Date(t0))).hasSize(2); + assertThat(dao.findAfterDate(session, new Date(t1))).hasSize(1); + assertThat(dao.findAfterDate(session, new Date(t2))).hasSize(0); + } } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java index 5aeb9fbfaa7..3b9a1a9ab78 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java @@ -19,9 +19,13 @@ */ package org.sonar.core.rule; +import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Result; +import org.apache.ibatis.annotations.Select; import org.sonar.api.rule.RuleKey; +import java.sql.Timestamp; import java.util.List; public interface RuleMapper { @@ -60,4 +64,38 @@ public interface RuleMapper { void deleteParameter(Integer paramId); + final String SELECT_FIELDS="r.id,\n" + + " r.plugin_rule_key as \"ruleKey\",\n" + + " r.plugin_name as \"repositoryKey\",\n" + + " r.description,\n" + + " r.description_format as \"descriptionFormat\",\n" + + " r.status,\n" + + " r.name,\n" + + " r.plugin_config_key as \"configKey\",\n" + + " r.priority as \"severity\",\n" + + " r.is_template as \"isTemplate\",\n" + + " r.language as \"language\",\n" + + " r.template_id as \"templateId\",\n" + + " r.note_data as \"noteData\",\n" + + " r.note_user_login as \"noteUserLogin\",\n" + + " r.note_created_at as \"noteCreatedAt\",\n" + + " r.note_updated_at as \"noteUpdatedAt\",\n" + + " r.characteristic_id as \"subCharacteristicId\",\n" + + " r.default_characteristic_id as \"defaultSubCharacteristicId\",\n" + + " r.remediation_function as \"remediationFunction\",\n" + + " r.default_remediation_function as \"defaultRemediationFunction\",\n" + + " r.remediation_coeff as \"remediationCoefficient\",\n" + + " r.default_remediation_coeff as \"defaultRemediationCoefficient\",\n" + + " r.remediation_offset as \"remediationOffset\",\n" + + " r.default_remediation_offset as \"defaultRemediationOffset\",\n" + + " r.effort_to_fix_description as \"effortToFixDescription\",\n" + + " r.tags as \"tagsField\",\n" + + " r.system_tags as \"systemTagsField\",\n" + + " r.created_at as \"createdAt\",\n" + + " r.updated_at as \"updatedAt\""; + + @Select("SELECT " + SELECT_FIELDS + " FROM rules r WHERE r.updated_at IS NULL or r.updated_at >= #{date} ") + @Options(fetchSize = 200, useCache = false, flushCache = true) + @Result(javaType = RuleDto.class) + List selectAfterDate(@Param("date") Timestamp timestamp); } diff --git a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml index 6255c904b7d..bb2e84f6ca1 100644 --- a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml @@ -177,11 +177,5 @@ description=#{description} WHERE id=#{id} - - -- 2.39.5