From bc30d3796db53f64829651c6df64c18a9b490565 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 21 Mar 2017 16:10:27 +0100 Subject: [PATCH] SONAR-8889 fix Quality flaws in ActiveRuleIndexer --- .../index/ActiveRuleIndexer.java | 38 ++++--------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java index 2e1bc0654ba..f6c114471ba 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java @@ -19,17 +19,14 @@ */ package org.sonar.server.qualityprofile.index; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.FluentIterable; import java.util.Collection; import java.util.Iterator; import java.util.List; -import javax.annotation.Nonnull; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.sonar.api.utils.System2; +import org.sonar.core.util.stream.Collectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleKey; @@ -64,15 +61,12 @@ public class ActiveRuleIndexer extends BaseIndexer { } private long doIndex(BulkIndexer bulk, long lastUpdatedAt) { - DbSession dbSession = dbClient.openSession(false); long maxDate; - try { + try (DbSession dbSession = dbClient.openSession(false)) { ActiveRuleResultSetIterator rowIt = ActiveRuleResultSetIterator.create(dbClient, dbSession, lastUpdatedAt); maxDate = doIndex(bulk, rowIt); rowIt.close(); return maxDate; - } finally { - dbSession.close(); } } @@ -91,10 +85,11 @@ public class ActiveRuleIndexer extends BaseIndexer { } public void index(List changes) { - deleteKeys(FluentIterable.from(changes) - .filter(MatchDeactivatedRule.INSTANCE) - .transform(ActiveRuleChangeToKey.INSTANCE) - .toList()); + deleteKeys(changes.stream() + .filter(c -> c.getType().equals(ActiveRuleChange.Type.DEACTIVATED)) + .map(c -> c.getKey()) + .collect(Collectors.toList(changes.size()))); + index(); } @@ -129,23 +124,4 @@ public class ActiveRuleIndexer extends BaseIndexer { .parent(doc.key().ruleKey().toString()) .source(doc.getFields()); } - - private enum MatchDeactivatedRule implements Predicate { - INSTANCE; - - @Override - public boolean apply(@Nonnull ActiveRuleChange input) { - return input.getType().equals(ActiveRuleChange.Type.DEACTIVATED); - } - } - - private enum ActiveRuleChangeToKey implements Function { - INSTANCE; - - @Override - public ActiveRuleKey apply(@Nonnull ActiveRuleChange input) { - return input.getKey(); - } - } - } -- 2.39.5