3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.util.cache;
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.base.Splitter;
24 import com.google.common.collect.ImmutableSet;
25 import java.io.BufferedOutputStream;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.OutputStream;
30 import java.io.Serializable;
31 import java.util.Collections;
32 import java.util.Date;
34 import javax.annotation.CheckForNull;
35 import org.sonar.api.issue.impact.Severity;
36 import org.sonar.api.issue.impact.SoftwareQuality;
37 import org.sonar.api.rule.RuleKey;
38 import org.sonar.api.rules.CleanCodeAttribute;
39 import org.sonar.api.rules.RuleType;
40 import org.sonar.api.utils.Duration;
41 import org.sonar.api.utils.System2;
42 import org.sonar.core.issue.DefaultIssue;
43 import org.sonar.core.issue.DefaultIssueComment;
44 import org.sonar.core.issue.FieldDiffs;
45 import org.sonar.core.util.CloseableIterator;
46 import org.sonar.core.util.Protobuf;
47 import org.sonar.db.protobuf.DbIssues;
49 import static java.util.Optional.ofNullable;
51 public class ProtobufIssueDiskCache implements DiskCache<DefaultIssue> {
52 private static final String TAGS_SEPARATOR = ",";
53 private static final Splitter STRING_LIST_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
55 private final File file;
56 private final System2 system2;
58 public ProtobufIssueDiskCache(File file, System2 system2) {
60 this.system2 = system2;
64 public long fileSize() {
69 public CacheAppender<DefaultIssue> newAppender() {
71 return new ProtoCacheAppender();
72 } catch (FileNotFoundException e) {
73 throw new IllegalStateException(e);
78 public CloseableIterator<DefaultIssue> traverse() {
79 CloseableIterator<IssueCache.Issue> protoIterator = Protobuf.readStream(file, IssueCache.Issue.parser());
80 return new CloseableIterator<>() {
83 protected DefaultIssue doNext() {
84 if (protoIterator.hasNext()) {
85 return toDefaultIssue(protoIterator.next());
91 protected void doClose() {
92 protoIterator.close();
98 static DefaultIssue toDefaultIssue(IssueCache.Issue next) {
99 DefaultIssue defaultIssue = new DefaultIssue();
100 defaultIssue.setKey(next.getKey());
101 defaultIssue.setType(RuleType.valueOf(next.getRuleType()));
102 defaultIssue.setComponentUuid(next.hasComponentUuid() ? next.getComponentUuid() : null);
103 defaultIssue.setComponentKey(next.getComponentKey());
104 defaultIssue.setProjectUuid(next.getProjectUuid());
105 defaultIssue.setProjectKey(next.getProjectKey());
106 defaultIssue.setRuleKey(RuleKey.parse(next.getRuleKey()));
107 defaultIssue.setLanguage(next.hasLanguage() ? next.getLanguage() : null);
108 defaultIssue.setSeverity(next.hasSeverity() ? next.getSeverity() : null);
109 defaultIssue.setManualSeverity(next.getManualSeverity());
110 defaultIssue.setMessage(next.hasMessage() ? next.getMessage() : null);
111 defaultIssue.setMessageFormattings(next.hasMessageFormattings() ? next.getMessageFormattings() : null);
112 defaultIssue.setLine(next.hasLine() ? next.getLine() : null);
113 defaultIssue.setGap(next.hasGap() ? next.getGap() : null);
114 defaultIssue.setEffort(next.hasEffort() ? Duration.create(next.getEffort()) : null);
115 defaultIssue.setStatus(next.getStatus());
116 defaultIssue.setResolution(next.hasResolution() ? next.getResolution() : null);
117 defaultIssue.setAssigneeUuid(next.hasAssigneeUuid() ? next.getAssigneeUuid() : null);
118 defaultIssue.setAssigneeLogin(next.hasAssigneeLogin() ? next.getAssigneeLogin() : null);
119 defaultIssue.setChecksum(next.hasChecksum() ? next.getChecksum() : null);
120 defaultIssue.setAuthorLogin(next.hasAuthorLogin() ? next.getAuthorLogin() : null);
121 next.getCommentsList().forEach(c -> defaultIssue.addComment(toDefaultIssueComment(c)));
122 defaultIssue.setTags(ImmutableSet.copyOf(STRING_LIST_SPLITTER.split(next.getTags())));
123 defaultIssue.setCodeVariants(ImmutableSet.copyOf(STRING_LIST_SPLITTER.split(next.getCodeVariants())));
124 defaultIssue.setRuleDescriptionContextKey(next.hasRuleDescriptionContextKey() ? next.getRuleDescriptionContextKey() : null);
125 defaultIssue.setLocations(next.hasLocations() ? next.getLocations() : null);
126 defaultIssue.setIsFromExternalRuleEngine(next.getIsFromExternalRuleEngine());
127 defaultIssue.setCreationDate(new Date(next.getCreationDate()));
128 defaultIssue.setUpdateDate(next.hasUpdateDate() ? new Date(next.getUpdateDate()) : null);
129 defaultIssue.setCloseDate(next.hasCloseDate() ? new Date(next.getCloseDate()) : null);
130 defaultIssue.setCurrentChangeWithoutAddChange(next.hasCurrentChanges() ? toDefaultIssueChanges(next.getCurrentChanges()) : null);
131 defaultIssue.setNew(next.getIsNew());
132 defaultIssue.setIsOnChangedLine(next.getIsOnChangedLine());
133 defaultIssue.setIsNewCodeReferenceIssue(next.getIsNewCodeReferenceIssue());
134 defaultIssue.setCopied(next.getIsCopied());
135 defaultIssue.setBeingClosed(next.getBeingClosed());
136 defaultIssue.setOnDisabledRule(next.getOnDisabledRule());
137 defaultIssue.setChanged(next.getIsChanged());
138 defaultIssue.setSendNotifications(next.getSendNotifications());
139 defaultIssue.setSelectedAt(next.hasSelectedAt() ? next.getSelectedAt() : null);
140 defaultIssue.setQuickFixAvailable(next.getQuickFixAvailable());
141 defaultIssue.setPrioritizedRule(next.getIsPrioritizedRule());
142 defaultIssue.setIsNoLongerNewCodeReferenceIssue(next.getIsNoLongerNewCodeReferenceIssue());
143 defaultIssue.setCleanCodeAttribute(next.hasCleanCodeAttribute() ? CleanCodeAttribute.valueOf(next.getCleanCodeAttribute()) : null);
144 if (next.hasAnticipatedTransitionUuid()) {
145 defaultIssue.setAnticipatedTransitionUuid(next.getAnticipatedTransitionUuid());
148 for (IssueCache.Impact impact : next.getImpactsList()) {
149 defaultIssue.addImpact(SoftwareQuality.valueOf(impact.getSoftwareQuality()), Severity.valueOf(impact.getSeverity()));
151 for (IssueCache.FieldDiffs protoFieldDiffs : next.getChangesList()) {
152 defaultIssue.addChange(toDefaultIssueChanges(protoFieldDiffs));
158 static IssueCache.Issue toProto(IssueCache.Issue.Builder builder, DefaultIssue defaultIssue) {
160 builder.setKey(defaultIssue.key());
161 builder.setRuleType(defaultIssue.type().getDbConstant());
162 ofNullable(defaultIssue.getCleanCodeAttribute()).ifPresent(value -> builder.setCleanCodeAttribute(value.name()));
163 ofNullable(defaultIssue.componentUuid()).ifPresent(builder::setComponentUuid);
164 builder.setComponentKey(defaultIssue.componentKey());
165 builder.setProjectUuid(defaultIssue.projectUuid());
166 builder.setProjectKey(defaultIssue.projectKey());
167 builder.setRuleKey(defaultIssue.ruleKey().toString());
168 ofNullable(defaultIssue.language()).ifPresent(builder::setLanguage);
169 ofNullable(defaultIssue.severity()).ifPresent(builder::setSeverity);
170 builder.setManualSeverity(defaultIssue.manualSeverity());
171 ofNullable(defaultIssue.message()).ifPresent(builder::setMessage);
172 ofNullable(defaultIssue.getMessageFormattings()).ifPresent(m -> builder.setMessageFormattings((DbIssues.MessageFormattings) m));
173 ofNullable(defaultIssue.line()).ifPresent(builder::setLine);
174 ofNullable(defaultIssue.gap()).ifPresent(builder::setGap);
175 ofNullable(defaultIssue.effort()).map(Duration::toMinutes).ifPresent(builder::setEffort);
176 builder.setStatus(defaultIssue.status());
177 ofNullable(defaultIssue.resolution()).ifPresent(builder::setResolution);
178 ofNullable(defaultIssue.assignee()).ifPresent(builder::setAssigneeUuid);
179 ofNullable(defaultIssue.assigneeLogin()).ifPresent(builder::setAssigneeLogin);
180 ofNullable(defaultIssue.checksum()).ifPresent(builder::setChecksum);
181 ofNullable(defaultIssue.authorLogin()).ifPresent(builder::setAuthorLogin);
182 defaultIssue.defaultIssueComments().forEach(c -> builder.addComments(toProtoComment(c)));
183 ofNullable(defaultIssue.tags()).ifPresent(t -> builder.setTags(String.join(TAGS_SEPARATOR, t)));
184 ofNullable(defaultIssue.codeVariants()).ifPresent(codeVariant -> builder.setCodeVariants(String.join(TAGS_SEPARATOR, codeVariant)));
185 ofNullable(defaultIssue.getLocations()).ifPresent(l -> builder.setLocations((DbIssues.Locations) l));
186 defaultIssue.getRuleDescriptionContextKey().ifPresent(builder::setRuleDescriptionContextKey);
187 builder.setIsFromExternalRuleEngine(defaultIssue.isFromExternalRuleEngine());
188 builder.setCreationDate(defaultIssue.creationDate().getTime());
189 ofNullable(defaultIssue.updateDate()).map(Date::getTime).ifPresent(builder::setUpdateDate);
190 ofNullable(defaultIssue.closeDate()).map(Date::getTime).ifPresent(builder::setCloseDate);
191 ofNullable(defaultIssue.currentChange()).ifPresent(c -> builder.setCurrentChanges(toProtoIssueChanges(c)));
192 builder.setIsNew(defaultIssue.isNew());
193 builder.setIsOnChangedLine(defaultIssue.isOnChangedLine());
194 builder.setIsPrioritizedRule(defaultIssue.isPrioritizedRule());
195 builder.setIsNewCodeReferenceIssue(defaultIssue.isNewCodeReferenceIssue());
196 builder.setIsCopied(defaultIssue.isCopied());
197 builder.setBeingClosed(defaultIssue.isBeingClosed());
198 builder.setOnDisabledRule(defaultIssue.isOnDisabledRule());
199 builder.setIsChanged(defaultIssue.isChanged());
200 builder.setSendNotifications(defaultIssue.mustSendNotifications());
201 ofNullable(defaultIssue.selectedAt()).ifPresent(builder::setSelectedAt);
202 builder.setQuickFixAvailable(defaultIssue.isQuickFixAvailable());
203 builder.setIsNoLongerNewCodeReferenceIssue(defaultIssue.isNoLongerNewCodeReferenceIssue());
204 defaultIssue.getAnticipatedTransitionUuid().ifPresent(builder::setAnticipatedTransitionUuid);
207 for (Map.Entry<SoftwareQuality, Severity> impact : defaultIssue.impacts().entrySet()) {
208 builder.addImpacts(IssueCache.Impact.newBuilder()
209 .setSoftwareQuality(impact.getKey().name())
210 .setSeverity(impact.getValue().name())
213 for (FieldDiffs fieldDiffs : defaultIssue.changes()) {
214 builder.addChanges(toProtoIssueChanges(fieldDiffs));
217 return builder.build();
220 private static DefaultIssueComment toDefaultIssueComment(IssueCache.Comment comment) {
221 DefaultIssueComment issueComment = new DefaultIssueComment()
222 .setCreatedAt(new Date(comment.getCreatedAt()))
223 .setUpdatedAt(new Date(comment.getUpdatedAt()))
224 .setNew(comment.getIsNew())
225 .setKey(comment.getKey())
226 .setIssueKey(comment.getIssueKey())
227 .setMarkdownText(comment.getMarkdownText());
229 if (comment.hasUserUuid()) {
230 issueComment.setUserUuid(comment.getUserUuid());
235 private static IssueCache.Comment toProtoComment(DefaultIssueComment comment) {
236 IssueCache.Comment.Builder builder = IssueCache.Comment.newBuilder()
237 .setCreatedAt(comment.createdAt().getTime())
238 .setUpdatedAt(comment.updatedAt().getTime())
239 .setIsNew(comment.isNew())
240 .setKey(comment.key())
241 .setIssueKey(comment.issueKey())
242 .setMarkdownText(comment.markdownText());
244 if (comment.userUuid() != null) {
245 builder.setUserUuid(comment.userUuid());
247 return builder.build();
250 private static FieldDiffs toDefaultIssueChanges(IssueCache.FieldDiffs fieldDiffs) {
251 FieldDiffs defaultIssueFieldDiffs = new FieldDiffs()
252 .setUserUuid(fieldDiffs.getUserUuid())
253 .setCreationDate(new Date(fieldDiffs.getCreationDate()));
255 if (fieldDiffs.hasIssueKey()) {
256 defaultIssueFieldDiffs.setIssueKey(fieldDiffs.getIssueKey());
259 for (Map.Entry<String, IssueCache.Diff> e : fieldDiffs.getDiffsMap().entrySet()) {
260 defaultIssueFieldDiffs.setDiff(e.getKey(),
261 e.getValue().hasOldValue() ? e.getValue().getOldValue() : null,
262 e.getValue().hasNewValue() ? e.getValue().getNewValue() : null);
265 return defaultIssueFieldDiffs;
268 private static IssueCache.FieldDiffs toProtoIssueChanges(FieldDiffs fieldDiffs) {
269 IssueCache.FieldDiffs.Builder builder = IssueCache.FieldDiffs.newBuilder()
270 .setCreationDate(fieldDiffs.creationDate().getTime());
272 fieldDiffs.issueKey().ifPresent(builder::setIssueKey);
273 fieldDiffs.userUuid().ifPresent(builder::setUserUuid);
275 for (Map.Entry<String, FieldDiffs.Diff> e : fieldDiffs.diffs().entrySet()) {
276 IssueCache.Diff.Builder diffBuilder = IssueCache.Diff.newBuilder();
277 Serializable oldValue = e.getValue().oldValue();
278 if (oldValue != null) {
279 diffBuilder.setOldValue(oldValue.toString());
281 Serializable newValue = e.getValue().newValue();
282 if (newValue != null) {
283 diffBuilder.setNewValue(newValue.toString());
286 builder.putDiffs(e.getKey(), diffBuilder.build());
289 return builder.build();
292 private class ProtoCacheAppender implements CacheAppender<DefaultIssue> {
293 private final OutputStream out;
294 private final IssueCache.Issue.Builder builder;
296 private ProtoCacheAppender() throws FileNotFoundException {
297 this.out = new BufferedOutputStream(new FileOutputStream(file, true));
298 this.builder = IssueCache.Issue.newBuilder();
302 public CacheAppender append(DefaultIssue object) {
303 Protobuf.writeStream(Collections.singleton(toProto(builder, object)), out);
308 public void close() {