You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestResultSetIterator.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.test.index;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.InputStream;
  23. import java.io.OutputStreamWriter;
  24. import java.nio.charset.StandardCharsets;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import java.util.Collections;
  29. import java.util.Date;
  30. import java.util.List;
  31. import javax.annotation.Nullable;
  32. import org.elasticsearch.action.update.UpdateRequest;
  33. import org.elasticsearch.common.xcontent.XContentType;
  34. import org.sonar.api.utils.log.Loggers;
  35. import org.sonar.api.utils.text.JsonWriter;
  36. import org.sonar.db.DbClient;
  37. import org.sonar.db.DbSession;
  38. import org.sonar.db.ResultSetIterator;
  39. import org.sonar.db.protobuf.DbFileSources;
  40. import org.sonar.db.source.FileSourceDto;
  41. import org.sonar.server.es.EsUtils;
  42. import org.sonar.server.source.index.FileSourcesUpdaterHelper.Row;
  43. import static org.sonar.server.source.index.FileSourcesUpdaterHelper.preparedStatementToSelectFileSources;
  44. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILES;
  45. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILE_LINES;
  46. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILE_UUID;
  47. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_DURATION_IN_MS;
  48. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_FILE_UUID;
  49. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_MESSAGE;
  50. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_NAME;
  51. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_PROJECT_UUID;
  52. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_STACKTRACE;
  53. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_STATUS;
  54. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_TEST_UUID;
  55. import static org.sonar.server.test.index.TestIndexDefinition.FIELD_UPDATED_AT;
  56. import static org.sonar.server.test.index.TestIndexDefinition.INDEX_TYPE_TEST;
  57. /**
  58. * Scroll over table FILE_SOURCES of test type and directly parse data required to
  59. * populate the index tests/test
  60. */
  61. public class TestResultSetIterator extends ResultSetIterator<Row> {
  62. private TestResultSetIterator(PreparedStatement stmt) throws SQLException {
  63. super(stmt);
  64. }
  65. public static TestResultSetIterator create(DbClient dbClient, DbSession session, @Nullable String projectUuid) {
  66. try {
  67. return new TestResultSetIterator(preparedStatementToSelectFileSources(dbClient, session, FileSourceDto.Type.TEST, projectUuid));
  68. } catch (SQLException e) {
  69. throw new IllegalStateException("Fail to prepare SQL request to select all tests", e);
  70. }
  71. }
  72. @Override
  73. protected Row read(ResultSet rs) throws SQLException {
  74. String projectUuid = rs.getString(1);
  75. String fileUuid = rs.getString(2);
  76. Date updatedAt = new Date(rs.getLong(3));
  77. List<DbFileSources.Test> tests = parseData(fileUuid, rs.getBinaryStream(4));
  78. return toRow(projectUuid, fileUuid, updatedAt, tests);
  79. }
  80. private static List<DbFileSources.Test> parseData(String fileUuid, @Nullable InputStream dataInput) {
  81. List<DbFileSources.Test> tests = Collections.emptyList();
  82. if (dataInput != null) {
  83. try {
  84. tests = FileSourceDto.decodeTestData(dataInput);
  85. } catch (Exception e) {
  86. Loggers.get(TestResultSetIterator.class).warn(String.format("Invalid file_sources.binary_data on row with file_uuid='%s', test file will be ignored", fileUuid), e);
  87. }
  88. }
  89. return tests;
  90. }
  91. /**
  92. * Convert protobuf message to tests required for Elasticsearch indexing
  93. */
  94. public static Row toRow(String projectUuid, String fileUuid, Date updatedAt, List<DbFileSources.Test> tests) {
  95. Row result = new Row(projectUuid, fileUuid, updatedAt.getTime());
  96. for (DbFileSources.Test test : tests) {
  97. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  98. // all the fields must be present, even if value is null
  99. try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(bytes, StandardCharsets.UTF_8)).setSerializeNulls(true)) {
  100. writer.beginObject();
  101. writer.prop(FIELD_PROJECT_UUID, projectUuid);
  102. writer.prop(FIELD_FILE_UUID, fileUuid);
  103. writer.prop(FIELD_TEST_UUID, test.getUuid());
  104. writer.prop(FIELD_NAME, test.getName());
  105. writer.prop(FIELD_STATUS, test.hasStatus() ? test.getStatus().toString() : null);
  106. writer.prop(FIELD_DURATION_IN_MS, test.hasExecutionTimeMs() ? test.getExecutionTimeMs() : null);
  107. writer.prop(FIELD_MESSAGE, test.hasMsg() ? test.getMsg() : null);
  108. writer.prop(FIELD_STACKTRACE, test.hasStacktrace() ? test.getStacktrace() : null);
  109. writer.prop(FIELD_UPDATED_AT, EsUtils.formatDateTime(updatedAt));
  110. writer.name(FIELD_COVERED_FILES);
  111. writer.beginArray();
  112. for (DbFileSources.Test.CoveredFile coveredFile : test.getCoveredFileList()) {
  113. writer.beginObject();
  114. writer.prop(FIELD_COVERED_FILE_UUID, coveredFile.getFileUuid());
  115. writer.name(FIELD_COVERED_FILE_LINES).valueObject(coveredFile.getCoveredLineList());
  116. writer.endObject();
  117. }
  118. writer.endArray();
  119. writer.endObject();
  120. }
  121. // This is an optimization to reduce memory consumption and multiple conversions from Map to JSON.
  122. // UpdateRequest#doc() and #upsert() take the same parameter values, so:
  123. // - passing the same Map would execute two JSON serializations
  124. // - Map is a useless temporarily structure: read JDBC result set -> convert to map -> convert to JSON. Generating
  125. // directly JSON from result set is more efficient.
  126. byte[] jsonDoc = bytes.toByteArray();
  127. UpdateRequest updateRequest = new UpdateRequest(INDEX_TYPE_TEST.getIndex(), INDEX_TYPE_TEST.getType(), test.getUuid())
  128. .routing(projectUuid)
  129. .doc(jsonDoc, XContentType.JSON)
  130. .upsert(jsonDoc, XContentType.JSON);
  131. result.getUpdateRequests().add(updateRequest);
  132. }
  133. return result;
  134. }
  135. }