aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
blob: 56059915d722cf93d20d9ad7ab3d9f853a5cc5b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * SonarQube
 * Copyright (C) 2009-2025 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.scanner.scan.filesystem;

import com.google.common.collect.ImmutableMap;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.predicates.FileExtensionPredicate;
import org.sonar.core.language.UnanalyzedLanguages;
import org.sonar.scanner.scan.branch.BranchConfiguration;

import static org.sonar.api.utils.Preconditions.checkNotNull;
import static org.sonar.api.utils.Preconditions.checkState;

/**
 * Store of all files and dirs. Inclusion and
 * exclusion patterns are already applied.
 */
public class InputComponentStore extends DefaultFileSystem.Cache {
  private static final Map<UnanalyzedLanguages, Pattern> FILE_PATTERN_BY_LANGUAGE = ImmutableMap.of(
    UnanalyzedLanguages.C, Pattern.compile(".*\\.c", Pattern.CASE_INSENSITIVE),
    UnanalyzedLanguages.CPP, Pattern.compile(".*\\.cpp|.*\\.cc|.*\\.cxx|.*\\.c\\+\\+", Pattern.CASE_INSENSITIVE));

  private final SortedSet<String> globalLanguagesCache = new TreeSet<>();
  private final Map<String, SortedSet<String>> languagesCache = new HashMap<>();
  private final Map<String, InputFile> globalInputFileCache = new HashMap<>();
  private final Map<String, Map<String, InputFile>> inputFileByModuleCache = new LinkedHashMap<>();
  private final Map<InputFile, String> inputModuleKeyByFileCache = new HashMap<>();
  private final Map<String, DefaultInputModule> inputModuleCache = new HashMap<>();
  private final Map<String, InputComponent> inputComponents = new HashMap<>();
  private final Map<String, Set<InputFile>> filesByNameCache = new HashMap<>();
  private final Map<String, Set<InputFile>> filesByExtensionCache = new HashMap<>();
  private final BranchConfiguration branchConfiguration;
  private final SonarRuntime sonarRuntime;
  private final Map<String, Integer> notAnalysedFilesByLanguage = new HashMap<>();

  public InputComponentStore(BranchConfiguration branchConfiguration, SonarRuntime sonarRuntime) {
    this.branchConfiguration = branchConfiguration;
    this.sonarRuntime = sonarRuntime;
  }

  public Collection<InputComponent> all() {
    return inputComponents.values();
  }

  private Stream<DefaultInputFile> allFilesToPublishStream() {
    return globalInputFileCache.values().stream()
      .map(f -> (DefaultInputFile) f)
      .filter(DefaultInputFile::isPublished);
  }

  public Iterable<DefaultInputFile> allFilesToPublish() {
    return allFilesToPublishStream()::iterator;
  }

  public Iterable<DefaultInputFile> allChangedFilesToPublish() {
    return allFilesToPublishStream()
      .filter(f -> !branchConfiguration.isPullRequest() || f.status() != InputFile.Status.SAME)::iterator;
  }

  @Override
  public Collection<InputFile> inputFiles() {
    return globalInputFileCache.values();
  }

  public InputComponent getByKey(String key) {
    return inputComponents.get(key);
  }

  public Iterable<InputFile> filesByModule(String moduleKey) {
    return inputFileByModuleCache.getOrDefault(moduleKey, Collections.emptyMap()).values();
  }

  public InputComponentStore put(String moduleKey, InputFile inputFile) {
    DefaultInputFile file = (DefaultInputFile) inputFile;
    updateNotAnalysedCAndCppFileCount(file);

    addToLanguageCache(moduleKey, file);
    inputFileByModuleCache.computeIfAbsent(moduleKey, x -> new HashMap<>()).put(file.getModuleRelativePath(), inputFile);
    inputModuleKeyByFileCache.put(inputFile, moduleKey);
    globalInputFileCache.put(file.getProjectRelativePath(), inputFile);
    inputComponents.put(inputFile.key(), inputFile);
    filesByNameCache.computeIfAbsent(inputFile.filename(), x -> new LinkedHashSet<>()).add(inputFile);
    filesByExtensionCache.computeIfAbsent(FileExtensionPredicate.getExtension(inputFile), x -> new LinkedHashSet<>()).add(inputFile);
    return this;
  }

  private void addToLanguageCache(String moduleKey, DefaultInputFile inputFile) {
    String language = inputFile.language();
    if (language != null) {
      globalLanguagesCache.add(language);
      languagesCache.computeIfAbsent(moduleKey, k -> new TreeSet<>()).add(language);
    }
  }

  @CheckForNull
  public InputFile getFile(String moduleKey, String relativePath) {
    return inputFileByModuleCache.getOrDefault(moduleKey, Collections.emptyMap())
      .get(relativePath);
  }

  @Override
  @CheckForNull
  public InputFile inputFile(String relativePath) {
    return globalInputFileCache.get(relativePath);
  }

  public DefaultInputModule findModule(DefaultInputFile file) {
    return Optional.ofNullable(inputModuleKeyByFileCache.get(file)).map(inputModuleCache::get)
      .orElseThrow(() -> new IllegalStateException("No modules for file '" + file.toString() + "'"));
  }

  public void put(DefaultInputModule inputModule) {
    String key = inputModule.key();
    checkNotNull(inputModule);
    checkState(!inputComponents.containsKey(key), "Module '%s' already indexed", key);
    checkState(!inputModuleCache.containsKey(key), "Module '%s' already indexed", key);
    inputComponents.put(key, inputModule);
    inputModuleCache.put(key, inputModule);
  }

  @Override
  public Iterable<InputFile> getFilesByName(String filename) {
    return filesByNameCache.getOrDefault(filename, Collections.emptySet());
  }

  @Override
  public Iterable<InputFile> getFilesByExtension(String extension) {
    return filesByExtensionCache.getOrDefault(extension, Collections.emptySet());
  }

  @Override
  public SortedSet<String> languages() {
    return globalLanguagesCache;
  }

  public SortedSet<String> languages(String moduleKey) {
    return languagesCache.getOrDefault(moduleKey, Collections.emptySortedSet());
  }

  public Collection<DefaultInputModule> allModules() {
    return inputModuleCache.values();
  }

  @Override
  protected void doAdd(InputFile inputFile) {
    throw new UnsupportedOperationException();
  }

  private void updateNotAnalysedCAndCppFileCount(DefaultInputFile inputFile) {
    if (!SonarEdition.COMMUNITY.equals(sonarRuntime.getEdition())) {
      return;
    }

    FILE_PATTERN_BY_LANGUAGE.forEach((language, filePattern) -> {
      if (filePattern.matcher(inputFile.filename()).matches()) {
        notAnalysedFilesByLanguage.put(language.toString(), notAnalysedFilesByLanguage.getOrDefault(language.toString(), 0) + 1);
      }
    });
  }

  public Map<String, Integer> getNotAnalysedFilesByLanguage() {
    return ImmutableMap.copyOf(notAnalysedFilesByLanguage);
  }
}