Skip to content

Commit

Permalink
Refactor persisting of sources
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses authored and sonartech committed Sep 19, 2018
1 parent 500ded1 commit 11f1dc5
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 517 deletions.
Expand Up @@ -27,13 +27,12 @@
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import org.sonar.ce.task.projectanalysis.component.Component; import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.ComponentVisitor; import org.sonar.ce.task.projectanalysis.component.ComponentVisitor;
import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
import org.sonar.ce.task.projectanalysis.metric.Metric;
import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit; import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
import org.sonar.ce.task.projectanalysis.measure.Measure; import org.sonar.ce.task.projectanalysis.measure.Measure;
import org.sonar.ce.task.projectanalysis.measure.MeasureRepository; import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;

import org.sonar.ce.task.projectanalysis.metric.Metric;
import org.sonar.ce.task.projectanalysis.metric.MetricRepository;


import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;


Expand Down Expand Up @@ -217,7 +216,7 @@ public Counter getCounter(Formula formula) {
} }
} }


private class CreateMeasureContextImpl implements CreateMeasureContext { private static class CreateMeasureContextImpl implements CreateMeasureContext {
private final Component component; private final Component component;
private final Metric metric; private final Metric metric;


Expand Down

This file was deleted.

@@ -0,0 +1,105 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.ce.task.projectanalysis.source;

import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.scm.Changeset;
import org.sonar.core.hash.SourceHashComputer;
import org.sonar.core.util.CloseableIterator;
import org.sonar.db.protobuf.DbFileSources;

public class FileSourceDataComputer {
private final SourceLinesRepository sourceLinesRepository;
private final SourceLineReadersFactory sourceLineReadersFactory;
private final SourceLinesHashRepository sourceLinesHash;
private final SourceHashComputer sourceHashComputer;

public FileSourceDataComputer(SourceLinesRepository sourceLinesRepository, SourceLineReadersFactory sourceLineReadersFactory, SourceLinesHashRepository sourceLinesHash) {
this.sourceLinesRepository = sourceLinesRepository;
this.sourceLineReadersFactory = sourceLineReadersFactory;
this.sourceLinesHash = sourceLinesHash;
this.sourceHashComputer = new SourceHashComputer();
}

public Data compute(Component file) {
try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
SourceLineReadersFactory.LineReaders lineReaders = sourceLineReadersFactory.getLineReaders(file)) {

SourceLinesHashRepositoryImpl.LineHashesComputer lineHashesComputer = sourceLinesHash.getLineHashesComputerToPersist(file);
DbFileSources.Data.Builder fileSourceBuilder = DbFileSources.Data.newBuilder();
int currentLine = 0;

while (linesIterator.hasNext()) {
currentLine++;
read(fileSourceBuilder, lineHashesComputer, lineReaders, currentLine, linesIterator.next(), linesIterator.hasNext());
}

return new Data(fileSourceBuilder.build(), lineHashesComputer.getResult(), sourceHashComputer.getHash(), lineReaders.getLatestChangeWithRevision());
}
}

private void read(DbFileSources.Data.Builder fileSourceBuilder, SourceLinesHashRepositoryImpl.LineHashesComputer lineHashesComputer,
SourceLineReadersFactory.LineReaders lineReaders, int currentLine, String lineSource, boolean hasNextLine) {
sourceHashComputer.addLine(lineSource, hasNextLine);
lineHashesComputer.addLine(lineSource);

DbFileSources.Line.Builder lineBuilder = fileSourceBuilder
.addLinesBuilder()
.setSource(lineSource)
.setLine(currentLine);

lineReaders.read(lineBuilder);
}

public static class Data {
private final DbFileSources.Data fileSourceData;
private final List<String> lineHashes;
private final String srcHash;
private final Changeset latestChangeWithRevision;

public Data(DbFileSources.Data fileSourceData, List<String> lineHashes, String srcHash, @Nullable Changeset latestChangeWithRevision) {
this.fileSourceData = fileSourceData;
this.lineHashes = lineHashes;
this.srcHash = srcHash;
this.latestChangeWithRevision = latestChangeWithRevision;
}

public String getSrcHash() {
return srcHash;
}

public List<String> getLineHashes() {
return lineHashes;
}

public DbFileSources.Data getLineData() {
return fileSourceData;
}

@CheckForNull
public Changeset getLatestChangeWithRevision() {
return latestChangeWithRevision;
}
}

}
@@ -0,0 +1,114 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.ce.task.projectanalysis.source;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.duplication.DuplicationRepository;
import org.sonar.ce.task.projectanalysis.scm.Changeset;
import org.sonar.ce.task.projectanalysis.scm.ScmInfo;
import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepository;
import org.sonar.ce.task.projectanalysis.source.linereader.CoverageLineReader;
import org.sonar.ce.task.projectanalysis.source.linereader.DuplicationLineReader;
import org.sonar.ce.task.projectanalysis.source.linereader.HighlightingLineReader;
import org.sonar.ce.task.projectanalysis.source.linereader.LineReader;
import org.sonar.ce.task.projectanalysis.source.linereader.RangeOffsetConverter;
import org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader;
import org.sonar.ce.task.projectanalysis.source.linereader.SymbolsLineReader;
import org.sonar.core.util.CloseableIterator;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.scanner.protocol.output.ScannerReport;

public class SourceLineReadersFactory {
private final BatchReportReader reportReader;
private final ScmInfoRepository scmInfoRepository;
private final DuplicationRepository duplicationRepository;

SourceLineReadersFactory(BatchReportReader reportReader, ScmInfoRepository scmInfoRepository, DuplicationRepository duplicationRepository) {
this.reportReader = reportReader;
this.scmInfoRepository = scmInfoRepository;
this.duplicationRepository = duplicationRepository;
}

public LineReaders getLineReaders(Component component) {
List<LineReader> readers = new ArrayList<>();
List<CloseableIterator<?>> closeables = new ArrayList<>();
ScmLineReader scmLineReader = null;

int componentRef = component.getReportAttributes().getRef();
CloseableIterator<ScannerReport.LineCoverage> coverageIt = reportReader.readComponentCoverage(componentRef);
closeables.add(coverageIt);
readers.add(new CoverageLineReader(coverageIt));

Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
if (scmInfoOptional.isPresent()) {
scmLineReader = new ScmLineReader(scmInfoOptional.get());
readers.add(scmLineReader);
}

RangeOffsetConverter rangeOffsetConverter = new RangeOffsetConverter();
CloseableIterator<ScannerReport.SyntaxHighlightingRule> highlightingIt = reportReader.readComponentSyntaxHighlighting(componentRef);
closeables.add(highlightingIt);
readers.add(new HighlightingLineReader(component, highlightingIt, rangeOffsetConverter));

CloseableIterator<ScannerReport.Symbol> symbolsIt = reportReader.readComponentSymbols(componentRef);
closeables.add(symbolsIt);
readers.add(new SymbolsLineReader(component, symbolsIt, rangeOffsetConverter));
readers.add(new DuplicationLineReader(duplicationRepository.getDuplications(component)));

return new LineReaders(readers, scmLineReader, closeables);
}

static class LineReaders implements AutoCloseable, LineReader {
final List<LineReader> readers;
@Nullable
final ScmLineReader scmLineReader;
final List<CloseableIterator<?>> closeables;

LineReaders(List<LineReader> readers, @Nullable ScmLineReader scmLineReader, List<CloseableIterator<?>> closeables) {
this.readers = readers;
this.scmLineReader = scmLineReader;
this.closeables = closeables;
}

@Override public void close() {
for (CloseableIterator<?> reportIterator : closeables) {
reportIterator.close();
}
}

@Override public void read(DbFileSources.Line.Builder lineBuilder) {
for (LineReader r : readers) {
r.read(lineBuilder);
}
}

@CheckForNull
public Changeset getLatestChangeWithRevision() {
return scmLineReader == null ? null : scmLineReader.getLatestChangeWithRevision();
}
}

}
Expand Up @@ -27,7 +27,6 @@
import org.sonar.core.hash.SourceLineHashesComputer; import org.sonar.core.hash.SourceLineHashesComputer;
import org.sonar.core.util.CloseableIterator; import org.sonar.core.util.CloseableIterator;
import org.sonar.db.source.LineHashVersion; import org.sonar.db.source.LineHashVersion;
import org.sonar.ce.task.projectanalysis.component.Component;


public class SourceLinesHashRepositoryImpl implements SourceLinesHashRepository { public class SourceLinesHashRepositoryImpl implements SourceLinesHashRepository {
private final SourceLinesRepository sourceLinesRepository; private final SourceLinesRepository sourceLinesRepository;
Expand Down Expand Up @@ -95,7 +94,7 @@ private List<String> createLineHashes(Component component, Optional<LineRange[]>
return processor.getResult(); return processor.getResult();
} }


public static interface LineHashesComputer { public interface LineHashesComputer {
void addLine(String line); void addLine(String line);


List<String> getResult(); List<String> getResult();
Expand Down
Expand Up @@ -21,6 +21,7 @@


import org.sonar.db.protobuf.DbFileSources; import org.sonar.db.protobuf.DbFileSources;


@FunctionalInterface
public interface LineReader { public interface LineReader {


void read(DbFileSources.Line.Builder lineBuilder); void read(DbFileSources.Line.Builder lineBuilder);
Expand Down

0 comments on commit 11f1dc5

Please sign in to comment.