diff options
author | Daniel Schwarz <daniel.schwarz@sonarsource.com> | 2017-08-08 11:30:50 +0200 |
---|---|---|
committer | Daniel Schwarz <bartfastiel@users.noreply.github.com> | 2017-08-09 15:09:54 +0200 |
commit | fbbb1a0ebed1211229fdb010111d1b8d99ad4ca8 (patch) | |
tree | 8d945af5b462b223b1da7bf89ce4fee0c38ffdbb /server/sonar-server/src | |
parent | 21faf016e9f58984a4a55b205c9b99b883f181ef (diff) | |
download | sonarqube-fbbb1a0ebed1211229fdb010111d1b8d99ad4ca8.tar.gz sonarqube-fbbb1a0ebed1211229fdb010111d1b8d99ad4ca8.zip |
SONAR-8798 fix quality flaws
Diffstat (limited to 'server/sonar-server/src')
10 files changed, 106 insertions, 138 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java index 9078c0b2d96..41b07f569e6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java @@ -32,7 +32,6 @@ import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator; import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter; import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters; import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters.InternalBucket; @@ -156,6 +155,6 @@ public class ComponentIndex { SearchHits hitList = docs.getHits(); SearchHit[] hits = hitList.getHits(); - return new ComponentHitsPerQualifier(bucket.getKey(), ComponentHit.fromSearchHits(hits), hitList.totalHits()); + return new ComponentHitsPerQualifier(bucket.getKey(), ComponentHit.fromSearchHits(hits), hitList.getTotalHits()); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java index 8881940b02a..cf931dff496 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java @@ -151,8 +151,8 @@ public class BulkIndexer { while (true) { SearchHit[] hits = searchResponse.getHits().getHits(); for (SearchHit hit : hits) { - SearchHitField routing = hit.field("_routing"); - DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(hit.index(), hit.type(), hit.getId()); + SearchHitField routing = hit.getField("_routing"); + DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(hit.getIndex(), hit.getType(), hit.getId()); if (routing != null) { deleteRequestBuilder.setRouting(routing.getValue()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/EsUtils.java b/server/sonar-server/src/main/java/org/sonar/server/es/EsUtils.java index 12a012d1b2d..d9cabf1bb59 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/EsUtils.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/EsUtils.java @@ -65,7 +65,7 @@ public class EsUtils { return docs; } - public static LinkedHashMap<String, Long> termsToMap(Terms terms) { + public static Map<String, Long> termsToMap(Terms terms) { LinkedHashMap<String, Long> map = new LinkedHashMap<>(); List<? extends Terms.Bucket> buckets = terms.getBuckets(); for (Terms.Bucket bucket : buckets) { @@ -115,58 +115,19 @@ public class EsUtils { return SPECIAL_REGEX_CHARS.matcher(str).replaceAll("\\\\$0"); } - private static class DocScrollIterator<D extends BaseDoc> implements Iterator<D> { - - private final EsClient esClient; - private final String scrollId; - private final Function<Map<String, Object>, D> docConverter; - - private final Queue<SearchHit> hits = new ArrayDeque<>(); - - private DocScrollIterator(EsClient esClient, SearchResponse scrollResponse, Function<Map<String, Object>, D> docConverter) { - this.esClient = esClient; - this.scrollId = scrollResponse.getScrollId(); - this.docConverter = docConverter; - Collections.addAll(hits, scrollResponse.getHits().getHits()); - } - - @Override - public boolean hasNext() { - if (hits.isEmpty()) { - SearchScrollRequestBuilder esRequest = esClient.prepareSearchScroll(scrollId) - .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES)); - Collections.addAll(hits, esRequest.get().getHits().getHits()); - } - return !hits.isEmpty(); - } - - @Override - public D next() { - if (!hasNext()) { - throw new NoSuchElementException(); - } - return docConverter.apply(hits.poll().getSource()); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Cannot remove item when scrolling"); - } - } - - public static <ID> Iterator<ID> scrollIds(EsClient esClient, SearchResponse scrollResponse, Function<String, ID> idConverter) { + public static <I> Iterator<I> scrollIds(EsClient esClient, SearchResponse scrollResponse, Function<String, I> idConverter) { return new IdScrollIterator<>(esClient, scrollResponse, idConverter); } - private static class IdScrollIterator<ID> implements Iterator<ID> { + private static class IdScrollIterator<I> implements Iterator<I> { private final EsClient esClient; private final String scrollId; - private final Function<String, ID> idConverter; + private final Function<String, I> idConverter; private final Queue<SearchHit> hits = new ArrayDeque<>(); - private IdScrollIterator(EsClient esClient, SearchResponse scrollResponse, Function<String, ID> idConverter) { + private IdScrollIterator(EsClient esClient, SearchResponse scrollResponse, Function<String, I> idConverter) { this.esClient = esClient; this.scrollId = scrollResponse.getScrollId(); this.idConverter = idConverter; @@ -184,7 +145,7 @@ public class EsUtils { } @Override - public ID next() { + public I next() { if (!hasNext()) { throw new NoSuchElementException(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java b/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java index 306c05752f6..bba7ef58450 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java @@ -338,55 +338,63 @@ public class NewIndex { } public NewIndexType build() { - Map<String, Object> hash = new TreeMap<>(); if (subFields.isEmpty()) { - hash.putAll(ImmutableMap.of( - "type", getFieldType(), - "index", disableSearch ? INDEX_NOT_SEARCHABLE : INDEX_SEARCHABLE, - "norms", valueOf(!disableNorms), - "store", valueOf(store))); - if (getFieldData()) { - hash.put(FIELD_FIELDDATA, FIELDDATA_ENABLED); - } - } else { - hash.put("type", getFieldType()); - - Map<String, Object> multiFields = new TreeMap<>(subFields); - - if (termVectorWithPositionOffsets) { - multiFields.entrySet().forEach(entry -> { - Object subFieldMapping = entry.getValue(); - if (subFieldMapping instanceof Map) { - entry.setValue( - addFieldToMapping( - (Map<String, String>) subFieldMapping, - FIELD_TERM_VECTOR, "with_positions_offsets")); - } - }); - hash.put(FIELD_TERM_VECTOR, "with_positions_offsets"); - } - if (getFieldData()) { - multiFields.entrySet().forEach(entry -> { - Object subFieldMapping = entry.getValue(); - if (subFieldMapping instanceof Map) { - entry.setValue( - addFieldToMapping( - (Map<String, String>) subFieldMapping, - FIELD_FIELDDATA, FIELDDATA_ENABLED)); - } - }); - hash.put(FIELD_FIELDDATA, FIELDDATA_ENABLED); - } - - multiFields.put(fieldName, ImmutableMap.of( - "type", getFieldType(), - "index", INDEX_SEARCHABLE, - "norms", "false", - "store", valueOf(store))); - - hash.put("fields", multiFields); + return buildWithoutSubfields(); + } + return buildWithSubfields(); + } + + private NewIndexType buildWithoutSubfields() { + Map<String, Object> hash = new TreeMap<>(); + hash.putAll(ImmutableMap.of( + "type", getFieldType(), + INDEX, disableSearch ? INDEX_NOT_SEARCHABLE : INDEX_SEARCHABLE, + "norms", valueOf(!disableNorms), + "store", valueOf(store))); + if (getFieldData()) { + hash.put(FIELD_FIELDDATA, FIELDDATA_ENABLED); + } + return indexType.setProperty(fieldName, hash); + } + + private NewIndexType buildWithSubfields() { + Map<String, Object> hash = new TreeMap<>(); + hash.put("type", getFieldType()); + + Map<String, Object> multiFields = new TreeMap<>(subFields); + + if (termVectorWithPositionOffsets) { + multiFields.entrySet().forEach(entry -> { + Object subFieldMapping = entry.getValue(); + if (subFieldMapping instanceof Map) { + entry.setValue( + addFieldToMapping( + (Map<String, String>) subFieldMapping, + FIELD_TERM_VECTOR, "with_positions_offsets")); + } + }); + hash.put(FIELD_TERM_VECTOR, "with_positions_offsets"); + } + if (getFieldData()) { + multiFields.entrySet().forEach(entry -> { + Object subFieldMapping = entry.getValue(); + if (subFieldMapping instanceof Map) { + entry.setValue( + addFieldToMapping( + (Map<String, String>) subFieldMapping, + FIELD_FIELDDATA, FIELDDATA_ENABLED)); + } + }); + hash.put(FIELD_FIELDDATA, FIELDDATA_ENABLED); } + multiFields.put(fieldName, ImmutableMap.of( + "type", getFieldType(), + INDEX, INDEX_SEARCHABLE, + "norms", "false", + "store", valueOf(store))); + hash.put("fields", multiFields); + return indexType.setProperty(fieldName, hash); } @@ -463,7 +471,7 @@ public class NewIndex { public NestedFieldBuilder addKeywordField(String fieldName) { return setProperty(fieldName, ImmutableMap.of( "type", FIELD_TYPE_KEYWORD, - "index", INDEX_SEARCHABLE)); + INDEX, INDEX_SEARCHABLE)); } public NestedFieldBuilder addDoubleField(String fieldName) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/StickyFacetBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/es/StickyFacetBuilder.java index b714d3bbfbc..d8e387f10f1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/StickyFacetBuilder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/StickyFacetBuilder.java @@ -81,7 +81,8 @@ public class StickyFacetBuilder { * @param selected the terms, that the user already has selected * @return the (global) aggregation, that can be added on top level of the elasticsearch request */ - public AggregationBuilder buildStickyFacet(String fieldName, String facetName, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, Object... selected) { + public AggregationBuilder buildStickyFacet(String fieldName, String facetName, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, + Object... selected) { return buildStickyFacet(fieldName, facetName, FACET_DEFAULT_SIZE, additionalAggregationFilter, selected); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java index 56196237dd4..7d9931d074c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java @@ -284,13 +284,13 @@ public class ProjectMeasuresIndex { filters.put("__authorization", authorizationTypeSupport.createQueryFilter()); Multimap<String, MetricCriterion> metricCriterionMultimap = ArrayListMultimap.create(); query.getMetricCriteria().forEach(metricCriterion -> metricCriterionMultimap.put(metricCriterion.getMetricKey(), metricCriterion)); - metricCriterionMultimap.asMap().entrySet().forEach(entry -> { + metricCriterionMultimap.asMap().forEach((key, value) -> { BoolQueryBuilder metricFilters = boolQuery(); - entry.getValue() + value .stream() .map(ProjectMeasuresIndex::toQuery) .forEach(metricFilters::must); - filters.put(entry.getKey(), metricFilters); + filters.put(key, metricFilters); }); query.getQualityGateStatus() @@ -308,18 +308,12 @@ public class ProjectMeasuresIndex { query.getTags() .ifPresent(tags -> filters.put(FIELD_TAGS, termsQuery(FIELD_TAGS, tags))); - createTextQueryFilter(query).ifPresent(queryBuilder -> filters.put("textQuery", queryBuilder)); + query.getQueryText() + .map(ProjectsTextSearchQueryFactory::createQuery) + .ifPresent(queryBuilder -> filters.put("textQuery", queryBuilder)); return filters; } - private static Optional<QueryBuilder> createTextQueryFilter(ProjectMeasuresQuery query) { - Optional<String> queryText = query.getQueryText(); - if (!queryText.isPresent()) { - return Optional.empty(); - } - return Optional.of(ProjectsTextSearchQueryFactory.createQuery(queryText.get())); - } - private static QueryBuilder toQuery(MetricCriterion criterion) { if (criterion.isNoData()) { return boolQuery().mustNot( diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java index 57f63f63196..ee0a7256bea 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -215,7 +215,6 @@ public class RuleIndex { /* Build main filter (match based) */ private static Map<String, QueryBuilder> buildFilters(RuleQuery query) { - Map<String, QueryBuilder> filters = new HashMap<>(); /* Add enforced filter on rules that are REMOVED */ @@ -255,14 +254,8 @@ public class RuleIndex { } if (isNotEmpty(query.getTags())) { - BoolQueryBuilder q = boolQuery(); - query.getTags().stream() - .map(tag -> boolQuery() - .filter(QueryBuilders.termQuery(FIELD_RULE_EXTENSION_TAGS, tag)) - .filter(termsQuery(FIELD_RULE_EXTENSION_SCOPE, RuleExtensionScope.system().getScope(), RuleExtensionScope.organization(query.getOrganization()).getScope()))) - .map(childQuery -> JoinQueryBuilders.hasChildQuery(INDEX_TYPE_RULE_EXTENSION.getType(), childQuery, ScoreMode.None)) - .forEach(q::should); - filters.put(FIELD_RULE_EXTENSION_TAGS, q); + filters.put(FIELD_RULE_EXTENSION_TAGS, + buildTagsFilter(query.getTags(), query.getOrganization())); } Collection<RuleType> types = query.getTypes(); @@ -301,20 +294,7 @@ public class RuleIndex { /* Implementation of activation query */ QProfileDto profile = query.getQProfile(); if (query.getActivation() != null && profile != null) { - - // ActiveRule Filter (profile and inheritance) - BoolQueryBuilder activeRuleFilter = boolQuery(); - addTermFilter(activeRuleFilter, FIELD_ACTIVE_RULE_PROFILE_UUID, profile.getRulesProfileUuid()); - addTermFilter(activeRuleFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance()); - addTermFilter(activeRuleFilter, FIELD_ACTIVE_RULE_SEVERITY, query.getActiveSeverities()); - - // ChildQuery - QueryBuilder childQuery; - if (activeRuleFilter.hasClauses()) { - childQuery = activeRuleFilter; - } else { - childQuery = matchAllQuery(); - } + QueryBuilder childQuery = buildActivationFilter(query, profile); if (TRUE.equals(query.getActivation())) { filters.put("activation", @@ -339,6 +319,34 @@ public class RuleIndex { return filters; } + private static BoolQueryBuilder buildTagsFilter(Collection<String> tags, OrganizationDto organization) { + BoolQueryBuilder q = boolQuery(); + tags.stream() + .map(tag -> boolQuery() + .filter(QueryBuilders.termQuery(FIELD_RULE_EXTENSION_TAGS, tag)) + .filter(termsQuery(FIELD_RULE_EXTENSION_SCOPE, RuleExtensionScope.system().getScope(), RuleExtensionScope.organization(organization).getScope()))) + .map(childQuery -> JoinQueryBuilders.hasChildQuery(INDEX_TYPE_RULE_EXTENSION.getType(), childQuery, ScoreMode.None)) + .forEach(q::should); + return q; + } + + private static QueryBuilder buildActivationFilter(RuleQuery query, QProfileDto profile) { + // ActiveRule Filter (profile and inheritance) + BoolQueryBuilder activeRuleFilter = boolQuery(); + addTermFilter(activeRuleFilter, FIELD_ACTIVE_RULE_PROFILE_UUID, profile.getRulesProfileUuid()); + addTermFilter(activeRuleFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance()); + addTermFilter(activeRuleFilter, FIELD_ACTIVE_RULE_SEVERITY, query.getActiveSeverities()); + + // ChildQuery + QueryBuilder childQuery; + if (activeRuleFilter.hasClauses()) { + childQuery = activeRuleFilter; + } else { + childQuery = matchAllQuery(); + } + return childQuery; + } + private static BoolQueryBuilder addTermFilter(BoolQueryBuilder filter, String field, @Nullable Collection<String> values) { if (isNotEmpty(values)) { BoolQueryBuilder valuesFilter = boolQuery(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/index/TestIndex.java b/server/sonar-server/src/main/java/org/sonar/server/test/index/TestIndex.java index 6e57de3be20..90dd3cc000d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/test/index/TestIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/test/index/TestIndex.java @@ -53,7 +53,7 @@ public class TestIndex { .setSize(1) .setQuery(boolQuery().must(matchAllQuery()).filter(termQuery(FIELD_TEST_UUID, testUuid))) .get().getHits().getHits()) { - coveredFiles.addAll(new TestDoc(hit.sourceAsMap()).coveredFiles()); + coveredFiles.addAll(new TestDoc(hit.getSourceAsMap()).coveredFiles()); } return coveredFiles; @@ -97,7 +97,7 @@ public class TestIndex { .setQuery(boolQuery().must(matchAllQuery()).filter(termQuery(FIELD_TEST_UUID, testUuid))) .get().getHits().getHits(); if (hits.length > 0) { - return Optional.of(new TestDoc(hits[0].sourceAsMap())); + return Optional.of(new TestDoc(hits[0].getSourceAsMap())); } return Optional.absent(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndex.java b/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndex.java index 8eb0f0213e8..0ddb194e46d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndex.java @@ -90,7 +90,7 @@ public class UserIndex { .should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_SCM_ACCOUNTS), scmAccount)))) .setSize(3); for (SearchHit hit : request.get().getHits().getHits()) { - result.add(new UserDoc(hit.sourceAsMap())); + result.add(new UserDoc(hit.getSourceAsMap())); } } return result; diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java index 41e70c47322..29fac927c51 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.util.List; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.sonar.api.config.internal.MapSettings; @@ -210,7 +209,6 @@ public class SearchActionComponentsTest { .assertJson(this.getClass(), "no_issue.json"); } - @Ignore @Test public void search_since_leak_period_on_project() throws Exception { ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(otherOrganization2, "P1").setDbKey("PK1")); @@ -239,7 +237,6 @@ public class SearchActionComponentsTest { .assertJson(this.getClass(), "search_since_leak_period.json"); } - @Ignore @Test public void search_since_leak_period_on_file_in_module_project() throws Exception { ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(defaultOrganization, "P1").setDbKey("PK1")); |