]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6258 Remove hack setting updated_at to 0 by only indexing file sources of curre... 221/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 16 Apr 2015 16:17:57 +0000 (18:17 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 17 Apr 2015 07:00:57 +0000 (09:00 +0200)
18 files changed:
server/sonar-server-benchmarks/src/test/java/org/sonar/server/benchmark/SourceDbBenchmarkTest.java
server/sonar-server/src/main/java/org/sonar/server/computation/source/SymbolsLineReader.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/IndexSourceLinesStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java
server/sonar-server/src/main/java/org/sonar/server/es/BaseIndexer.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceFileResultSetIterator.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineIndexer.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/IndexSourceLinesStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistFileSourcesStepTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexerTest.java
server/sonar-server/src/test/java/org/sonar/server/source/index/SourceFileResultSetIteratorTest.java
server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexerTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/index_source.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-result.xml [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/update_source_date_on_sources_with_update_at_to_zero.xml [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceFileResultSetIteratorTest/filter_by_project.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceFileResultSetIteratorTest/filter_by_project_and_date.xml [new file with mode: 0644]
sonar-batch-protocol/src/main/gen-java/org/sonar/server/source/db/FileSourceTestDb.java

index d29374d2f57d964eaf127cabd4a9af7bbe7b62bd..9bc46b01d205d98d253d0d7605be8c450a611e29 100644 (file)
@@ -73,7 +73,7 @@ public class SourceDbBenchmarkTest {
 
     try {
       long start = System.currentTimeMillis();
-      SourceFileResultSetIterator it = SourceFileResultSetIterator.create(dbClient, connection, 0L);
+      SourceFileResultSetIterator it = SourceFileResultSetIterator.create(dbClient, connection, 0L, null);
       while (it.hasNext()) {
         SourceFileResultSetIterator.Row row = it.next();
         assertThat(row.getLineUpdateRequests().size()).isEqualTo(NUMBER_OF_LINES);
index 9045e0eb9a3cbbb8b7a235923414cc5105d7bcf6..db57bcb52c8c3c23ecfc1186efd6964791891059 100644 (file)
@@ -24,11 +24,16 @@ import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.server.source.db.FileSourceDb;
 
 import java.io.Serializable;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import static com.google.common.collect.Lists.newArrayList;
-import static com.google.common.collect.Maps.newHashMap;
-import static com.google.common.collect.Sets.newHashSet;
 
 public class SymbolsLineReader implements LineReader {
 
@@ -39,7 +44,7 @@ public class SymbolsLineReader implements LineReader {
     this.symbols = newArrayList(symbols);
     // Sort symbols to have deterministic results and avoid false variation that would lead to an unnecessary update of the source files
     // data
-    Collections.sort(this.symbols, new SymbolsDuplication());
+    Collections.sort(this.symbols, new SymbolsComparator());
 
     this.idsBySymbol = createIdsBySymbolMap(this.symbols);
   }
@@ -78,8 +83,8 @@ public class SymbolsLineReader implements LineReader {
   }
 
   private List<BatchReport.Symbols.Symbol> findSymbolsMatchingLine(int line) {
-    List<BatchReport.Symbols.Symbol> lineSymbols = newArrayList();
-    Set<BatchReport.Symbols.Symbol> symbolsIndex = newHashSet();
+    List<BatchReport.Symbols.Symbol> lineSymbols = new ArrayList<>();
+    Set<BatchReport.Symbols.Symbol> symbolsIndex = new HashSet<>();
     for (BatchReport.Symbols.Symbol symbol : symbols) {
       if (matchLine(symbol.getDeclaration(), line) && !symbolsIndex.contains(symbol)) {
         lineSymbols.add(symbol);
@@ -101,7 +106,7 @@ public class SymbolsLineReader implements LineReader {
   }
 
   private Map<BatchReport.Symbols.Symbol, Integer> createIdsBySymbolMap(List<BatchReport.Symbols.Symbol> symbols) {
-    Map<BatchReport.Symbols.Symbol, Integer> map = newHashMap();
+    Map<BatchReport.Symbols.Symbol, Integer> map = new HashMap<>();
     int symbolId = 1;
     for (BatchReport.Symbols.Symbol symbol : symbols) {
       map.put(symbol, symbolId);
@@ -110,7 +115,7 @@ public class SymbolsLineReader implements LineReader {
     return map;
   }
 
-  private static class SymbolsDuplication implements Comparator<BatchReport.Symbols.Symbol>, Serializable {
+  private static class SymbolsComparator implements Comparator<BatchReport.Symbols.Symbol>, Serializable {
     @Override
     public int compare(BatchReport.Symbols.Symbol o1, BatchReport.Symbols.Symbol o2) {
       if (o1.getDeclaration().getStartLine() == o2.getDeclaration().getStartLine()) {
index 46e8836c1afa16463e0a29cb956f2eb5b446b86c..8bcc854162b4a9c622f8f362586ebd0fe2620ad6 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(DbClient dbClient, System2 system2, SourceLineIndexer indexer) {
-    this.dbClient = dbClient;
-    this.system2 = system2;
+  public IndexSourceLinesStep(SourceLineIndexer indexer) {
     this.indexer = indexer;
   }
 
@@ -46,20 +38,7 @@ 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);
-    }
+    indexer.index(context.getProject().uuid());
   }
 
   @Override
index de067227d8bfbbfab58af77888a008ccefec7691..974a5678a497b3edc61e2328d99ca13878b4bf9d 100644 (file)
@@ -36,18 +36,24 @@ import org.sonar.core.persistence.DbSession;
 import org.sonar.core.persistence.MyBatis;
 import org.sonar.core.source.db.FileSourceDto;
 import org.sonar.server.computation.ComputationContext;
-import org.sonar.server.computation.source.*;
+import org.sonar.server.computation.source.ComputeFileSourceData;
+import org.sonar.server.computation.source.CoverageLineReader;
+import org.sonar.server.computation.source.DuplicationLineReader;
+import org.sonar.server.computation.source.HighlightingLineReader;
+import org.sonar.server.computation.source.LineReader;
+import org.sonar.server.computation.source.ReportIterator;
+import org.sonar.server.computation.source.ScmLineReader;
+import org.sonar.server.computation.source.SymbolsLineReader;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.source.db.FileSourceDb;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import static com.google.common.collect.Lists.newArrayList;
-
 public class PersistFileSourcesStep implements ComputationStep {
 
   private final DbClient dbClient;
@@ -131,8 +137,7 @@ public class PersistFileSourcesStep implements ComputationStep {
         .setDataHash(dataHash)
         .setLineHashes(lineHashes)
         .setCreatedAt(system2.now())
-        // TODO set current date here when indexing sources in E/S will be done in this class
-        .setUpdatedAt(0L);
+        .setUpdatedAt(system2.now());
       dbClient.fileSourceDao().insert(fileSourcesContext.session, dto);
       fileSourcesContext.session.commit();
     } else {
@@ -147,8 +152,7 @@ public class PersistFileSourcesStep implements ComputationStep {
           .setLineHashes(lineHashes);
         // Optimization only change updated at when updating binary data to avoid unecessary indexation by E/S
         if (binaryDataUpdated) {
-          // TODO set current date here when indexing sources in E/S will be done in this class
-          previousDto.setUpdatedAt(0L);
+          previousDto.setUpdatedAt(system2.now());
         }
         dbClient.fileSourceDao().update(previousDto);
         fileSourcesContext.session.commit();
@@ -169,8 +173,8 @@ public class PersistFileSourcesStep implements ComputationStep {
   }
 
   private static class LineReaders {
-    private final List<LineReader> lineReaders = newArrayList();
-    private final List<ReportIterator> reportIterators = newArrayList();
+    private final List<LineReader> lineReaders = new ArrayList<>();
+    private final List<ReportIterator> reportIterators = new ArrayList<>();
 
     LineReaders(BatchReportReader reportReader, int componentRef) {
       File coverageFile = reportReader.readComponentCoverage(componentRef);
index 39a0f443e895f88e5a1d417e3bd5bd17a7c55cf8..548437c11ee71558b8740c8a5b7d1e0b1f8d36fc 100644 (file)
@@ -49,7 +49,7 @@ public abstract class BaseIndexer implements ServerComponent, Startable {
   private boolean enabled = false;
 
   protected BaseIndexer(EsClient client, long threadKeepAliveSeconds, String indexName, String typeName,
-                        String dateFieldName) {
+    String dateFieldName) {
     this.indexName = indexName;
     this.typeName = typeName;
     this.dateFieldName = dateFieldName;
@@ -58,14 +58,14 @@ public abstract class BaseIndexer implements ServerComponent, Startable {
       threadKeepAliveSeconds, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
   }
 
-  public void index() {
+  public void index(final IndexerTask task) {
     if (enabled) {
       final long requestedAt = System.currentTimeMillis();
       Future submit = executor.submit(new Runnable() {
         @Override
         public void run() {
           if (requestedAt > lastUpdatedAt) {
-            long l = doIndex(lastUpdatedAt);
+            long l = task.index(lastUpdatedAt);
             // l can be 0 if no documents were indexed
             lastUpdatedAt = Math.max(l, lastUpdatedAt);
           }
@@ -79,6 +79,15 @@ public abstract class BaseIndexer implements ServerComponent, Startable {
     }
   }
 
+  public void index() {
+    index(new IndexerTask() {
+      @Override
+      public long index(long lastUpdatedAt) {
+        return doIndex(lastUpdatedAt);
+      }
+    });
+  }
+
   protected abstract long doIndex(long lastUpdatedAt);
 
   public BaseIndexer setEnabled(boolean b) {
@@ -96,4 +105,8 @@ public abstract class BaseIndexer implements ServerComponent, Startable {
     executor.shutdown();
   }
 
+  public interface IndexerTask {
+    long index(long lastUpdatedAt);
+  }
+
 }
index 3158d3cef7a79d0932caa6d16d1bbc51f295d9a8..5403a6b9d547bc667279ca58e90901a026d78b66 100644 (file)
@@ -29,6 +29,8 @@ import org.sonar.server.db.ResultSetIterator;
 import org.sonar.server.es.EsUtils;
 import org.sonar.server.source.db.FileSourceDb;
 
+import javax.annotation.Nullable;
+
 import java.io.ByteArrayOutputStream;
 import java.io.OutputStreamWriter;
 import java.sql.Connection;
@@ -79,16 +81,23 @@ public class SourceFileResultSetIterator extends ResultSetIterator<SourceFileRes
     "updated_at",
     "binary_data"
   };
-  private static final String SQL_ALL = "select " + StringUtils.join(FIELDS, ",") + " from file_sources";
-  private static final String SQL_AFTER_DATE = SQL_ALL + " where updated_at>?";
 
-  public static SourceFileResultSetIterator create(DbClient dbClient, Connection connection, long afterDate) {
+  private static final String SQL_ALL = "SELECT " + StringUtils.join(FIELDS, ",") + " FROM file_sources ";
+  private static final String AFTER_DATE_FILTER = "updated_at>? ";
+  private static final String PROJECT_FILTER = "project_uuid=? ";
+
+  public static SourceFileResultSetIterator create(DbClient dbClient, Connection connection, long afterDate, @Nullable String projectUuid) {
     try {
-      String sql = afterDate > 0L ? SQL_AFTER_DATE : SQL_ALL;
+      String sql = createSQL(afterDate, projectUuid);
       // rows are big, so they are scrolled once at a time (one row in memory at a time)
       PreparedStatement stmt = dbClient.newScrollingSingleRowSelectStatement(connection, sql);
+      int index = 1;
       if (afterDate > 0L) {
-        stmt.setLong(1, afterDate);
+        stmt.setLong(index, afterDate);
+        index++;
+      }
+      if (projectUuid != null) {
+        stmt.setString(index, projectUuid);
       }
       return new SourceFileResultSetIterator(stmt);
     } catch (SQLException e) {
@@ -96,6 +105,25 @@ public class SourceFileResultSetIterator extends ResultSetIterator<SourceFileRes
     }
   }
 
+  private static String createSQL(long afterDate, @Nullable String projectUuid) {
+    String sql = SQL_ALL;
+    if (afterDate > 0L || projectUuid != null) {
+      sql += "WHERE ";
+      boolean isFirst = true;
+      if (afterDate > 0L) {
+        sql += AFTER_DATE_FILTER;
+        isFirst = false;
+      }
+      if (projectUuid != null) {
+        if (!isFirst) {
+          sql += "AND ";
+        }
+        sql += PROJECT_FILTER;
+      }
+    }
+    return sql;
+  }
+
   private SourceFileResultSetIterator(PreparedStatement stmt) throws SQLException {
     super(stmt);
   }
@@ -130,62 +158,62 @@ public class SourceFileResultSetIterator extends ResultSetIterator<SourceFileRes
 
       // unit tests
       if (line.hasUtLineHits()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_UT_LINE_HITS,  line.getUtLineHits());
+        writer.prop(SourceLineIndexDefinition.FIELD_UT_LINE_HITS, line.getUtLineHits());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_UT_LINE_HITS).valueObject(null);
       }
       if (line.hasUtConditions()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_UT_CONDITIONS,  line.getUtConditions());
+        writer.prop(SourceLineIndexDefinition.FIELD_UT_CONDITIONS, line.getUtConditions());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_UT_CONDITIONS).valueObject(null);
       }
       if (line.hasUtCoveredConditions()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS,  line.getUtCoveredConditions());
+        writer.prop(SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS, line.getUtCoveredConditions());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS).valueObject(null);
       }
 
       // IT
       if (line.hasItLineHits()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_IT_LINE_HITS,  line.getItLineHits());
+        writer.prop(SourceLineIndexDefinition.FIELD_IT_LINE_HITS, line.getItLineHits());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_IT_LINE_HITS).valueObject(null);
       }
       if (line.hasItConditions()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_IT_CONDITIONS,  line.getItConditions());
+        writer.prop(SourceLineIndexDefinition.FIELD_IT_CONDITIONS, line.getItConditions());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_IT_CONDITIONS).valueObject(null);
       }
       if (line.hasItCoveredConditions()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS,  line.getItCoveredConditions());
+        writer.prop(SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS, line.getItCoveredConditions());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS).valueObject(null);
       }
 
       // Overall coverage
       if (line.hasOverallLineHits()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS,  line.getOverallLineHits());
+        writer.prop(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS, line.getOverallLineHits());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS).valueObject(null);
       }
       if (line.hasOverallConditions()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS,  line.getOverallConditions());
+        writer.prop(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS, line.getOverallConditions());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS).valueObject(null);
       }
       if (line.hasOverallCoveredConditions()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS,  line.getOverallCoveredConditions());
+        writer.prop(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS, line.getOverallCoveredConditions());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS).valueObject(null);
       }
 
       if (line.hasHighlighting()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_HIGHLIGHTING,  line.getHighlighting());
+        writer.prop(SourceLineIndexDefinition.FIELD_HIGHLIGHTING, line.getHighlighting());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_HIGHLIGHTING).valueObject(null);
       }
       if (line.hasSymbols()) {
-        writer.prop(SourceLineIndexDefinition.FIELD_SYMBOLS,  line.getSymbols());
+        writer.prop(SourceLineIndexDefinition.FIELD_SYMBOLS, line.getSymbols());
       } else {
         writer.name(SourceLineIndexDefinition.FIELD_SYMBOLS).valueObject(null);
       }
index 011cb348ffa6b9b28fc32c8b59c4033f387865b1..8382856261be7cc47f33f26414a4c4c70d9f9b76 100644 (file)
@@ -29,6 +29,8 @@ import org.sonar.server.es.BaseIndexer;
 import org.sonar.server.es.BulkIndexer;
 import org.sonar.server.es.EsClient;
 
+import javax.annotation.Nullable;
+
 import java.sql.Connection;
 import java.util.Iterator;
 
@@ -48,15 +50,28 @@ public class SourceLineIndexer extends BaseIndexer {
     this.dbClient = dbClient;
   }
 
+  public void index(final String projectUuid){
+    super.index(new IndexerTask() {
+      @Override
+      public long index(long lastUpdatedAt) {
+        return doIndex(lastUpdatedAt, projectUuid);
+      }
+    });
+  }
+
   @Override
   protected long doIndex(long lastUpdatedAt) {
+    return doIndex(lastUpdatedAt, null);
+  }
+
+  private long doIndex(long lastUpdatedAt, @Nullable String projectUuid) {
     final BulkIndexer bulk = new BulkIndexer(esClient, SourceLineIndexDefinition.INDEX);
     bulk.setLarge(lastUpdatedAt == 0L);
 
     DbSession dbSession = dbClient.openSession(false);
     Connection dbConnection = dbSession.getConnection();
     try {
-      SourceFileResultSetIterator rowIt = SourceFileResultSetIterator.create(dbClient, dbConnection, lastUpdatedAt);
+      SourceFileResultSetIterator rowIt = SourceFileResultSetIterator.create(dbClient, dbConnection, lastUpdatedAt, projectUuid);
       long maxUpdatedAt = doIndex(bulk, rowIt);
       rowIt.close();
       return maxUpdatedAt;
index 8b5d1f9aad571118323718e2b86120456e126017..5f99a49d85c6500ef8872eff686f6a713aa7a914 100644 (file)
@@ -25,7 +25,6 @@ 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;
@@ -40,12 +39,10 @@ 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 {
 
@@ -55,21 +52,18 @@ public class IndexSourceLinesStepTest extends BaseStepTest {
   @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 {
     SourceLineIndexer sourceLineIndexer = new SourceLineIndexer(dbClient, esTester.client());
     sourceLineIndexer.setEnabled(true);
-    return new IndexSourceLinesStep(dbClient, system2, sourceLineIndexer);
+    return new IndexSourceLinesStep(sourceLineIndexer);
   }
 
   @Test
@@ -78,22 +72,18 @@ public class IndexSourceLinesStepTest extends BaseStepTest {
   }
 
   @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");
+  public void index_source() throws Exception {
+    dbTester.prepareDbUnit(getClass(), "index_source.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()));
   }
 }
index 717b1030528f34a7eac02a4fdb6f07f60ea328e4..dc503775f8a5e2110878c1a37285f70c81a953d6 100644 (file)
@@ -22,7 +22,11 @@ package org.sonar.server.computation.step;
 
 import com.google.common.collect.Lists;
 import org.apache.commons.io.FileUtils;
-import org.junit.*;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.api.utils.System2;
@@ -113,7 +117,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
     assertThat(fileSourceDto.getDataHash()).isNotEmpty();
     assertThat(fileSourceDto.getLineHashes()).isNotEmpty();
     assertThat(fileSourceDto.getCreatedAt()).isEqualTo(now);
-    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(0L);
+    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now);
 
     FileSourceDb.Data data = FileSourceDto.decodeData(fileSourceDto.getBinaryData());
     assertThat(data.getLinesCount()).isEqualTo(2);
@@ -374,7 +378,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
     assertThat(dbTester.countRowsOfTable("file_sources")).isEqualTo(1);
     FileSourceDto fileSourceDto = dbClient.fileSourceDao().select(FILE_UUID);
     assertThat(fileSourceDto.getCreatedAt()).isEqualTo(past);
-    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(0L);
+    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now);
   }
 
   @Test
index f77ba3148b10e7e971d6249b48d6b21c038a5437..6bfe8928e9bc10ad5aa54704394be9939fa4a036 100644 (file)
@@ -54,7 +54,7 @@ public class IssueAuthorizationIndexerTest {
   @Test
   public void index_nothing() throws Exception {
     IssueAuthorizationIndexer indexer = createIndexer();
-    indexer.doIndex(0L);
+    indexer.index();
 
     assertThat(esTester.countDocuments("issues", "authorization")).isZero();
   }
index 6775d2845b350b67a3678cbc364ea26e03113417..1b6942ffdfc3635346b05859f9d9ce7fab3971a5 100644 (file)
@@ -48,6 +48,8 @@ public class SourceFileResultSetIteratorTest {
 
   Connection connection;
 
+  SourceFileResultSetIterator iterator;
+
   @Before
   public void setUp() throws Exception {
     dbClient = new DbClient(db.database(), db.myBatis());
@@ -56,6 +58,9 @@ public class SourceFileResultSetIteratorTest {
 
   @After
   public void after() throws Exception {
+    if (iterator != null) {
+      iterator.close();
+    }
     connection.close();
   }
 
@@ -64,9 +69,9 @@ public class SourceFileResultSetIteratorTest {
     db.prepareDbUnit(getClass(), "shared.xml");
     FileSourceTesting.updateDataColumn(connection, "F1", FileSourceTesting.newFakeData(3).build());
 
-    SourceFileResultSetIterator it = SourceFileResultSetIterator.create(dbClient, connection, 0L);
-    assertThat(it.hasNext()).isTrue();
-    SourceFileResultSetIterator.Row row = it.next();
+    iterator = SourceFileResultSetIterator.create(dbClient, connection, 0L, null);
+    assertThat(iterator.hasNext()).isTrue();
+    SourceFileResultSetIterator.Row row = iterator.next();
     assertThat(row.getProjectUuid()).isEqualTo("P1");
     assertThat(row.getFileUuid()).isEqualTo("F1");
     assertThat(row.getUpdatedAt()).isEqualTo(1416239042000L);
@@ -91,8 +96,7 @@ public class SourceFileResultSetIteratorTest {
       MapEntry.entry(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS, 7),
       MapEntry.entry(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS, 8),
       MapEntry.entry(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS, 9)
-      );
-    it.close();
+    );
   }
 
   /**
@@ -105,8 +109,8 @@ public class SourceFileResultSetIteratorTest {
     dataBuilder.addLinesBuilder().setLine(1).build();
     FileSourceTesting.updateDataColumn(connection, "F1", dataBuilder.build());
 
-    SourceFileResultSetIterator it = SourceFileResultSetIterator.create(dbClient, connection, 0L);
-    SourceFileResultSetIterator.Row row = it.next();
+    iterator = SourceFileResultSetIterator.create(dbClient, connection, 0L, null);
+    SourceFileResultSetIterator.Row row = iterator.next();
     assertThat(row.getProjectUuid()).isEqualTo("P1");
     assertThat(row.getFileUuid()).isEqualTo("F1");
     assertThat(row.getUpdatedAt()).isEqualTo(1416239042000L);
@@ -117,7 +121,7 @@ public class SourceFileResultSetIteratorTest {
       MapEntry.entry(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "P1"),
       MapEntry.entry(SourceLineIndexDefinition.FIELD_FILE_UUID, "F1"),
       MapEntry.entry(SourceLineIndexDefinition.FIELD_LINE, 1)
-      );
+    );
     // null values
     assertThat(doc).containsKeys(
       SourceLineIndexDefinition.FIELD_SCM_REVISION,
@@ -134,16 +138,48 @@ public class SourceFileResultSetIteratorTest {
       SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS,
       SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS
     );
-    it.close();
   }
 
   @Test
   public void filter_by_date() throws Exception {
     db.prepareDbUnit(getClass(), "shared.xml");
 
-    SourceFileResultSetIterator iterator = SourceFileResultSetIterator.create(dbClient, connection, 2000000000000L);
+    iterator = SourceFileResultSetIterator.create(dbClient, connection, 2000000000000L, null);
+    assertThat(iterator.hasNext()).isFalse();
+  }
+
+  @Test
+  public void filter_by_project() throws Exception {
+    db.prepareDbUnit(getClass(), "filter_by_project.xml");
+    FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder();
+    dataBuilder.addLinesBuilder().setLine(1).build();
+    FileSourceTesting.updateDataColumn(connection, "F1", dataBuilder.build());
+
+    iterator = SourceFileResultSetIterator.create(dbClient, connection, 0L, "P1");
+
+    SourceFileResultSetIterator.Row row = iterator.next();
+    assertThat(row.getProjectUuid()).isEqualTo("P1");
+    assertThat(row.getFileUuid()).isEqualTo("F1");
+
+    // File from other project P2 is not returned
+    assertThat(iterator.hasNext()).isFalse();
+  }
+
+  @Test
+  public void filter_by_project_and_date() throws Exception {
+    db.prepareDbUnit(getClass(), "filter_by_project_and_date.xml");
+    FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder();
+    dataBuilder.addLinesBuilder().setLine(1).build();
+    FileSourceTesting.updateDataColumn(connection, "F1", dataBuilder.build());
+
+    iterator = SourceFileResultSetIterator.create(dbClient, connection, 1400000000000L, "P1");
+
+    SourceFileResultSetIterator.Row row = iterator.next();
+    assertThat(row.getProjectUuid()).isEqualTo("P1");
+    assertThat(row.getFileUuid()).isEqualTo("F1");
+
+    // File F2 is not returned
     assertThat(iterator.hasNext()).isFalse();
-    iterator.close();
   }
 
   @Test
@@ -152,7 +188,7 @@ public class SourceFileResultSetIteratorTest {
 
     FileSourceTesting.updateDataColumn(connection, "F1", "THIS_IS_NOT_PROTOBUF".getBytes());
 
-    SourceFileResultSetIterator iterator = SourceFileResultSetIterator.create(dbClient, connection, 0L);
+    iterator = SourceFileResultSetIterator.create(dbClient, connection, 0L, null);
     try {
       assertThat(iterator.hasNext()).isTrue();
       iterator.next();
@@ -160,6 +196,5 @@ public class SourceFileResultSetIteratorTest {
     } catch (IllegalStateException e) {
       // ok
     }
-    iterator.close();
   }
 }
index 14b6c10b1a7d90d1b6b22af7e4a7e0915394bea1..2a0952307c00b95d66a487c4fbd5c843bd7a5014 100644 (file)
@@ -48,7 +48,24 @@ import java.util.Map;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.entry;
-import static org.sonar.server.source.index.SourceLineIndexDefinition.*;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_DUPLICATIONS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_FILE_UUID;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_IT_CONDITIONS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_IT_LINE_HITS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_LINE;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_PROJECT_UUID;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_SCM_AUTHOR;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_SCM_REVISION;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_SOURCE;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_UT_CONDITIONS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.FIELD_UT_LINE_HITS;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.INDEX;
+import static org.sonar.server.source.index.SourceLineIndexDefinition.TYPE;
 
 @Category(DbTests.class)
 public class SourceLineIndexerTest {
@@ -70,7 +87,7 @@ public class SourceLineIndexerTest {
   }
 
   @Test
-  public void index_source_lines_from_db() throws Exception {
+  public void index_source_lines() throws Exception {
     db.prepareDbUnit(getClass(), "db.xml");
 
     Connection connection = db.openConnection();
@@ -81,6 +98,30 @@ public class SourceLineIndexerTest {
     assertThat(countDocuments()).isEqualTo(3);
   }
 
+  @Test
+  public void index_source_lines_from_project() throws Exception {
+    db.prepareDbUnit(getClass(), "db.xml");
+
+    Connection connection = db.openConnection();
+    FileSourceTesting.updateDataColumn(connection, "FILE_UUID", FileSourceTesting.newRandomData(3).build());
+    connection.close();
+
+    indexer.index("PROJECT_UUID");
+    assertThat(countDocuments()).isEqualTo(3);
+  }
+
+  @Test
+  public void index_nothing_from_unknown_project() throws Exception {
+    db.prepareDbUnit(getClass(), "db.xml");
+
+    Connection connection = db.openConnection();
+    FileSourceTesting.updateDataColumn(connection, "FILE_UUID", FileSourceTesting.newRandomData(3).build());
+    connection.close();
+
+    indexer.index("UNKNOWN");
+    assertThat(countDocuments()).isZero();
+  }
+
   /**
    * File F1 in project P1 has one line -> to be updated
    * File F2 in project P1 has one line -> untouched
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/index_source.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/IndexSourceLinesStepTest/index_source.xml
new file mode 100644 (file)
index 0000000..bfa7d65
--- /dev/null
@@ -0,0 +1,12 @@
+<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"
+                test_data="[null]"
+                created_at="1500000000000" updated_at="0"/>
+
+  <file_sources id="102" project_uuid="DCBA" file_uuid="FILE2_UUID"
+                binary_data="edcba" data_hash="hash2" line_hashes="CBA\nFDE\nIHG" src_hash="FILE2_HASH"
+                created_at="1500000000000" updated_at="0"/>
+
+</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-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
deleted file mode 100644 (file)
index db05b2c..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<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"
-                test_data="[null]"
-                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
deleted file mode 100644 (file)
index 6360875..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<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"
-                test_data="[null]"
-                created_at="1500000000000" updated_at="0"/>
-
-</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceFileResultSetIteratorTest/filter_by_project.xml b/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceFileResultSetIteratorTest/filter_by_project.xml
new file mode 100644 (file)
index 0000000..aeeffbd
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <file_sources id="1" project_uuid="P1" file_uuid="F1" created_at="1416238020000" updated_at="1416239042000"
+                binary_data="" data_hash="" />
+
+  <file_sources id="2" project_uuid="P2" file_uuid="F2" created_at="1416238020000" updated_at="1416239042000"
+                binary_data="" data_hash="" />
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceFileResultSetIteratorTest/filter_by_project_and_date.xml b/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceFileResultSetIteratorTest/filter_by_project_and_date.xml
new file mode 100644 (file)
index 0000000..db7a997
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <file_sources id="1" project_uuid="P1" file_uuid="F1" created_at="1416238020000" updated_at="1416239042000"
+                binary_data="" data_hash="" />
+
+  <file_sources id="2" project_uuid="P1" file_uuid="F2" created_at="1416238020000" updated_at="1300000000000"
+                binary_data="" data_hash="" />
+
+</dataset>
index ac69f62aa29bcd3be7d250d8b3acdcb332327a67..47c041fb8c4a1d86991b18002de48df04b073d06 100644 (file)
@@ -278,14 +278,26 @@ public final class FileSourceTestDb {
 
       /**
        * <code>optional string key = 2;</code>
+       *
+       * <pre>
+       *TODO TBE - should the key and long_name specified directly ?
+       * </pre>
        */
       boolean hasKey();
       /**
        * <code>optional string key = 2;</code>
+       *
+       * <pre>
+       *TODO TBE - should the key and long_name specified directly ?
+       * </pre>
        */
       java.lang.String getKey();
       /**
        * <code>optional string key = 2;</code>
+       *
+       * <pre>
+       *TODO TBE - should the key and long_name specified directly ?
+       * </pre>
        */
       com.google.protobuf.ByteString
           getKeyBytes();
@@ -511,12 +523,20 @@ public final class FileSourceTestDb {
       private java.lang.Object key_;
       /**
        * <code>optional string key = 2;</code>
+       *
+       * <pre>
+       *TODO TBE - should the key and long_name specified directly ?
+       * </pre>
        */
       public boolean hasKey() {
         return ((bitField0_ & 0x00000002) == 0x00000002);
       }
       /**
        * <code>optional string key = 2;</code>
+       *
+       * <pre>
+       *TODO TBE - should the key and long_name specified directly ?
+       * </pre>
        */
       public java.lang.String getKey() {
         java.lang.Object ref = key_;
@@ -534,6 +554,10 @@ public final class FileSourceTestDb {
       }
       /**
        * <code>optional string key = 2;</code>
+       *
+       * <pre>
+       *TODO TBE - should the key and long_name specified directly ?
+       * </pre>
        */
       public com.google.protobuf.ByteString
           getKeyBytes() {
@@ -1023,12 +1047,20 @@ public final class FileSourceTestDb {
         private java.lang.Object key_ = "";
         /**
          * <code>optional string key = 2;</code>
+         *
+         * <pre>
+         *TODO TBE - should the key and long_name specified directly ?
+         * </pre>
          */
         public boolean hasKey() {
           return ((bitField0_ & 0x00000002) == 0x00000002);
         }
         /**
          * <code>optional string key = 2;</code>
+         *
+         * <pre>
+         *TODO TBE - should the key and long_name specified directly ?
+         * </pre>
          */
         public java.lang.String getKey() {
           java.lang.Object ref = key_;
@@ -1046,6 +1078,10 @@ public final class FileSourceTestDb {
         }
         /**
          * <code>optional string key = 2;</code>
+         *
+         * <pre>
+         *TODO TBE - should the key and long_name specified directly ?
+         * </pre>
          */
         public com.google.protobuf.ByteString
             getKeyBytes() {
@@ -1062,6 +1098,10 @@ public final class FileSourceTestDb {
         }
         /**
          * <code>optional string key = 2;</code>
+         *
+         * <pre>
+         *TODO TBE - should the key and long_name specified directly ?
+         * </pre>
          */
         public Builder setKey(
             java.lang.String value) {
@@ -1075,6 +1115,10 @@ public final class FileSourceTestDb {
         }
         /**
          * <code>optional string key = 2;</code>
+         *
+         * <pre>
+         *TODO TBE - should the key and long_name specified directly ?
+         * </pre>
          */
         public Builder clearKey() {
           bitField0_ = (bitField0_ & ~0x00000002);
@@ -1084,6 +1128,10 @@ public final class FileSourceTestDb {
         }
         /**
          * <code>optional string key = 2;</code>
+         *
+         * <pre>
+         *TODO TBE - should the key and long_name specified directly ?
+         * </pre>
          */
         public Builder setKeyBytes(
             com.google.protobuf.ByteString value) {