Skip to content

Commit

Permalink
feat: introduce java.time to java-bigtable
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomarquezp committed Nov 14, 2024
1 parent a94df4a commit 71aa9a8
Show file tree
Hide file tree
Showing 71 changed files with 830 additions and 495 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

package com.google.cloud.bigtable.admin.v2.models;

import static com.google.api.gax.util.TimeConversionUtils.toThreetenInstant;

import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.bigtable.admin.v2.BackupName;
import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.protobuf.util.Timestamps;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.threeten.bp.Instant;

/**
* A backup lets you save a copy of a table's schema and data and restore the backup to a new table
Expand Down Expand Up @@ -166,23 +168,41 @@ public String getInstanceId() {
return instanceId;
}

/** This method is obsolete. Use {@link #getExpireTimeInstant()} instead. */
@ObsoleteApi("Use getExpireTimeInstant() instead")
public org.threeten.bp.Instant getExpireTime() {
return toThreetenInstant(getExpireTimeInstant());
}

/** Get the expire time of this backup. */
public Instant getExpireTime() {
return Instant.ofEpochMilli(Timestamps.toMillis(proto.getExpireTime()));
public java.time.Instant getExpireTimeInstant() {
return java.time.Instant.ofEpochMilli(Timestamps.toMillis(proto.getExpireTime()));
}

/** This method is obsolete. Use {@link #getStartTimeInstant()} instead. */
@ObsoleteApi("Use getStartTimeInstant() instead")
public @Nullable org.threeten.bp.Instant getStartTime() {
return toThreetenInstant(getStartTimeInstant());
}

/** Get the start time when this backup is taken. */
public @Nullable Instant getStartTime() {
public @Nullable java.time.Instant getStartTimeInstant() {
if (proto.hasStartTime()) {
return Instant.ofEpochMilli(Timestamps.toMillis(proto.getStartTime()));
return java.time.Instant.ofEpochMilli(Timestamps.toMillis(proto.getStartTime()));
}
return null;
}

/** This method is obsolete. Use {@link #getStartTimeInstant()} instead. */
@ObsoleteApi("Use getStartTimeInstant() instead")
public @Nullable org.threeten.bp.Instant getEndTime() {
return toThreetenInstant(getEndTimeInstant());
}

/** Get the end time when the creation of this backup has completed. */
public @Nullable Instant getEndTime() {
public @Nullable java.time.Instant getEndTimeInstant() {
if (proto.hasEndTime()) {
return Instant.ofEpochMilli(Timestamps.toMillis(proto.getEndTime()));
return java.time.Instant.ofEpochMilli(Timestamps.toMillis(proto.getEndTime()));
}
return null;
}
Expand All @@ -202,11 +222,18 @@ public BackupType getBackupType() {
return BackupType.fromProto(proto.getBackupType());
}

/** This method is obsolete. Use {@link #getHotToStandardTimeInstant()} instead. */
@ObsoleteApi("Use getHotToStandardTimeInstant() instead.")
@Nullable
public org.threeten.bp.Instant getHotToStandardTime() {
return toThreetenInstant(getHotToStandardTimeInstant());
}

/** Get the time at which this backup will be converted from a hot backup to a standard backup. */
@Nullable
public Instant getHotToStandardTime() {
public java.time.Instant getHotToStandardTimeInstant() {
if (proto.hasHotToStandardTime()) {
return Instant.ofEpochMilli(Timestamps.toMillis(proto.getHotToStandardTime()));
return java.time.Instant.ofEpochMilli(Timestamps.toMillis(proto.getHotToStandardTime()));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package com.google.cloud.bigtable.admin.v2.models;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeInstant;

import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.protobuf.util.Timestamps;
import javax.annotation.Nonnull;
import org.threeten.bp.Instant;

/** Build CopyBackupRequest for {@link com.google.bigtable.admin.v2.CopyBackupRequest}. */
public final class CopyBackupRequest {
Expand Down Expand Up @@ -75,7 +77,13 @@ public CopyBackupRequest setDestination(String clusterId, String backupId) {
return this;
}

public CopyBackupRequest setExpireTime(Instant expireTime) {
/** This method is obsolete. Use {@link #setExpireTimeInstant(java.time.Instant)} instead. */
@ObsoleteApi("Use setExpireTimeInstant(java.time.Instant) instead.")
public CopyBackupRequest setExpireTime(org.threeten.bp.Instant expireTime) {
return setExpireTimeInstant(toJavaTimeInstant(expireTime));
}

public CopyBackupRequest setExpireTimeInstant(java.time.Instant expireTime) {
Preconditions.checkNotNull(expireTime);
requestBuilder.setExpireTime(Timestamps.fromMillis(expireTime.toEpochMilli()));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package com.google.cloud.bigtable.admin.v2.models;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeInstant;

import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.protobuf.util.Timestamps;
import javax.annotation.Nonnull;
import org.threeten.bp.Instant;

/** Fluent wrapper for {@link com.google.bigtable.admin.v2.CreateBackupRequest} */
public final class CreateBackupRequest {
Expand Down Expand Up @@ -50,7 +52,13 @@ public CreateBackupRequest setSourceTableId(String sourceTableId) {
return this;
}

public CreateBackupRequest setExpireTime(Instant expireTime) {
/** This method is obsolete. Use {@link #setExpireTimeInstant(java.time.Instant)} instead. */
@ObsoleteApi("Use setExpireTimeInstant(java.time.Instant) instead.")
public CreateBackupRequest setExpireTime(org.threeten.bp.Instant expireTime) {
return setExpireTimeInstant(toJavaTimeInstant(expireTime));
}

public CreateBackupRequest setExpireTimeInstant(java.time.Instant expireTime) {
Preconditions.checkNotNull(expireTime);
requestBuilder
.getBackupBuilder()
Expand All @@ -64,10 +72,18 @@ public CreateBackupRequest setBackupType(Backup.BackupType backupType) {
return this;
}

/**
* This method is obsolete. Use {@link #setHotToStandardTimeInstant(java.time.Instant)} instead.
*/
@ObsoleteApi("Use setHotToStandardTimeInstant(java.time.Instant) instead.")
public CreateBackupRequest setHotToStandardTime(org.threeten.bp.Instant hotToStandardTime) {
return setHotToStandardTimeInstant(toJavaTimeInstant(hotToStandardTime));
}

// The time at which this backup will be converted from a hot backup to a standard backup. Only
// applicable for hot backups. If not set, the backup will remain as a hot backup until it is
// deleted.
public CreateBackupRequest setHotToStandardTime(Instant hotToStandardTime) {
public CreateBackupRequest setHotToStandardTimeInstant(java.time.Instant hotToStandardTime) {
Preconditions.checkNotNull(hotToStandardTime);
requestBuilder
.getBackupBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
*/
package com.google.cloud.bigtable.admin.v2.models;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;

import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.bigtable.admin.v2.ChangeStreamConfig;
import com.google.bigtable.admin.v2.ColumnFamily;
import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString;
import com.google.protobuf.Duration;
import javax.annotation.Nonnull;
import org.threeten.bp.Duration;

/**
* Fluent wrapper for {@link com.google.bigtable.admin.v2.CreateTableRequest}
Expand Down Expand Up @@ -107,15 +110,24 @@ public CreateTableRequest addSplit(ByteString key) {
return this;
}

/**
* This method is obsolete. Use {@link #addChangeStreamRetentionDuration(java.time.Duration)}
* instead.
*/
@ObsoleteApi("Use addChangeStreamRetentionDuration(java.time.Duration) instead.")
public CreateTableRequest addChangeStreamRetention(org.threeten.bp.Duration retention) {
return addChangeStreamRetentionDuration(toJavaTimeDuration(retention));
}

/** Add change stream retention period between 1 day and 7 days */
public CreateTableRequest addChangeStreamRetention(Duration retention) {
public CreateTableRequest addChangeStreamRetentionDuration(java.time.Duration retention) {
Preconditions.checkNotNull(retention);
requestBuilder
.getTableBuilder()
.setChangeStreamConfig(
ChangeStreamConfig.newBuilder()
.setRetentionPeriod(
com.google.protobuf.Duration.newBuilder()
Duration.newBuilder()
.setSeconds(retention.getSeconds())
.setNanos(retention.getNano())
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package com.google.cloud.bigtable.admin.v2.models;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;
import static com.google.api.gax.util.TimeConversionUtils.toThreetenDuration;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.bigtable.admin.v2.GcRule;
import com.google.bigtable.admin.v2.GcRule.Intersection;
import com.google.bigtable.admin.v2.GcRule.Union;
Expand All @@ -26,7 +30,6 @@
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import org.threeten.bp.Duration;

// TODO(igorbernstein2): the distinction between GcRule & GCRule is too subtle, use fully qualified
// names for the protos.
Expand Down Expand Up @@ -70,15 +73,21 @@ public VersionRule maxVersions(int maxVersion) {
* @param timeUnit - timeunit for the age
*/
public DurationRule maxAge(long maxAge, TimeUnit timeUnit) {
return maxAge(Duration.ofNanos(TimeUnit.NANOSECONDS.convert(maxAge, timeUnit)));
return maxAgeDuration(
java.time.Duration.ofNanos(TimeUnit.NANOSECONDS.convert(maxAge, timeUnit)));
}

/** This method is obsolete. Use {@link #maxAgeDuration(java.time.Duration)} instead. */
@ObsoleteApi("Use maxAgeDuration(java.time.Duration) instead.")
public DurationRule maxAge(org.threeten.bp.Duration duration) {
return maxAgeDuration(toJavaTimeDuration(duration));
}
/**
* Creates a new instance of the DurationRule
*
* @param duration - age expressed as duration
*/
public DurationRule maxAge(Duration duration) {
public DurationRule maxAgeDuration(java.time.Duration duration) {
return new DurationRule(duration);
}

Expand All @@ -91,8 +100,9 @@ public DefaultRule defaultRule() {
public GCRule fromProto(GcRule source) {
switch (source.getRuleCase()) {
case MAX_AGE:
return GCRULES.maxAge(
Duration.ofSeconds(source.getMaxAge().getSeconds(), source.getMaxAge().getNanos()));
return GCRULES.maxAgeDuration(
java.time.Duration.ofSeconds(
source.getMaxAge().getSeconds(), source.getMaxAge().getNanos()));

case MAX_NUM_VERSIONS:
return GCRULES.maxVersions(source.getMaxNumVersions());
Expand Down Expand Up @@ -287,16 +297,22 @@ public GcRule toProto() {
public static final class DurationRule implements GCRule {
private final com.google.protobuf.Duration.Builder builder;

private DurationRule(Duration duration) {
private DurationRule(java.time.Duration duration) {
this.builder =
com.google.protobuf.Duration.newBuilder()
.setSeconds(duration.getSeconds())
.setNanos(duration.getNano());
}

/** This method is obsolete. Use {@link #getMaxAgeDuration()} instead. */
@ObsoleteApi("Use getMaxAgeDuration() instead.")
public org.threeten.bp.Duration getMaxAge() {
return toThreetenDuration(getMaxAgeDuration());
}

/** Gets the configured maximum age */
public Duration getMaxAge() {
return Duration.ofSeconds(builder.getSeconds(), builder.getNanos());
public java.time.Duration getMaxAgeDuration() {
return java.time.Duration.ofSeconds(builder.getSeconds(), builder.getNanos());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package com.google.cloud.bigtable.admin.v2.models;

import static com.google.api.gax.util.TimeConversionUtils.toThreetenDuration;

import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.bigtable.admin.v2.TableName;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
Expand All @@ -25,7 +28,6 @@
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nonnull;
import org.threeten.bp.Duration;

/** Wrapper for {@link Table} protocol buffer object */
public final class Table {
Expand Down Expand Up @@ -104,7 +106,7 @@ public com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState toProto(
private final Map<String, ReplicationState> replicationStatesByClusterId;
private final List<ColumnFamily> columnFamilies;

private final Duration changeStreamRetention;
private final java.time.Duration changeStreamRetention;

@InternalApi
public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto) {
Expand All @@ -123,10 +125,10 @@ public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto)
columnFamilies.add(ColumnFamily.fromProto(entry.getKey(), entry.getValue()));
}

Duration changeStreamConfig = null;
java.time.Duration changeStreamConfig = null;
if (proto.hasChangeStreamConfig()) {
changeStreamConfig =
Duration.ofSeconds(
java.time.Duration.ofSeconds(
proto.getChangeStreamConfig().getRetentionPeriod().getSeconds(),
proto.getChangeStreamConfig().getRetentionPeriod().getNanos());
}
Expand All @@ -142,7 +144,7 @@ private Table(
TableName tableName,
Map<String, ReplicationState> replicationStatesByClusterId,
List<ColumnFamily> columnFamilies,
Duration changeStreamRetention) {
java.time.Duration changeStreamRetention) {
this.instanceId = tableName.getInstance();
this.id = tableName.getTable();
this.replicationStatesByClusterId = replicationStatesByClusterId;
Expand All @@ -168,7 +170,13 @@ public List<ColumnFamily> getColumnFamilies() {
return columnFamilies;
}

public Duration getChangeStreamRetention() {
/** This method is obsolete. Use {@link #getChangeStreamRetentionDuration()} instead. */
@ObsoleteApi("Use getChangeStreamRetention() instead.")
public org.threeten.bp.Duration getChangeStreamRetention() {
return toThreetenDuration(getChangeStreamRetentionDuration());
}

public java.time.Duration getChangeStreamRetentionDuration() {
return changeStreamRetention;
}

Expand Down
Loading

0 comments on commit 71aa9a8

Please sign in to comment.