Skip to content

Commit

Permalink
add AnalysisMetadataHolder and remove use of BatchReportReader amap
Browse files Browse the repository at this point in the history
this removes a dependency of some steps to BatchReportReader and will make them easier to use (if required) in Views for example
  • Loading branch information
sns-seb committed Aug 24, 2015
1 parent a7a4f06 commit f5f10c7
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 83 deletions.
@@ -0,0 +1,29 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.analysis;

import java.util.Date;

public interface AnalysisMetadataHolder {
/**
* @throws IllegalStateException if no analysis date has been set
*/
Date getAnalysisDate();
}
@@ -0,0 +1,30 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.analysis;

import java.util.Date;

public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {

/**
* @throws IllegalStateException if the analysis date has already been set
*/
void setAnalysisDate(Date date);
}
@@ -0,0 +1,42 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.analysis;

import java.util.Date;
import javax.annotation.CheckForNull;

import static com.google.common.base.Preconditions.checkState;

public class ReportAnalysisMetadataHolder implements MutableAnalysisMetadataHolder {
@CheckForNull
private Long analysisDate;

@Override
public void setAnalysisDate(Date date) {
checkState(analysisDate == null, "Analysis date has already been set");
this.analysisDate = date.getTime();
}

@Override
public Date getAnalysisDate() {
checkState(analysisDate != null, "Analysis date has not been set");
return new Date(this.analysisDate);
}
}
@@ -0,0 +1,24 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

@ParametersAreNonnullByDefault
package org.sonar.server.computation.analysis;

import javax.annotation.ParametersAreNonnullByDefault;
Expand Up @@ -27,6 +27,7 @@
import org.sonar.server.computation.ReportProcessor; import org.sonar.server.computation.ReportProcessor;
import org.sonar.server.computation.ReportQueue; import org.sonar.server.computation.ReportQueue;
import org.sonar.server.computation.activity.ActivityManager; import org.sonar.server.computation.activity.ActivityManager;
import org.sonar.server.computation.analysis.ReportAnalysisMetadataHolder;
import org.sonar.server.computation.batch.BatchReportDirectoryHolderImpl; import org.sonar.server.computation.batch.BatchReportDirectoryHolderImpl;
import org.sonar.server.computation.batch.BatchReportReaderImpl; import org.sonar.server.computation.batch.BatchReportReaderImpl;
import org.sonar.server.computation.component.DbIdsRepositoryImpl; import org.sonar.server.computation.component.DbIdsRepositoryImpl;
Expand Down Expand Up @@ -109,6 +110,7 @@ private static List componentClasses() {
MetricModule.class, MetricModule.class,


// holders // holders
ReportAnalysisMetadataHolder.class,
BatchReportDirectoryHolderImpl.class, BatchReportDirectoryHolderImpl.class,
ReportTreeRootHolderImpl.class, ReportTreeRootHolderImpl.class,
PeriodsHolderImpl.class, PeriodsHolderImpl.class,
Expand Down
Expand Up @@ -19,15 +19,14 @@
*/ */
package org.sonar.server.computation.issue; package org.sonar.server.computation.issue;


import java.util.Date;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.api.issue.Issue; import org.sonar.api.issue.Issue;
import org.sonar.api.utils.internal.Uuids; import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.IssueChangeContext; import org.sonar.core.issue.IssueChangeContext;
import org.sonar.core.issue.IssueUpdater; import org.sonar.core.issue.IssueUpdater;
import org.sonar.core.issue.workflow.IssueWorkflow; import org.sonar.core.issue.workflow.IssueWorkflow;
import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.computation.analysis.AnalysisMetadataHolder;


/** /**
* Sets the appropriate fields when an issue is : * Sets the appropriate fields when an issue is :
Expand All @@ -44,11 +43,11 @@ public class IssueLifecycle {
private final IssueUpdater updater; private final IssueUpdater updater;
private final DebtCalculator debtCalculator; private final DebtCalculator debtCalculator;


public IssueLifecycle(BatchReportReader reportReader, IssueWorkflow workflow, IssueUpdater updater, DebtCalculator debtCalculator) { public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueUpdater updater, DebtCalculator debtCalculator) {
this.workflow = workflow; this.workflow = workflow;
this.updater = updater; this.updater = updater;
this.debtCalculator = debtCalculator; this.debtCalculator = debtCalculator;
this.changeContext = IssueChangeContext.createScan(new Date(reportReader.readMetadata().getAnalysisDate())); this.changeContext = IssueChangeContext.createScan(analysisMetadataHolder.getAnalysisDate());
} }


public void initNewOpenIssue(DefaultIssue issue) { public void initNewOpenIssue(DefaultIssue issue) {
Expand Down
Expand Up @@ -21,32 +21,38 @@


import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import java.util.Date;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolder;
import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.computation.batch.BatchReportReader;
import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.ComponentImpl; import org.sonar.server.computation.component.ComponentImpl;
import org.sonar.server.computation.component.MutableTreeRootHolder; import org.sonar.server.computation.component.MutableTreeRootHolder;


/** /**
* Populates the {@link MutableTreeRootHolder} from the {@link BatchReportReader} * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
*/ */
public class BuildComponentTreeStep implements ComputationStep { public class BuildComponentTreeStep implements ComputationStep {
private final BatchReportReader reportReader; private final BatchReportReader reportReader;
private final MutableTreeRootHolder mutableTreeRootHolder; private final MutableTreeRootHolder treeRootHolder;
private final MutableAnalysisMetadataHolder analysisMetadataHolder;


public BuildComponentTreeStep(BatchReportReader reportReader, MutableTreeRootHolder mutableTreeRootHolder) { public BuildComponentTreeStep(BatchReportReader reportReader, MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
this.reportReader = reportReader; this.reportReader = reportReader;
this.mutableTreeRootHolder = mutableTreeRootHolder; this.treeRootHolder = treeRootHolder;
this.analysisMetadataHolder = analysisMetadataHolder;
} }


@Override @Override
public void execute() { public void execute() {
mutableTreeRootHolder.setRoot(buildComponentRoot()); BatchReport.Metadata metadata = reportReader.readMetadata();
treeRootHolder.setRoot(buildComponentRoot(metadata));
analysisMetadataHolder.setAnalysisDate(new Date(metadata.getAnalysisDate()));
} }


private Component buildComponentRoot() { private Component buildComponentRoot(BatchReport.Metadata metadata) {
int rootComponentRef = reportReader.readMetadata().getRootComponentRef(); int rootComponentRef = metadata.getRootComponentRef();
BatchReport.Component component = reportReader.readComponent(rootComponentRef); BatchReport.Component component = reportReader.readComponent(rootComponentRef);
return new ComponentImpl(component, buildChildren(component)); return new ComponentImpl(component, buildChildren(component));
} }
Expand Down
Expand Up @@ -41,7 +41,7 @@
import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto; import org.sonar.db.component.SnapshotDto;
import org.sonar.db.component.SnapshotQuery; import org.sonar.db.component.SnapshotQuery;
import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.computation.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.TreeRootHolder; import org.sonar.server.computation.component.TreeRootHolder;
import org.sonar.server.computation.period.Period; import org.sonar.server.computation.period.Period;
Expand All @@ -68,15 +68,15 @@ public class FeedPeriodsStep implements ComputationStep {
private final DbClient dbClient; private final DbClient dbClient;
private final Settings settings; private final Settings settings;
private final TreeRootHolder treeRootHolder; private final TreeRootHolder treeRootHolder;
private final BatchReportReader batchReportReader; private final AnalysisMetadataHolder analysisMetadataHolder;
private final PeriodsHolderImpl periodsHolder; private final PeriodsHolderImpl periodsHolder;


public FeedPeriodsStep(DbClient dbClient, Settings settings, TreeRootHolder treeRootHolder, BatchReportReader batchReportReader, public FeedPeriodsStep(DbClient dbClient, Settings settings, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
PeriodsHolderImpl periodsHolder) { PeriodsHolderImpl periodsHolder) {
this.dbClient = dbClient; this.dbClient = dbClient;
this.settings = settings; this.settings = settings;
this.treeRootHolder = treeRootHolder; this.treeRootHolder = treeRootHolder;
this.batchReportReader = batchReportReader; this.analysisMetadataHolder = analysisMetadataHolder;
this.periodsHolder = periodsHolder; this.periodsHolder = periodsHolder;
} }


Expand All @@ -96,7 +96,8 @@ private List<Period> buildPeriods(DbSession session) {
// No project on first analysis, no period // No project on first analysis, no period
if (projectDto.isPresent()) { if (projectDto.isPresent()) {
List<Period> periods = new ArrayList<>(5); List<Period> periods = new ArrayList<>(5);
PeriodResolver periodResolver = new PeriodResolver(session, projectDto.get().getId(), batchReportReader.readMetadata().getAnalysisDate(), project.getReportAttributes().getVersion(), PeriodResolver periodResolver = new PeriodResolver(session, projectDto.get().getId(), analysisMetadataHolder.getAnalysisDate().getTime(),
project.getReportAttributes().getVersion(),
// TODO qualifier will be different for Views // TODO qualifier will be different for Views
Qualifiers.PROJECT); Qualifiers.PROJECT);


Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis; import org.sonar.db.MyBatis;
import org.sonar.db.event.EventDto; import org.sonar.db.event.EventDto;
import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.computation.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.ComponentVisitor; import org.sonar.server.computation.component.ComponentVisitor;
import org.sonar.server.computation.component.DbIdsRepository; import org.sonar.server.computation.component.DbIdsRepository;
Expand All @@ -42,17 +42,17 @@ public class PersistEventsStep implements ComputationStep {


private final DbClient dbClient; private final DbClient dbClient;
private final System2 system2; private final System2 system2;
private final BatchReportReader reportReader; private final AnalysisMetadataHolder analysisMetadataHolder;
private final TreeRootHolder treeRootHolder; private final TreeRootHolder treeRootHolder;
private final EventRepository eventRepository; private final EventRepository eventRepository;
private final DbIdsRepository dbIdsRepository; private final DbIdsRepository dbIdsRepository;


public PersistEventsStep(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, BatchReportReader reportReader, public PersistEventsStep(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
EventRepository eventRepository, DbIdsRepository dbIdsRepository) { EventRepository eventRepository, DbIdsRepository dbIdsRepository) {
this.dbClient = dbClient; this.dbClient = dbClient;
this.system2 = system2; this.system2 = system2;
this.treeRootHolder = treeRootHolder; this.treeRootHolder = treeRootHolder;
this.reportReader = reportReader; this.analysisMetadataHolder = analysisMetadataHolder;
this.eventRepository = eventRepository; this.eventRepository = eventRepository;
this.dbIdsRepository = dbIdsRepository; this.dbIdsRepository = dbIdsRepository;
} }
Expand All @@ -61,7 +61,7 @@ public PersistEventsStep(DbClient dbClient, System2 system2, TreeRootHolder tree
public void execute() { public void execute() {
final DbSession session = dbClient.openSession(false); final DbSession session = dbClient.openSession(false);
try { try {
long analysisDate = reportReader.readMetadata().getAnalysisDate(); long analysisDate = analysisMetadataHolder.getAnalysisDate().getTime();
new PersistEventComponentCrawler(session, analysisDate).visit(treeRootHolder.getRoot()); new PersistEventComponentCrawler(session, analysisDate).visit(treeRootHolder.getRoot());
session.commit(); session.commit();
} finally { } finally {
Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.component.SnapshotDto; import org.sonar.db.component.SnapshotDto;
import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.computation.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.DbIdsRepositoryImpl; import org.sonar.server.computation.component.DbIdsRepositoryImpl;
import org.sonar.server.computation.component.MutableDbIdsRepository; import org.sonar.server.computation.component.MutableDbIdsRepository;
Expand All @@ -44,16 +44,16 @@ public class PersistSnapshotsStep implements ComputationStep {
private final System2 system2; private final System2 system2;
private final DbClient dbClient; private final DbClient dbClient;
private final TreeRootHolder treeRootHolder; private final TreeRootHolder treeRootHolder;
private final BatchReportReader reportReader; private final AnalysisMetadataHolder analysisMetadataHolder;
private final MutableDbIdsRepository dbIdsRepository; private final MutableDbIdsRepository dbIdsRepository;
private final PeriodsHolder periodsHolder; private final PeriodsHolder periodsHolder;


public PersistSnapshotsStep(System2 system2, DbClient dbClient, TreeRootHolder treeRootHolder, BatchReportReader reportReader, public PersistSnapshotsStep(System2 system2, DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
MutableDbIdsRepository dbIdsRepository, PeriodsHolder periodsHolder) { MutableDbIdsRepository dbIdsRepository, PeriodsHolder periodsHolder) {
this.system2 = system2; this.system2 = system2;
this.dbClient = dbClient; this.dbClient = dbClient;
this.treeRootHolder = treeRootHolder; this.treeRootHolder = treeRootHolder;
this.reportReader = reportReader; this.analysisMetadataHolder = analysisMetadataHolder;
this.dbIdsRepository = dbIdsRepository; this.dbIdsRepository = dbIdsRepository;
this.periodsHolder = periodsHolder; this.periodsHolder = periodsHolder;
} }
Expand All @@ -63,7 +63,7 @@ public void execute() {
DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
Component root = treeRootHolder.getRoot(); Component root = treeRootHolder.getRoot();
ProcessPersistSnapshot processPersistSnapshot = new ProcessPersistSnapshot(session, reportReader.readMetadata().getAnalysisDate()); ProcessPersistSnapshot processPersistSnapshot = new ProcessPersistSnapshot(session, analysisMetadataHolder.getAnalysisDate().getTime());
processPersistSnapshot.process(root, null); processPersistSnapshot.process(root, null);
session.commit(); session.commit();
} finally { } finally {
Expand Down
@@ -0,0 +1,42 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.analysis;

import java.util.Date;
import org.junit.rules.ExternalResource;

public class MutableAnalysisMetadataHolderRule extends ExternalResource implements MutableAnalysisMetadataHolder {
private ReportAnalysisMetadataHolder delegate = new ReportAnalysisMetadataHolder();

@Override
protected void before() throws Throwable {
delegate = new ReportAnalysisMetadataHolder();
}

@Override
public Date getAnalysisDate() {
return delegate.getAnalysisDate();
}

@Override
public void setAnalysisDate(Date date) {
delegate.setAnalysisDate(date);
}
}

0 comments on commit f5f10c7

Please sign in to comment.