private JdepsState doLoadJdepsFiles()

in java/src/com/google/idea/blaze/java/sync/jdeps/JdepsFileReader.java [109:220]


  private JdepsState doLoadJdepsFiles(
      Project project,
      BlazeContext context,
      ArtifactLocationDecoder decoder,
      @Nullable JdepsState oldState,
      Collection<TargetIdeInfo> targetsToLoad,
      SyncMode syncMode)
      throws InterruptedException, ExecutionException {
    Map<OutputArtifact, TargetKey> fileToTargetMap = Maps.newHashMap();
    for (TargetIdeInfo target : targetsToLoad) {
      BlazeArtifact output = resolveJdepsOutput(decoder, target);
      if (output instanceof OutputArtifact) {
        fileToTargetMap.put((OutputArtifact) output, target.getKey());
      }
    }

    ArtifactsDiff diff =
        ArtifactsDiff.diffArtifacts(
            oldState != null ? oldState.getArtifactState() : null, fileToTargetMap.keySet());

    // TODO: handle prefetching for arbitrary OutputArtifacts
    List<OutputArtifact> outputArtifacts = diff.getUpdatedOutputs();
    // b/187759986: if there was no build, then all the needed artifacts should've been cached
    // already. Additional logging to identify what is going wrong.
    if (!outputArtifacts.isEmpty() && !SyncMode.involvesBlazeBuild(syncMode)) {
      logger.warn(
          "ArtifactDiff: "
              + outputArtifacts.size()
              + " outputs need to be updated during SyncMode.NO_BUILD ");
      if (oldState == null) {
        logger.warn("ArtifactDiff: oldState == null, we failed to load prior JdepsState.");
      } else {
        // Do not list all artifacts since it may be pretty long.
        if (oldState.getArtifactState().size() != fileToTargetMap.size()) {
          logger.warn(
              "Existing artifact state does not match with target map."
                  + " [oldState.getArtifactState().size() = "
                  + oldState.getArtifactState().size()
                  + ", fileToTargetMap.size() = "
                  + fileToTargetMap.size()
                  + "]");
        }
      }
    }

    ListenableFuture<?> downloadArtifactsFuture =
        RemoteArtifactPrefetcher.getInstance()
            .downloadArtifacts(
                /* projectName= */ project.getName(),
                /* outputArtifacts= */ BlazeArtifact.getRemoteArtifacts(outputArtifacts));
    ListenableFuture<?> fetchLocalFilesFuture =
        PrefetchService.getInstance()
            .prefetchFiles(BlazeArtifact.getLocalFiles(outputArtifacts), true, false);
    if (!FutureUtil.waitForFuture(
            context, Futures.allAsList(downloadArtifactsFuture, fetchLocalFilesFuture))
        .timed("FetchJdeps", EventType.Prefetching)
        .withProgressMessage("Reading jdeps files...")
        .run()
        .success()) {
      return null;
    }

    AtomicLong totalSizeLoaded = new AtomicLong(0);

    List<ListenableFuture<Result>> futures = Lists.newArrayList();
    for (OutputArtifact updatedFile : outputArtifacts) {
      futures.add(
          FetchExecutor.EXECUTOR.submit(
              () -> {
                totalSizeLoaded.addAndGet(updatedFile.getLength());
                try (InputStream inputStream = updatedFile.getInputStream()) {
                  Deps.Dependencies dependencies = Deps.Dependencies.parseFrom(inputStream);
                  if (dependencies == null) {
                    return null;
                  }
                  List<String> deps =
                      dependencies.getDependencyList().stream()
                          .filter(dep -> relevantDep(dep))
                          .map(Dependency::getPath)
                          .collect(toImmutableList());
                  TargetKey targetKey = fileToTargetMap.get(updatedFile);
                  return new Result(updatedFile, targetKey, deps);
                } catch (IOException e) {
                  logger.info("Could not read jdeps file: " + updatedFile);
                  return null;
                }
              }));
    }

    JdepsState.Builder state = JdepsState.builder();
    if (oldState != null) {
      state.list.addAll(oldState.data);
    }
    state.removeArtifacts(
        diff.getUpdatedOutputs().stream()
            .map(OutputArtifact::toArtifactState)
            .collect(toImmutableList()));
    state.removeArtifacts(diff.getRemovedOutputs());
    for (Result result : Futures.allAsList(futures).get()) {
      if (result != null) {
        state.list.add(
            JdepsData.create(
                result.targetKey, result.dependencies, result.output.toArtifactState()));
      }
    }
    context.output(
        PrintOutput.log(
            String.format(
                "Loaded %d jdeps files, total size %dkB",
                diff.getUpdatedOutputs().size(), totalSizeLoaded.get() / 1024)));
    return state.build();
  }