From: Julien Lancelot Date: Mon, 13 Apr 2015 11:48:15 +0000 (+0200) Subject: SONAR-6258 Close LineReaders when all lines have been read X-Git-Tag: 5.2-RC1~2300 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a7b6712ecd7c63b9a9626035632aef1de064b1af;p=sonarqube.git SONAR-6258 Close LineReaders when all lines have been read --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java index de2fa9ca7aa..26e71e31af9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java @@ -20,8 +20,6 @@ package org.sonar.server.computation.step; -import com.google.common.base.Predicates; -import com.google.common.collect.Iterables; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; @@ -91,12 +89,14 @@ public class PersistFileSourcesStep implements ComputationStep { BatchReport.Component component = reportReader.readComponent(componentRef); if (component.getType().equals(Constants.ComponentType.FILE)) { LineIterator linesIterator = sourceLinesIterator(reportReader.readFileSource(componentRef)); + LineReaders lineReaders = new LineReaders(reportReader, componentRef); try { - ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, dataLineReaders(reportReader, componentRef), component.getLines()); + ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), component.getLines()); ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute(); persistSource(fileSourcesContext, fileSourceData, component); } finally { linesIterator.close(); + lineReaders.close(); } } @@ -113,21 +113,6 @@ public class PersistFileSourcesStep implements ComputationStep { } } - private List dataLineReaders(BatchReportReader reportReader, int componentRef) { - List lineReaders = newArrayList(); - - File coverageFile = reportReader.readComponentCoverage(componentRef); - BatchReport.Scm scmReport = reportReader.readComponentScm(componentRef); - File highlightingFile = reportReader.readComponentSyntaxHighlighting(componentRef); - - lineReaders.add(coverageFile != null ? new CoverageLineReader(new ReportIterator<>(coverageFile, BatchReport.Coverage.PARSER)) : null); - lineReaders.add(scmReport != null ? new ScmLineReader(scmReport) : null); - lineReaders.add(highlightingFile != null ? new HighlightingLineReader(new ReportIterator<>(highlightingFile, BatchReport.SyntaxHighlighting.PARSER)) : null); - - Iterables.removeIf(lineReaders, Predicates.isNull()); - return lineReaders; - } - private void persistSource(FileSourcesContext fileSourcesContext, ComputeFileSourceData.Data fileSourceData, BatchReport.Component component) { FileSourceDb.Data fileData = fileSourceData.getFileSourceData(); @@ -183,6 +168,41 @@ public class PersistFileSourcesStep implements ComputationStep { } } + private static class LineReaders { + private final List lineReaders = newArrayList(); + private final List reportIterators = newArrayList(); + + LineReaders(BatchReportReader reportReader, int componentRef) { + File coverageFile = reportReader.readComponentCoverage(componentRef); + BatchReport.Scm scmReport = reportReader.readComponentScm(componentRef); + File highlightingFile = reportReader.readComponentSyntaxHighlighting(componentRef); + + if (coverageFile != null) { + ReportIterator coverageReportIterator = new ReportIterator<>(coverageFile, BatchReport.Coverage.PARSER); + reportIterators.add(coverageReportIterator); + lineReaders.add(new CoverageLineReader(coverageReportIterator)); + } + if (scmReport != null) { + lineReaders.add(new ScmLineReader(scmReport)); + } + if (highlightingFile != null) { + ReportIterator syntaxHighlightingReportIterator = new ReportIterator<>(highlightingFile, BatchReport.SyntaxHighlighting.PARSER); + reportIterators.add(syntaxHighlightingReportIterator); + lineReaders.add(new HighlightingLineReader(syntaxHighlightingReportIterator)); + } + } + + List readers(){ + return lineReaders; + } + + void close(){ + for (ReportIterator reportIterator : reportIterators) { + reportIterator.close(); + } + } + } + @Override public String getDescription() { return "Persist file sources";