]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5237 - Created Base and Rule Normalizer classes
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 30 Apr 2014 12:34:19 +0000 (14:34 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 30 Apr 2014 12:37:06 +0000 (14:37 +0200)
sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/search/Index.java
sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java
sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java

index 6a733d4e6c63cbba0a18e22389c1f74a2066766f..48ef50aaa790c598a723a1d3bee8868f56f94010 100644 (file)
 package org.sonar.server.rule2;
 
 import com.google.common.collect.ImmutableSet;
-import org.apache.commons.beanutils.BeanUtils;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.core.cluster.WorkQueue;
 import org.sonar.core.profiling.Profiling;
-import org.sonar.core.qualityprofile.db.ActiveRuleDao;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
 import org.sonar.core.rule.RuleConstants;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.server.es.ESNode;
@@ -37,9 +34,6 @@ import org.sonar.server.search.QueryOptions;
 import org.sonar.server.search.Results;
 
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 
 import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@@ -51,11 +45,9 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> {
   public static final Set<String> PUBLIC_FIELDS = ImmutableSet.of("repositoryKey", "key", "name", "desc",
     "lang", "severity", "status", "tags", "sysTags", "createdAt", "updatedAt");
 
-  private final ActiveRuleDao activeRuleDao;
-
-  public RuleIndex(WorkQueue workQueue, RuleDao dao, ActiveRuleDao ActiveRuleDao, Profiling profiling, ESNode node) {
-    super(workQueue, dao, profiling, node);
-    this.activeRuleDao = ActiveRuleDao;
+  public RuleIndex(RuleNormalizer normalizer, WorkQueue workQueue,
+    Profiling profiling, ESNode node) {
+    super(normalizer, workQueue, profiling, node);
   }
 
   @Override
@@ -125,88 +117,16 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> {
       return mapping.endObject()
         .endObject().endObject();
 
-      // return jsonBuilder().startObject()
-      // .startObject("issue")
-      // .startObject("properties")
-      // .startObject("component.path")
-      // .field("type", "string")
-      // .field("index_analyzer", "path_analyzer")
-      // .field("search_analyzer", "keyword")
-      // .endObject()
-      // .startObject("rule.name")
-      // .field("type", "string")
-      // .field("analyzer", "keyword")
-      // .endObject()
-      // .startObject("root.id")
-      // .field("type", "multi_field")
-      // .startObject("fields")
-      // .startObject("str")
-      // .field("type", "string")
-      // .field("index","analyzed")
-      // .field("analyzer", "default")
-      // .endObject()
-      // .startObject("num")
-      // .field("type", "long")
-      // .field("index","analyzed")
-      // .endObject()
-      // .endObject()
-      // .endObject()
-      // .endObject().endObject();
     } catch (IOException e) {
       LOG.error("Could not create mapping for {}", this.getIndexName());
       return null;
     }
   }
 
-  @Override
-  public XContentBuilder normalize(RuleKey key) {
-
-    try {
-
-      RuleDto rule = dao.getByKey(key);
-
-      XContentBuilder document = jsonBuilder().startObject();
-
-      Map<String, Object> properties = BeanUtils.describe(rule);
-
-      for (Entry<String, Object> property : properties.entrySet()) {
-        LOG.trace("NORMALIZING: {} -> {}", property.getKey(), property.getValue());
-        document.field(property.getKey(), property.getValue());
-      }
-
-      document.startArray("active");
-      for (ActiveRuleDto activeRule : activeRuleDao.selectByRuleId(rule.getId())) {
-        document.startObject();
-        Map<String, Object> activeRuleProperties = BeanUtils.describe(activeRule);
-        for (Entry<String, Object> activeRuleProp : activeRuleProperties.entrySet()) {
-          LOG.trace("NORMALIZING: --- {} -> {}", activeRuleProp.getKey(), activeRuleProp.getValue());
-          document.field(activeRuleProp.getKey(), activeRuleProp.getValue());
-        }
-        document.endObject();
-      }
-      document.endArray();
-
-      return document.endObject();
-    } catch (IOException e) {
-      LOG.error("Could not normalize {} in {} because {}",
-        key, this.getClass().getSimpleName(), e.getMessage());
-    } catch (IllegalAccessException e) {
-      LOG.error("Could not normalize {} in {} because {}",
-        key, this.getClass().getSimpleName(), e.getMessage());
-    } catch (InvocationTargetException e) {
-      LOG.error("Could not normalize {} in {} because {}",
-        key, this.getClass().getSimpleName(), e.getMessage());
-    } catch (NoSuchMethodException e) {
-      LOG.error("Could not normalize {} in {} because {}",
-        key, this.getClass().getSimpleName(), e.getMessage());
-    }
-    return null;
-  }
-
   public Results search(RuleQuery query, QueryOptions options) {
+
     throw new UnsupportedOperationException("TODO");
 
   }
 
-
 }
diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java
new file mode 100644 (file)
index 0000000..050a453
--- /dev/null
@@ -0,0 +1,78 @@
+package org.sonar.server.rule2;
+
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.core.qualityprofile.db.ActiveRuleDao;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.server.search.BaseNormalizer;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
+
+public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
+
+  private RuleDao ruleDao;
+  private ActiveRuleDao activeRuleDao;
+
+  public enum RuleFields {
+    KEY("key"),
+    RULE_KEY("ruleKey"),
+    REPOSITORY_KEY("repositoryKey"),
+    NAME("name"),
+    CREATED_AT("createdAt");
+
+    private final String key;
+
+    private RuleFields(final String key) {
+      this.key = key;
+    }
+
+    public String key() {
+      return key;
+    }
+
+    public String toString() {
+      return key;
+    }
+  }
+
+  public RuleNormalizer(RuleDao ruleDao, ActiveRuleDao activeRuleDao) {
+    this.ruleDao = ruleDao;
+    this.activeRuleDao = activeRuleDao;
+  }
+
+  @Override
+  public XContentBuilder normalize(RuleKey key) throws IOException {
+    return normalize(ruleDao.getByKey(key));
+  }
+
+  @Override
+  public XContentBuilder normalize(RuleDto rule) throws IOException {
+
+    XContentBuilder document = jsonBuilder().startObject();
+
+    indexField(RuleFields.KEY.key(), rule.getKey(), document);
+    indexField(RuleFields.NAME.key(), rule.getName(), document);
+    indexField(RuleFields.CREATED_AT.key(), rule.getCreatedAt(), document);
+    indexField(RuleFields.RULE_KEY.key(), rule.getRuleKey(), document);
+    indexField(RuleFields.REPOSITORY_KEY.key(), rule.getRepositoryKey(), document);
+
+    // document.startArray("active");
+    // for (ActiveRuleDto activeRule : activeRuleDao.selectByRuleId(rule.getId())) {
+    // document.startObject();
+    // Map<String, Object> activeRuleProperties = BeanUtils.describe(activeRule);
+    // for (Entry<String, Object> activeRuleProp : activeRuleProperties.entrySet()) {
+    // LOG.trace("NORMALIZING: --- {} -> {}", activeRuleProp.getKey(), activeRuleProp.getValue());
+    // document.field(activeRuleProp.getKey(), activeRuleProp.getValue());
+    // }
+    // document.endObject();
+    // }
+    // document.endArray();
+
+    return document.endObject();
+  }
+
+}
index c6e7bcf74c894f33d849c39f245a07a0702744a4..f7703c3894eff3f2ae9255fa7c9e81ae2ddc7bf1 100644 (file)
@@ -32,7 +32,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.core.cluster.WorkQueue;
-import org.sonar.core.db.Dao;
 import org.sonar.core.db.Dto;
 import org.sonar.core.profiling.Profiling;
 import org.sonar.core.profiling.Profiling.Level;
@@ -56,20 +55,15 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
   private final Profiling profiling;
   private Client client;
   private final ESNode node;
-  private IndexSynchronizer<K> synchronizer;
-  protected Dao<E, K> dao;
+  protected BaseNormalizer<E,K> normalizer;
 
-  public BaseIndex(WorkQueue workQueue, Dao<E, K> dao, Profiling profiling, ESNode node) {
+  public BaseIndex(BaseNormalizer<E,K> normalizer, WorkQueue workQueue,
+                Profiling profiling, ESNode node) {
+    this.normalizer = normalizer;
     this.profiling = profiling;
-    this.synchronizer = new IndexSynchronizer<K>(this, dao, workQueue);
-    this.dao = dao;
     this.node = node;
   }
 
-  protected Dao<?, K> getDao() {
-    return this.dao;
-  }
-
   protected Client getClient() {
     return this.client;
   }
@@ -84,9 +78,6 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
     /* Setup the index if necessary */
     this.intializeIndex();
-
-    /* Launch synchronization */
-//    synchronizer.start();
   }
 
   @Override
@@ -102,25 +93,6 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
   public void connect() {
     this.client = this.node.client();
-    // /* Settings to access our local ES node */
-    // Settings settings = ImmutableSettings.settingsBuilder()
-    // .put("client.transport.sniff", true)
-    // .put("cluster.name", ES_CLUSTER_NAME)
-    // .put("node.name", "localclient_")
-    // .build();
-    //
-    // this.client = new TransportClient(settings)
-    // .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT));
-    //
-    // /*
-    // * Cannot do that yet, need version >= 1.0
-    // * ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
-    // * if (nodes.isEmpty()) {
-    // * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!");
-    // * } else {
-    // * log.info("connected to nodes: " + nodes.toString());
-    // * }
-    // */
   }
 
   /* Cluster And ES Stats/Client methods */
@@ -182,14 +154,14 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
   @Override
   public void insert(K key) {
     try {
-      XContentBuilder doc = this.normalize(key);
+      XContentBuilder doc = normalizer.normalize(key);
       String keyvalue = this.getKeyValue(key);
       if (doc != null && keyvalue != null && !keyvalue.isEmpty()) {
         LOG.debug("Update document with key {}", key);
         IndexResponse result = getClient().index(
           new IndexRequest(this.getIndexName(),
             this.getType(), keyvalue)
-            .source(this.normalize(key))).get();
+            .source(doc)).get();
       } else {
         LOG.error("Could not normalize document {} for insert in ",
           key, this.getIndexName());
@@ -204,10 +176,11 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
   public void update(K key) {
     try {
       LOG.info("Update document with key {}", key);
+      XContentBuilder doc = normalizer.normalize(key);
       UpdateResponse result = getClient().update(
         new UpdateRequest(this.getIndexName(),
           this.getType(), this.getKeyValue(key))
-          .doc(this.normalize(key))).get();
+          .doc(doc)).get();
     } catch (Exception e) {
       LOG.error("Could not update documet for index {}: {}",
         this.getIndexName(), e.getMessage());
diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java b/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java
new file mode 100644 (file)
index 0000000..f092051
--- /dev/null
@@ -0,0 +1,63 @@
+package org.sonar.server.search;
+
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.core.db.Dto;
+
+import java.io.IOException;
+import java.io.Serializable;
+
+public abstract class BaseNormalizer<E extends Dto<K>, K extends Serializable> {
+
+  public abstract XContentBuilder normalize(K key) throws IOException;
+
+  public abstract XContentBuilder normalize(E dto) throws IOException;
+
+  private static final Logger LOG = LoggerFactory.getLogger(BaseNormalizer.class);
+
+  protected void indexField(String field, Object value, XContentBuilder document){
+    try {
+      document.field(field,value);
+    } catch (IOException e) {
+      LOG.error("Could not set {} to {} in ESDocument", field, value);
+    }
+  }
+
+//  protected void indexField(Fields field, Object dto, XContentBuilder document) {
+//    try {
+//      document.field(field.key(), field.method.invoke(dto));
+//    } catch (IllegalArgumentException e) {
+//      // TODO Auto-generated catch block
+//      e.printStackTrace();
+//    } catch (IOException e) {
+//      // TODO Auto-generated catch block
+//      e.printStackTrace();
+//    } catch (IllegalAccessException e) {
+//      // TODO Auto-generated catch block
+//      e.printStackTrace();
+//    } catch (InvocationTargetException e) {
+//      // TODO Auto-generated catch block
+//      e.printStackTrace();
+//    }
+//  }
+//
+//
+//
+//private static Method getReadMethod(String method){
+//  try {
+//    return RuleDto.class.getDeclaredMethod(method);
+//  } catch (SecurityException e) {
+//    // TODO Auto-generated catch block
+//    e.printStackTrace();
+//  } catch (NoSuchMethodException e) {
+//    // TODO Auto-generated catch block
+//    e.printStackTrace();
+//  }
+//  return null;
+//}
+
+
+
+
+}
index b81427f9b0cfe7f1b31d2169690e14dccc51b098..bb29abb781bfe2ae01361fb5eef277e13500770e 100644 (file)
  */
 package org.sonar.server.search;
 
-import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.picocontainer.Startable;
 
 import javax.annotation.CheckForNull;
+
 import java.io.Serializable;
 
 public interface Index<K extends Serializable> extends Startable {
@@ -38,8 +38,6 @@ public interface Index<K extends Serializable> extends Startable {
 
   void delete(K key);
 
-  XContentBuilder normalize(K key);
-
   Long getLastSynchronization();
 
   void setLastSynchronization(Long time);
index 6680efacd8403cbd3bfbb159aa70ec1581e57965..abaeac31bc7133006c9da103dc5300d1e80d8566 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.search;
 import com.google.common.collect.ImmutableList;
 import org.sonar.server.cluster.LocalQueueWorker;
 import org.sonar.server.rule2.RuleIndex;
+import org.sonar.server.rule2.RuleNormalizer;
 
 import java.util.List;
 
@@ -31,6 +32,7 @@ public final class IndexUtils {
   @SuppressWarnings("unchecked")
   public static List<Class> getIndexClasses() {
     return ImmutableList.<Class>of(
+      RuleNormalizer.class,
       RuleIndex.class,
       LocalQueueWorker.class
     );
index 6f721af10514e1278c417226ff458a01f4331527..cff60333d7fca917d11dd5e0f96e1da4a4bfdced 100644 (file)
@@ -105,7 +105,7 @@ public class RuleMediumTest {
     dao.insert(dto);
 
     Hit hit = index.getByKey(dto.getKey());
-    assertThat(hit.getFields().get("ruleKey")).isEqualTo(dto.getRuleKey());
+    assertThat(hit.getFields().get(RuleNormalizer.RuleFields.RULE_KEY.key())).isEqualTo(dto.getRuleKey().toString());
   }
 
   @Test