]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9928 Index and filter organization in component ES index
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 11 Oct 2017 15:27:09 +0000 (17:27 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Thu, 12 Oct 2017 12:50:59 +0000 (14:50 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java

index eafeece2962f607f18a15d2448d098f191afba40..3a8c1fd3132306d53a1983575c6f86e2f41c1a19 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.server.es.BaseDoc;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_KEY;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_LANGUAGE;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME;
+import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_ORGANIZATION_UUID;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_PROJECT_UUID;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_QUALIFIER;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_UUID;
@@ -107,4 +108,13 @@ public class ComponentDoc extends BaseDoc {
     setField(FIELD_LANGUAGE, s);
     return this;
   }
+
+  public String getOrganization() {
+    return getField(FIELD_ORGANIZATION_UUID);
+  }
+
+  public ComponentDoc setOrganization(String s) {
+    setField(FIELD_ORGANIZATION_UUID, s);
+    return this;
+  }
 }
index 489699055c581e8d5ec0e2371ac64458a740b1f1..8a492913ef4865f9a2559708078afe39f1855e28 100644 (file)
@@ -58,6 +58,7 @@ import static org.elasticsearch.index.query.QueryBuilders.termsQuery;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_KEY;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_LANGUAGE;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME;
+import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_ORGANIZATION_UUID;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_QUALIFIER;
 import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_TYPE_COMPONENT;
 import static org.sonar.server.component.index.ComponentIndexDefinition.NAME_ANALYZERS;
@@ -94,7 +95,8 @@ public class ComponentIndex {
       esQuery.must(ComponentTextSearchQueryFactory.createQuery(componentTextSearchQuery, ComponentTextSearchFeatureRepertoire.values()));
     });
     setEmptiable(query.getQualifiers(), q -> esQuery.must(termsQuery(FIELD_QUALIFIER, q)));
-    setNullable(query.getLanguage(), l -> esQuery.must(termsQuery(FIELD_LANGUAGE, l)));
+    setNullable(query.getLanguage(), l -> esQuery.must(termQuery(FIELD_LANGUAGE, l)));
+    setNullable(query.getOrganizationUuid(), o -> esQuery.must(termQuery(FIELD_ORGANIZATION_UUID, o)));
     requestBuilder.setQuery(esQuery);
     requestBuilder.addSort(SORTABLE_ANALYZER.subField(FIELD_NAME), SortOrder.ASC);
 
index 352dd847b65996aab4427d9643bbc6c0d2111314..923cf6746a26e67106ff7d8751bebc094492e495 100644 (file)
@@ -37,6 +37,7 @@ public class ComponentIndexDefinition implements IndexDefinition {
   public static final IndexType INDEX_TYPE_COMPONENT = new IndexType("components", "component");
   public static final String FIELD_UUID = "uuid";
   public static final String FIELD_PROJECT_UUID = "project_uuid";
+  public static final String FIELD_ORGANIZATION_UUID = "organization_uuid";
   public static final String FIELD_KEY = "key";
   public static final String FIELD_NAME = "name";
   public static final String FIELD_QUALIFIER = "qualifier";
@@ -75,5 +76,6 @@ public class ComponentIndexDefinition implements IndexDefinition {
 
     mapping.keywordFieldBuilder(FIELD_QUALIFIER).build();
     mapping.keywordFieldBuilder(FIELD_LANGUAGE).disableNorms().build();
+    mapping.keywordFieldBuilder(FIELD_ORGANIZATION_UUID).disableNorms().build();
   }
 }
index f0b195e528833c1045edf417b9e81c9fd39662c2..2063fb35e0fa10ceae2d2a14f98c8d73c822800f 100644 (file)
@@ -187,6 +187,7 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe
       .setName(component.name())
       .setKey(component.getDbKey())
       .setProjectUuid(component.projectUuid())
+      .setOrganization(component.getOrganizationUuid())
       .setLanguage(component.language())
       .setQualifier(component.qualifier());
   }
index 10a5bbb8c861c4e81a23701ae06bb3646dbe6f98..206447f55633e39bee4b826411ec75ac4fe5223b 100644 (file)
 package org.sonar.server.component.index;
 
 import java.util.Collection;
+import javax.annotation.CheckForNull;
 
 import static java.util.Collections.emptySet;
 import static java.util.Collections.unmodifiableCollection;
 
 public class ComponentQuery {
+  private final String organizationUuid;
   private final String query;
   private final Collection<String> qualifiers;
   private final String language;
 
   private ComponentQuery(Builder builder) {
+    this.organizationUuid = builder.organizationUuid;
     this.query = builder.query;
     this.qualifiers = builder.qualifiers;
     this.language = builder.language;
   }
 
+  @CheckForNull
+  public String getOrganizationUuid() {
+    return organizationUuid;
+  }
+
+  @CheckForNull
   public String getQuery() {
     return query;
   }
@@ -44,6 +53,7 @@ public class ComponentQuery {
     return qualifiers;
   }
 
+  @CheckForNull
   public String getLanguage() {
     return language;
   }
@@ -53,6 +63,7 @@ public class ComponentQuery {
   }
 
   public static class Builder {
+    private String organizationUuid;
     private String query;
     private Collection<String> qualifiers = emptySet();
     private String language;
@@ -61,6 +72,11 @@ public class ComponentQuery {
       // enforce static factory method
     }
 
+    public Builder setOrganization(String organizationUuid) {
+      this.organizationUuid = organizationUuid;
+      return this;
+    }
+
     public Builder setQuery(String query) {
       this.query = query;
       return this;
index 52e3b3e981df7b2a0f8ee1acf5e81f7ce37d20b1..99d7d9583f77b37c9c5ff31a5e38561338d1cce0 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.SearchIdResult;
 import org.sonar.server.es.SearchOptions;
@@ -106,6 +107,19 @@ public class ComponentIndexSearchTest {
     assertThat(result.getIds()).containsExactlyInAnyOrder(file.uuid());
   }
 
+  @Test
+  public void filter_by_organization() {
+    OrganizationDto organization = db.organizations().insert();
+    OrganizationDto anotherOrganization = db.organizations().insert();
+    ComponentDto project = db.components().insertPrivateProject(organization);
+    ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization);
+    index(project, anotherProject);
+
+    SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setOrganization(organization.getUuid()).build(), new SearchOptions());
+
+    assertThat(result.getIds()).containsExactlyInAnyOrder(project.uuid());
+  }
+
   @Test
   public void order_by_name_case_insensitive() {
     ComponentDto project2 = db.components().insertPrivateProject(p -> p.setName("PROJECT 2"));
index 85d0fb6a03f77c2c3b8ce2ead68b66813fada545..b2bac18453dd177b44963a15fe39d5e735622123 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentUpdateDto;
 import org.sonar.db.es.EsQueueDto;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.IndexingResult;
 import org.sonar.server.es.ProjectIndexer;
@@ -84,7 +85,8 @@ public class ComponentIndexerTest {
 
   @Test
   public void map_fields() {
-    ComponentDto project = db.components().insertPrivateProject(p -> p.setLanguage("java"));
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = db.components().insertPrivateProject(organization, p -> p.setLanguage("java"));
 
     underTest.indexOnStartup(emptySet());
 
@@ -95,6 +97,7 @@ public class ComponentIndexerTest {
     assertThat(doc.getProjectUuid()).isEqualTo(project.projectUuid());
     assertThat(doc.getName()).isEqualTo(project.name());
     assertThat(doc.getLanguage()).isEqualTo(project.language());
+    assertThat(doc.getOrganization()).isEqualTo(project.getOrganizationUuid());
   }
 
   @Test