aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-10-11 17:27:09 +0200
committerTeryk Bellahsene <teryk@users.noreply.github.com>2017-10-12 14:50:59 +0200
commitbbea92754dcb6f644da0b661c7770ee4c6fcfeaa (patch)
tree7181a69b4cb8c08742dca49c838f0f84c4350a6a /server
parent09440b9414e4c541140958df31cfa02959f31805 (diff)
downloadsonarqube-bbea92754dcb6f644da0b661c7770ee4c6fcfeaa.tar.gz
sonarqube-bbea92754dcb6f644da0b661c7770ee4c6fcfeaa.zip
SONAR-9928 Index and filter organization in component ES index
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java10
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java1
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java16
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java14
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java5
7 files changed, 50 insertions, 2 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java
index eafeece2962..3a8c1fd3132 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java
@@ -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;
+ }
}
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 489699055c5..8a492913ef4 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
@@ -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);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java
index 352dd847b65..923cf6746a2 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java
@@ -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();
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
index f0b195e5288..2063fb35e0f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
@@ -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());
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java
index 10a5bbb8c86..206447f5563 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java
@@ -21,21 +21,30 @@
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;
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java
index 52e3b3e981d..99d7d9583f7 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java
@@ -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;
@@ -107,6 +108,19 @@ public class ComponentIndexSearchTest {
}
@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"));
ComponentDto project3 = db.components().insertPrivateProject(p -> p.setName("project 3"));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java
index 85d0fb6a03f..b2bac18453d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java
@@ -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