]> source.dussan.org Git - sonarqube.git/blob
3dc527a31ad12afa0304058aa93ec389891be9f4
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.util.cache;
21
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;
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.OutputStream;
30 import java.util.Collections;
31 import java.util.Date;
32 import java.util.Map;
33 import javax.annotation.CheckForNull;
34 import org.sonar.api.rule.RuleKey;
35 import org.sonar.api.rules.RuleType;
36 import org.sonar.api.utils.Duration;
37 import org.sonar.api.utils.System2;
38 import org.sonar.core.issue.DefaultIssue;
39 import org.sonar.core.issue.DefaultIssueComment;
40 import org.sonar.core.issue.FieldDiffs;
41 import org.sonar.core.util.CloseableIterator;
42 import org.sonar.core.util.Protobuf;
43 import org.sonar.db.protobuf.DbIssues;
44
45 import static java.util.Optional.ofNullable;
46
47 public class ProtobufIssueDiskCache implements DiskCache<DefaultIssue> {
48   private static final String TAGS_SEPARATOR = ",";
49   private static final Splitter TAGS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
50
51   private final File file;
52   private final System2 system2;
53
54   public ProtobufIssueDiskCache(File file, System2 system2) {
55     this.file = file;
56     this.system2 = system2;
57   }
58
59   @Override
60   public long fileSize() {
61     return file.length();
62   }
63
64   @Override
65   public CacheAppender<DefaultIssue> newAppender() {
66     try {
67       return new ProtoCacheAppender();
68     } catch (FileNotFoundException e) {
69       throw new IllegalStateException(e);
70     }
71   }
72
73   @Override
74   public CloseableIterator<DefaultIssue> traverse() {
75     CloseableIterator<IssueCache.Issue> protoIterator = Protobuf.readStream(file, IssueCache.Issue.parser());
76     return new CloseableIterator<DefaultIssue>() {
77       @CheckForNull
78       @Override
79       protected DefaultIssue doNext() {
80         if (protoIterator.hasNext()) {
81           return toDefaultIssue(protoIterator.next());
82         }
83         return null;
84       }
85
86       @Override
87       protected void doClose() {
88         protoIterator.close();
89       }
90     };
91   }
92
93   private static DefaultIssue toDefaultIssue(IssueCache.Issue next) {
94     DefaultIssue defaultIssue = new DefaultIssue();
95     defaultIssue.setKey(next.getKey());
96     defaultIssue.setType(RuleType.valueOf(next.getRuleType()));
97     defaultIssue.setComponentUuid(next.hasComponentUuid() ? next.getComponentUuid() : null);
98     defaultIssue.setComponentKey(next.getComponentKey());
99     defaultIssue.setModuleUuid(next.hasModuleUuid() ? next.getModuleUuid() : null);
100     defaultIssue.setModuleUuidPath(next.hasModuleUuidPath() ? next.getModuleUuidPath() : null);
101     defaultIssue.setProjectUuid(next.getProjectUuid());
102     defaultIssue.setProjectKey(next.getProjectKey());
103     defaultIssue.setRuleKey(RuleKey.parse(next.getRuleKey()));
104     defaultIssue.setLanguage(next.hasLanguage() ? next.getLanguage() : null);
105     defaultIssue.setSeverity(next.hasSeverity() ? next.getSeverity() : null);
106     defaultIssue.setManualSeverity(next.getManualSeverity());
107     defaultIssue.setMessage(next.hasMessage() ? next.getMessage() : null);
108     defaultIssue.setLine(next.hasLine() ? next.getLine() : null);
109     defaultIssue.setGap(next.hasGap() ? next.getGap() : null);
110     defaultIssue.setEffort(next.hasEffort() ? Duration.create(next.getEffort()) : null);
111     defaultIssue.setStatus(next.getStatus());
112     defaultIssue.setResolution(next.hasResolution() ? next.getResolution() : null);
113     defaultIssue.setAssigneeUuid(next.hasAssigneeUuid() ? next.getAssigneeUuid() : null);
114     defaultIssue.setChecksum(next.hasChecksum() ? next.getChecksum() : null);
115     defaultIssue.setAttributes(next.getAttributesMap());
116     defaultIssue.setAuthorLogin(next.hasAuthorLogin() ? next.getAuthorLogin() : null);
117     next.getCommentsList().forEach(c -> defaultIssue.addComment(toDefaultIssueComment(c)));
118     defaultIssue.setTags(ImmutableSet.copyOf(TAGS_SPLITTER.split(next.hasTags() ? "" : next.getTags())));
119     defaultIssue.setLocations(next.hasLocations() ? next.getLocations() : null);
120     defaultIssue.setIsFromExternalRuleEngine(next.getIsFromExternalRuleEngine());
121     defaultIssue.setCreationDate(new Date(next.getCreationDate()));
122     defaultIssue.setUpdateDate(next.hasUpdateDate() ? new Date(next.getUpdateDate()) : null);
123     defaultIssue.setCloseDate(next.hasCloseDate() ? new Date(next.getCloseDate()) : null);
124     defaultIssue.setCurrentChangeWithoutAddChange(next.hasCurrentChanges() ? toDefaultIssueChanges(next.getCurrentChanges()) : null);
125     defaultIssue.setNew(next.getIsNew());
126     defaultIssue.setCopied(next.getIsCopied());
127     defaultIssue.setBeingClosed(next.getBeingClosed());
128     defaultIssue.setOnDisabledRule(next.getOnDisabledRule());
129     defaultIssue.setChanged(next.getIsChanged());
130     defaultIssue.setSendNotifications(next.getSendNotifications());
131     defaultIssue.setSelectedAt(next.hasSelectedAt() ? next.getSelectedAt() : null);
132
133     for (IssueCache.FieldDiffs protoFieldDiffs : next.getChangesList()) {
134       defaultIssue.addChange(toDefaultIssueChanges(protoFieldDiffs));
135     }
136
137     return defaultIssue;
138   }
139
140   private static IssueCache.Issue toProto(IssueCache.Issue.Builder builder, DefaultIssue defaultIssue) {
141     builder.clear();
142     builder.setKey(defaultIssue.key());
143     builder.setRuleType(defaultIssue.type().getDbConstant());
144     ofNullable(defaultIssue.componentUuid()).ifPresent(builder::setComponentUuid);
145     builder.setComponentKey(defaultIssue.componentKey());
146     ofNullable(defaultIssue.moduleUuid()).ifPresent(builder::setModuleUuid);
147     ofNullable(defaultIssue.moduleUuidPath()).ifPresent(builder::setModuleUuidPath);
148     builder.setProjectUuid(defaultIssue.projectUuid());
149     builder.setProjectKey(defaultIssue.projectKey());
150     builder.setRuleKey(defaultIssue.ruleKey().toString());
151     ofNullable(defaultIssue.language()).ifPresent(builder::setLanguage);
152     ofNullable(defaultIssue.severity()).ifPresent(builder::setSeverity);
153     builder.setManualSeverity(defaultIssue.manualSeverity());
154     ofNullable(defaultIssue.message()).ifPresent(builder::setMessage);
155     ofNullable(defaultIssue.line()).ifPresent(builder::setLine);
156     ofNullable(defaultIssue.gap()).ifPresent(builder::setGap);
157     ofNullable(defaultIssue.effort()).map(Duration::toMinutes).ifPresent(builder::setEffort);
158     builder.setStatus(defaultIssue.status());
159     ofNullable(defaultIssue.resolution()).ifPresent(builder::setResolution);
160     ofNullable(defaultIssue.assignee()).ifPresent(builder::setAssigneeUuid);
161     ofNullable(defaultIssue.checksum()).ifPresent(builder::setChecksum);
162     ofNullable(defaultIssue.attributes()).ifPresent(builder::putAllAttributes);
163     ofNullable(defaultIssue.authorLogin()).ifPresent(builder::setAuthorLogin);
164     defaultIssue.defaultIssueComments().forEach(c -> builder.addComments(toProtoComment(c)));
165     ofNullable(defaultIssue.tags()).ifPresent(t -> builder.setTags(String.join(TAGS_SEPARATOR, t)));
166     ofNullable(defaultIssue.getLocations()).ifPresent(l -> builder.setLocations((DbIssues.Locations) l));
167     builder.setIsFromExternalRuleEngine(defaultIssue.isFromExternalRuleEngine());
168     builder.setCreationDate(defaultIssue.creationDate().getTime());
169     ofNullable(defaultIssue.updateDate()).map(Date::getTime).ifPresent(builder::setUpdateDate);
170     ofNullable(defaultIssue.closeDate()).map(Date::getTime).ifPresent(builder::setCloseDate);
171     ofNullable(defaultIssue.currentChange()).ifPresent(c -> builder.setCurrentChanges(toProtoIssueChanges(c)));
172     builder.setIsNew(defaultIssue.isNew());
173     builder.setIsCopied(defaultIssue.isCopied());
174     builder.setBeingClosed(defaultIssue.isBeingClosed());
175     builder.setOnDisabledRule(defaultIssue.isOnDisabledRule());
176     builder.setIsChanged(defaultIssue.isChanged());
177     builder.setSendNotifications(defaultIssue.mustSendNotifications());
178     ofNullable(defaultIssue.selectedAt()).ifPresent(builder::setSelectedAt);
179
180     for (FieldDiffs fieldDiffs : defaultIssue.changes()) {
181       builder.addChanges(toProtoIssueChanges(fieldDiffs));
182     }
183
184     return builder.build();
185   }
186
187   private static DefaultIssueComment toDefaultIssueComment(IssueCache.Comment comment) {
188     DefaultIssueComment issueComment = new DefaultIssueComment()
189       .setCreatedAt(new Date(comment.getCreatedAt()))
190       .setUpdatedAt(new Date(comment.getUpdatedAt()))
191       .setNew(comment.getIsNew())
192       .setKey(comment.getKey())
193       .setIssueKey(comment.getIssueKey())
194       .setMarkdownText(comment.getMarkdownText());
195
196     if (comment.hasUserUuid()) {
197       issueComment.setUserUuid(comment.getUserUuid());
198     }
199     return issueComment;
200   }
201
202   private static IssueCache.Comment toProtoComment(DefaultIssueComment comment) {
203     IssueCache.Comment.Builder builder = IssueCache.Comment.newBuilder()
204       .setCreatedAt(comment.createdAt().getTime())
205       .setUpdatedAt(comment.updatedAt().getTime())
206       .setIsNew(comment.isNew())
207       .setKey(comment.key())
208       .setIssueKey(comment.issueKey())
209       .setMarkdownText(comment.markdownText());
210
211     if (comment.userUuid() != null) {
212       builder.setUserUuid(comment.userUuid());
213     }
214     return builder.build();
215   }
216
217   private static FieldDiffs toDefaultIssueChanges(IssueCache.FieldDiffs fieldDiffs) {
218     FieldDiffs defaultIssueFieldDiffs = new FieldDiffs()
219       .setUserUuid(fieldDiffs.getUserUuid())
220       .setCreationDate(new Date(fieldDiffs.getCreationDate()));
221
222     if (fieldDiffs.hasIssueKey()) {
223       defaultIssueFieldDiffs.setIssueKey(fieldDiffs.getIssueKey());
224     }
225
226     for (Map.Entry<String, IssueCache.Diff> e : fieldDiffs.getDiffsMap().entrySet()) {
227       defaultIssueFieldDiffs.setDiff(e.getKey(),
228         e.getValue().hasOldValue() ? e.getValue().getOldValue() : null,
229         e.getValue().hasNewValue() ? e.getValue().getNewValue() : null);
230     }
231
232     return defaultIssueFieldDiffs;
233   }
234
235   private static IssueCache.FieldDiffs toProtoIssueChanges(FieldDiffs fieldDiffs) {
236     IssueCache.FieldDiffs.Builder builder = IssueCache.FieldDiffs.newBuilder()
237       .setCreationDate(fieldDiffs.creationDate().getTime());
238
239     if (fieldDiffs.issueKey() != null) {
240       builder.setIssueKey(fieldDiffs.issueKey());
241     }
242
243     String userUuid = fieldDiffs.userUuid();
244     if (userUuid != null) {
245       builder.setUserUuid(userUuid);
246     }
247
248     for (Map.Entry<String, FieldDiffs.Diff> e : fieldDiffs.diffs().entrySet()) {
249       IssueCache.Diff.Builder diffBuilder = IssueCache.Diff.newBuilder();
250       if (e.getValue().oldValue() != null) {
251         diffBuilder.setOldValue(e.getValue().oldValue().toString());
252       }
253       if (e.getValue().newValue() != null) {
254         diffBuilder.setNewValue(e.getValue().newValue().toString());
255
256       }
257       builder.putDiffs(e.getKey(), diffBuilder.build());
258     }
259
260     return builder.build();
261   }
262
263   private class ProtoCacheAppender implements CacheAppender<DefaultIssue> {
264     private final OutputStream out;
265     private final IssueCache.Issue.Builder builder;
266
267     private ProtoCacheAppender() throws FileNotFoundException {
268       this.out = new BufferedOutputStream(new FileOutputStream(file, true));
269       this.builder = IssueCache.Issue.newBuilder();
270     }
271
272     @Override
273     public CacheAppender append(DefaultIssue object) {
274       Protobuf.writeStream(Collections.singleton(toProto(builder, object)), out);
275       return this;
276     }
277
278     @Override
279     public void close() {
280       system2.close(out);
281     }
282   }
283 }