3 * Copyright (C) 2009-2020 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.base.Joiner;
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.rule.RuleKey;
36 import org.sonar.api.rules.RuleType;
37 import org.sonar.api.utils.Duration;
38 import org.sonar.api.utils.System2;
39 import org.sonar.core.issue.DefaultIssue;
40 import org.sonar.core.issue.DefaultIssueComment;
41 import org.sonar.core.issue.FieldDiffs;
42 import org.sonar.core.util.CloseableIterator;
43 import org.sonar.core.util.Protobuf;
44 import org.sonar.db.protobuf.DbIssues;
46 import static java.util.Optional.ofNullable;
48 public class ProtobufIssueDiskCache implements DiskCache<DefaultIssue> {
49 private static final String TAGS_SEPARATOR = ",";
50 private static final Splitter TAGS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
52 private final File file;
53 private final System2 system2;
55 public ProtobufIssueDiskCache(File file, System2 system2) {
57 this.system2 = system2;
61 public long fileSize() {
66 public CacheAppender<DefaultIssue> newAppender() {
68 return new ProtoCacheAppender();
69 } catch (FileNotFoundException e) {
70 throw new IllegalStateException(e);
75 public CloseableIterator<DefaultIssue> traverse() {
76 CloseableIterator<IssueCache.Issue> protoIterator = Protobuf.readStream(file, IssueCache.Issue.parser());
77 return new CloseableIterator<DefaultIssue>() {
80 protected DefaultIssue doNext() {
81 if (protoIterator.hasNext()) {
82 return toDefaultIssue(protoIterator.next());
88 protected void doClose() {
89 protoIterator.close();
94 private static DefaultIssue toDefaultIssue(IssueCache.Issue next) {
95 DefaultIssue defaultIssue = new DefaultIssue();
96 defaultIssue.setKey(next.getKey());
97 defaultIssue.setType(RuleType.valueOf(next.getRuleType()));
98 defaultIssue.setComponentUuid(next.hasComponentUuid() ? next.getComponentUuid() : null);
99 defaultIssue.setComponentKey(next.getComponentKey());
100 defaultIssue.setModuleUuid(next.hasModuleUuid() ? next.getModuleUuid() : null);
101 defaultIssue.setModuleUuidPath(next.hasModuleUuidPath() ? next.getModuleUuidPath() : null);
102 defaultIssue.setProjectUuid(next.getProjectUuid());
103 defaultIssue.setProjectKey(next.getProjectKey());
104 defaultIssue.setRuleKey(RuleKey.parse(next.getRuleKey()));
105 defaultIssue.setLanguage(next.hasLanguage() ? next.getLanguage() : null);
106 defaultIssue.setSeverity(next.hasSeverity() ? next.getSeverity() : null);
107 defaultIssue.setManualSeverity(next.getManualSeverity());
108 defaultIssue.setMessage(next.hasMessage() ? next.getMessage() : null);
109 defaultIssue.setLine(next.hasLine() ? next.getLine() : null);
110 defaultIssue.setGap(next.hasGap() ? next.getGap() : null);
111 defaultIssue.setEffort(next.hasEffort() ? Duration.create(next.getEffort()) : null);
112 defaultIssue.setStatus(next.getStatus());
113 defaultIssue.setResolution(next.hasResolution() ? next.getResolution() : null);
114 defaultIssue.setAssigneeUuid(next.hasAssigneeUuid() ? next.getAssigneeUuid() : null);
115 defaultIssue.setChecksum(next.hasChecksum() ? next.getChecksum() : null);
116 defaultIssue.setAttributes(next.getAttributesMap());
117 defaultIssue.setAuthorLogin(next.hasAuthorLogin() ? next.getAuthorLogin() : null);
118 next.getCommentsList().forEach(c -> defaultIssue.addComment(toDefaultIssueComment(c)));
119 defaultIssue.setTags(ImmutableSet.copyOf(TAGS_SPLITTER.split(next.hasTags() ? "" : next.getTags())));
120 defaultIssue.setLocations(next.hasLocations() ? next.getLocations() : null);
121 defaultIssue.setIsFromExternalRuleEngine(next.getIsFromExternalRuleEngine());
122 defaultIssue.setCreationDate(new Date(next.getCreationDate()));
123 defaultIssue.setUpdateDate(next.hasUpdateDate() ? new Date(next.getUpdateDate()) : null);
124 defaultIssue.setCloseDate(next.hasCloseDate() ? new Date(next.getCloseDate()) : null);
125 defaultIssue.setCurrentChangeWithoutAddChange(next.hasCurrentChanges() ? toDefaultIssueChanges(next.getCurrentChanges()) : null);
126 defaultIssue.setNew(next.getIsNew());
127 defaultIssue.setCopied(next.getIsCopied());
128 defaultIssue.setBeingClosed(next.getBeingClosed());
129 defaultIssue.setOnDisabledRule(next.getOnDisabledRule());
130 defaultIssue.setChanged(next.getIsChanged());
131 defaultIssue.setSendNotifications(next.getSendNotifications());
132 defaultIssue.setSelectedAt(next.hasSelectedAt() ? next.getSelectedAt() : null);
134 for (IssueCache.FieldDiffs protoFieldDiffs : next.getChangesList()) {
135 defaultIssue.addChange(toDefaultIssueChanges(protoFieldDiffs));
141 private static IssueCache.Issue toProto(IssueCache.Issue.Builder builder, DefaultIssue defaultIssue) {
143 builder.setKey(defaultIssue.key());
144 builder.setRuleType(defaultIssue.type().getDbConstant());
145 ofNullable(defaultIssue.componentUuid()).ifPresent(builder::setComponentUuid);
146 builder.setComponentKey(defaultIssue.componentKey());
147 ofNullable(defaultIssue.moduleUuid()).ifPresent(builder::setModuleUuid);
148 ofNullable(defaultIssue.moduleUuidPath()).ifPresent(builder::setModuleUuidPath);
149 builder.setProjectUuid(defaultIssue.projectUuid());
150 builder.setProjectKey(defaultIssue.projectKey());
151 builder.setRuleKey(defaultIssue.ruleKey().toString());
152 ofNullable(defaultIssue.language()).ifPresent(builder::setLanguage);
153 ofNullable(defaultIssue.severity()).ifPresent(builder::setSeverity);
154 builder.setManualSeverity(defaultIssue.manualSeverity());
155 ofNullable(defaultIssue.message()).ifPresent(builder::setMessage);
156 ofNullable(defaultIssue.line()).ifPresent(builder::setLine);
157 ofNullable(defaultIssue.gap()).ifPresent(builder::setGap);
158 ofNullable(defaultIssue.effort()).map(Duration::toMinutes).ifPresent(builder::setEffort);
159 builder.setStatus(defaultIssue.status());
160 ofNullable(defaultIssue.resolution()).ifPresent(builder::setResolution);
161 ofNullable(defaultIssue.assignee()).ifPresent(builder::setAssigneeUuid);
162 ofNullable(defaultIssue.checksum()).ifPresent(builder::setChecksum);
163 ofNullable(defaultIssue.attributes()).ifPresent(builder::putAllAttributes);
164 ofNullable(defaultIssue.authorLogin()).ifPresent(builder::setAuthorLogin);
165 defaultIssue.defaultIssueComments().forEach(c -> builder.addComments(toProtoComment(c)));
166 ofNullable(defaultIssue.tags()).ifPresent(t -> builder.setTags(String.join(TAGS_SEPARATOR, t)));
167 ofNullable(defaultIssue.getLocations()).ifPresent(l -> builder.setLocations((DbIssues.Locations) l));
168 builder.setIsFromExternalRuleEngine(defaultIssue.isFromExternalRuleEngine());
169 builder.setCreationDate(defaultIssue.creationDate().getTime());
170 ofNullable(defaultIssue.updateDate()).map(Date::getTime).ifPresent(builder::setUpdateDate);
171 ofNullable(defaultIssue.closeDate()).map(Date::getTime).ifPresent(builder::setCloseDate);
172 ofNullable(defaultIssue.currentChange()).ifPresent(c -> builder.setCurrentChanges(toProtoIssueChanges(c)));
173 builder.setIsNew(defaultIssue.isNew());
174 builder.setIsCopied(defaultIssue.isCopied());
175 builder.setBeingClosed(defaultIssue.isBeingClosed());
176 builder.setOnDisabledRule(defaultIssue.isOnDisabledRule());
177 builder.setIsChanged(defaultIssue.isChanged());
178 builder.setSendNotifications(defaultIssue.mustSendNotifications());
179 ofNullable(defaultIssue.selectedAt()).ifPresent(builder::setSelectedAt);
181 for (FieldDiffs fieldDiffs : defaultIssue.changes()) {
182 builder.addChanges(toProtoIssueChanges(fieldDiffs));
185 return builder.build();
188 private static DefaultIssueComment toDefaultIssueComment(IssueCache.Comment comment) {
189 DefaultIssueComment issueComment = new DefaultIssueComment()
190 .setCreatedAt(new Date(comment.getCreatedAt()))
191 .setUpdatedAt(new Date(comment.getUpdatedAt()))
192 .setNew(comment.getIsNew())
193 .setKey(comment.getKey())
194 .setIssueKey(comment.getIssueKey())
195 .setMarkdownText(comment.getMarkdownText());
197 if (comment.hasUserUuid()) {
198 issueComment.setUserUuid(comment.getUserUuid());
203 private static IssueCache.Comment toProtoComment(DefaultIssueComment comment) {
204 IssueCache.Comment.Builder builder = IssueCache.Comment.newBuilder()
205 .setCreatedAt(comment.createdAt().getTime())
206 .setUpdatedAt(comment.updatedAt().getTime())
207 .setIsNew(comment.isNew())
208 .setKey(comment.key())
209 .setIssueKey(comment.issueKey())
210 .setMarkdownText(comment.markdownText());
212 if (comment.userUuid() != null) {
213 builder.setUserUuid(comment.userUuid());
215 return builder.build();
218 private static FieldDiffs toDefaultIssueChanges(IssueCache.FieldDiffs fieldDiffs) {
219 FieldDiffs defaultIssueFieldDiffs = new FieldDiffs()
220 .setUserUuid(fieldDiffs.getUserUuid())
221 .setCreationDate(new Date(fieldDiffs.getCreationDate()));
223 if (fieldDiffs.hasIssueKey()) {
224 defaultIssueFieldDiffs.setIssueKey(fieldDiffs.getIssueKey());
227 for (Map.Entry<String, IssueCache.Diff> e : fieldDiffs.getDiffsMap().entrySet()) {
228 defaultIssueFieldDiffs.setDiff(e.getKey(),
229 e.getValue().hasOldValue() ? e.getValue().getOldValue() : null,
230 e.getValue().hasNewValue() ? e.getValue().getNewValue() : null);
233 return defaultIssueFieldDiffs;
236 private static IssueCache.FieldDiffs toProtoIssueChanges(FieldDiffs fieldDiffs) {
237 IssueCache.FieldDiffs.Builder builder = IssueCache.FieldDiffs.newBuilder()
238 .setCreationDate(fieldDiffs.creationDate().getTime());
240 if (fieldDiffs.issueKey() != null) {
241 builder.setIssueKey(fieldDiffs.issueKey());
244 String userUuid = fieldDiffs.userUuid();
245 if (userUuid != null) {
246 builder.setUserUuid(userUuid);
249 for (Map.Entry<String, FieldDiffs.Diff> e : fieldDiffs.diffs().entrySet()) {
250 IssueCache.Diff.Builder diffBuilder = IssueCache.Diff.newBuilder();
251 Serializable oldValue = e.getValue().oldValue();
252 if (oldValue != null) {
253 diffBuilder.setOldValue(oldValue.toString());
255 Serializable newValue = e.getValue().newValue();
256 if (newValue != null) {
257 diffBuilder.setNewValue(newValue.toString());
260 builder.putDiffs(e.getKey(), diffBuilder.build());
263 return builder.build();
266 private class ProtoCacheAppender implements CacheAppender<DefaultIssue> {
267 private final OutputStream out;
268 private final IssueCache.Issue.Builder builder;
270 private ProtoCacheAppender() throws FileNotFoundException {
271 this.out = new BufferedOutputStream(new FileOutputStream(file, true));
272 this.builder = IssueCache.Issue.newBuilder();
276 public CacheAppender append(DefaultIssue object) {
277 Protobuf.writeStream(Collections.singleton(toProto(builder, object)), out);
282 public void close() {