aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server/src
diff options
context:
space:
mode:
authorDaniel Schwarz <bartfastiel@users.noreply.github.com>2017-02-16 15:49:18 +0100
committerGitHub <noreply@github.com>2017-02-16 15:49:18 +0100
commitb4b1940277e877c853fbe2b32696c3cb1d50f816 (patch)
tree1ac68ea775e2644cd0e812d4bb281c0493b907b7 /server/sonar-server/src
parentcdc1c25040fccdf84b46250fa5ad84bb1dde252a (diff)
downloadsonarqube-b4b1940277e877c853fbe2b32696c3cb1d50f816.tar.gz
sonarqube-b4b1940277e877c853fbe2b32696c3cb1d50f816.zip
SONAR-7282 only reindex components if the index is empty
Diffstat (limited to 'server/sonar-server/src')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java17
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java45
2 files changed, 61 insertions, 1 deletions
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 285e285cc66..d0a4b9ffc4e 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
@@ -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;
@@ -59,11 +60,25 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe
}
/**
+ * 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);
}
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 1d1add24b1f..9175e225d5e 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,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);
}
914/stable28 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
summaryrefslogtreecommitdiffstats
path: root/lib/private/share/searchresultsorter.php
blob: 27f94a694ac68daadae063bfb10801cbe6954a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53