Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bigtable: wrap proto enums #3659

Merged
merged 9 commits into from
Sep 18, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public CreateInstanceRequest setDisplayName(@Nonnull String displayName) {
@SuppressWarnings("WeakerAccess")
public CreateInstanceRequest setType(@Nonnull Instance.Type type) {
Preconditions.checkNotNull(type);
Preconditions.checkArgument(type != Instance.Type.NOT_KNOWN, "Type must be specified");

This comment was marked as spam.

This comment was marked as spam.

builder.getInstanceBuilder().setType(type.toProto());
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
*/
public final class Instance {
public enum Type {
/** The type of the instance is unknown */
NOT_KNOWN(com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED),
/** An instance meant for production use. `serve_nodes` must be set on the cluster. */
PRODUCTION(com.google.bigtable.admin.v2.Instance.Type.PRODUCTION),
/** The instance is meant for development and testing purposes only. */
DEVELOPMENT(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT);
DEVELOPMENT(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT),
/** The type of instance is not known by this client. Please upgrade your client. */
UNRECOGNIZED(com.google.bigtable.admin.v2.Instance.Type.UNRECOGNIZED);

private final com.google.bigtable.admin.v2.Instance.Type proto;

Expand All @@ -46,12 +46,15 @@ public enum Type {
*/
@InternalApi
public static Type fromProto(com.google.bigtable.admin.v2.Instance.Type proto) {
Preconditions.checkNotNull(proto);
Preconditions.checkArgument(proto != com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED,
"Server instance type must always be specified");
for (Type type : values()) {
if (type.proto.equals(proto)) {
return type;
}
}
return NOT_KNOWN;
return UNRECOGNIZED;
}

Type(com.google.bigtable.admin.v2.Instance.Type proto) {
Expand All @@ -69,15 +72,17 @@ public com.google.bigtable.admin.v2.Instance.Type toProto() {
}

public enum State {
/** The state of the instance could not be determined. */
/** The state of the instance could not be determined by the server. */
NOT_KNOWN(com.google.bigtable.admin.v2.Instance.State.STATE_NOT_KNOWN),
/** The instance has been successfully created and can serve requests to its tables. */
READY(com.google.bigtable.admin.v2.Instance.State.READY),
/**
* The instance is currently being created, and may be destroyed if the creation process
* encounters an error.
*/
CREATING(com.google.bigtable.admin.v2.Instance.State.CREATING);
CREATING(com.google.bigtable.admin.v2.Instance.State.CREATING),
/** The state of instance is not known by this client. Please upgrade your client. */
UNRECOGNIZED(com.google.bigtable.admin.v2.Instance.State.UNRECOGNIZED);

private final com.google.bigtable.admin.v2.Instance.State proto;

Expand All @@ -87,12 +92,13 @@ public enum State {
*/
@InternalApi
public static State fromProto(com.google.bigtable.admin.v2.Instance.State proto) {
Preconditions.checkNotNull(proto);
for (State state : values()) {
if (state.proto.equals(proto)) {
return state;
}
}
return NOT_KNOWN;
return UNRECOGNIZED;
}

State(com.google.bigtable.admin.v2.Instance.State proto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.cloud.bigtable.admin.v2.models;

import com.google.api.core.InternalApi;
import com.google.common.base.Preconditions;

/** Storage media types for persisting Bigtable data. */
public enum StorageType {
Expand All @@ -24,7 +25,9 @@ public enum StorageType {
/** Flash (SSD) storage should be used. */
HDD(com.google.bigtable.admin.v2.StorageType.HDD),
/** Magnetic drive (HDD) storage should be used. */
SSD(com.google.bigtable.admin.v2.StorageType.SSD);
SSD(com.google.bigtable.admin.v2.StorageType.SSD),
/** The storage type is not known by this client. Please upgrade your client. */
UNRECOGNIZED(com.google.bigtable.admin.v2.StorageType.UNRECOGNIZED);

private final com.google.bigtable.admin.v2.StorageType proto;

Expand All @@ -34,12 +37,14 @@ public enum StorageType {
*/
@InternalApi
public static StorageType fromProto(com.google.bigtable.admin.v2.StorageType proto) {
Preconditions.checkNotNull(proto);

for (StorageType type : values()) {
if (type.proto.equals(proto)) {
return type;
}
}
return NOT_KNOWN;
return UNRECOGNIZED;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.core.InternalApi;
import com.google.bigtable.admin.v2.TableName;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
Expand Down Expand Up @@ -50,7 +51,11 @@ public enum ReplicationState {
* The table can serve Data API requests from this cluster. Depending on replication delay,
* reads may not immediately reflect the state of the table in other clusters.
*/
READY(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.READY);
READY(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.READY),

/** The replication state of table is not known by this client. Please upgrade your client. */
UNRECOGNIZED(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.UNRECOGNIZED);


private final com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto;

Expand All @@ -60,12 +65,14 @@ public enum ReplicationState {
*/
@InternalApi
public static ReplicationState fromProto(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto) {
Preconditions.checkNotNull(proto);

for (ReplicationState state : values()) {
if (state.proto.equals(proto)) {
return state;
}
}
return NOT_KNOWN;
return UNRECOGNIZED;
}

ReplicationState(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Arrays;
import java.util.Collection;
import com.google.common.collect.Lists;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -74,63 +72,44 @@ public void testRequiresName() {

@Test
public void testTypeEnumUpToDate() {
Multimap<Instance.Type, com.google.bigtable.admin.v2.Instance.Type> modelToProtoMap =
ArrayListMultimap.create();
List<com.google.bigtable.admin.v2.Instance.Type> validProtoValues =
Lists.newArrayList(com.google.bigtable.admin.v2.Instance.Type.values());

for (com.google.bigtable.admin.v2.Instance.Type protoValue : com.google.bigtable.admin.v2.Instance.Type
.values()) {
Instance.Type modelValue = Instance.Type.fromProto(protoValue);
modelToProtoMap.put(modelValue, protoValue);
}

// Make sure all model values are used
assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Instance.Type.values()));

// Make sure unknown is handled properly (it has multiple mappings)
assertThat(modelToProtoMap).valuesForKey(Instance.Type.NOT_KNOWN).containsExactly(
com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED,
com.google.bigtable.admin.v2.Instance.Type.UNRECOGNIZED
);
// TYPE_UNSPECIFIED is not surfaced
validProtoValues.remove(com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED);

// Make sure everything else has exactly 1 mapping
modelToProtoMap.removeAll(Instance.Type.NOT_KNOWN);
Exception actualError = null;
try {
Instance.Type.fromProto(com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED);
} catch (Exception e) {
actualError = e;
}
assertThat(actualError).isInstanceOf(IllegalArgumentException.class);

for (Instance.Type modelState : modelToProtoMap.keySet()) {
Collection<com.google.bigtable.admin.v2.Instance.Type> protoStates = modelToProtoMap
.get(modelState);
List<Instance.Type> validModelValues = Lists.newArrayList(Instance.Type.values());

assertThat(protoStates).hasSize(1);
List<Instance.Type> actualModelValues = Lists.newArrayList();
for (com.google.bigtable.admin.v2.Instance.Type protoValue : validProtoValues) {
actualModelValues.add(Instance.Type.fromProto(protoValue));
}

assertThat(actualModelValues).containsExactlyElementsIn(validModelValues);
}

@Test
public void testStateEnumUpToDate() {
Multimap<Instance.State, com.google.bigtable.admin.v2.Instance.State> modelToProtoMap =
ArrayListMultimap.create();
List<com.google.bigtable.admin.v2.Instance.State> validProtoValues =
Lists.newArrayList(com.google.bigtable.admin.v2.Instance.State.values());

for (com.google.bigtable.admin.v2.Instance.State protoValue : com.google.bigtable.admin.v2.Instance.State
.values()) {
Instance.State modelValue = Instance.State.fromProto(protoValue);
modelToProtoMap.put(modelValue, protoValue);
}

// Make sure all model values are used
assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Instance.State.values()));

// Make sure unknown is handled properly (it has multiple mappings)
assertThat(modelToProtoMap).valuesForKey(Instance.State.NOT_KNOWN).containsExactly(
com.google.bigtable.admin.v2.Instance.State.STATE_NOT_KNOWN,
com.google.bigtable.admin.v2.Instance.State.UNRECOGNIZED
);

// Make sure everything else has exactly 1 mapping
modelToProtoMap.removeAll(Instance.State.NOT_KNOWN);
List<Instance.State> validModelValues = Lists.newArrayList(Instance.State.values());

for (Instance.State modelState : modelToProtoMap.keySet()) {
Collection<com.google.bigtable.admin.v2.Instance.State> protoStates = modelToProtoMap
.get(modelState);
List<Instance.State> actualModelValues = Lists.newArrayList();

assertThat(protoStates).hasSize(1);
for (com.google.bigtable.admin.v2.Instance.State protoValue : validProtoValues) {
Instance.State modelValue = Instance.State.fromProto(protoValue);
actualModelValues.add(modelValue);
}

assertThat(actualModelValues).containsExactlyElementsIn(validModelValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Arrays;
import java.util.Collection;
import com.google.common.collect.Lists;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -30,32 +28,18 @@ public class StorageTypeTest {

@Test
public void testUpToDate() {
Multimap<StorageType, com.google.bigtable.admin.v2.StorageType> modelToProtoMap =
ArrayListMultimap.create();
List<com.google.bigtable.admin.v2.StorageType> validProtoValues =
Lists.newArrayList(com.google.bigtable.admin.v2.StorageType.values());

for (com.google.bigtable.admin.v2.StorageType protoValue : com.google.bigtable.admin.v2.StorageType
.values()) {
StorageType modelValue = StorageType.fromProto(protoValue);
modelToProtoMap.put(modelValue, protoValue);
}

// Make sure all model values are used
assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(StorageType.values()));

// Make sure unknown is handled properly (it has multiple mappings)
assertThat(modelToProtoMap).valuesForKey(StorageType.NOT_KNOWN).containsExactly(
com.google.bigtable.admin.v2.StorageType.UNRECOGNIZED,
com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED
);
List<StorageType> validModelValues = Lists.newArrayList(StorageType.values());

// Make sure everything else has exactly 1 mapping
modelToProtoMap.removeAll(StorageType.NOT_KNOWN);
List<StorageType> actualModelValues = Lists.newArrayList();

for (StorageType modelState : modelToProtoMap.keySet()) {
Collection<com.google.bigtable.admin.v2.StorageType> protoStates = modelToProtoMap
.get(modelState);

assertThat(protoStates).hasSize(1);
for (com.google.bigtable.admin.v2.StorageType protoValue : validProtoValues) {
StorageType modelValue = StorageType.fromProto(protoValue);
actualModelValues.add(modelValue);
}

assertThat(actualModelValues).containsExactlyElementsIn(validModelValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import com.google.bigtable.admin.v2.GcRule;
import com.google.bigtable.admin.v2.Table.TimestampGranularity;
import com.google.bigtable.admin.v2.TableName;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Arrays;
import java.util.Collection;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map.Entry;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -92,33 +90,20 @@ public void testFromProto() {

@Test
public void testReplicationStateEnumUpToDate() {
Multimap<Table.ReplicationState, com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState> modelToProtoMap =
ArrayListMultimap.create();
List<com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState> validProtoValues =
Lists.newArrayList(
com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.values());

for (com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState protoValue : com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState
.values()) {
Table.ReplicationState modelValue = Table.ReplicationState.fromProto(protoValue);
modelToProtoMap.put(modelValue, protoValue);
}

// Make sure all model values are used
assertThat(modelToProtoMap.keys())
.containsAllIn(Arrays.asList(Table.ReplicationState.values()));

// Make sure unknown is handled properly (it has multiple mappings)
assertThat(modelToProtoMap).valuesForKey(Table.ReplicationState.NOT_KNOWN).containsExactly(
com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.STATE_NOT_KNOWN,
com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.UNRECOGNIZED
);

// Make sure everything else has exactly 1 mapping
modelToProtoMap.removeAll(Table.ReplicationState.NOT_KNOWN);
List<Table.ReplicationState> validModelValues = Lists
.newArrayList(Table.ReplicationState.values());

for (Table.ReplicationState modelState : modelToProtoMap.keySet()) {
Collection<com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState> protoStates = modelToProtoMap
.get(modelState);
List<Table.ReplicationState> actualModelValues = Lists.newArrayList();

assertThat(protoStates).hasSize(1);
for (com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState protoValue : validProtoValues) {
Table.ReplicationState modelValue = Table.ReplicationState.fromProto(protoValue);
actualModelValues.add(modelValue);
}

assertThat(actualModelValues).containsExactlyElementsIn(validModelValues);
}
}