]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 30 Apr 2014 22:11:22 +0000 (00:11 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 30 Apr 2014 22:11:22 +0000 (00:11 +0200)
sonar-server/src/main/java/org/sonar/server/db/BaseDao.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java

index fd588c97f2e57b57bb768e72bf6d19b1645ed5ee..fe4e7381dacff5a17b91ff44ca3a908e2256c25d 100644 (file)
@@ -31,7 +31,7 @@ import java.io.Serializable;
 public abstract class BaseDao<E extends Dto<K>, K extends Serializable>
   implements Dao<E, K> {
 
-  protected MyBatis mybatis;
+  protected final MyBatis mybatis;
 
   protected BaseDao(MyBatis myBatis) {
     this.mybatis = myBatis;
@@ -56,9 +56,11 @@ public abstract class BaseDao<E extends Dto<K>, K extends Serializable>
   @Override
   public E getByKey(K key) {
     DbSession session = getMyBatis().openSession(false);
-    E item = this.doGetByKey(key, session);
-    MyBatis.closeQuietly(session);
-    return item;
+    try {
+      return this.doGetByKey(key, session);
+    } finally {
+      session.close();
+    }
   }
 
   @Override
index 4b0fc473172d76bdb06cb391f1491d4e7de8a750..9c7c5a58da017a94f6e34d54e6996ec623943ba9 100644 (file)
@@ -42,7 +42,6 @@ import org.sonar.server.search.BaseIndex;
 import org.sonar.server.search.Hit;
 import org.sonar.server.search.QueryOptions;
 import org.sonar.server.search.Results;
-import org.yecht.Data;
 
 import java.io.IOException;
 import java.util.Map;
@@ -185,7 +184,7 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> {
     for (SearchHit esHit : esResult.getHits().getHits()) {
       Hit hit = new Hit(esHit.score());
       for (Map.Entry<String, SearchHitField> entry : esHit.fields().entrySet()) {
-        if(entry.getValue().getValues().size()>1) {
+        if (entry.getValue().getValues().size() > 1) {
           hit.getFields().put(entry.getKey(), entry.getValue().getValues());
         } else {
           hit.getFields().put(entry.getKey(), entry.getValue().getValue());
index e11d98879a029bfe579d62e6138840687dc05df8..85b03c23e0a1aec0c43eb2731e45848dda16bd6b 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.server.rule2;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.check.Cardinality;
-import org.sonar.core.qualityprofile.db.ActiveRuleDao;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.server.search.BaseNormalizer;
 
@@ -33,7 +32,6 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
 public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
 
   private RuleDao ruleDao;
-  private ActiveRuleDao activeRuleDao;
 
   public enum RuleField {
     KEY("key"),
@@ -65,9 +63,8 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     }
   }
 
-  public RuleNormalizer(RuleDao ruleDao, ActiveRuleDao activeRuleDao) {
+  public RuleNormalizer(RuleDao ruleDao) {
     this.ruleDao = ruleDao;
-    this.activeRuleDao = activeRuleDao;
   }
 
   @Override
@@ -77,10 +74,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
 
   @Override
   public XContentBuilder normalize(RuleDto rule) throws IOException {
-
     XContentBuilder document = jsonBuilder().startObject();
-
-
     indexField(RuleField.KEY.key(), rule.getRuleKey(), document);
     indexField(RuleField.REPOSITORY.key(), rule.getRepositoryKey(), document);
     indexField(RuleField.NAME.key(), rule.getName(), document);
@@ -92,7 +86,6 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     indexField(RuleField.LANGUAGE.key(), rule.getLanguage(), document);
     indexField(RuleField.INTERNAL_KEY.key(), rule.getConfigKey(), document);
     indexField(RuleField.TEMPLATE.key(), rule.getCardinality() == Cardinality.MULTIPLE, document);
-
     indexField(RuleField.TAGS.key(), rule.getName(), document);
     indexField(RuleField.SYSTEM_TAGS.key(), rule.getName(), document);
 
index 59aa9d28b7bc8f9f340f13a79fdc18efd7decd59..0193500bd8d288682042971c1305e802a278254f 100644 (file)
  */
 package org.sonar.server.search;
 
-import org.elasticsearch.action.admin.cluster.stats.ClusterStatsNodes;
 import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
-import org.elasticsearch.action.delete.DeleteResponse;
 import org.elasticsearch.action.get.GetResponse;
 import org.elasticsearch.action.index.IndexRequest;
-import org.elasticsearch.action.index.IndexResponse;
 import org.elasticsearch.action.update.UpdateRequest;
-import org.elasticsearch.action.update.UpdateResponse;
 import org.elasticsearch.client.Client;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.index.query.BoolFilterBuilder;
@@ -37,8 +33,6 @@ import org.slf4j.LoggerFactory;
 import org.sonar.core.cluster.WorkQueue;
 import org.sonar.core.db.Dto;
 import org.sonar.core.profiling.Profiling;
-import org.sonar.core.profiling.Profiling.Level;
-import org.sonar.core.profiling.StopWatch;
 import org.sonar.server.es.ESNode;
 
 import java.io.IOException;
@@ -47,29 +41,21 @@ import java.util.Collection;
 
 public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implements Index<K> {
 
-  private static final String PROFILE_DOMAIN = "es";
-
   private static final Logger LOG = LoggerFactory.getLogger(BaseIndex.class);
 
-  public static final String ES_CLUSTER_NAME = "sonarqube";
-
-  private static final String LOCAL_ES_NODE_HOST = "localhost";
-  private static final int LOCAL_ES_NODE_PORT = 9300;
-
   private final Profiling profiling;
-  private Client client;
   private final ESNode node;
   protected BaseNormalizer<E, K> normalizer;
 
   public BaseIndex(BaseNormalizer<E, K> normalizer, WorkQueue workQueue,
-    Profiling profiling, ESNode node) {
+                   Profiling profiling, ESNode node) {
     this.normalizer = normalizer;
     this.profiling = profiling;
     this.node = node;
   }
 
   protected Client getClient() {
-    return this.client;
+    return node.client();
   }
 
   /* Component Methods */
@@ -77,26 +63,13 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
   @Override
   public void start() {
 
-    /* Connect to the local ES Cluster */
-    this.connect();
-
     /* Setup the index if necessary */
     this.intializeIndex();
   }
 
   @Override
   public void stop() {
-    if (client != null) {
-      client.close();
-    }
-  }
-
-  private StopWatch createWatch() {
-    return profiling.start(PROFILE_DOMAIN, Level.FULL);
-  }
 
-  public void connect() {
-    this.client = this.node.client();
   }
 
   /* Cluster And ES Stats/Client methods */
@@ -105,7 +78,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
     String index = this.getIndexName();
 
-    IndicesExistsResponse indexExistsResponse = client.admin().indices()
+    IndicesExistsResponse indexExistsResponse = getClient().admin().indices()
       .prepareExists(index).execute().actionGet();
 
     if (!indexExistsResponse.isExists()) {
@@ -120,22 +93,13 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
         e.printStackTrace();
       }
 
-      client.admin().indices().prepareCreate(index)
+      getClient().admin().indices().prepareCreate(index)
         .setSettings(getIndexSettings())
         .addMapping(getType(), getMapping())
         .execute().actionGet();
     }
   }
 
-  public ClusterStatsNodes getNodesStats() {
-    StopWatch watch = createWatch();
-    try {
-      return client.admin().cluster().prepareClusterStats().get().getNodesStats();
-    } finally {
-      watch.stop("ping from transport client");
-    }
-  }
-
   /* Index management methods */
 
   protected abstract XContentBuilder getIndexSettings();
@@ -159,21 +123,22 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
   public void insert(K key) {
     try {
       XContentBuilder doc = normalizer.normalize(key);
-      String keyValue = this.getKeyValue(key);
+      String keyValue = getKeyValue(key);
       if (doc != null && keyValue != null && !keyValue.isEmpty()) {
-        LOG.debug("Update document with key {}", key);
-        IndexResponse result = getClient().index(
+        LOG.debug("Insert document with key {}", key);
+        getClient().index(
           new IndexRequest(this.getIndexName(),
-            this.getType(), keyValue)
-            .refresh(true)
-            .source(doc)).get();
+            getType(), keyValue)
+            //.refresh(true)
+            .source(doc)
+        ).get();
       } else {
-        LOG.error("Could not normalize document {} for insert in ",
-          key, this.getIndexName());
+        LOG.error("Could not normalize document {} for insert in {}",
+          key, getIndexName());
       }
     } catch (Exception e) {
-      LOG.error("Could not update documet for index {}: {}",
-        this.getIndexName(), e.getMessage());
+      LOG.error("Could not update document for index {}: {}",
+        getIndexName(), e.getMessage());
     }
   }
 
@@ -182,11 +147,12 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
     try {
       LOG.info("Update document with key {}", key);
       XContentBuilder doc = normalizer.normalize(key);
-      UpdateResponse result = getClient().update(
+      getClient().update(
         new UpdateRequest(this.getIndexName(),
           this.getType(), this.getKeyValue(key))
-                .refresh(true)
-          .doc(doc)).get();
+          //.refresh(true)
+          .doc(doc)
+      ).get();
     } catch (Exception e) {
       LOG.error("Could not update documet for index {}: {}",
         this.getIndexName(), e.getMessage());
@@ -196,7 +162,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
   @Override
   public void delete(K key) {
     LOG.info("Deleting document with key {}", key);
-    DeleteResponse result = getClient()
+    getClient()
       .prepareDelete(this.getIndexName(), this.getType(), this.getKeyValue(key))
       .get();
   }
@@ -229,8 +195,8 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
     if (values != null && !values.isEmpty()) {
       BoolFilterBuilder valuesFilter = FilterBuilders.boolFilter();
       for (String value : values) {
-          FilterBuilder valueFilter = FilterBuilders.termFilter(field, value);
-          valuesFilter.should(valueFilter);
+        FilterBuilder valueFilter = FilterBuilders.termFilter(field, value);
+        valuesFilter.should(valueFilter);
       }
       filter.must(valuesFilter);
     }