*/
package org.sonar.batch.index;
-import javax.annotation.Nullable;
-
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.SetMultimap;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.LoggerFactory;
-import org.sonar.api.database.model.MeasureDataDto;
-import org.sonar.api.database.model.MeasureDto;
+import org.sonar.api.database.model.MeasureMapper;
import org.sonar.api.database.model.MeasureModel;
-import org.sonar.api.database.model.MeasureModelMapper;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.utils.SonarException;
import org.sonar.core.persistence.MyBatis;
+import javax.annotation.Nullable;
+
import java.util.Collection;
import java.util.List;
import java.util.Map;
-import static com.google.common.collect.Iterables.filter;
-
import static com.google.common.base.Predicates.not;
+import static com.google.common.collect.Iterables.filter;
public final class MeasurePersister {
private final MyBatis mybatis;
public void dump() {
LoggerFactory.getLogger(getClass()).debug("{} measures to dump", unsavedMeasuresByResource.size());
- List<MeasureDto> measuresToSave = getMeasuresToSave();
- insert(filter(measuresToSave, HAS_LARGE_DATA));
- batchInsert(filter(measuresToSave, not(HAS_LARGE_DATA)));
+ List<MeasureModel> measures = getMeasuresToSave();
+ insert(filter(measures, HAS_MEASURE_DATA));
+ batchInsert(filter(measures, not(HAS_MEASURE_DATA)));
}
public void saveMeasure(Resource resource, Measure measure) {
&& (measure.getVariation5() == null || NumberUtils.compare(measure.getVariation5().doubleValue(), 0.0) == 0);
}
- private List<MeasureDto> getMeasuresToSave() {
- List<MeasureDto> batch = Lists.newArrayList();
+ private List<MeasureModel> getMeasuresToSave() {
+ List<MeasureModel> measures = Lists.newArrayList();
Map<Resource, Collection<Measure>> map = unsavedMeasuresByResource.asMap();
for (Map.Entry<Resource, Collection<Measure>> entry : map.entrySet()) {
Snapshot snapshot = resourcePersister.getSnapshot(entry.getKey());
for (Measure measure : entry.getValue()) {
if (shouldPersistMeasure(resource, measure)) {
- batch.add(new MeasureDto(model(measure).setSnapshotId(snapshot.getId())));
+ measures.add(model(measure).setSnapshotId(snapshot.getId()));
}
}
}
unsavedMeasuresByResource.clear();
- return batch;
+ return measures;
}
private MeasureModel model(Measure measure) {
return model;
}
- private void batchInsert(Iterable<MeasureDto> values) {
+ private void batchInsert(Iterable<MeasureModel> values) {
SqlSession session = mybatis.openBatchSession();
try {
- MeasureModelMapper mapper = session.getMapper(MeasureModelMapper.class);
- for (MeasureDto value : values) {
+ MeasureMapper mapper = session.getMapper(MeasureMapper.class);
+
+ for (MeasureModel value : values) {
mapper.insert(value);
}
+
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}
- private void insert(Iterable<MeasureDto> values) {
+ private void insert(Iterable<MeasureModel> values) {
SqlSession session = mybatis.openSession();
try {
- MeasureModelMapper mapper = session.getMapper(MeasureModelMapper.class);
- for (MeasureDto value : values) {
+ MeasureMapper mapper = session.getMapper(MeasureMapper.class);
+
+ for (MeasureModel value : values) {
mapper.insert(value);
- mapper.insertData(new MeasureDataDto(value.getId(), value.getSnapshotId(), value.getMeasureData().getData()));
+ mapper.insertData(value);
}
+
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
private MeasureModel insert(Measure measure, Snapshot snapshot) {
- MeasureModel model = model(measure).setSnapshotId(snapshot.getId());
+ MeasureModel value = model(measure);
+ value.setSnapshotId(snapshot.getId());
SqlSession session = mybatis.openSession();
try {
- MeasureModelMapper mapper = session.getMapper(MeasureModelMapper.class);
- MeasureDto value = new MeasureDto(model);
+ MeasureMapper mapper = session.getMapper(MeasureMapper.class);
+
mapper.insert(value);
if (value.getMeasureData() != null) {
- mapper.insertData(new MeasureDataDto(value.getId(), value.getSnapshotId(), value.getMeasureData().getData()));
+ mapper.insertData(value);
}
+
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
- return model;
+ return value;
}
private MeasureModel update(Measure measure) {
- MeasureModel model = model(measure);
- model.setId(measure.getId());
+ MeasureModel value = model(measure);
+ value.setId(measure.getId());
SqlSession session = mybatis.openSession();
try {
- MeasureModelMapper mapper = session.getMapper(MeasureModelMapper.class);
- mapper.update(new MeasureDto(model));
+ MeasureMapper mapper = session.getMapper(MeasureMapper.class);
+
+ mapper.update(value);
+
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
- return model;
+ return value;
}
- private static final Predicate<MeasureDto> HAS_LARGE_DATA = new Predicate<MeasureDto>() {
- public boolean apply(@Nullable MeasureDto measure) {
+ private static final Predicate<MeasureModel> HAS_MEASURE_DATA = new Predicate<MeasureModel>() {
+ public boolean apply(@Nullable MeasureModel measure) {
return (null != measure) && (measure.getMeasureData() != null);
}
};
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;
-import org.sonar.api.database.model.MeasureDataDto;
-import org.sonar.api.database.model.MeasureDto;
-import org.sonar.api.database.model.MeasureModelMapper;
+import org.sonar.api.database.model.MeasureMapper;
+import org.sonar.api.database.model.MeasureModel;
import org.sonar.core.dashboard.ActiveDashboardDto;
import org.sonar.core.dashboard.ActiveDashboardMapper;
import org.sonar.core.dashboard.DashboardDto;
loadAlias(conf, "UserRole", UserRoleDto.class);
loadAlias(conf, "Widget", WidgetDto.class);
loadAlias(conf, "WidgetProperty", WidgetPropertyDto.class);
- loadAlias(conf, "MeasureDto", MeasureDto.class);
- loadAlias(conf, "MeasureDataDto", MeasureDataDto.class);
+ loadAlias(conf, "MeasureModel", MeasureModel.class);
loadMapper(conf, ActiveDashboardMapper.class);
loadMapper(conf, AuthorMapper.class);
loadMapper(conf, UserMapper.class);
loadMapper(conf, WidgetMapper.class);
loadMapper(conf, WidgetPropertyMapper.class);
- loadMapper(conf, MeasureModelMapper.class);
+ loadMapper(conf, MeasureMapper.class);
sessionFactory = new SqlSessionFactoryBuilder().build(conf);
return this;
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.api.database.model;
-
-import java.util.Date;
-
-public class MeasureDto {
- private final Long id;
- private final Double value;
- private final String textValue;
- private final Integer tendency;
- private final Integer metricId;
- private final Integer snapshotId;
- private final Integer projectId;
- private final String description;
- private final Date measureDate;
- private final Integer ruleId;
- private final Integer rulePriority;
- private final String alertStatus;
- private final String alertText;
- private final Double variationValue1;
- private final Double variationValue2;
- private final Double variationValue3;
- private final Double variationValue4;
- private final Double variationValue5;
- private final String url;
- private final Integer characteristicId;
- private final Integer personId;
- private final MeasureData measureData;
-
- public MeasureDto(MeasureModel model) {
- id = model.getId();
- value = model.getValue();
- textValue = model.getTextValue();
- tendency = model.getTendency();
- metricId = model.getMetricId();
- snapshotId = model.getSnapshotId();
- projectId = model.getProjectId();
- description = model.getDescription();
- measureDate = model.getMeasureDate();
- ruleId = model.getRuleId();
- rulePriority = (null == model.getRulePriority()) ? null : model.getRulePriority().ordinal();
- alertStatus = (null == model.getAlertStatus()) ? null : model.getAlertStatus().name();
- alertText = model.getAlertText();
- variationValue1 = model.getVariationValue1();
- variationValue2 = model.getVariationValue2();
- variationValue3 = model.getVariationValue3();
- variationValue4 = model.getVariationValue4();
- variationValue5 = model.getVariationValue5();
- url = model.getUrl();
- characteristicId = (null == model.getCharacteristic()) ? null : model.getCharacteristic().getId();
- personId = model.getPersonId();
- measureData = model.getMeasureData();
- }
-
- public Long getId() {
- return id;
- }
-
- public Double getValue() {
- return value;
- }
-
- public String getTextValue() {
- return textValue;
- }
-
- public Integer getTendency() {
- return tendency;
- }
-
- public Integer getMetricId() {
- return metricId;
- }
-
- public Integer getSnapshotId() {
- return snapshotId;
- }
-
- public Integer getProjectId() {
- return projectId;
- }
-
- public String getDescription() {
- return description;
- }
-
- public Date getMeasureDate() {
- return measureDate;
- }
-
- public Integer getRuleId() {
- return ruleId;
- }
-
- public Integer getRulePriority() {
- return rulePriority;
- }
-
- public String getAlertStatus() {
- return alertStatus;
- }
-
- public String getAlertText() {
- return alertText;
- }
-
- public Double getVariationValue1() {
- return variationValue1;
- }
-
- public Double getVariationValue2() {
- return variationValue2;
- }
-
- public Double getVariationValue3() {
- return variationValue3;
- }
-
- public Double getVariationValue4() {
- return variationValue4;
- }
-
- public Double getVariationValue5() {
- return variationValue5;
- }
-
- public String getUrl() {
- return url;
- }
-
- public Integer getCharacteristicId() {
- return characteristicId;
- }
-
- public Integer getPersonId() {
- return personId;
- }
-
- public MeasureData getMeasureData() {
- return measureData;
- }
-}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.database.model;
+
+public interface MeasureMapper {
+ void insert(MeasureModel measure);
+
+ void insertData(MeasureModel data);
+
+ void update(MeasureModel measure);
+}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.api.database.model;
-
-public interface MeasureModelMapper {
- void insert(MeasureDto measure);
-
- void insertData(MeasureDataDto data);
-
- void update(MeasureDto measure);
-}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.api.database.model.MeasureMapper">
+
+ <insert id="insert" parameterType="MeasureModel" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+ <selectKey order="BEFORE" resultType="Long" keyProperty="id">
+ select project_measures_seq.NEXTVAL from DUAL
+ </selectKey>
+ INSERT INTO project_measures (id,
+ value, metric_id, snapshot_id, rule_id, text_value, tendency, measure_date,
+ project_id, alert_status, alert_text, url, description, rule_priority, characteristic_id, variation_value_1,
+ variation_value_2, variation_value_3, variation_value_4, variation_value_5, person_id)
+ VALUES (#{id},
+ #{value}, #{metricId}, #{snapshotId}, #{ruleId}, #{textValue, jdbcType=VARCHAR}, #{tendency},
+ #{measureDate, jdbcType=TIMESTAMP}, #{projectId}, #{alertStatus, jdbcType=VARCHAR}, #{alertText, jdbcType=VARCHAR},
+ #{url, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{rulePriority.ordinal}, #{characteristic.id}, #{variationValue1},
+ #{variationValue2}, #{variationValue3}, #{variationValue4}, #{variationValue5}, #{personId}
+ )
+ </insert>
+
+ <insert id="insertData" parameterType="MeasureModel" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+ <selectKey order="BEFORE" resultType="Long" keyProperty="id">
+ select measure_data_seq.NEXTVAL from DUAL
+ </selectKey>
+ INSERT INTO measure_data (id, measure_id, snapshot_id, data)
+ VALUES (#{id}, #{measureData.measure.id}, #{snapshotId}, #{measureData.data})
+ </insert>
+
+ <update id="update" parameterType="MeasureModel">
+ UPDATE project_measures
+ SET
+ value = #{value},
+ metric_id = #{metricId},
+ rule_id = #{ruleId},
+ text_value = #{textValue, jdbcType=VARCHAR},
+ tendency = #{tendency},
+ alert_status = #{alertStatus, jdbcType=VARCHAR},
+ alert_text = #{alertText, jdbcType=VARCHAR},
+ url = #{url, jdbcType=VARCHAR},
+ description = #{description, jdbcType=VARCHAR},
+ rule_priority = #{rulePriority.ordinal},
+ characteristic_id = #{characteristic.id},
+ variation_value_1 = #{variationValue1},
+ variation_value_2 = #{variationValue2},
+ variation_value_3 = #{variationValue3},
+ variation_value_4 = #{variationValue4},
+ variation_value_5 = #{variationValue5},
+ person_id = #{personId}
+ WHERE id = #{id}
+ </update>
+
+</mapper>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.api.database.model.MeasureMapper">
+
+ <insert id="insert" parameterType="MeasureModel" useGeneratedKeys="true" keyProperty="id">
+ INSERT INTO project_measures (
+ value, metric_id, snapshot_id, rule_id, text_value, tendency, measure_date,
+ project_id, alert_status, alert_text, url, description, rule_priority, characteristic_id, variation_value_1,
+ variation_value_2, variation_value_3, variation_value_4, variation_value_5, person_id)
+ VALUES (
+ #{value}, #{metricId}, #{snapshotId}, #{ruleId}, #{textValue, jdbcType=VARCHAR}, #{tendency},
+ #{measureDate, jdbcType=TIMESTAMP}, #{projectId}, #{alertStatus, jdbcType=VARCHAR}, #{alertText, jdbcType=VARCHAR},
+ #{url, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{rulePriority.ordinal}, #{characteristic.id}, #{variationValue1},
+ #{variationValue2}, #{variationValue3}, #{variationValue4}, #{variationValue5}, #{personId}
+ )
+ </insert>
+
+ <insert id="insertData" parameterType="MeasureModel" useGeneratedKeys="true" keyProperty="id">
+ INSERT INTO measure_data (measure_id, snapshot_id, data)
+ VALUES (#{measureData.measure.id}, #{snapshotId}, #{measureData.data})
+ </insert>
+
+ <update id="update" parameterType="MeasureModel">
+ UPDATE project_measures
+ SET
+ value = #{value},
+ metric_id = #{metricId},
+ rule_id = #{ruleId},
+ text_value = #{textValue, jdbcType=VARCHAR},
+ tendency = #{tendency},
+ alert_status = #{alertStatus, jdbcType=VARCHAR},
+ alert_text = #{alertText, jdbcType=VARCHAR},
+ url = #{url, jdbcType=VARCHAR},
+ description = #{description, jdbcType=VARCHAR},
+ rule_priority = #{rulePriority.ordinal},
+ characteristic_id = #{characteristic.id},
+ variation_value_1 = #{variationValue1},
+ variation_value_2 = #{variationValue2},
+ variation_value_3 = #{variationValue3},
+ variation_value_4 = #{variationValue4},
+ variation_value_5 = #{variationValue5},
+ person_id = #{personId}
+ WHERE id = #{id}
+ </update>
+
+</mapper>
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8" ?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
-<mapper namespace="org.sonar.api.database.model.MeasureModelMapper">
-
- <insert id="insert" parameterType="MeasureDto" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
- <selectKey order="BEFORE" resultType="Long" keyProperty="id">
- select project_measures_seq.NEXTVAL from DUAL
- </selectKey>
- INSERT INTO project_measures (id,
- value, metric_id, snapshot_id, rule_id, text_value, tendency, measure_date,
- project_id, alert_status, alert_text, url, description, rule_priority, characteristic_id, variation_value_1,
- variation_value_2, variation_value_3, variation_value_4, variation_value_5, person_id)
- VALUES (#{id},
- #{value}, #{metricId}, #{snapshotId}, #{ruleId}, #{textValue, jdbcType=VARCHAR}, #{tendency},
- #{measureDate, jdbcType=TIMESTAMP}, #{projectId}, #{alertStatus, jdbcType=VARCHAR}, #{alertText, jdbcType=VARCHAR},
- #{url, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{rulePriority}, #{characteristicId}, #{variationValue1},
- #{variationValue2}, #{variationValue3}, #{variationValue4}, #{variationValue5}, #{personId}
- )
- </insert>
-
-
- <update id="update" parameterType="MeasureDto">
- UPDATE project_measures
- SET
- value = #{value},
- metric_id = #{metricId},
- rule_id = #{ruleId},
- text_value = #{textValue, jdbcType=VARCHAR},
- tendency = #{tendency},
- alert_status = #{alertStatus, jdbcType=VARCHAR},
- alert_text = #{alertText, jdbcType=VARCHAR},
- url = #{url, jdbcType=VARCHAR},
- description = #{description, jdbcType=VARCHAR},
- rule_priority = #{rulePriority},
- characteristic_id = #{characteristicId},
- variation_value_1 = #{variationValue1},
- variation_value_2 = #{variationValue2},
- variation_value_3 = #{variationValue3},
- variation_value_4 = #{variationValue4},
- variation_value_5 = #{variationValue5},
- person_id = #{personId}
- WHERE id = #{id}
- </update>
-
-</mapper>
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8" ?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
-<mapper namespace="org.sonar.api.database.model.MeasureModelMapper">
-
- <insert id="insert" parameterType="MeasureDto" useGeneratedKeys="true" keyProperty="id">
- INSERT INTO project_measures (
- value, metric_id, snapshot_id, rule_id, text_value, tendency, measure_date,
- project_id, alert_status, alert_text, url, description, rule_priority, characteristic_id, variation_value_1,
- variation_value_2, variation_value_3, variation_value_4, variation_value_5, person_id)
- VALUES (
- #{value}, #{metricId}, #{snapshotId}, #{ruleId}, #{textValue, jdbcType=VARCHAR}, #{tendency},
- #{measureDate, jdbcType=TIMESTAMP}, #{projectId}, #{alertStatus, jdbcType=VARCHAR}, #{alertText, jdbcType=VARCHAR},
- #{url, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{rulePriority}, #{characteristicId}, #{variationValue1},
- #{variationValue2}, #{variationValue3}, #{variationValue4}, #{variationValue5}, #{personId}
- )
- </insert>
-
- <insert id="insertData" parameterType="MeasureDataDto" useGeneratedKeys="true" keyProperty="id">
- INSERT INTO measure_data (measure_id, snapshot_id, data)
- VALUES (#{measureId}, #{snapshotId}, #{data})
- </insert>
-
- <update id="update" parameterType="MeasureDto">
- UPDATE project_measures
- SET
- value = #{value},
- metric_id = #{metricId},
- rule_id = #{ruleId},
- text_value = #{textValue, jdbcType=VARCHAR},
- tendency = #{tendency},
- alert_status = #{alertStatus, jdbcType=VARCHAR},
- alert_text = #{alertText, jdbcType=VARCHAR},
- url = #{url, jdbcType=VARCHAR},
- description = #{description, jdbcType=VARCHAR},
- rule_priority = #{rulePriority},
- characteristic_id = #{characteristicId},
- variation_value_1 = #{variationValue1},
- variation_value_2 = #{variationValue2},
- variation_value_3 = #{variationValue3},
- variation_value_4 = #{variationValue4},
- variation_value_5 = #{variationValue5},
- person_id = #{personId}
- WHERE id = #{id}
- </update>
-
-</mapper>