All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.storage.BidiWriteCtx Maven / Gradle / Ivy

There is a newer version: 2.45.0
Show newest version
/*
 * Copyright 2023 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.storage;

import static com.google.cloud.storage.StorageV2ProtoUtils.fmtProto;

import com.google.cloud.storage.BidiWriteCtx.BidiWriteObjectRequestBuilderFactory;
import com.google.cloud.storage.Crc32cValue.Crc32cLengthKnown;
import com.google.storage.v2.BidiWriteObjectRequest;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.checkerframework.checker.nullness.qual.Nullable;

final class BidiWriteCtx {

  private final RequestFactoryT requestFactory;

  private final AtomicLong totalSentBytes;
  private final AtomicLong confirmedBytes;
  private final AtomicReference cumulativeCrc32c;

  BidiWriteCtx(RequestFactoryT requestFactory) {
    this.requestFactory = requestFactory;
    this.totalSentBytes = new AtomicLong(0);
    this.confirmedBytes = new AtomicLong(0);
    this.cumulativeCrc32c = new AtomicReference<>();
  }

  public RequestFactoryT getRequestFactory() {
    return requestFactory;
  }

  public BidiWriteObjectRequest.Builder newRequestBuilder() {
    return requestFactory.newBuilder();
  }

  public AtomicLong getTotalSentBytes() {
    return totalSentBytes;
  }

  public AtomicLong getConfirmedBytes() {
    return confirmedBytes;
  }

  public AtomicReference getCumulativeCrc32c() {
    return cumulativeCrc32c;
  }

  // TODO: flush this out more
  boolean isDirty() {
    return confirmedBytes.get() == totalSentBytes.get();
  }

  @Override
  public String toString() {
    return "ServerState{"
        + "requestFactory="
        + requestFactory
        + ", totalSentBytes="
        + totalSentBytes
        + ", confirmedBytes="
        + confirmedBytes
        + ", totalSentCrc32c="
        + cumulativeCrc32c
        + '}';
  }

  interface BidiWriteObjectRequestBuilderFactory {
    BidiWriteObjectRequest.Builder newBuilder();

    @Nullable
    String bucketName();

    static BidiSimpleWriteObjectRequestBuilderFactory simple(BidiWriteObjectRequest req) {
      return new BidiSimpleWriteObjectRequestBuilderFactory(req);
    }
  }

  static final class BidiSimpleWriteObjectRequestBuilderFactory
      implements BidiWriteObjectRequestBuilderFactory {
    private final BidiWriteObjectRequest req;

    private BidiSimpleWriteObjectRequestBuilderFactory(BidiWriteObjectRequest req) {
      this.req = req;
    }

    @Override
    public BidiWriteObjectRequest.Builder newBuilder() {
      return req.toBuilder();
    }

    @Override
    public @Nullable String bucketName() {
      if (req.hasWriteObjectSpec() && req.getWriteObjectSpec().hasResource()) {
        return req.getWriteObjectSpec().getResource().getBucket();
      }
      return null;
    }

    @Override
    public String toString() {
      return "SimpleBidiWriteObjectRequestBuilderFactory{" + "req=" + fmtProto(req) + '}';
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy