]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7282 only reindex components if the index is empty
authorDaniel Schwarz <bartfastiel@users.noreply.github.com>
Thu, 16 Feb 2017 14:49:18 +0000 (15:49 +0100)
committerGitHub <noreply@github.com>
Thu, 16 Feb 2017 14:49:18 +0000 (15:49 +0100)
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 285e285cc66ac3d49966f6be5ace6b5b3c691d8a..d0a4b9ffc4e80e0757e7f0f763e25e1db48d09c0 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.component.index;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Throwables;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.Arrays;
@@ -58,12 +59,26 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe
     this.esClient = esClient;
   }
 
+  /**
+   * Make sure, that the component index is filled.
+   */
+  public void index() {
+    if (isEmpty()) {
+      reindex();
+    }
+  }
+
+  @VisibleForTesting
+  boolean isEmpty() {
+    return esClient.prepareSearch(INDEX_COMPONENTS).setTypes(TYPE_COMPONENT).setSize(0).get().getHits().getTotalHits() <= 0;
+  }
+
   /**
    * Copy all components of all projects to the elastic search index.
    * <p>
    * <b>Warning</b>: This should only be called on an empty index and it should only be called during startup.
    */
-  public void index() {
+  private void reindex() {
     doIndexByProjectUuid(null);
   }
 
index 1d1add24b1f6aa5b1fe2afc0aaaed09d353784e2..9175e225d5ed33a6fc4cba068158def2f5b5224f 100644 (file)
@@ -32,11 +32,17 @@ 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.EsClient;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.ProjectIndexer;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.elasticsearch.index.query.QueryBuilders.termQuery;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 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;
@@ -143,6 +149,45 @@ public class ComponentIndexerTest {
     assertMatches("NewFile", 1);
   }
 
+  @Test
+  public void full_reindexing_on_empty_index() {
+
+    // insert
+    ComponentDto project = dbTester.components().insertProject();
+    dbTester.components().insertComponent(ComponentTesting.newFileDto(project).setName("OldFile"));
+
+    // verify insert
+    index();
+    assertMatches("OldFile", 1);
+  }
+
+  @Test
+  public void full_reindexing_should_not_do_anything_if_index_is_not_empty() {
+    EsClient esMock = mock(EsClient.class);
+
+    // attempt to start indexing
+    ComponentIndexer indexer = spy(new ComponentIndexer(dbClient, esMock));
+    doReturn(false).when(indexer).isEmpty();
+    indexer.index();
+
+    // verify, that index has not been altered
+    verify(indexer).index();
+    verify(indexer).isEmpty();
+    verifyNoMoreInteractions(indexer);
+    verifyNoMoreInteractions(esMock);
+  }
+
+  @Test
+  public void isEmpty_should_return_true_if_index_is_empty() {
+    assertThat(createIndexer().isEmpty()).isTrue();
+  }
+
+  @Test
+  public void isEmpty_should_return_false_if_index_is_not_empty() {
+    index_one_project();
+    assertThat(createIndexer().isEmpty()).isFalse();
+  }
+
   private void insert(ComponentDto component) {
     dbTester.components().insertComponent(component);
   }