Java Code Examples for com.google.common.io.ByteSource#size()

The following examples show how to use com.google.common.io.ByteSource#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpArtifactCacheBinaryProtocol.java    From buck with Apache License 2.0 5 votes vote down vote up
public StoreRequest(ArtifactInfo info, ByteSource payloadSource) throws IOException {
  this.payloadSource = payloadSource;
  this.rawKeys = createKeysHeader(info.getRuleKeys());
  this.rawMetadata =
      createMetadataHeader(info.getRuleKeys(), info.getMetadata(), payloadSource);
  this.contentLength =
      rawKeys.length + Integer.SIZE / Byte.SIZE + rawMetadata.length + payloadSource.size();
}
 
Example 2
Source File: HttpArtifactCacheBinaryProtocol.java    From buck with Apache License 2.0 5 votes vote down vote up
public FetchResponse(
    ImmutableSet<RuleKey> ruleKeys,
    ImmutableMap<String, String> metadata,
    ByteSource payloadSource)
    throws IOException {
  this.payloadSource = payloadSource;
  this.rawMetadata = createMetadataHeader(ruleKeys, metadata, payloadSource);
  this.contentLength = Integer.SIZE / Byte.SIZE + rawMetadata.length + payloadSource.size();
}
 
Example 3
Source File: ThriftArtifactCache.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
protected StoreResult storeImpl(ArtifactInfo info, Path file) throws IOException {
  ByteSource artifact =
      new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
          return getProjectFilesystem().newFileInputStream(file);
        }
      };

  BuckCacheStoreRequest storeRequest = new BuckCacheStoreRequest();
  ArtifactMetadata artifactMetadata =
      infoToMetadata(
          info,
          artifact,
          getRepository(),
          scheduleType,
          producerId,
          producerHostname,
          artifact.size());
  storeRequest.setMetadata(artifactMetadata);
  PayloadInfo payloadInfo = new PayloadInfo();
  long artifactSizeBytes = artifact.size();
  payloadInfo.setSizeBytes(artifactSizeBytes);
  BuckCacheRequest cacheRequest = newCacheRequest();
  cacheRequest.addToPayloads(payloadInfo);
  cacheRequest.setType(BuckCacheRequestType.STORE);
  cacheRequest.setStoreRequest(storeRequest);

  // Handling for file system issues may lead us to upload empty manifests
  if (artifactSizeBytes == 0 && info.isManifest()) {
    throw new IOException(
        String.format("Trying to upload a 0 size Manifest entry %s", info.getRuleKeys()));
  }

  if (LOG.isVerboseEnabled()) {
    LOG.verbose(
        String.format(
            "Storing artifact with metadata: [%s].",
            ThriftUtil.thriftToDebugJson(artifactMetadata)));
  }

  ThriftArtifactCacheProtocol.Request request =
      ThriftArtifactCacheProtocol.createRequest(PROTOCOL, cacheRequest, artifact);
  Request.Builder builder = toOkHttpRequest(request);
  ImmutableStoreResult.Builder resultBuilder = ImmutableStoreResult.builder();
  resultBuilder.setRequestSizeBytes(request.getRequestLengthBytes());
  try (HttpResponse httpResponse = storeClient.makeRequest(hybridThriftEndpoint, builder)) {
    if (httpResponse.statusCode() != 200) {
      throw new IOException(
          String.format(
              "Failed to store cache artifact with HTTP status code [%d:%s] "
                  + " to url [%s] for build target [%s] that has size [%d] bytes.",
              httpResponse.statusCode(),
              httpResponse.statusMessage(),
              httpResponse.requestUrl(),
              info.getBuildTarget().orElse(null),
              artifactSizeBytes));
    }

    try (ThriftArtifactCacheProtocol.Response response =
        ThriftArtifactCacheProtocol.parseResponse(PROTOCOL, httpResponse.getBody())) {
      BuckCacheResponse cacheResponse = response.getThriftData();
      if (!cacheResponse.isWasSuccessful()) {
        reportFailureWithFormatKey(
            "Failed to store artifact with thriftErrorMessage=[%s] "
                + "url=[%s] artifactSizeBytes=[%d]",
            response.getThriftData().getErrorMessage(),
            httpResponse.requestUrl(),
            artifactSizeBytes);
      }

      resultBuilder.setArtifactContentHash(storeRequest.getMetadata().artifactPayloadMd5);
      resultBuilder.setWasStoreSuccessful(cacheResponse.isWasSuccessful());

      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "Debug info for cache store request: artifactMetadata=[%s] response=[%s]",
            ThriftUtil.thriftToDebugJson(artifactMetadata),
            ThriftUtil.thriftToDebugJson(cacheResponse));
      }
    }
  }
  return resultBuilder.build();
}