]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8889 fix Quality flaws in ActiveRuleIndexer
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 21 Mar 2017 15:10:27 +0000 (16:10 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:38:34 +0000 (17:38 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java

index 2e1bc0654ba1dc030d7c6bd8fd656cc18a6f5bfc..f6c114471ba77fa628b740d8d33913ddabe7e3ea 100644 (file)
  */
 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<ActiveRuleChange> 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<ActiveRuleChange> {
-    INSTANCE;
-
-    @Override
-    public boolean apply(@Nonnull ActiveRuleChange input) {
-      return input.getType().equals(ActiveRuleChange.Type.DEACTIVATED);
-    }
-  }
-
-  private enum ActiveRuleChangeToKey implements Function<ActiveRuleChange, ActiveRuleKey> {
-    INSTANCE;
-
-    @Override
-    public ActiveRuleKey apply(@Nonnull ActiveRuleChange input) {
-      return input.getKey();
-    }
-  }
-
 }