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.

InputComponentStore.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.scanner.scan.filesystem;
  21. import com.google.common.base.Preconditions;
  22. import com.google.common.collect.LinkedHashMultimap;
  23. import com.google.common.collect.SetMultimap;
  24. import com.google.common.collect.Table;
  25. import com.google.common.collect.TreeBasedTable;
  26. import java.util.Collection;
  27. import java.util.Collections;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.Optional;
  31. import java.util.SortedSet;
  32. import java.util.TreeSet;
  33. import java.util.stream.Stream;
  34. import javax.annotation.CheckForNull;
  35. import org.sonar.api.batch.fs.InputComponent;
  36. import org.sonar.api.batch.fs.InputFile;
  37. import org.sonar.api.batch.fs.internal.DefaultFileSystem;
  38. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  39. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  40. import org.sonar.api.batch.fs.internal.FileExtensionPredicate;
  41. import org.sonar.scanner.scan.branch.BranchConfiguration;
  42. /**
  43. * Store of all files and dirs. Inclusion and
  44. * exclusion patterns are already applied.
  45. */
  46. public class InputComponentStore extends DefaultFileSystem.Cache {
  47. private final SortedSet<String> globalLanguagesCache = new TreeSet<>();
  48. private final Map<String, SortedSet<String>> languagesCache = new HashMap<>();
  49. private final Map<String, InputFile> globalInputFileCache = new HashMap<>();
  50. private final Table<String, String, InputFile> inputFileByModuleCache = TreeBasedTable.create();
  51. private final Map<InputFile, String> inputModuleKeyByFileCache = new HashMap<>();
  52. private final Map<String, DefaultInputModule> inputModuleCache = new HashMap<>();
  53. private final Map<String, InputComponent> inputComponents = new HashMap<>();
  54. private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create();
  55. private final SetMultimap<String, InputFile> filesByExtensionCache = LinkedHashMultimap.create();
  56. private final BranchConfiguration branchConfiguration;
  57. public InputComponentStore(BranchConfiguration branchConfiguration) {
  58. this.branchConfiguration = branchConfiguration;
  59. }
  60. public Collection<InputComponent> all() {
  61. return inputComponents.values();
  62. }
  63. private Stream<DefaultInputFile> allFilesToPublishStream() {
  64. return globalInputFileCache.values().stream()
  65. .map(f -> (DefaultInputFile) f)
  66. .filter(DefaultInputFile::isPublished);
  67. }
  68. public Iterable<DefaultInputFile> allFilesToPublish() {
  69. return allFilesToPublishStream()::iterator;
  70. }
  71. public Iterable<DefaultInputFile> allChangedFilesToPublish() {
  72. return allFilesToPublishStream()
  73. .filter(f -> !branchConfiguration.isShortOrPullRequest() || f.status() != InputFile.Status.SAME)::iterator;
  74. }
  75. @Override
  76. public Collection<InputFile> inputFiles() {
  77. return globalInputFileCache.values();
  78. }
  79. public InputComponent getByKey(String key) {
  80. return inputComponents.get(key);
  81. }
  82. public Iterable<InputFile> filesByModule(String moduleKey) {
  83. return inputFileByModuleCache.row(moduleKey).values();
  84. }
  85. public InputComponentStore put(String moduleKey, InputFile inputFile) {
  86. DefaultInputFile file = (DefaultInputFile) inputFile;
  87. addToLanguageCache(moduleKey, file);
  88. inputFileByModuleCache.put(moduleKey, file.getModuleRelativePath(), inputFile);
  89. inputModuleKeyByFileCache.put(inputFile, moduleKey);
  90. globalInputFileCache.put(file.getProjectRelativePath(), inputFile);
  91. inputComponents.put(inputFile.key(), inputFile);
  92. filesByNameCache.put(inputFile.filename(), inputFile);
  93. filesByExtensionCache.put(FileExtensionPredicate.getExtension(inputFile), inputFile);
  94. return this;
  95. }
  96. private void addToLanguageCache(String moduleKey, DefaultInputFile inputFile) {
  97. String language = inputFile.language();
  98. if (language != null) {
  99. globalLanguagesCache.add(language);
  100. languagesCache.computeIfAbsent(moduleKey, k -> new TreeSet<>()).add(language);
  101. }
  102. }
  103. @CheckForNull
  104. public InputFile getFile(String moduleKey, String relativePath) {
  105. return inputFileByModuleCache.get(moduleKey, relativePath);
  106. }
  107. @Override
  108. @CheckForNull
  109. public InputFile inputFile(String relativePath) {
  110. return globalInputFileCache.get(relativePath);
  111. }
  112. public DefaultInputModule findModule(DefaultInputFile file) {
  113. return Optional.ofNullable(inputModuleKeyByFileCache.get(file)).map(inputModuleCache::get)
  114. .orElseThrow(() -> new IllegalStateException("No modules for file '" + file.toString() + "'"));
  115. }
  116. public void put(DefaultInputModule inputModule) {
  117. String key = inputModule.key();
  118. Preconditions.checkNotNull(inputModule);
  119. Preconditions.checkState(!inputComponents.containsKey(key), "Module '%s' already indexed", key);
  120. Preconditions.checkState(!inputModuleCache.containsKey(key), "Module '%s' already indexed", key);
  121. inputComponents.put(key, inputModule);
  122. inputModuleCache.put(key, inputModule);
  123. }
  124. @Override
  125. public Iterable<InputFile> getFilesByName(String filename) {
  126. return filesByNameCache.get(filename);
  127. }
  128. @Override
  129. public Iterable<InputFile> getFilesByExtension(String extension) {
  130. return filesByExtensionCache.get(extension);
  131. }
  132. @Override
  133. public SortedSet<String> languages() {
  134. return globalLanguagesCache;
  135. }
  136. public SortedSet<String> languages(String moduleKey) {
  137. return languagesCache.getOrDefault(moduleKey, Collections.emptySortedSet());
  138. }
  139. public Collection<DefaultInputModule> allModules() {
  140. return inputModuleCache.values();
  141. }
  142. @Override
  143. protected void doAdd(InputFile inputFile) {
  144. throw new UnsupportedOperationException();
  145. }
  146. }