]> source.dussan.org Git - sonarqube.git/commitdiff
Support empty files in SourceLineResultSetIterator
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 26 Nov 2014 22:02:56 +0000 (23:02 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 26 Nov 2014 22:02:56 +0000 (23:02 +0100)
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineDoc.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineIndexDefinition.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineIndexer.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineResultSetIterator.java
server/sonar-server/src/main/java/org/sonar/server/source/index/package-info.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineResultSetIteratorTest.java
server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/empty-file.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/shared.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/source-with-scm.xml [deleted file]

index 84451822dffbac729e7aa941c56cd1a27186be20..047cc24ea0f8faff752ece3512b3dc443d977487 100644 (file)
@@ -103,7 +103,7 @@ public class SourceLineDoc extends BaseDoc {
   }
 
   public Date updateDate() {
-    return getField(BaseNormalizer.UPDATED_AT_FIELD);
+    return getFieldAsDate(BaseNormalizer.UPDATED_AT_FIELD);
   }
 
   public void setUpdateDate(Date updatedAt) {
index b1ce5cd981d5297b9aef738bda00e31cd15b9dd2..fc2eb002ed6667db07ba71320ecb1dfd30bf1831 100644 (file)
@@ -39,7 +39,7 @@ public class SourceLineIndexDefinition implements IndexDefinition {
 
   public static final String INDEX_SOURCE_LINES = "sourcelines";
 
-  public static final String TYPE_SOURCE_LINE = "sourceLine";
+  public static final String TYPE_SOURCE_LINE = "sourceline";
 
 
   private final Settings settings;
@@ -60,7 +60,7 @@ public class SourceLineIndexDefinition implements IndexDefinition {
       // else keep defaults (one shard)
     }
 
-    // type "sourceLine"
+    // type "sourceline"
     NewIndex.NewIndexType sourceLineMapping = index.createType(TYPE_SOURCE_LINE);
     sourceLineMapping.stringFieldBuilder(FIELD_PROJECT_UUID).build();
     sourceLineMapping.stringFieldBuilder(FIELD_FILE_UUID).build();
index 2895663d2cc3bc8ec81223be372818744f7b04e3..6116ea942d78db681f5309147eb103ede29f3d0e 100644 (file)
@@ -30,9 +30,6 @@ import java.sql.Connection;
 import java.util.Collection;
 import java.util.Iterator;
 
-/**
- * Not thread-safe
- */
 public class SourceLineIndexer implements ServerComponent {
 
   private final DbClient dbClient;
@@ -47,7 +44,6 @@ public class SourceLineIndexer implements ServerComponent {
   }
 
   public void indexSourceLines(boolean large) {
-    // TODO support timezones
     final BulkIndexer bulk = new BulkIndexer(esClient, SourceLineIndexDefinition.INDEX_SOURCE_LINES);
     bulk.setLarge(large);
 
index 94ebaf9ad24ecb47ab133364f6230e26f84d2a61..0bafa2fc5120b2db5b30c91bc2a752d4c9ee9035 100644 (file)
@@ -51,10 +51,8 @@ class SourceLineResultSetIterator extends ResultSetIterator<Collection<SourceLin
     // column 1
     "project_uuid",
     "file_uuid",
-    "created_at",
     "updated_at",
-    "data",
-    "data_hash"
+    "data"
   };
 
   private static final String SQL_ALL = "select " + StringUtils.join(FIELDS, ",") + " from file_sources";
@@ -82,41 +80,44 @@ class SourceLineResultSetIterator extends ResultSetIterator<Collection<SourceLin
   protected Collection<SourceLineDoc> read(ResultSet rs) throws SQLException {
     String projectUuid = rs.getString(1);
     String fileUuid = rs.getString(2);
-    Date updatedAt = new Date(SqlUtil.getLong(rs, 4));
+    Date updatedAt = new Date(SqlUtil.getLong(rs, 3));
+    String csv = rs.getString(4);
 
-    int line = 1;
     List<SourceLineDoc> lines = Lists.newArrayList();
-    CSVParser csvParser = null;
-    try {
-      csvParser = new CSVParser(new StringReader(rs.getString(5)), CSVFormat.DEFAULT);
+    if (StringUtils.isNotEmpty(csv)) {
+      int line = 1;
+      CSVParser csvParser = null;
+      try {
+        csvParser = new CSVParser(new StringReader(csv), CSVFormat.DEFAULT);
 
-      for(CSVRecord csvRecord: csvParser) {
-        SourceLineDoc doc = new SourceLineDoc(Maps.<String, Object>newHashMapWithExpectedSize(9));
+        for (CSVRecord csvRecord : csvParser) {
+          SourceLineDoc doc = new SourceLineDoc(Maps.<String, Object>newHashMapWithExpectedSize(9));
 
-        doc.setProjectUuid(projectUuid);
-        doc.setFileUuid(fileUuid);
-        doc.setLine(line);
-        doc.setUpdateDate(updatedAt);
-        doc.setScmRevision(csvRecord.get(0));
-        doc.setScmAuthor(csvRecord.get(1));
-        doc.setScmDate(DateUtils.parseDateTimeQuietly(csvRecord.get(2)));
-        // doc.setLineHits(csvRecord.get(3));
-        // doc.setConditions(csvRecord.get(4));
-        // doc.setCoveredConditions(csvRecord.get(5));
-        doc.setHighlighting(csvRecord.get(6));
-        doc.setSource(csvRecord.get(csvRecord.size() - 1));
+          doc.setProjectUuid(projectUuid);
+          doc.setFileUuid(fileUuid);
+          doc.setLine(line);
+          doc.setUpdateDate(updatedAt);
+          doc.setScmRevision(csvRecord.get(0));
+          doc.setScmAuthor(csvRecord.get(1));
+          doc.setScmDate(DateUtils.parseDateTimeQuietly(csvRecord.get(2)));
+          // doc.setLineHits(csvRecord.get(3));
+          // doc.setConditions(csvRecord.get(4));
+          // doc.setCoveredConditions(csvRecord.get(5));
+          doc.setHighlighting(csvRecord.get(6));
+          doc.setSource(csvRecord.get(csvRecord.size() - 1));
 
-        lines.add(doc);
+          lines.add(doc);
 
-        line ++;
+          line++;
+        }
+      } catch (IOException ioError) {
+        throw new IllegalStateException("Impossible to open stream for file_sources.data with file_uuid " + fileUuid);
+      } catch (ArrayIndexOutOfBoundsException lineError) {
+        throw new IllegalStateException(
+          String.format("Impossible to parse source line data, stuck at line %d", line), lineError);
+      } finally {
+        IOUtils.closeQuietly(csvParser);
       }
-    } catch(IOException ioError) {
-      throw new IllegalStateException("Impossible to open stream for file_sources.data with file_uuid " + fileUuid);
-    } catch(ArrayIndexOutOfBoundsException lineError) {
-      throw new IllegalStateException(
-        String.format("Impossible to parse source line data, stuck at line %d", line), lineError);
-    } finally {
-      IOUtils.closeQuietly(csvParser);
     }
 
     return lines;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/index/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/source/index/package-info.java
new file mode 100644 (file)
index 0000000..78d3dff
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.server.source.index;
+
+import javax.annotation.ParametersAreNonnullByDefault;
index b79636e89c2fcf11968cc8da2e299f211e1200c6..57ffb0f6e4d4f1be84e1f784dff4542d10ceefb7 100644 (file)
@@ -24,12 +24,12 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
-import org.sonar.api.utils.DateUtils;
 import org.sonar.core.persistence.TestDatabase;
 import org.sonar.server.db.DbClient;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;
+import java.util.Collection;
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -58,7 +58,7 @@ public class SourceLineResultSetIteratorTest {
 
   @Test
   public void should_generate_source_line_documents() throws Exception {
-    db.prepareDbUnit(getClass(), "source-with-scm.xml");
+    db.prepareDbUnit(getClass(), "shared.xml");
     PreparedStatement stmt = connection.prepareStatement("UPDATE file_sources SET data = ? WHERE id=1");
     stmt.setString(1, "aef12a,alice,2014-04-25T12:34:56+0100,,,,polop,class Foo {\r\n" +
       "abe465,bob,2014-07-25T12:34:56+0100,,,,,  // Empty\r\n" +
@@ -84,16 +84,26 @@ public class SourceLineResultSetIteratorTest {
 
   @Test
   public void should_ignore_lines_already_handled() throws Exception {
-    db.prepareDbUnit(getClass(), "source-with-scm.xml");
+    db.prepareDbUnit(getClass(), "shared.xml");
 
     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, db.openConnection(),
-      DateUtils.parseDateTime("2014-11-17T16:44:02+0100").getTime());
+      2000000000000L);
     assertThat(iterator.hasNext()).isFalse();
   }
 
+  @Test
+  public void parse_empty_file() throws Exception {
+    db.prepareDbUnit(getClass(), "empty-file.xml");
+
+    SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, db.openConnection(), 0L);
+    assertThat(iterator.hasNext()).isTrue();
+    Collection<SourceLineDoc> lines = iterator.next();
+    assertThat(lines).isEmpty();
+  }
+
   @Test
   public void should_fail_on_bad_csv() throws Exception {
-    db.prepareDbUnit(getClass(), "source-with-scm.xml");
+    db.prepareDbUnit(getClass(), "shared.xml");
     PreparedStatement stmt = connection.prepareStatement("UPDATE file_sources SET data = ? WHERE id=1");
     stmt.setString(1, "plouf");
     stmt.executeUpdate();
@@ -108,7 +118,6 @@ public class SourceLineResultSetIteratorTest {
       // ok
     } finally {
       iterator.close();
-
     }
   }
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/empty-file.xml b/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/empty-file.xml
new file mode 100644 (file)
index 0000000..83fcc11
--- /dev/null
@@ -0,0 +1,6 @@
+<dataset>
+
+  <file_sources id="1" project_uuid="uuid-MyProject" file_uuid="uuid-MyFile.xoo" created_at="1416238020000" updated_at="1416239042000"
+                data="" data_hash="" line_hashes=""/>
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/shared.xml b/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/shared.xml
new file mode 100644 (file)
index 0000000..f1498ba
--- /dev/null
@@ -0,0 +1,6 @@
+<dataset>
+
+  <file_sources id="1" project_uuid="uuid-MyProject" file_uuid="uuid-MyFile.xoo" created_at="1416238020000" updated_at="1416239042000"
+    data="" data_hash="" />
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/source-with-scm.xml b/server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineResultSetIteratorTest/source-with-scm.xml
deleted file mode 100644 (file)
index f1498ba..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-<dataset>
-
-  <file_sources id="1" project_uuid="uuid-MyProject" file_uuid="uuid-MyFile.xoo" created_at="1416238020000" updated_at="1416239042000"
-    data="" data_hash="" />
-
-</dataset>