}
public Date updateDate() {
- return getField(BaseNormalizer.UPDATED_AT_FIELD);
+ return getFieldAsDate(BaseNormalizer.UPDATED_AT_FIELD);
}
public void setUpdateDate(Date updatedAt) {
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;
// 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();
import java.util.Collection;
import java.util.Iterator;
-/**
- * Not thread-safe
- */
public class SourceLineIndexer implements ServerComponent {
private final DbClient dbClient;
}
public void indexSourceLines(boolean large) {
- // TODO support timezones
final BulkIndexer bulk = new BulkIndexer(esClient, SourceLineIndexDefinition.INDEX_SOURCE_LINES);
bulk.setLarge(large);
// 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";
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;
--- /dev/null
+/*
+ * 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;
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;
@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" +
@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();
// ok
} finally {
iterator.close();
-
}
}
}
--- /dev/null
+<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>
--- /dev/null
+<dataset>
+
+ <file_sources id="1" project_uuid="uuid-MyProject" file_uuid="uuid-MyFile.xoo" created_at="1416238020000" updated_at="1416239042000"
+ data="" data_hash="" />
+
+</dataset>
+++ /dev/null
-<dataset>
-
- <file_sources id="1" project_uuid="uuid-MyProject" file_uuid="uuid-MyFile.xoo" created_at="1416238020000" updated_at="1416239042000"
- data="" data_hash="" />
-
-</dataset>