aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2019-04-24 10:05:21 +0200
committerSonarTech <sonartech@sonarsource.com>2019-04-25 20:21:05 +0200
commitb60211a0d99cc4efca6b244199d39780a464a65a (patch)
tree2c5bb08bc9b37eb697ade2982a0299379053d852
parent9ee9132d8b4b41cbec7f8ceede2ebfcf2524e9b3 (diff)
downloadsonarqube-b60211a0d99cc4efca6b244199d39780a464a65a.tar.gz
sonarqube-b60211a0d99cc4efca6b244199d39780a464a65a.zip
Refactor code to avoid costly loop
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java16
1 files changed, 6 insertions, 10 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
index 4a92f584a30..5e5b7e83087 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
@@ -28,6 +28,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Stream;
@@ -50,6 +51,7 @@ public class InputComponentStore extends DefaultFileSystem.Cache {
private final Map<String, SortedSet<String>> languagesCache = new HashMap<>();
private final Map<String, InputFile> globalInputFileCache = new HashMap<>();
private final Table<String, String, InputFile> inputFileByModuleCache = TreeBasedTable.create();
+ private final Map<InputFile, String> inputModuleKeyByFileCache = new HashMap<>();
// indexed by key with branch
private final Map<String, DefaultInputModule> inputModuleCache = new HashMap<>();
private final Map<String, InputComponent> inputComponents = new HashMap<>();
@@ -77,8 +79,7 @@ public class InputComponentStore extends DefaultFileSystem.Cache {
public Iterable<DefaultInputFile> allChangedFilesToPublish() {
return allFilesToPublishStream()
- .filter(f -> !branchConfiguration.isShortOrPullRequest() || f.status() != InputFile.Status.SAME)
- ::iterator;
+ .filter(f -> !branchConfiguration.isShortOrPullRequest() || f.status() != InputFile.Status.SAME)::iterator;
}
@Override
@@ -98,6 +99,7 @@ public class InputComponentStore extends DefaultFileSystem.Cache {
DefaultInputFile file = (DefaultInputFile) inputFile;
addToLanguageCache(moduleKey, file);
inputFileByModuleCache.put(moduleKey, file.getModuleRelativePath(), inputFile);
+ inputModuleKeyByFileCache.put(inputFile, moduleKey);
globalInputFileCache.put(file.getProjectRelativePath(), inputFile);
inputComponents.put(inputFile.key(), inputFile);
filesByNameCache.put(inputFile.filename(), inputFile);
@@ -129,15 +131,9 @@ public class InputComponentStore extends DefaultFileSystem.Cache {
return inputModuleCache.get(moduleKeyWithBranch);
}
- @CheckForNull
public DefaultInputModule findModule(DefaultInputFile file) {
- return inputFileByModuleCache
- .cellSet()
- .stream()
- .filter(c -> c.getValue().equals(file))
- .findFirst()
- .map(c -> (DefaultInputModule) inputComponents.get(c.getRowKey()))
- .orElse(null);
+ return Optional.ofNullable(inputModuleKeyByFileCache.get(file)).map(this::getModule)
+ .orElseThrow(() -> new IllegalStateException("No modules for file '" + file.toString() + "'"));
}
public void put(DefaultInputModule inputModule) {