Future fetchAsync()

in api/src/main/java/com/google/appengine/api/log/LogServiceImpl.java [66:157]


  Future<LogQueryResult> fetchAsync(LogQuery query) {
    LogReadRequest.Builder request =
        LogReadRequest.newBuilder().setAppId(getCurrentEnvironmentOrThrow().getAppId());

    Long startTimeUs = query.getStartTimeUsec();
    if (startTimeUs != null) {
      request.setStartTime(startTimeUs);
    }

    Long endTimeUs = query.getEndTimeUsec();
    if (endTimeUs != null) {
      request.setEndTime(endTimeUs);
    }

    int batchSize = requireNonNull(query.getBatchSize(), "Null batch size");
    request.setCount(batchSize);

    LogLevel minLogLevel = query.getMinLogLevel();
    if (minLogLevel != null) {
      request.setMinimumLogLevel(minLogLevel.ordinal());
    }

    request
        .setIncludeIncomplete(query.getIncludeIncomplete())
        .setIncludeAppLogs(query.getIncludeAppLogs());

    // Use a set to de-dupe entries.
    Set<LogQuery.Version> convertedModuleInfos = Sets.newTreeSet(LogQuery.VERSION_COMPARATOR);

    // NOTE: LogQuery enforces that at most one of these lists is populated.
    if (!query.getMajorVersionIds().isEmpty()) {
      for (String versionId : query.getMajorVersionIds()) {
        convertedModuleInfos.add(new LogQuery.Version("default", versionId));
      }
    } else if (!query.getVersions().isEmpty()) {
      convertedModuleInfos.addAll(query.getVersions());
    } else {
      String currentVersionId = getCurrentEnvironmentOrThrow().getVersionId();
      // Get just the major version id - for 1.2332 it is just '1'.
      String versionId = currentVersionId.split("\\.")[0];
      convertedModuleInfos.add(
          new LogQuery.Version(getCurrentEnvironmentOrThrow().getModuleId(), versionId));
    }

    for (LogQuery.Version moduleInfo : convertedModuleInfos) {
      LogModuleVersion.Builder requestModuleVersion = request.addModuleVersionBuilder();
      if (!moduleInfo.getModuleId().equals("default")) {
        requestModuleVersion.setModuleId(moduleInfo.getModuleId());
      }
      requestModuleVersion.setVersionId(moduleInfo.getVersionId());
    }

    for (String requestId : query.getRequestIds()) {
      request.addRequestId(ByteString.copyFromUtf8(requestId));
    }

    String offset = query.getOffset();
    if (offset != null) {
      request.setOffset(LogQueryResult.parseOffset(offset));
    }

    final LogQuery finalizedQuery = query;
    ApiProxy.ApiConfig apiConfig = new ApiProxy.ApiConfig();

    Future<byte[]> responseBytes =
        ApiProxy.makeAsyncCall(PACKAGE, READ_RPC_NAME, request.build().toByteArray(), apiConfig);
    return new FutureWrapper<byte[], LogQueryResult>(responseBytes) {
      @Override
      protected LogQueryResult wrap(byte @Nullable[] responseBytes) {
        try {
          LogReadResponse response =
              LogReadResponse.parseFrom(responseBytes, ExtensionRegistry.getEmptyRegistry());
          return new LogQueryResult(response, finalizedQuery);
        } catch (InvalidProtocolBufferException | UninitializedMessageException e) {
          throw new LogServiceException("Could not parse LogReadResponse", e);
        }
      }

      @Override
      protected Throwable convertException(Throwable cause) {
        if (cause instanceof ApiProxy.ApplicationException) {
          ApiProxy.ApplicationException e = (ApiProxy.ApplicationException) cause;
          ErrorCode errorCode = LogServiceError.ErrorCode.forNumber(e.getApplicationError());
          if (errorCode == LogServiceError.ErrorCode.INVALID_REQUEST) {
            return new InvalidRequestException(e.getErrorDetail());
          }
          return new LogServiceException(e.getErrorDetail());
        }
        return cause;
      }
    };
  }