1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/*
* 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.batch.index;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.SetMultimap;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.LoggerFactory;
import org.sonar.api.database.model.MeasureMapper;
import org.sonar.api.database.model.MeasureModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.RuleMeasure;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.utils.SonarException;
import org.sonar.core.persistence.MyBatis;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public final class MeasurePersister {
private final MyBatis mybatis;
private final ResourcePersister resourcePersister;
private final RuleFinder ruleFinder;
private final MemoryOptimizer memoryOptimizer;
private final SetMultimap<Resource, Measure> unsavedMeasuresByResource = LinkedHashMultimap.create();
private boolean delayedMode = false;
public MeasurePersister(MyBatis mybatis, ResourcePersister resourcePersister, RuleFinder ruleFinder, MemoryOptimizer memoryOptimizer) {
this.mybatis = mybatis;
this.resourcePersister = resourcePersister;
this.ruleFinder = ruleFinder;
this.memoryOptimizer = memoryOptimizer;
}
public void setDelayedMode(boolean delayedMode) {
this.delayedMode = delayedMode;
}
public Measure reloadMeasure(Measure measure) {
return memoryOptimizer.reloadMeasure(measure);
}
public void dump() {
LoggerFactory.getLogger(getClass()).debug("{} measures to dump", unsavedMeasuresByResource.size());
insert(getMeasuresToSave());
}
public void saveMeasure(Resource resource, Measure measure) {
if (shouldSaveLater(measure)) {
unsavedMeasuresByResource.put(resource, measure);
return;
}
MeasureModel model = insertOrUpdate(resource, measure);
if (model != null) {
memoryOptimizer.evictDataMeasure(measure, model);
}
}
private MeasureModel insertOrUpdate(Resource resource, Measure measure) {
Snapshot snapshot = resourcePersister.getSnapshotOrFail(resource);
if (measure.getId() != null) {
return update(measure, snapshot);
}
if (shouldPersistMeasure(resource, measure)) {
MeasureModel insert = insert(measure, snapshot);
measure.setId(insert.getId());
return insert;
}
return null;
}
private boolean shouldSaveLater(Measure measure) {
return delayedMode && measure.getPersistenceMode().useMemory();
}
@VisibleForTesting
static boolean shouldPersistMeasure(Resource resource, Measure measure) {
return measure.getPersistenceMode().useDatabase() &&
!(ResourceUtils.isEntity(resource) && measure.isBestValue());
}
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()) {
Resource resource = entry.getKey();
Snapshot snapshot = resourcePersister.getSnapshot(entry.getKey());
for (Measure measure : entry.getValue()) {
if (shouldPersistMeasure(resource, measure)) {
measures.add(model(measure).setSnapshotId(snapshot.getId()));
}
}
}
unsavedMeasuresByResource.clear();
return measures;
}
private MeasureModel model(Measure measure) {
MeasureModel model = new MeasureModel();
model.setMetricId(measure.getMetric().getId()); // we assume that the index has updated the metric
model.setDescription(measure.getDescription());
model.setData(measure.getData());
model.setAlertStatus(measure.getAlertStatus());
model.setAlertText(measure.getAlertText());
model.setTendency(measure.getTendency());
model.setVariationValue1(measure.getVariation1());
model.setVariationValue2(measure.getVariation2());
model.setVariationValue3(measure.getVariation3());
model.setVariationValue4(measure.getVariation4());
model.setVariationValue5(measure.getVariation5());
model.setUrl(measure.getUrl());
model.setCharacteristic(measure.getCharacteristic());
model.setPersonId(measure.getPersonId());
if (measure.getValue() != null) {
model.setValue(measure.getValue().doubleValue());
} else {
model.setValue(null);
}
if (measure instanceof RuleMeasure) {
RuleMeasure ruleMeasure = (RuleMeasure) measure;
model.setRulePriority(ruleMeasure.getSeverity());
if (ruleMeasure.getRule() != null) {
Rule ruleWithId = ruleFinder.findByKey(ruleMeasure.getRule().getRepositoryKey(), ruleMeasure.getRule().getKey());
if (ruleWithId == null) {
throw new SonarException("Can not save a measure with unknown rule " + ruleMeasure);
}
model.setRuleId(ruleWithId.getId());
}
}
return model;
}
private void insert(Iterable<MeasureModel> values) {
SqlSession session = mybatis.openSession();
try {
MeasureMapper mapper = session.getMapper(MeasureMapper.class);
for (MeasureModel value : values) {
mapper.insert(value);
if (value.getMeasureData() != null) {
mapper.insertData(value.getMeasureData());
}
}
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}
private MeasureModel insert(Measure measure, Snapshot snapshot) {
MeasureModel value = model(measure);
value.setSnapshotId(snapshot.getId());
SqlSession session = mybatis.openSession();
try {
MeasureMapper mapper = session.getMapper(MeasureMapper.class);
mapper.insert(value);
if (value.getMeasureData() != null) {
mapper.insertData(value.getMeasureData());
}
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
return value;
}
private MeasureModel update(Measure measure, Snapshot snapshot) {
MeasureModel value = model(measure);
value.setId(measure.getId());
value.setSnapshotId(snapshot.getId());
SqlSession session = mybatis.openSession();
try {
MeasureMapper mapper = session.getMapper(MeasureMapper.class);
mapper.update(value);
mapper.deleteData(value);
if (value.getMeasureData() != null) {
mapper.insertData(value.getMeasureData());
}
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
return value;
}
}
|