]> source.dussan.org Git - sonarqube.git/commitdiff
Refactor ES definition of fields
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 31 Oct 2014 09:24:33 +0000 (10:24 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 31 Oct 2014 09:33:37 +0000 (10:33 +0100)
Do not use reflection to load ES index fields

server/sonar-server/src/main/java/org/sonar/server/activity/index/ActivityNormalizer.java
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationNormalizer.java
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueNormalizer.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java
server/sonar-server/src/main/java/org/sonar/server/search/Indexable.java

index 8568ab632d57a09bea1dc7dfa475e60ea3ac959d..2e64ed487a7917a40b8433aada67ff1a9547e673 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.activity.index;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import org.elasticsearch.action.support.replication.ReplicationType;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.sonar.api.utils.KeyValueFormat;
@@ -29,15 +30,16 @@ import org.sonar.server.search.BaseNormalizer;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.Indexable;
 
-import java.lang.reflect.Field;
-import java.util.*;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * @since 4.4
  */
 public class ActivityNormalizer extends BaseNormalizer<ActivityDto, String> {
 
-
   public static final class LogFields extends Indexable {
 
     public static final IndexField KEY = add(IndexField.Type.STRING, "key");
@@ -48,22 +50,7 @@ public class ActivityNormalizer extends BaseNormalizer<ActivityDto, String> {
     public static final IndexField LOGIN = addSearchable(IndexField.Type.STRING, "login");
     public static final IndexField DETAILS = addSearchable(IndexField.Type.OBJECT, "details");
     public static final IndexField MESSAGE = addSearchable(IndexField.Type.STRING, "message");
-
-    public static Set<IndexField> ALL_FIELDS = getAllFields();
-
-    private static Set<IndexField> getAllFields() {
-      Set<IndexField> fields = new HashSet<IndexField>();
-      for (Field classField : LogFields.class.getDeclaredFields()) {
-        if (classField.getType().isAssignableFrom(IndexField.class)) {
-          try {
-            fields.add(IndexField.class.cast(classField.get(null)));
-          } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
-          }
-        }
-      }
-      return fields;
-    }
+    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(KEY, TYPE, ACTION, CREATED_AT, UPDATED_AT, LOGIN, DETAILS, MESSAGE);
   }
 
   public ActivityNormalizer(DbClient db) {
@@ -84,7 +71,7 @@ public class ActivityNormalizer extends BaseNormalizer<ActivityDto, String> {
 
     logDoc.put(LogFields.DETAILS.field(), KeyValueFormat.parse(dto.getData()));
 
-   /* Creating updateRequest */
+    /* Creating updateRequest */
     return ImmutableList.of(new UpdateRequest()
       .id(dto.getKey())
       .replicationType(ReplicationType.ASYNC)
index 53ccd57ca187ee47a5e63dfceff4d9713bc897dd..1519c428399929f70c9fac5d1d9315f6ba73cee7 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.issue.index;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.sonar.core.issue.db.IssueAuthorizationDto;
 import org.sonar.server.db.DbClient;
@@ -29,8 +30,10 @@ import org.sonar.server.search.BaseNormalizer;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.Indexable;
 
-import java.lang.reflect.Field;
-import java.util.*;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class IssueAuthorizationNormalizer extends BaseNormalizer<IssueAuthorizationDto, String> {
 
@@ -46,21 +49,7 @@ public class IssueAuthorizationNormalizer extends BaseNormalizer<IssueAuthorizat
     public static final IndexField USERS = add(IndexField.Type.STRING, "users");
     public static final IndexField UPDATED_AT = add(IndexField.Type.DATE, BaseNormalizer.UPDATED_AT_FIELD);
 
-    public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
-    private static Set<IndexField> getAllFields() {
-      Set<IndexField> fields = new HashSet<IndexField>();
-      for (Field classField : IssueAuthorizationField.class.getDeclaredFields()) {
-        if (classField.getType().isAssignableFrom(IndexField.class)) {
-          try {
-            fields.add(IndexField.class.cast(classField.get(null)));
-          } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
-          }
-        }
-      }
-      return fields;
-    }
+    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(PROJECT, PERMISSION, GROUPS, USERS, UPDATED_AT);
   }
 
   @Override
index 777ba466f288682a2c8564acb215dc7bd3446e35..aef44c588b5fc0c6a1976357346e11d10968cd2f 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.issue.index;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.sonar.api.rule.Severity;
 import org.sonar.core.issue.db.IssueDto;
@@ -29,8 +30,6 @@ import org.sonar.server.search.BaseNormalizer;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.Indexable;
 
-import java.lang.reflect.Field;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -74,21 +73,10 @@ public class IssueNormalizer extends BaseNormalizer<IssueDto, String> {
     public static final IndexField RULE_KEY = add(IndexField.Type.STRING, "ruleKey");
     public static final IndexField FILE_PATH = addSortable(IndexField.Type.STRING, "filePath");
 
-    public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
-    private static final Set<IndexField> getAllFields() {
-      Set<IndexField> fields = new HashSet<IndexField>();
-      for (Field classField : IssueField.class.getDeclaredFields()) {
-        if (classField.getType().isAssignableFrom(IndexField.class)) {
-          try {
-            fields.add(IndexField.class.cast(classField.get(null)));
-          } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
-          }
-        }
-      }
-      return fields;
-    }
+    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(KEY, CREATED_AT, UPDATED_AT, PROJECT, COMPONENT,
+      MODULE, MODULE_PATH, ACTION_PLAN, ASSIGNEE, ATTRIBUTES, AUTHOR_LOGIN, DEBT, EFFORT, ISSUE_CREATED_AT,
+      ISSUE_UPDATED_AT, ISSUE_CLOSE_DATE, LINE, MESSAGE, RESOLUTION, REPORTER, STATUS, SEVERITY, SEVERITY_VALUE,
+      LANGUAGE, RULE_KEY, FILE_PATH);
   }
 
   @Override
index bdb38fdcf4059b7257f1cff29cef206e4acd7db1..4dfe45cc78f3b8f6ef07362ca3c3b94c4cf106cd 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.qualityprofile.index;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import org.elasticsearch.action.support.replication.ReplicationType;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.sonar.core.persistence.DbSession;
@@ -35,10 +36,8 @@ import org.sonar.server.search.BaseNormalizer;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.Indexable;
 
-import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -58,42 +57,14 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
     public static final IndexField CREATED_AT = addSortable(IndexField.Type.DATE, "createdAt");
     public static final IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, UPDATED_AT_FIELD);
 
-    public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
-    private static Set<IndexField> getAllFields() {
-      Set<IndexField> fields = new HashSet<IndexField>();
-      for (Field classField : ActiveRuleField.class.getDeclaredFields()) {
-        if (classField.getType().isAssignableFrom(IndexField.class)) {
-          try {
-            fields.add(IndexField.class.cast(classField.get(null)));
-          } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Can not introspect active rule fields", e);
-          }
-        }
-      }
-      return fields;
-    }
+    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(KEY, INHERITANCE, PROFILE_KEY, SEVERITY, PARENT_KEY, RULE_KEY, PARAMS, CREATED_AT, UPDATED_AT);
 
   }
 
   public static class ActiveRuleParamField extends Indexable {
     public static final IndexField NAME = add(IndexField.Type.STRING, "name");
     public static final IndexField VALUE = add(IndexField.Type.STRING, "value");
-    public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
-    private static Set<IndexField> getAllFields() {
-      Set<IndexField> fields = new HashSet<IndexField>();
-      for (Field classField : ActiveRuleParamField.class.getDeclaredFields()) {
-        if (classField.getType().isAssignableFrom(IndexField.class)) {
-          try {
-            fields.add(IndexField.class.cast(classField.get(null)));
-          } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Can not introspect active rule param fields", e);
-          }
-        }
-      }
-      return fields;
-    }
+    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(NAME, VALUE);
   }
 
   public ActiveRuleNormalizer(DbClient db) {
@@ -150,7 +121,7 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
         .doc(newRule)
         .upsert(getUpsertFor(ActiveRuleField.ALL_FIELDS, newRule)));
 
-      //Get the RuleParameters
+      // Get the RuleParameters
       for (ActiveRuleParamDto param : db.activeRuleDao().findParamsByActiveRuleKey(session, key)) {
         requests.addAll(normalizeNested(param, key));
       }
@@ -191,27 +162,27 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
     newParam.put(ActiveRuleParamField.VALUE.field(), param.getValue());
 
     return ImmutableList.of(new UpdateRequest()
-        .replicationType(ReplicationType.ASYNC)
-        .routing(key.ruleKey().toString())
-        .id(key.toString())
-        .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
-    );
+      .replicationType(ReplicationType.ASYNC)
+      .routing(key.ruleKey().toString())
+      .id(key.toString())
+      .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
+      );
   }
 
   private List<UpdateRequest> nestedDelete(ActiveRuleParamDto param, ActiveRuleKey key) {
     return ImmutableList.of(new UpdateRequest()
-        .replicationType(ReplicationType.ASYNC)
-        .routing(key.ruleKey().toString())
-        .id(key.toString())
-        .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
-    );
+      .replicationType(ReplicationType.ASYNC)
+      .routing(key.ruleKey().toString())
+      .id(key.toString())
+      .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
+      );
   }
 }
index 54eae4488f725023c88eeddcb56607a348312b9f..c322c0ae488dddd10e889f674ea4360ea1175d93 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.rule.index;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.sonar.api.rule.RuleKey;
@@ -54,21 +55,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     public static final IndexField DESCRIPTION = addSearchable(IndexField.Type.TEXT, "description");
     public static final IndexField DEFAULT_VALUE = add(IndexField.Type.STRING, "defaultValue");
 
-    public static Set<IndexField> ALL_FIELDS = getAllFields();
-
-    private static final Set<IndexField> getAllFields() {
-      Set<IndexField> fields = new HashSet<IndexField>();
-      for (Field classField : RuleParamField.class.getDeclaredFields()) {
-        if (classField.getType().isAssignableFrom(IndexField.class)) {
-          try {
-            fields.add(IndexField.class.cast(classField.get(null)));
-          } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
-          }
-        }
-      }
-      return fields;
-    }
+    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(NAME, TYPE, DESCRIPTION, DEFAULT_VALUE);
   }
 
   public static final class RuleField extends Indexable {
@@ -116,22 +103,16 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     public static final IndexField ALL_TAGS = addSearchable(IndexField.Type.STRING, "allTags");
     public static final IndexField PARAMS = addEmbedded("params", RuleParamField.ALL_FIELDS);
 
-    public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
-    private static final Set<IndexField> getAllFields() {
-      Set<IndexField> fields = new HashSet<IndexField>();
-      for (Field classField : RuleField.class.getDeclaredFields()) {
-        if (classField.getType().isAssignableFrom(IndexField.class)) {
-          try {
-            fields.add(IndexField.class.cast(classField.get(null)));
-          } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
-          }
-        }
-      }
-      return fields;
-    }
+    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(ID, KEY, _KEY, REPOSITORY, RULE_KEY, NAME, CREATED_AT,
+      UPDATED_AT, HTML_DESCRIPTION, MARKDOWN_DESCRIPTION, SEVERITY, STATUS, FIX_DESCRIPTION,
+      LANGUAGE, TAGS, SYSTEM_TAGS, INTERNAL_KEY, IS_TEMPLATE, TEMPLATE_KEY, DEFAULT_DEBT_FUNCTION_TYPE,
+      DEFAULT_DEBT_FUNCTION_COEFFICIENT, DEFAULT_DEBT_FUNCTION_OFFSET, DEBT_FUNCTION_TYPE, DEBT_FUNCTION_COEFFICIENT,
+      DEBT_FUNCTION_OFFSET, DEFAULT_CHARACTERISTIC, DEFAULT_SUB_CHARACTERISTIC, CHARACTERISTIC, SUB_CHARACTERISTIC,
+      NOTE, NOTE_LOGIN, NOTE_CREATED_AT, NOTE_UPDATED_AT, ALL_TAGS, PARAMS);
 
+    /**
+     * Warning - O(n) complexity
+     */
     public static IndexField of(String fieldName) {
       for (IndexField field : ALL_FIELDS) {
         if (field.field().equals(fieldName)) {
@@ -323,24 +304,24 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     newParam.put(RuleParamField.DEFAULT_VALUE.field(), param.getDefaultValue());
 
     return ImmutableList.of(new UpdateRequest()
-        .id(key.toString())
-        .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
-    );
+      .id(key.toString())
+      .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
+      );
   }
 
   private List<UpdateRequest> nestedDelete(RuleParamDto param, RuleKey key) {
     return ImmutableList.of(new UpdateRequest()
-        .id(key.toString())
-        .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
-        .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
-    );
+      .id(key.toString())
+      .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
+      .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
+      );
   }
 
 }
index eb993d1feb39834e69f6605393c50a7c946262de..f22bd33a11da52423c88a03d17c708d91cd34782 100644 (file)
@@ -23,47 +23,27 @@ import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 
 import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
 
 public class Indexable {
 
-  private static final Set<IndexField> ALL_FIELDS = new HashSet<IndexField>();
-
-  public static IndexField add(IndexField.Type type, String field){
-    IndexField indexField = new IndexField(type, field);
-    ALL_FIELDS.add(indexField);
-    return indexField;
+  public static IndexField add(IndexField.Type type, String field) {
+    return new IndexField(type, field);
   }
 
   public static IndexField addEmbedded(String field, Collection<IndexField> nestedFields) {
-    IndexField indexField = new IndexField(IndexField.Type.OBJECT, field, nestedFields);
-    ALL_FIELDS.add(indexField);
-    return indexField;
+    return new IndexField(IndexField.Type.OBJECT, field, nestedFields);
   }
 
-
-  public static IndexField addSearchable(IndexField.Type type, String field){
-    IndexField indexField = new IndexField(type, field)
-      .setSearchable(true);
-    ALL_FIELDS.add(indexField);
-    return indexField;
+  public static IndexField addSearchable(IndexField.Type type, String field) {
+    return new IndexField(type, field).setSearchable(true);
   }
 
-
   public static IndexField addSortableAndSearchable(IndexField.Type type, String field) {
-    IndexField indexField = new IndexField(type, field)
-      .setSearchable(true)
-      .setSortable(true);
-    ALL_FIELDS.add(indexField);
-    return indexField;
+    return new IndexField(type, field).setSearchable(true).setSortable(true);
   }
 
-  public static IndexField addSortable(IndexField.Type type, String field){
-    IndexField indexField = new IndexField(type, field)
-      .setSortable(true);
-    ALL_FIELDS.add(indexField);
-    return indexField;
+  public static IndexField addSortable(IndexField.Type type, String field) {
+    return new IndexField(type, field).setSortable(true);
   }
 
   @Override
@@ -71,4 +51,3 @@ public class Indexable {
     return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
   }
 }
-