diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-31 10:24:33 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-31 10:33:37 +0100 |
commit | b700157a6d099554a7ef68f918daec2c97a34f27 (patch) | |
tree | 6f53c29bdec19023cd80914e3b4259a744d508eb /server | |
parent | 289659e866ae02caa2aa538d0d7405641ae27f6c (diff) | |
download | sonarqube-b700157a6d099554a7ef68f918daec2c97a34f27.tar.gz sonarqube-b700157a6d099554a7ef68f918daec2c97a34f27.zip |
Refactor ES definition of fields
Do not use reflection to load ES index fields
Diffstat (limited to 'server')
6 files changed, 73 insertions, 178 deletions
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 8568ab632d5..2e64ed487a7 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 @@ -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) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationNormalizer.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationNormalizer.java index 53ccd57ca18..1519c428399 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationNormalizer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueAuthorizationNormalizer.java @@ -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 diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueNormalizer.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueNormalizer.java index 777ba466f28..aef44c588b5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueNormalizer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueNormalizer.java @@ -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 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 bdb38fdcf40..4dfe45cc78f 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 @@ -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()) + ); } } 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 54eae4488f7..c322c0ae488 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 @@ -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()) + ); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/Indexable.java b/server/sonar-server/src/main/java/org/sonar/server/search/Indexable.java index eb993d1feb3..f22bd33a11d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/Indexable.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/Indexable.java @@ -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); } } - |