]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5237 - Fixed implicit limit on results for RuleIndex::getByIds()
authorStephane Gamard <stephane.gamard@searchbox.com>
Tue, 29 Jul 2014 12:55:27 +0000 (14:55 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Tue, 29 Jul 2014 12:58:34 +0000 (14:58 +0200)
sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java

index 3906b7f27fdd751dc6bc14eb09299b29cb2b2d25..94424812b3cbf59ac2c1d179ff4ea59c24c85391 100644 (file)
@@ -423,13 +423,27 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
    */
   @Deprecated
   public List<Rule> getByIds(Collection<Integer> ids) {
-    SearchResponse response = getClient().prepareSearch(this.getIndexName())
+    SearchResponse scrollResp = getClient().prepareSearch(this.getIndexName())
       .setTypes(this.getIndexType())
+      .setSearchType(SearchType.SCAN)
+      .setScroll(TimeValue.timeValueSeconds(3L))
+      .setSize(100)
       .setQuery(QueryBuilders.termsQuery(RuleNormalizer.RuleField.ID.field(), ids))
       .get();
+
     List<Rule> rules = newArrayList();
-    for (SearchHit hit : response.getHits()) {
-      rules.add(toDoc(hit.getSource()));
+    while (true) {
+      scrollResp = getClient()
+        .prepareSearchScroll(scrollResp.getScrollId())
+        .setScroll(TimeValue.timeValueSeconds(3L))
+        .get();
+      for (SearchHit hit : scrollResp.getHits()) {
+        rules.add(toDoc(hit.getSource()));
+      }
+      //Break condition: No hits are returned
+      if (scrollResp.getHits().getHits().length == 0) {
+        break;
+      }
     }
     return rules;
   }
index 3b3950d699361e1a4557eb14627c563c2f8399be..927ea0838a9447a2837dbb1f26493da6c5d9c676 100644 (file)
@@ -54,8 +54,10 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
@@ -930,6 +932,19 @@ public class RuleIndexMediumTest {
     assertThat(index.search(availableSinceNowQuery, new QueryOptions()).getHits()).hasSize(0);
   }
 
+  @Test
+  public void scroll_byIds() throws Exception {
+    Set<Integer> ids = new HashSet<Integer>();
+    for (int i = 0; i < 150; i++) {
+      RuleDto rule = RuleTesting.newDto(RuleKey.of("scroll", "r_" + i));
+      dao.insert(dbSession, rule);
+      dbSession.commit();
+      ids.add(rule.getId());
+    }
+    List<Rule> rules = index.getByIds(ids);
+    assertThat(rules).hasSize(ids.size());
+  }
+
   private static List<String> ruleKeys(List<Rule> rules){
     return newArrayList(Iterables.transform(rules, new Function<Rule, String>() {
       @Override