]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7282 make ComponentIndexer clean up contents before indexing
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Wed, 11 Jan 2017 18:18:31 +0000 (19:18 +0100)
committerDaniel Schwarz <daniel.schwarz@sonarsource.com>
Mon, 16 Jan 2017 16:31:45 +0000 (17:31 +0100)
When a project and it's sub-components get indexed, remove any existing documents for that project right before.

server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java

index 722244ff1f8562762f5c37482b32eecf7a31e4ec..9c147fc268ad8660178706ebba90e13681580e88 100644 (file)
@@ -36,6 +36,8 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.server.es.BulkIndexer;
 import org.sonar.server.es.EsClient;
 
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
+import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_PROJECT_UUID;
 import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_COMPONENTS;
 import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT;
 
@@ -53,6 +55,8 @@ public class ComponentIndexer implements Startable {
 
   /**
    * Copy all components of all projects to the elastic search index.
+   * <p>
+   * <b>Warning</b>: This should only be called on an empty index. It does not purge anything.
    */
   public void index() {
     try (DbSession dbSession = dbClient.openSession(false)) {
@@ -68,6 +72,7 @@ public class ComponentIndexer implements Startable {
    */
   public void index(String projectUuid) {
     try (DbSession dbSession = dbClient.openSession(false)) {
+      purge(projectUuid);
       index(
         dbClient
           .componentDao()
@@ -76,7 +81,15 @@ public class ComponentIndexer implements Startable {
     }
   }
 
-  public void index(ComponentDto... docs) {
+  private void purge(String projectUuid) {
+    BulkIndexer.delete(esClient, INDEX_COMPONENTS, esClient
+      .prepareSearch(INDEX_COMPONENTS)
+      .setTypes(TYPE_COMPONENT)
+      .setFetchSource(false)
+      .setQuery(termQuery(FIELD_PROJECT_UUID, projectUuid)));
+  }
+
+  void index(ComponentDto... docs) {
     Future<?> submit = executor.submit(() -> indexNow(docs));
     try {
       Uninterruptibles.getUninterruptibly(submit);
index bd3f0ce9a8ae7063ac14abae348ab3f60ed0f087..64e89c164f0fe06aef5821fbb44d853b33e16a52 100644 (file)
@@ -29,11 +29,14 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.component.ComponentUpdateDto;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.organization.OrganizationTesting;
 import org.sonar.server.es.EsTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
+import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME;
 import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_COMPONENTS;
 import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT;
 
@@ -97,11 +100,40 @@ public class ComponentIndexerTest {
     assertThat(count()).isEqualTo(2);
   }
 
+  @Test
+  public void reindex_project() {
+
+    // insert
+    ComponentDto component = ComponentTesting.newProjectDto(organization, "UUID-1").setName("OldName");
+    insert(component);
+
+    // verify insert
+    index(component);
+    assertMatches("OldName", 1);
+
+    // modify
+    component.setName("NewName");
+    update(component);
+
+    // verify modification
+    index(component);
+    assertMatches("OldName", 0);
+    assertMatches("NewName", 1);
+  }
+
   private void insert(ComponentDto component) {
     dbClient.componentDao().insert(dbSession, component);
     dbSession.commit();
   }
 
+  private void update(ComponentDto component) {
+    ComponentUpdateDto updateComponent = ComponentUpdateDto.copyFrom(component);
+    updateComponent.setBChanged(true);
+    dbClient.componentDao().update(dbSession, updateComponent);
+    dbClient.componentDao().applyBChangesForRootComponentUuid(dbSession, "UUID-1");
+    dbSession.commit();
+  }
+
   private void index() {
     createIndexer().index();
   }
@@ -118,6 +150,17 @@ public class ComponentIndexerTest {
     return esTester.countDocuments(INDEX_COMPONENTS, TYPE_COMPONENT);
   }
 
+  private void assertMatches(String nameQuery, int numberOfMatches) {
+    assertThat(
+      esTester.client()
+        .prepareSearch(INDEX_COMPONENTS)
+        .setTypes(TYPE_COMPONENT)
+        .setQuery(termQuery(FIELD_NAME, nameQuery))
+        .get()
+        .getHits()
+        .getHits().length).isEqualTo(numberOfMatches);
+  }
+
   private ComponentIndexer createIndexer() {
     return new ComponentIndexer(dbTester.getDbClient(), esTester.client());
   }