]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Implementation of IndexSynchronizer
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 23 May 2014 18:25:23 +0000 (20:25 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 23 May 2014 18:27:20 +0000 (20:27 +0200)
sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml
sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/persistence/ActiveRuleDao.java
sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java

index b6fc979e329aa1e4467cb9b8b06aacad3c1fa29b..9f53644fd29b94b8bc2412dfacc8b2e87902ddde 100644 (file)
 
     <select id="selectAllKeysAfterTimestamp" parameterType="Date" resultType="map">
         SELECT
-        rule.plugin_rule_key as ruleField,
-        rule.plugin_name as repositoryField,
-        profile.name as profileField,
-        profile.language as languageField
+        r.plugin_rule_key as ruleField,
+        r.plugin_name as repositoryField,
+        qp.name as profileField,
+        qp.language as languageField
         FROM active_rules a
         <include refid="activeRuleKeyJoin"/>
         <where>
-            WHERE a.updated_at IS NULL or a.updated_at &gt;= #{id}
+            a.updated_at IS NULL or a.updated_at &gt;= #{id}
         </where>
     </select>
 
index de2240db45e41a1dd9a7901db4c78f93bdb90349..58120327a55d74480162f7251532e024009fec3d 100644 (file)
@@ -538,6 +538,9 @@ class ServerComponents {
     startupContainer.addSingleton(CleanPreviewAnalysisCache.class);
     startupContainer.addSingleton(CopyRequirementsFromCharacteristicsToRules.class);
 
+    /** Index startup Synchronization */
+    startupContainer.addSingleton(IndexSynchronizer.class);
+
     DoPrivileged.execute(new DoPrivileged.Task() {
       @Override
       protected void doPrivileged() {
index cbe56e8f7cc512a3efe1167d78eb9ee57a6b55cc..2f0732dc05e1e608c09a75cdca365a4245403051 100644 (file)
@@ -37,8 +37,8 @@ import org.sonar.core.qualityprofile.db.QualityProfileKey;
 import org.sonar.core.rule.RuleDto;
 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.IndexDefinition;
 import org.sonar.server.search.action.IndexAction;
 import org.sonar.server.search.action.KeyIndexAction;
 
@@ -58,7 +58,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
 
   @VisibleForTesting
   public ActiveRuleDao(QualityProfileDao profileDao, RuleDao ruleDao, System2 system) {
-    super(new ActiveRuleIndexDefinition(), ActiveRuleMapper.class, system);
+    super(IndexDefinition.ACTIVE_RULE, ActiveRuleMapper.class, system);
     this.ruleDao = ruleDao;
     this.profileDao = profileDao;
   }
@@ -75,6 +75,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
         session.enqueue(new KeyIndexAction<ActiveRuleKey>(getIndexType(), IndexAction.Method.UPSERT, key));
       }
     });
+    session.commit();
   }
 
 
@@ -135,6 +136,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
   @Override
   protected void doDeleteByKey(ActiveRuleKey key, DbSession session) {
     ActiveRuleDto rule = this.getByKey(key, session);
+    mapper(session).deleteParameters(rule.getId());
     mapper(session).delete(rule.getId());
   }
 
@@ -201,7 +203,6 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
 
   public void deleteByProfileKey(QualityProfileKey profileKey, DbSession session) {
     /** Functional cascade for params */
-    this.removeParamByProfileKey(profileKey, session);
     for (ActiveRuleDto activeRule : this.findByProfileKey(profileKey, session)) {
       this.delete(activeRule, session);
     }
index 63cb4dbf52412e5140df115de1936ed734f40e22..a457cbea05841e7a78ab0fb3d070c8d21e7f8d98 100644 (file)
  */
 package org.sonar.server.search;
 
-  import org.sonar.server.db.DbClient;
+import org.picocontainer.Startable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.server.db.DbClient;
 
 /**
  * @since 4.4
  */
-public class IndexSynchronizer {
+public class IndexSynchronizer implements Startable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(IndexSynchronizer.class);
 
   private final DbClient db;
   private final IndexClient index;
@@ -33,4 +39,20 @@ public class IndexSynchronizer {
     this.db = db;
     this.index = index;
   }
+
+  @Override
+  public void start() {
+    /* synchronize all activeRules until we have mng tables in INDEX */
+    DbSession session = db.openSession(true);
+    LOG.info("Starting DB to Index synchronization");
+    long start = System.currentTimeMillis();
+    db.activeRuleDao().synchronizeAfter(0, session);
+    LOG.info("Synchronization done in {}ms...", (System.currentTimeMillis()-start));
+    session.close();
+  }
+
+  @Override
+  public void stop() {
+
+  }
 }