]> source.dussan.org Git - sonarqube.git/commitdiff
Batch should set updated date to 0 on updated sources and Compute should update this... 156/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 17 Mar 2015 15:43:27 +0000 (16:43 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 Mar 2015 08:22:45 +0000 (09:22 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/step/IndexSourceLinesStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/IndexSourceLinesStepTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero-result.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero.xml [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDao.java
sonar-core/src/main/java/org/sonar/core/source/db/FileSourceMapper.java
sonar-core/src/main/resources/org/sonar/core/source/db/FileSourceMapper.xml
sonar-core/src/test/java/org/sonar/core/source/db/FileSourceDaoTest.java
sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero.xml [new file with mode: 0644]

index 7746f78c72a68c771da1de675e15979c4db762fd..46e8836c1afa16463e0a29cb956f2eb5b446b86c 100644 (file)
 package org.sonar.server.computation.step;
 
 import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.utils.System2;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
 import org.sonar.server.computation.ComputationContext;
+import org.sonar.server.db.DbClient;
 import org.sonar.server.source.index.SourceLineIndexer;
 
 public class IndexSourceLinesStep implements ComputationStep {
 
+  private final DbClient dbClient;
+  private final System2 system2;
   private final SourceLineIndexer indexer;
 
-  public IndexSourceLinesStep(SourceLineIndexer indexer) {
+  public IndexSourceLinesStep(DbClient dbClient, System2 system2, SourceLineIndexer indexer) {
+    this.dbClient = dbClient;
+    this.system2 = system2;
     this.indexer = indexer;
   }
 
@@ -38,9 +46,22 @@ public class IndexSourceLinesStep implements ComputationStep {
 
   @Override
   public void execute(ComputationContext context) {
+    updateSourceUpdateDate(context.getProject().uuid());
     indexer.index();
   }
 
+  // Temporary solution to only index in E/S updated sources from current project
+  // Should be removed when source wil be persisted in compute engine
+  private void updateSourceUpdateDate(String projectUuid){
+    DbSession session = dbClient.openSession(true);
+    try {
+      dbClient.fileSourceDao().updateDateWhenUpdatedDateIsZero(session, projectUuid, system2.now());
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
   @Override
   public String getDescription() {
     return "Index source lines";
index fb0042ba9ea85ededafa65919df25906728ab813..8b5d1f9aad571118323718e2b86120456e126017 100644 (file)
  */
 package org.sonar.server.computation.step;
 
+import org.elasticsearch.search.SearchHit;
+import org.junit.Before;
+import org.junit.ClassRule;
 import org.junit.Test;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.utils.System2;
+import org.sonar.batch.protocol.output.BatchReportReader;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.core.source.db.FileSourceDao;
+import org.sonar.server.component.ComponentTesting;
+import org.sonar.server.computation.ComputationContext;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.es.EsTester;
+import org.sonar.server.source.db.FileSourceTesting;
+import org.sonar.server.source.index.SourceLineDoc;
+import org.sonar.server.source.index.SourceLineIndexDefinition;
 import org.sonar.server.source.index.SourceLineIndexer;
 
 import java.io.IOException;
+import java.sql.Connection;
+import java.util.Date;
+import java.util.List;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class IndexSourceLinesStepTest extends BaseStepTest {
 
-  @Test
-  public void supportedProjectQualifiers() throws Exception {
+  @ClassRule
+  public static DbTester dbTester = new DbTester();
+
+  @ClassRule
+  public static EsTester esTester = new EsTester().addDefinitions(new SourceLineIndexDefinition(new Settings()));
+
+  System2 system2;
+
+  DbClient dbClient;
 
+  @Before
+  public void setUp() throws Exception {
+    dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), new FileSourceDao(null));
+    system2 = mock(System2.class);
   }
 
   @Override
   protected ComputationStep step() throws IOException {
-    return new IndexSourceLinesStep(mock(SourceLineIndexer.class));
+    SourceLineIndexer sourceLineIndexer = new SourceLineIndexer(dbClient, esTester.client());
+    sourceLineIndexer.setEnabled(true);
+    return new IndexSourceLinesStep(dbClient, system2, sourceLineIndexer);
+  }
+
+  @Test
+  public void supported_project_qualifiers() throws Exception {
+    assertThat(step().supportedProjectQualifiers()).containsOnly(Qualifiers.PROJECT);
+  }
+
+  @Test
+  public void update_source_date_on_sources_with_update_at_to_zero() throws Exception {
+    when(system2.now()).thenReturn(150000000002L);
+    dbTester.prepareDbUnit(getClass(), "update_source_date_on_sources_with_update_at_to_zero.xml");
+    Connection connection = dbTester.openConnection();
+    FileSourceTesting.updateDataColumn(connection, "FILE1_UUID", FileSourceTesting.newRandomData(1).build());
+    connection.close();
+
+    step().execute(new ComputationContext(mock(BatchReportReader.class), ComponentTesting.newProjectDto("ABCD")));
+
+    dbTester.assertDbUnit(getClass(), "update_source_date_on_sources_with_update_at_to_zero-result.xml");
+
+    List<SearchHit> docs = esTester.getDocuments(SourceLineIndexDefinition.INDEX, SourceLineIndexDefinition.TYPE);
+    assertThat(docs).hasSize(1);
+    SourceLineDoc doc = new SourceLineDoc(docs.get(0).sourceAsMap());
+    assertThat(doc.projectUuid()).isEqualTo("ABCD");
+    assertThat(doc.fileUuid()).isEqualTo("FILE1_UUID");
+    assertThat(doc.updateDate()).isEqualTo(new Date(system2.now()));
   }
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero-result.xml
new file mode 100644 (file)
index 0000000..2a8f8a0
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+
+  <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="1500000000002"/>
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero.xml
new file mode 100644 (file)
index 0000000..3113571
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+
+  <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="0"/>
+
+</dataset>
index dd084a834c23c6b9fef142b2397ae0f33d949276..e27d9eca16858d544188af97a85f94a046e29ebd 100644 (file)
@@ -102,7 +102,7 @@ public class SourcePersister implements ScanPersister {
         .setSrcHash(metadata.hash())
         .setLineHashes(lineHashesAsMd5Hex(inputFile))
         .setCreatedAt(system2.now())
-        .setUpdatedAt(system2.now());
+        .setUpdatedAt(0L);
       mapper.insert(dto);
       session.commit();
     } else {
@@ -115,7 +115,7 @@ public class SourcePersister implements ScanPersister {
           .setLineHashes(lineHashesAsMd5Hex(inputFile));
         // Optimization do not change updated at when updating src_hash to avoid indexation by E/S
         if (!dataHash.equals(previousDto.getDataHash())) {
-          previousDto.setUpdatedAt(system2.now());
+          previousDto.setUpdatedAt(0L);
         }
         mapper.update(previousDto);
         session.commit();
index eced8499a4d4ec414d1dc9629499050eb9a64880..16864674f20353a1fbc3f4f3dd6910572a4fae2d 100644 (file)
@@ -121,4 +121,8 @@ public class FileSourceDao implements BatchComponent, ServerComponent, DaoCompon
     }
   }
 
+  public void updateDateWhenUpdatedDateIsZero(DbSession session, String projectUuid, long updateDate) {
+    session.getMapper(FileSourceMapper.class).updateDateWhenUpdatedDateIsZero(projectUuid, updateDate);
+  }
+
 }
index e887689235796d948e705c192fb40e3b01ee6d94..633e4d140ed30b6da1d4efeba466719b310b9920 100644 (file)
@@ -20,6 +20,8 @@
 
 package org.sonar.core.source.db;
 
+import org.apache.ibatis.annotations.Param;
+
 import javax.annotation.CheckForNull;
 
 import java.util.List;
@@ -35,6 +37,8 @@ public interface FileSourceMapper {
 
   void update(FileSourceDto dto);
 
+  void updateDateWhenUpdatedDateIsZero(@Param("projectUuid") String projectUuid, @Param("date") Long updateDate);
+
   @CheckForNull
   String selectLineHashes(String fileUuid);
 }
index 2edae7f055c1ca3eef7c541b28f47b054152c2d3..49ede1dfb3061bb6cd43784275df870a5ceffac5 100644 (file)
   </select>
   
   <insert id="insert" parameterType="org.sonar.core.source.db.FileSourceDto" useGeneratedKeys="false">
-    insert into file_sources (project_uuid, file_uuid, created_at, updated_at, binary_data, line_hashes, data_hash, src_hash)
-    values (#{projectUuid,jdbcType=VARCHAR}, #{fileUuid,jdbcType=VARCHAR}, #{createdAt,jdbcType=BIGINT},
+    INSERT INTO file_sources (project_uuid, file_uuid, created_at, updated_at, binary_data, line_hashes, data_hash, src_hash)
+    VALUES (#{projectUuid,jdbcType=VARCHAR}, #{fileUuid,jdbcType=VARCHAR}, #{createdAt,jdbcType=BIGINT},
     #{updatedAt,jdbcType=BIGINT}, #{binaryData,jdbcType=BLOB}, #{lineHashes,jdbcType=CLOB},
     #{dataHash,jdbcType=VARCHAR}, #{srcHash,jdbcType=VARCHAR})
   </insert>
   
   <update id="update" parameterType="org.sonar.core.source.db.FileSourceDto" useGeneratedKeys="false">
-    update file_sources set
+    UPDATE file_sources SET
       updated_at = #{updatedAt,jdbcType=BIGINT},
       binary_data = #{binaryData,jdbcType=BLOB},
       line_hashes = #{lineHashes,jdbcType=CLOB},
       data_hash = #{dataHash,jdbcType=VARCHAR},
       src_hash = #{srcHash,jdbcType=VARCHAR}
-    where id = #{id}
+    WHERE id = #{id}
+  </update>
+
+  <update id="updateDateWhenUpdatedDateIsZero" parameterType="org.sonar.core.source.db.FileSourceDto" useGeneratedKeys="false">
+    UPDATE file_sources SET
+    updated_at = #{date,jdbcType=BIGINT}
+    WHERE project_uuid = #{projectUuid}
+      AND updated_at = 0
   </update>
 
   <select id="selectLineHashes" parameterType="string" resultType="String">
index aed0f04fd71549511876b6050970dcc9f07a4050..8f2ed1fb45a1155c80bd37983c8799b7171b1d71 100644 (file)
@@ -128,6 +128,16 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
     checkTable("update", "file_sources", "project_uuid", "file_uuid", "data_hash", "line_hashes", "src_hash", "created_at", "updated_at");
   }
 
+  @Test
+  public void update_date_when_updated_date_is_zero() throws Exception {
+    setupData("update_date_when_updated_date_is_zero");
+
+    dao.updateDateWhenUpdatedDateIsZero(session, "ABCD", 1500000000002L);
+    session.commit();
+
+    checkTable("update_date_when_updated_date_is_zero", "file_sources", "project_uuid", "file_uuid", "data_hash", "line_hashes", "src_hash", "created_at", "updated_at");
+  }
+
   private static class ReaderToStringFunction implements Function<Reader, String> {
 
     String result = null;
diff --git a/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero-result.xml b/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero-result.xml
new file mode 100644 (file)
index 0000000..931bab0
--- /dev/null
@@ -0,0 +1,18 @@
+<dataset>
+
+  <!-- Updated -->
+  <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="1500000000002"/>
+
+  <!-- Not updated because updated_at is not null -->
+  <file_sources id="102" project_uuid="ABCD" file_uuid="FILE2_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="1500000000000"/>
+
+  <!-- Not updated because on another project -->
+  <file_sources id="103" project_uuid="BCDE" file_uuid="FILE3_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="0"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero.xml b/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero.xml
new file mode 100644 (file)
index 0000000..9782d5c
--- /dev/null
@@ -0,0 +1,16 @@
+<dataset>
+
+  <!-- Only this source should be updated -->
+  <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="0"/>
+
+  <file_sources id="102" project_uuid="ABCD" file_uuid="FILE2_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="1500000000000"/>
+
+  <file_sources id="103" project_uuid="BCDE" file_uuid="FILE3_UUID"
+                binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH"
+                created_at="1500000000000" updated_at="0"/>
+
+</dataset>