[go: up one dir, main page]

Skip to content

Commit

Permalink
feat: Add models for type APIs (#2160)
Browse files Browse the repository at this point in the history
Change-Id: I5882459c862cc1deda11606e181cee38ac4d9099
  • Loading branch information
steveniemitz committed Mar 8, 2024
1 parent 430dffe commit 8277ea8
Show file tree
Hide file tree
Showing 24 changed files with 999 additions and 21 deletions.
12 changes: 12 additions & 0 deletions google-cloud-bigtable/clirr-ignored-differences.xml
Expand Up @@ -180,4 +180,16 @@
<className>com/google/cloud/bigtable/data/v2/models/MutateRowsException</className>
<method>*</method>
</difference>
<!-- InternalApi was updated -->
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/bigtable/data/v2/models/MutationApi</className>
<method>*</method>
</difference>
<!-- InternalApi was updated -->
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/bigtable/data/v2/models/ChangeStreamRecordAdapter$ChangeStreamRecordBuilder</className>
<method>*</method>
</difference>
</differences>
Expand Up @@ -18,7 +18,6 @@
import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES;

import com.google.api.core.InternalApi;
import com.google.bigtable.admin.v2.GcRule;
import com.google.bigtable.admin.v2.GcRule.RuleCase;
import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule;
import com.google.common.base.MoreObjects;
Expand All @@ -28,35 +27,44 @@
public final class ColumnFamily {
private final String id;
private final GCRule rule;
private final Type valueType;

@InternalApi
public static ColumnFamily fromProto(String id, com.google.bigtable.admin.v2.ColumnFamily proto) {
// TODO(igorbernstein): can getGcRule ever be null?
GcRule ruleProto = MoreObjects.firstNonNull(proto.getGcRule(), GcRule.getDefaultInstance());

return new ColumnFamily(id, GCRULES.fromProto(ruleProto));
return new ColumnFamily(
id, GCRULES.fromProto(proto.getGcRule()), Type.fromProto(proto.getValueType()));
}

private ColumnFamily(String id, GCRule rule) {
private ColumnFamily(String id, GCRule rule, Type type) {
this.id = id;
this.rule = rule;
this.valueType = type;
}

/** Gets the column family's id. */
public String getId() {
return id;
}

/** Get's the GCRule configured for the column family. */
/** Gets the GCRule configured for the column family. */
public GCRule getGCRule() {
return rule;
}

/* Gets the valueType configured for the column family. */
public Type getValueType() {
return valueType;
}

/** Returns true if a GCRule has been configured for the family. */
public boolean hasGCRule() {
return !RuleCase.RULE_NOT_SET.equals(rule.toProto().getRuleCase());
}

public boolean hasValueType() {
return valueType.getClass() != Type.Raw.class;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -66,16 +74,22 @@ public boolean equals(Object o) {
return false;
}
ColumnFamily that = (ColumnFamily) o;
return Objects.equal(id, that.id) && Objects.equal(rule, that.rule);
return Objects.equal(id, that.id)
&& Objects.equal(rule, that.rule)
&& Objects.equal(valueType, that.valueType);
}

@Override
public int hashCode() {
return Objects.hashCode(id, rule);
return Objects.hashCode(id, rule, valueType);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("id", id).add("GCRule", rule).toString();
return MoreObjects.toStringHelper(this)
.add("id", id)
.add("GCRule", rule)
.add("valueType", valueType)
.toString();
}
}
Expand Up @@ -63,11 +63,40 @@ public CreateTableRequest addFamily(String familyId) {
*
* @see GCRule for available options.
*/
public CreateTableRequest addFamily(String familyId, GCRule gcRule) {
public CreateTableRequest addFamily(@Nonnull String familyId, @Nonnull GCRule gcRule) {
return addFamily(familyId, gcRule, Type.raw());
}

/**
* Adds a new columnFamily with a {@link Type} to the configuration. Please note that calling this
* method with the same familyId will overwrite the previous family.
*
* @see Type for available options.
*/
public CreateTableRequest addFamily(@Nonnull String familyId, @Nonnull Type valueType) {
return addFamily(familyId, GCRules.GCRULES.defaultRule(), valueType);
}

/**
* Adds a new columnFamily with a {@link GCRule} and {@link Type} to the configuration. Please
* note that calling this method with the same familyId will overwrite the previous family.
*
* @see GCRule for available options.
* @see Type for available options.
*/
public CreateTableRequest addFamily(
@Nonnull String familyId, @Nonnull GCRule gcRule, @Nonnull Type valueType) {
Preconditions.checkNotNull(familyId);
requestBuilder
.getTableBuilder()
.putColumnFamilies(familyId, ColumnFamily.newBuilder().setGcRule(gcRule.toProto()).build());
Preconditions.checkNotNull(gcRule);
Preconditions.checkNotNull(valueType);

ColumnFamily.Builder builder = ColumnFamily.newBuilder().setGcRule(gcRule.toProto());

// Don't set the type if it's the default ("raw")
if (!valueType.equals(Type.raw())) {
builder.setValueType(valueType.toProto());
}
requestBuilder.getTableBuilder().putColumnFamilies(familyId, builder.build());
return this;
}

Expand Down
Expand Up @@ -55,9 +55,31 @@ public ModifyColumnFamiliesRequest addFamily(String familyId) {

/** Configures the name and {@link GCRule} of the new {@link ColumnFamily} to be created */
public ModifyColumnFamiliesRequest addFamily(String familyId, GCRule gcRule) {
return addFamily(familyId, gcRule, Type.raw());
}

/** Configures the name and {@link Type} of the new {@link ColumnFamily} to be created */
public ModifyColumnFamiliesRequest addFamily(String familyId, Type valueType) {
return addFamily(familyId, GCRules.GCRULES.defaultRule(), valueType);
}

/**
* Configures the name, {@link GCRule}, and {@link Type} of the new {@link ColumnFamily} to be
* created
*/
public ModifyColumnFamiliesRequest addFamily(
@Nonnull String familyId, @Nonnull GCRule gcRule, @Nonnull Type valueType) {
Preconditions.checkNotNull(gcRule);
Preconditions.checkNotNull(valueType);

Modification.Builder modification = Modification.newBuilder().setId(familyId);
modification.getCreateBuilder().setGcRule(gcRule.toProto());
com.google.bigtable.admin.v2.ColumnFamily.Builder createBuilder =
modification.getCreateBuilder().setGcRule(gcRule.toProto());

if (!valueType.equals(Type.raw())) {
createBuilder.setValueType(valueType.toProto());
}

modFamilyRequest.addModifications(modification.build());
return this;
}
Expand Down

0 comments on commit 8277ea8

Please sign in to comment.