3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
22 import static com.google.common.collect.FluentIterable.from;
23 import static java.util.Optional.ofNullable;
24 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
25 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
26 import static org.sonar.db.component.ComponentDto.formatUuidPathFromParent;
27 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
29 import java.util.Collection;
30 import java.util.Date;
32 import java.util.Optional;
34 import java.util.function.Function;
35 import java.util.stream.Collectors;
37 import javax.annotation.CheckForNull;
38 import javax.annotation.Nonnull;
39 import javax.annotation.Nullable;
41 import org.apache.commons.io.FilenameUtils;
42 import org.apache.commons.lang.StringUtils;
43 import org.sonar.api.resources.Qualifiers;
44 import org.sonar.api.resources.Scopes;
45 import org.sonar.api.utils.System2;
46 import org.sonar.core.util.stream.MoreCollectors;
47 import org.sonar.db.DbClient;
48 import org.sonar.db.DbSession;
49 import org.sonar.db.component.ComponentDto;
50 import org.sonar.db.component.ComponentUpdateDto;
51 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
52 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
53 import org.sonar.server.computation.task.projectanalysis.component.BranchPersister;
54 import org.sonar.server.computation.task.projectanalysis.component.Component;
55 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
56 import org.sonar.server.computation.task.projectanalysis.component.DbIdsRepositoryImpl;
57 import org.sonar.server.computation.task.projectanalysis.component.MutableDbIdsRepository;
58 import org.sonar.server.computation.task.projectanalysis.component.MutableDisabledComponentsHolder;
59 import org.sonar.server.computation.task.projectanalysis.component.PathAwareCrawler;
60 import org.sonar.server.computation.task.projectanalysis.component.PathAwareVisitor;
61 import org.sonar.server.computation.task.projectanalysis.component.PathAwareVisitorAdapter;
62 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
63 import org.sonar.server.computation.task.step.ComputationStep;
65 import com.google.common.base.Predicate;
68 * Persist report components
69 * Also feed the components cache {@link DbIdsRepositoryImpl} with component ids
71 public class PersistComponentsStep implements ComputationStep {
72 private final DbClient dbClient;
73 private final TreeRootHolder treeRootHolder;
74 private final MutableDbIdsRepository dbIdsRepository;
75 private final System2 system2;
76 private final MutableDisabledComponentsHolder disabledComponentsHolder;
77 private final AnalysisMetadataHolder analysisMetadataHolder;
78 private final BranchPersister branchPersister;
80 public PersistComponentsStep(DbClient dbClient, TreeRootHolder treeRootHolder,
81 MutableDbIdsRepository dbIdsRepository, System2 system2,
82 MutableDisabledComponentsHolder disabledComponentsHolder, AnalysisMetadataHolder analysisMetadataHolder,
83 BranchPersister branchPersister) {
84 this.dbClient = dbClient;
85 this.treeRootHolder = treeRootHolder;
86 this.dbIdsRepository = dbIdsRepository;
87 this.system2 = system2;
88 this.disabledComponentsHolder = disabledComponentsHolder;
89 this.analysisMetadataHolder = analysisMetadataHolder;
90 this.branchPersister = branchPersister;
94 public String getDescription() {
95 return "Persist components";
99 public void execute() {
100 try (DbSession dbSession = dbClient.openSession(false)) {
101 branchPersister.persist(dbSession);
103 String projectUuid = treeRootHolder.getRoot().getUuid();
105 // safeguard, reset all rows to b-changed=false
106 dbClient.componentDao().resetBChangedForRootComponentUuid(dbSession, projectUuid);
108 Map<String, ComponentDto> existingDtosByKeys = indexExistingDtosByKey(dbSession);
109 boolean isRootPrivate = isRootPrivate(treeRootHolder.getRoot(), existingDtosByKeys);
110 String mainBranchProjectUuid = loadProjectUuidOfMainBranch();
112 // Insert or update the components in database. They are removed from existingDtosByKeys
114 new PathAwareCrawler<>(new PersistComponentStepsVisitor(existingDtosByKeys, dbSession, mainBranchProjectUuid))
115 .visit(treeRootHolder.getRoot());
117 disableRemainingComponents(dbSession, existingDtosByKeys.values());
118 ensureConsistentVisibility(dbSession, projectUuid, isRootPrivate);
125 * See {@link ComponentDto#mainBranchProjectUuid} : value is null on main branches, otherwise it is
126 * the uuid of the main branch.
129 private String loadProjectUuidOfMainBranch() {
130 Optional<Branch> branch = analysisMetadataHolder.getBranch();
131 if (branch.isPresent() && !branch.get().isMain()) {
132 return analysisMetadataHolder.getProject().getUuid();
137 private void disableRemainingComponents(DbSession dbSession, Collection<ComponentDto> dtos) {
138 Set<String> uuids = dtos.stream()
139 .filter(ComponentDto::isEnabled)
140 .map(ComponentDto::uuid)
141 .collect(MoreCollectors.toSet(dtos.size()));
142 dbClient.componentDao().updateBEnabledToFalse(dbSession, uuids);
143 disabledComponentsHolder.setUuids(uuids);
146 private void ensureConsistentVisibility(DbSession dbSession, String projectUuid, boolean isRootPrivate) {
147 dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, projectUuid, isRootPrivate);
150 private static boolean isRootPrivate(Component root, Map<String, ComponentDto> existingDtosByKeys) {
151 String rootKey = root.getKey();
152 ComponentDto rootDto = existingDtosByKeys.get(rootKey);
153 if (rootDto == null) {
154 if (Component.Type.VIEW == root.getType()) {
157 throw new IllegalStateException(String.format("The project '%s' is not stored in the database, during a project analysis.", rootKey));
159 return rootDto.isPrivate();
163 * Returns a mutable map of the components currently persisted in database for the project, including
164 * disabled components.
166 private Map<String, ComponentDto> indexExistingDtosByKey(DbSession session) {
167 return dbClient.componentDao().selectAllComponentsFromProjectKey(session, treeRootHolder.getRoot().getKey())
169 .collect(Collectors.toMap(ComponentDto::getDbKey, Function.identity()));
172 private class PersistComponentStepsVisitor extends PathAwareVisitorAdapter<ComponentDtoHolder> {
174 private final Map<String, ComponentDto> existingComponentDtosByKey;
175 private final DbSession dbSession;
177 private final String mainBranchProjectUuid;
179 PersistComponentStepsVisitor(Map<String, ComponentDto> existingComponentDtosByKey, DbSession dbSession, @Nullable String mainBranchProjectUuid) {
181 CrawlerDepthLimit.LEAVES,
183 new SimpleStackElementFactory<ComponentDtoHolder>() {
185 public ComponentDtoHolder createForAny(Component component) {
186 return new ComponentDtoHolder();
190 public ComponentDtoHolder createForFile(Component file) {
191 // no need to create holder for file since they are always leaves of the Component tree
196 public ComponentDtoHolder createForProjectView(Component projectView) {
197 // no need to create holder for file since they are always leaves of the Component tree
201 this.existingComponentDtosByKey = existingComponentDtosByKey;
202 this.dbSession = dbSession;
203 this.mainBranchProjectUuid = mainBranchProjectUuid;
207 public void visitProject(Component project, Path<ComponentDtoHolder> path) {
208 ComponentDto dto = createForProject(project);
209 path.current().setDto(persistAndPopulateCache(project, dto));
213 public void visitModule(Component module, Path<ComponentDtoHolder> path) {
214 ComponentDto dto = createForModule(module, path);
215 path.current().setDto(persistAndPopulateCache(module, dto));
219 public void visitDirectory(Component directory, Path<ComponentDtoHolder> path) {
220 ComponentDto dto = createForDirectory(directory, path);
221 path.current().setDto(persistAndPopulateCache(directory, dto));
225 public void visitFile(Component file, Path<ComponentDtoHolder> path) {
226 ComponentDto dto = createForFile(file, path);
227 persistAndPopulateCache(file, dto);
231 public void visitView(Component view, Path<ComponentDtoHolder> path) {
232 ComponentDto dto = createForView(view);
233 path.current().setDto(persistAndPopulateCache(view, dto));
237 public void visitSubView(Component subView, Path<ComponentDtoHolder> path) {
238 ComponentDto dto = createForSubView(subView, path);
239 path.current().setDto(persistAndPopulateCache(subView, dto));
243 public void visitProjectView(Component projectView, Path<ComponentDtoHolder> path) {
244 ComponentDto dto = createForProjectView(projectView, path);
245 persistAndPopulateCache(projectView, dto);
248 private ComponentDto persistAndPopulateCache(Component component, ComponentDto dto) {
249 ComponentDto projectDto = persistComponent(dto);
250 addToCache(component, projectDto);
254 private ComponentDto persistComponent(ComponentDto componentDto) {
255 ComponentDto existingComponent = existingComponentDtosByKey.remove(componentDto.getDbKey());
256 if (existingComponent == null) {
257 dbClient.componentDao().insert(dbSession, componentDto);
260 Optional<ComponentUpdateDto> update = compareForUpdate(existingComponent, componentDto);
261 if (update.isPresent()) {
262 ComponentUpdateDto updateDto = update.get();
263 dbClient.componentDao().update(dbSession, updateDto);
265 // update the fields in memory in order the PathAwareVisitor.Path
267 existingComponent.setCopyComponentUuid(updateDto.getBCopyComponentUuid());
268 existingComponent.setDescription(updateDto.getBDescription());
269 existingComponent.setEnabled(updateDto.isBEnabled());
270 existingComponent.setUuidPath(updateDto.getBUuidPath());
271 existingComponent.setLanguage(updateDto.getBLanguage());
272 existingComponent.setLongName(updateDto.getBLongName());
273 existingComponent.setModuleUuid(updateDto.getBModuleUuid());
274 existingComponent.setModuleUuidPath(updateDto.getBModuleUuidPath());
275 existingComponent.setName(updateDto.getBName());
276 existingComponent.setPath(updateDto.getBPath());
277 existingComponent.setQualifier(updateDto.getBQualifier());
279 return existingComponent;
282 private void addToCache(Component component, ComponentDto componentDto) {
283 dbIdsRepository.setComponentId(component, componentDto.getId());
286 public ComponentDto createForProject(Component project) {
287 ComponentDto res = createBase(project);
289 res.setScope(Scopes.PROJECT);
290 res.setQualifier(Qualifiers.PROJECT);
291 res.setName(project.getName());
292 res.setLongName(res.name());
293 res.setDescription(project.getDescription());
295 res.setProjectUuid(res.uuid());
296 res.setRootUuid(res.uuid());
297 res.setUuidPath(UUID_PATH_OF_ROOT);
298 res.setModuleUuidPath(UUID_PATH_SEPARATOR + res.uuid() + UUID_PATH_SEPARATOR);
303 public ComponentDto createForModule(Component module, PathAwareVisitor.Path<ComponentDtoHolder> path) {
304 ComponentDto res = createBase(module);
306 res.setScope(Scopes.PROJECT);
307 res.setQualifier(Qualifiers.MODULE);
308 res.setName(module.getName());
309 res.setLongName(res.name());
310 res.setPath(module.getReportAttributes().getPath());
311 res.setDescription(module.getDescription());
313 setRootAndParentModule(res, path);
318 public ComponentDto createForDirectory(Component directory, PathAwareVisitor.Path<ComponentDtoHolder> path) {
319 ComponentDto res = createBase(directory);
321 res.setScope(Scopes.DIRECTORY);
322 res.setQualifier(Qualifiers.DIRECTORY);
323 res.setName(directory.getReportAttributes().getPath());
324 res.setLongName(directory.getReportAttributes().getPath());
325 res.setPath(directory.getReportAttributes().getPath());
327 setParentModuleProperties(res, path);
332 public ComponentDto createForFile(Component file, PathAwareVisitor.Path<ComponentDtoHolder> path) {
333 ComponentDto res = createBase(file);
335 res.setScope(Scopes.FILE);
336 res.setQualifier(getFileQualifier(file));
337 res.setName(FilenameUtils.getName(file.getReportAttributes().getPath()));
338 res.setLongName(file.getReportAttributes().getPath());
339 res.setPath(file.getReportAttributes().getPath());
340 res.setLanguage(file.getFileAttributes().getLanguageKey());
342 setParentModuleProperties(res, path);
347 private ComponentDto createForView(Component view) {
348 ComponentDto res = createBase(view);
350 res.setScope(Scopes.PROJECT);
351 res.setQualifier(view.getViewAttributes().getType().getQualifier());
352 res.setName(view.getName());
353 res.setDescription(view.getDescription());
354 res.setLongName(res.name());
356 res.setProjectUuid(res.uuid());
357 res.setRootUuid(res.uuid());
358 res.setUuidPath(UUID_PATH_OF_ROOT);
359 res.setModuleUuidPath(UUID_PATH_SEPARATOR + res.uuid() + UUID_PATH_SEPARATOR);
364 private ComponentDto createForSubView(Component subView, PathAwareVisitor.Path<ComponentDtoHolder> path) {
365 ComponentDto res = createBase(subView);
367 res.setScope(Scopes.PROJECT);
368 res.setQualifier(Qualifiers.SUBVIEW);
369 res.setName(subView.getName());
370 res.setDescription(subView.getDescription());
371 res.setLongName(res.name());
372 res.setCopyComponentUuid(subView.getSubViewAttributes().getOriginalViewUuid());
374 setRootAndParentModule(res, path);
379 private ComponentDto createForProjectView(Component projectView, PathAwareVisitor.Path<ComponentDtoHolder> path) {
380 ComponentDto res = createBase(projectView);
382 res.setScope(Scopes.FILE);
383 res.setQualifier(Qualifiers.PROJECT);
384 res.setName(projectView.getName());
385 res.setLongName(res.name());
386 res.setCopyComponentUuid(projectView.getProjectViewAttributes().getProjectUuid());
388 setRootAndParentModule(res, path);
393 private ComponentDto createBase(Component component) {
394 String componentKey = component.getKey();
395 String componentUuid = component.getUuid();
397 ComponentDto componentDto = new ComponentDto();
398 componentDto.setOrganizationUuid(analysisMetadataHolder.getOrganization().getUuid());
399 componentDto.setUuid(componentUuid);
400 componentDto.setDbKey(componentKey);
401 componentDto.setDeprecatedKey(componentKey);
402 componentDto.setMainBranchProjectUuid(mainBranchProjectUuid);
403 componentDto.setEnabled(true);
404 componentDto.setCreatedAt(new Date(system2.now()));
410 * Applies to a node of type either MODULE, SUBVIEW, PROJECT_VIEW
412 private void setRootAndParentModule(ComponentDto res, PathAwareVisitor.Path<ComponentDtoHolder> path) {
413 ComponentDto rootDto = path.root().getDto();
414 res.setRootUuid(rootDto.uuid());
415 res.setProjectUuid(rootDto.uuid());
417 ComponentDto parentModule = path.parent().getDto();
418 res.setUuidPath(formatUuidPathFromParent(parentModule));
419 res.setModuleUuid(parentModule.uuid());
420 res.setModuleUuidPath(parentModule.moduleUuidPath() + res.uuid() + UUID_PATH_SEPARATOR);
425 * Applies to a node of type either DIRECTORY or FILE
427 private static void setParentModuleProperties(ComponentDto componentDto, PathAwareVisitor.Path<ComponentDtoHolder> path) {
428 componentDto.setProjectUuid(path.root().getDto().uuid());
430 ComponentDto parentModule = from(path.getCurrentPath())
431 .filter(ParentModulePathElement.INSTANCE)
434 .getElement().getDto();
435 componentDto.setUuidPath(formatUuidPathFromParent(path.parent().getDto()));
436 componentDto.setRootUuid(parentModule.uuid());
437 componentDto.setModuleUuid(parentModule.uuid());
438 componentDto.setModuleUuidPath(parentModule.moduleUuidPath());
442 private static Optional<ComponentUpdateDto> compareForUpdate(ComponentDto existing, ComponentDto target) {
443 boolean hasDifferences = !StringUtils.equals(existing.getCopyResourceUuid(), target.getCopyResourceUuid()) ||
444 !StringUtils.equals(existing.description(), target.description()) ||
445 !existing.isEnabled() ||
446 !StringUtils.equals(existing.getUuidPath(), target.getUuidPath()) ||
447 !StringUtils.equals(existing.language(), target.language()) ||
448 !StringUtils.equals(existing.longName(), target.longName()) ||
449 !StringUtils.equals(existing.moduleUuid(), target.moduleUuid()) ||
450 !StringUtils.equals(existing.moduleUuidPath(), target.moduleUuidPath()) ||
451 !StringUtils.equals(existing.name(), target.name()) ||
452 !StringUtils.equals(existing.path(), target.path()) ||
453 !StringUtils.equals(existing.qualifier(), target.qualifier());
455 ComponentUpdateDto update = null;
456 if (hasDifferences) {
457 update = ComponentUpdateDto
461 return ofNullable(update);
464 private static String getFileQualifier(Component component) {
465 return component.getFileAttributes().isUnitTest() ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
468 private static class ComponentDtoHolder {
469 private ComponentDto dto;
471 public ComponentDto getDto() {
475 public void setDto(ComponentDto dto) {
480 private enum ParentModulePathElement implements Predicate<PathAwareVisitor.PathElement<ComponentDtoHolder>> {
484 public boolean apply(@Nonnull PathAwareVisitor.PathElement<ComponentDtoHolder> input) {
485 return input.getComponent().getType() == Component.Type.MODULE
486 || input.getComponent().getType() == Component.Type.PROJECT;