You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PersistSnapshotsStep.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.server.computation.step;
  21. import javax.annotation.Nullable;
  22. import org.sonar.api.resources.Qualifiers;
  23. import org.sonar.api.resources.Scopes;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.db.component.SnapshotDto;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.server.computation.batch.BatchReportReader;
  28. import org.sonar.server.computation.component.Component;
  29. import org.sonar.server.computation.component.DbIdsRepository;
  30. import org.sonar.server.computation.component.TreeRootHolder;
  31. import org.sonar.server.computation.period.Period;
  32. import org.sonar.server.computation.period.PeriodsHolder;
  33. import org.sonar.server.db.DbClient;
  34. /**
  35. * Persist snapshots
  36. * Also feed the components cache {@link DbIdsRepository} with snapshot ids
  37. */
  38. public class PersistSnapshotsStep implements ComputationStep {
  39. private final System2 system2;
  40. private final DbClient dbClient;
  41. private final TreeRootHolder treeRootHolder;
  42. private final BatchReportReader reportReader;
  43. private final DbIdsRepository dbIdsRepository;
  44. private final PeriodsHolder periodsHolder;
  45. public PersistSnapshotsStep(System2 system2, DbClient dbClient, TreeRootHolder treeRootHolder, BatchReportReader reportReader, DbIdsRepository dbIdsRepository,
  46. PeriodsHolder periodsHolder) {
  47. this.system2 = system2;
  48. this.dbClient = dbClient;
  49. this.treeRootHolder = treeRootHolder;
  50. this.reportReader = reportReader;
  51. this.dbIdsRepository = dbIdsRepository;
  52. this.periodsHolder = periodsHolder;
  53. }
  54. @Override
  55. public void execute() {
  56. DbSession session = dbClient.openSession(false);
  57. try {
  58. Component root = treeRootHolder.getRoot();
  59. ProcessPersistSnapshot processPersistSnapshot = new ProcessPersistSnapshot(session, reportReader.readMetadata().getAnalysisDate());
  60. processPersistSnapshot.process(root, null);
  61. session.commit();
  62. } finally {
  63. session.close();
  64. }
  65. }
  66. private class ProcessPersistSnapshot {
  67. private final DbSession dbSession;
  68. private final long analysisDate;
  69. private long projectId;
  70. public ProcessPersistSnapshot(DbSession dbSession, long analysisDate) {
  71. this.dbSession = dbSession;
  72. this.analysisDate = analysisDate;
  73. }
  74. private void process(Component component, @Nullable SnapshotDto parentSnapshot) {
  75. long componentId = dbIdsRepository.getComponentId(component);
  76. switch (component.getType()) {
  77. case PROJECT:
  78. this.projectId = componentId;
  79. SnapshotDto projectSnapshot = persistSnapshot(componentId, Qualifiers.PROJECT, Scopes.PROJECT, component.getVersion(), parentSnapshot, true);
  80. addToCache(component, projectSnapshot);
  81. processChildren(component, projectSnapshot);
  82. break;
  83. case MODULE:
  84. SnapshotDto moduleSnapshot = persistSnapshot(componentId, Qualifiers.MODULE, Scopes.PROJECT, component.getVersion(), parentSnapshot, true);
  85. addToCache(component, moduleSnapshot);
  86. processChildren(component, moduleSnapshot);
  87. break;
  88. case DIRECTORY:
  89. SnapshotDto directorySnapshot = persistSnapshot(componentId, Qualifiers.DIRECTORY, Scopes.DIRECTORY, null, parentSnapshot, false);
  90. addToCache(component, directorySnapshot);
  91. processChildren(component, directorySnapshot);
  92. break;
  93. case FILE:
  94. SnapshotDto fileSnapshot = persistSnapshot(componentId, getFileQualifier(component), Scopes.FILE, null, parentSnapshot, false);
  95. addToCache(component, fileSnapshot);
  96. break;
  97. default:
  98. throw new IllegalStateException(String.format("Unsupported component type '%s'", component.getType()));
  99. }
  100. }
  101. private void processChildren(Component component, SnapshotDto parentSnapshot) {
  102. for (Component child : component.getChildren()) {
  103. process(child, parentSnapshot);
  104. }
  105. }
  106. private SnapshotDto persistSnapshot(long componentId, String qualifier, String scope, @Nullable String version, @Nullable SnapshotDto parentSnapshot, boolean setPeriods) {
  107. SnapshotDto snapshotDto = new SnapshotDto()
  108. .setRootProjectId(projectId)
  109. .setVersion(version)
  110. .setComponentId(componentId)
  111. .setQualifier(qualifier)
  112. .setScope(scope)
  113. .setLast(false)
  114. .setStatus(SnapshotDto.STATUS_UNPROCESSED)
  115. .setCreatedAt(analysisDate)
  116. .setBuildDate(system2.now());
  117. if (setPeriods) {
  118. updateSnapshotPeriods(snapshotDto);
  119. }
  120. if (parentSnapshot != null) {
  121. snapshotDto
  122. .setParentId(parentSnapshot.getId())
  123. .setRootId(parentSnapshot.getRootId() == null ? parentSnapshot.getId() : parentSnapshot.getRootId())
  124. .setDepth(parentSnapshot.getDepth() + 1)
  125. .setPath(parentSnapshot.getPath() + parentSnapshot.getId() + ".");
  126. } else {
  127. snapshotDto
  128. // On Oracle, the path will be null
  129. .setPath("")
  130. .setDepth(0);
  131. }
  132. dbClient.snapshotDao().insert(dbSession, snapshotDto);
  133. return snapshotDto;
  134. }
  135. private void updateSnapshotPeriods(SnapshotDto snapshotDto) {
  136. for (Period period : periodsHolder.getPeriods()) {
  137. int index = period.getIndex();
  138. snapshotDto.setPeriodMode(index, period.getMode());
  139. snapshotDto.setPeriodParam(index, period.getModeParameter());
  140. snapshotDto.setPeriodDate(index, period.getSnapshotDate());
  141. }
  142. }
  143. private void addToCache(Component component, SnapshotDto snapshotDto) {
  144. dbIdsRepository.setSnapshotId(component, snapshotDto.getId());
  145. }
  146. }
  147. private static String getFileQualifier(Component component) {
  148. return component.getFileAttributes().isUnitTest() ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
  149. }
  150. @Override
  151. public String getDescription() {
  152. return "Persist snapshots";
  153. }
  154. }