use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.
the class JarFilter method parseJavaFiles.
/**
* Finds the expected jar archive file name prefixes for the java files.
*/
private static List<String> parseJavaFiles(List<Path> javaFiles) throws IOException {
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
List<ListenableFuture<String>> futures = Lists.newArrayList();
for (final Path javaFile : javaFiles) {
futures.add(executorService.submit(() -> {
String packageString = getDeclaredPackageOfJavaFile(javaFile);
return packageString != null ? getArchiveFileNamePrefix(javaFile.toString(), packageString) : null;
}));
}
try {
List<String> archiveFileNamePrefixes = Futures.allAsList(futures).get();
List<String> result = Lists.newArrayList();
for (String archiveFileNamePrefix : archiveFileNamePrefixes) {
if (archiveFileNamePrefix != null) {
result.add(archiveFileNamePrefix);
}
}
return result;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException(e);
} catch (ExecutionException e) {
throw new IOException(e);
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.
the class PackageParser method parsePackageStrings.
@VisibleForTesting
Map<ArtifactLocation, String> parsePackageStrings(List<ArtifactLocation> sources) throws Exception {
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
Map<ArtifactLocation, ListenableFuture<String>> futures = Maps.newHashMap();
for (final ArtifactLocation source : sources) {
futures.put(source, executorService.submit(() -> getDeclaredPackageOfJavaFile(source)));
}
Map<ArtifactLocation, String> map = Maps.newHashMap();
for (Entry<ArtifactLocation, ListenableFuture<String>> entry : futures.entrySet()) {
String value = entry.getValue().get();
if (value != null) {
map.put(entry.getKey(), value);
}
}
return map;
}
use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.
the class BlazeIdeInterfaceAspectsImpl method updateState.
@Nullable
static State updateState(Project project, BlazeContext parentContext, @Nullable State prevState, ImmutableMap<File, Long> fileState, BlazeConfigurationHandler configHandler, WorkspaceLanguageSettings workspaceLanguageSettings, ImportRoots importRoots, AspectStrategy aspectStrategy, List<File> newFiles, List<File> removedFiles, boolean mergeWithOldState) {
Result<State> result = Scope.push(parentContext, (ScopedFunction<Result<State>>) context -> {
context.push(new TimingScope("UpdateTargetMap", EventType.Other));
ImmutableMap<File, Long> nextFileState = fileState;
if (mergeWithOldState && prevState != null) {
ImmutableMap.Builder<File, Long> fileStateBuilder = ImmutableMap.<File, Long>builder().putAll(fileState);
for (Map.Entry<File, Long> entry : prevState.fileState.entrySet()) {
if (!fileState.containsKey(entry.getKey())) {
fileStateBuilder.put(entry);
}
}
nextFileState = fileStateBuilder.build();
}
State state = new State();
state.fileState = nextFileState;
state.workspaceLanguageSettings = workspaceLanguageSettings;
state.aspectStrategyName = aspectStrategy.getName();
Map<TargetKey, TargetIdeInfo> targetMap = Maps.newHashMap();
if (prevState != null) {
targetMap.putAll(prevState.targetMap.map());
state.fileToTargetMapKey.putAll(prevState.fileToTargetMapKey);
}
if (!mergeWithOldState) {
for (File removedFile : removedFiles) {
TargetKey key = state.fileToTargetMapKey.remove(removedFile);
if (key != null) {
targetMap.remove(key);
}
}
}
AtomicLong totalSizeLoaded = new AtomicLong(0);
Set<LanguageClass> ignoredLanguages = Sets.newConcurrentHashSet();
ListeningExecutorService executor = BlazeExecutor.getInstance().getExecutor();
List<ListenableFuture<TargetFilePair>> futures = Lists.newArrayList();
for (File file : newFiles) {
futures.add(executor.submit(() -> {
totalSizeLoaded.addAndGet(file.length());
IntellijIdeInfo.TargetIdeInfo message = aspectStrategy.readAspectFile(file);
TargetIdeInfo target = protoToTarget(workspaceLanguageSettings, importRoots, message, ignoredLanguages);
return new TargetFilePair(file, target);
}));
}
Set<TargetKey> newTargets = new HashSet<>();
Set<String> configurations = new LinkedHashSet<>();
configurations.add(configHandler.defaultConfigurationPathComponent);
int duplicateTargetLabels = 0;
try {
for (TargetFilePair targetFilePair : Futures.allAsList(futures).get()) {
if (targetFilePair.target != null) {
File file = targetFilePair.file;
String config = configHandler.getConfigurationPathComponent(file);
configurations.add(config);
TargetKey key = targetFilePair.target.key;
if (targetMap.putIfAbsent(key, targetFilePair.target) == null) {
state.fileToTargetMapKey.forcePut(file, key);
} else {
if (!newTargets.add(key)) {
duplicateTargetLabels++;
}
if (Objects.equals(config, configHandler.defaultConfigurationPathComponent)) {
targetMap.put(key, targetFilePair.target);
state.fileToTargetMapKey.forcePut(file, key);
}
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return Result.error(null);
} catch (ExecutionException e) {
return Result.error(e);
}
context.output(PrintOutput.log(String.format("Loaded %d aspect files, total size %dkB", newFiles.size(), totalSizeLoaded.get() / 1024)));
if (duplicateTargetLabels > 0) {
context.output(new PerformanceWarning(String.format("There were %d duplicate rules, built with the following " + "configurations: %s.\nYour IDE sync is slowed down by ~%d%%.", duplicateTargetLabels, configurations, (100 * duplicateTargetLabels / targetMap.size()))));
}
ignoredLanguages.retainAll(LanguageSupport.availableAdditionalLanguages(workspaceLanguageSettings.getWorkspaceType()));
warnIgnoredLanguages(project, context, ignoredLanguages);
state.targetMap = new TargetMap(ImmutableMap.copyOf(targetMap));
return Result.of(state);
});
if (result.error != null) {
logger.error(result.error);
return null;
}
return result.result;
}
use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.
the class DirectoryStructure method computeRootDirectoryStructure.
private static DirectoryStructure computeRootDirectoryStructure(Project project, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet) throws ExecutionException, InterruptedException {
ListeningExecutorService executorService = FetchExecutor.EXECUTOR;
FileOperationProvider fileOperationProvider = FileOperationProvider.getInstance();
ImportRoots importRoots = ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project)).add(projectViewSet).build();
Collection<WorkspacePath> rootDirectories = importRoots.rootDirectories();
Set<WorkspacePath> excludeDirectories = importRoots.excludeDirectories();
List<ListenableFuture<PathStructurePair>> futures = Lists.newArrayListWithExpectedSize(rootDirectories.size());
for (WorkspacePath rootDirectory : rootDirectories) {
futures.add(walkDirectoryStructure(workspaceRoot, excludeDirectories, fileOperationProvider, executorService, rootDirectory));
}
ImmutableMap.Builder<WorkspacePath, DirectoryStructure> result = ImmutableMap.builder();
for (PathStructurePair pair : Futures.allAsList(futures).get()) {
if (pair != null) {
result.put(pair.path, pair.directoryStructure);
}
}
return new DirectoryStructure(result.build());
}
use of com.google.common.util.concurrent.ListeningExecutorService in project intellij by bazelbuild.
the class DirectoryStructure method walkDirectoryStructure.
private static ListenableFuture<PathStructurePair> walkDirectoryStructure(WorkspaceRoot workspaceRoot, Set<WorkspacePath> excludeDirectories, FileOperationProvider fileOperationProvider, ListeningExecutorService executorService, WorkspacePath workspacePath) {
if (excludeDirectories.contains(workspacePath)) {
return Futures.immediateFuture(null);
}
File file = workspaceRoot.fileForPath(workspacePath);
if (!fileOperationProvider.isDirectory(file)) {
return Futures.immediateFuture(null);
}
ListenableFuture<File[]> childrenFuture = executorService.submit(() -> fileOperationProvider.listFiles(file));
return Futures.transformAsync(childrenFuture, children -> {
if (children == null) {
return Futures.immediateFuture(null);
}
List<ListenableFuture<PathStructurePair>> futures = Lists.newArrayListWithExpectedSize(children.length);
for (File child : children) {
WorkspacePath childWorkspacePath;
try {
childWorkspacePath = workspaceRoot.workspacePathFor(child);
} catch (IllegalArgumentException e) {
// stop at directories with unhandled characters.
continue;
}
futures.add(walkDirectoryStructure(workspaceRoot, excludeDirectories, fileOperationProvider, executorService, childWorkspacePath));
}
return Futures.transform(Futures.allAsList(futures), (Function<List<PathStructurePair>, PathStructurePair>) pairs -> {
Builder<WorkspacePath, DirectoryStructure> result = ImmutableMap.builder();
for (PathStructurePair pair : pairs) {
if (pair != null) {
result.put(pair.path, pair.directoryStructure);
}
}
return new PathStructurePair(workspacePath, new DirectoryStructure(result.build()));
}, executorService);
}, executorService);
}
Aggregations