package org.sonar.batch.index;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Predicate;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.SetMultimap;
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.base.Predicates.not;
-import static com.google.common.collect.Iterables.filter;
-
public final class MeasurePersister {
private final MyBatis mybatis;
private final ResourcePersister resourcePersister;
public void dump() {
LoggerFactory.getLogger(getClass()).debug("{} measures to dump", unsavedMeasuresByResource.size());
- List<MeasureModel> measures = getMeasuresToSave();
- insert(filter(measures, HAS_MEASURE_DATA));
- batchInsert(filter(measures, not(HAS_MEASURE_DATA)));
+ insert(getMeasuresToSave());
}
public void saveMeasure(Resource resource, Measure measure) {
return model;
}
- private void batchInsert(Iterable<MeasureModel> values) {
- SqlSession session = mybatis.openSession();
- try {
- MeasureMapper mapper = session.getMapper(MeasureMapper.class);
-
- for (MeasureModel value : values) {
- mapper.insert(value);
- }
-
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
private void insert(Iterable<MeasureModel> values) {
SqlSession session = mybatis.openSession();
try {
for (MeasureModel value : values) {
mapper.insert(value);
- mapper.insertData(value);
+ if (value.getMeasureData() != null) {
+ mapper.insertData(value);
+ }
}
session.commit();
return value;
}
-
- private static final Predicate<MeasureModel> HAS_MEASURE_DATA = new Predicate<MeasureModel>() {
- public boolean apply(@Nullable MeasureModel measure) {
- return (null != measure) && (measure.getMeasureData() != null);
- }
- };
}