Search in sources :

Example 1 with Row

use of org.sonar.server.source.index.FileSourcesUpdaterHelper.Row in project sonarqube by SonarSource.

the class TestResultSetIterator method toRow.

/**
   * Convert protobuf message to tests required for Elasticsearch indexing
   */
public static Row toRow(String projectUuid, String fileUuid, Date updatedAt, List<DbFileSources.Test> tests) {
    Row result = new Row(projectUuid, fileUuid, updatedAt.getTime());
    for (DbFileSources.Test test : tests) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        // all the fields must be present, even if value is null
        try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(bytes, StandardCharsets.UTF_8)).setSerializeNulls(true)) {
            writer.beginObject();
            writer.prop(FIELD_PROJECT_UUID, projectUuid);
            writer.prop(FIELD_FILE_UUID, fileUuid);
            writer.prop(FIELD_TEST_UUID, test.getUuid());
            writer.prop(FIELD_NAME, test.getName());
            writer.prop(FIELD_STATUS, test.hasStatus() ? test.getStatus().toString() : null);
            writer.prop(FIELD_DURATION_IN_MS, test.hasExecutionTimeMs() ? test.getExecutionTimeMs() : null);
            writer.prop(FIELD_MESSAGE, test.hasMsg() ? test.getMsg() : null);
            writer.prop(FIELD_STACKTRACE, test.hasStacktrace() ? test.getStacktrace() : null);
            writer.prop(FIELD_UPDATED_AT, EsUtils.formatDateTime(updatedAt));
            writer.name(FIELD_COVERED_FILES);
            writer.beginArray();
            for (DbFileSources.Test.CoveredFile coveredFile : test.getCoveredFileList()) {
                writer.beginObject();
                writer.prop(FIELD_COVERED_FILE_UUID, coveredFile.getFileUuid());
                writer.name(FIELD_COVERED_FILE_LINES).valueObject(coveredFile.getCoveredLineList());
                writer.endObject();
            }
            writer.endArray();
            writer.endObject();
        }
        // This is an optimization to reduce memory consumption and multiple conversions from Map to JSON.
        // UpdateRequest#doc() and #upsert() take the same parameter values, so:
        // - passing the same Map would execute two JSON serializations
        // - Map is a useless temporarily structure: read JDBC result set -> convert to map -> convert to JSON. Generating
        // directly JSON from result set is more efficient.
        byte[] jsonDoc = bytes.toByteArray();
        UpdateRequest updateRequest = new UpdateRequest(INDEX_TYPE_TEST.getIndex(), INDEX_TYPE_TEST.getType(), test.getUuid()).routing(projectUuid).doc(jsonDoc).upsert(jsonDoc);
        result.getUpdateRequests().add(updateRequest);
    }
    return result;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) OutputStreamWriter(java.io.OutputStreamWriter) Row(org.sonar.server.source.index.FileSourcesUpdaterHelper.Row) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DbFileSources(org.sonar.db.protobuf.DbFileSources) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)1 JsonWriter (org.sonar.api.utils.text.JsonWriter)1 DbFileSources (org.sonar.db.protobuf.DbFileSources)1 Row (org.sonar.server.source.index.FileSourcesUpdaterHelper.Row)1