Skip to content

Commit 3cefb4f

Browse files
igorbernstein2pongad
authored andcommitted
Bigtable: table model improvements (#3640)
* Flatten cluster * expose column families as list only * use relative ids for tables & families
1 parent 47bd674 commit 3cefb4f

File tree

6 files changed

+90
-312
lines changed

6 files changed

+90
-312
lines changed

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
/** Wrapper for {@link ColumnFamily} protocol buffer object */
2828
public final class ColumnFamily {
29-
// TODO(igorbernstein2): rename this to `name`
3029
private final String id;
3130
private final GCRule rule;
3231

@@ -44,21 +43,21 @@ private ColumnFamily(String id, GCRule rule) {
4443
}
4544

4645
/**
47-
* Gets the columnfamily name
46+
* Gets the column family's id.
4847
*/
4948
public String getId() {
5049
return id;
5150
}
5251

5352
/**
54-
* Get's the GCRule configured for the columnfamily
53+
* Get's the GCRule configured for the column family.
5554
*/
5655
public GCRule getGCRule() {
5756
return rule;
5857
}
5958

6059
/**
61-
* Returns true if a GCRule has been configured for the family
60+
* Returns true if a GCRule has been configured for the family.
6261
*/
6362
public boolean hasGCRule() {
6463
return !RuleCase.RULE_NOT_SET.equals(rule.toProto().getRuleCase());

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public boolean equals(Object o) {
277277
return false;
278278
}
279279
VersionRule that = (VersionRule) o;
280-
return Objects.equal(builder, that.builder);
280+
return Objects.equal(builder.build(), that.builder.build());
281281
}
282282

283283
@Override
@@ -324,7 +324,7 @@ public boolean equals(Object o) {
324324
return false;
325325
}
326326
DurationRule that = (DurationRule) o;
327-
return Objects.equal(builder, that.builder);
327+
return Objects.equal(builder.build(), that.builder.build());
328328
}
329329

330330
@Override

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,68 @@
1616
package com.google.cloud.bigtable.admin.v2.models;
1717

1818
import com.google.api.core.InternalApi;
19+
import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState;
1920
import com.google.bigtable.admin.v2.TableName;
20-
import com.google.common.base.MoreObjects;
2121
import com.google.common.base.Objects;
22+
import com.google.common.collect.ImmutableList;
2223
import com.google.common.collect.ImmutableMap;
23-
import java.util.Collection;
24+
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Map.Entry;
2627
import javax.annotation.Nonnull;
2728

2829
/** Wrapper for {@link Table} protocol buffer object */
2930
public final class Table {
30-
private final TableName tableName;
31-
// TODO(igorbernstein2): avoid duplication of id as keys and embedded in the models
32-
private final Map<String, ClusterState> clusterStates;
33-
private final Map<String, ColumnFamily> columnFamilies;
31+
private final String id;
32+
private final String instanceId;
33+
private final Map<String, ReplicationState> replicationStatesByClusterId;
34+
private final List<ColumnFamily> columnFamilies;
3435

3536
@InternalApi
3637
public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto) {
37-
ImmutableMap.Builder<String, ClusterState> clusterStates = ImmutableMap.builder();
38+
ImmutableMap.Builder<String, ReplicationState> replicationStates = ImmutableMap.builder();
3839

3940
for (Entry<String, com.google.bigtable.admin.v2.Table.ClusterState> entry : proto.getClusterStatesMap().entrySet()) {
40-
clusterStates.put(entry.getKey(), ClusterState.fromProto(entry.getKey(), entry.getValue()));
41+
replicationStates.put(entry.getKey(), entry.getValue().getReplicationState());
4142
}
4243

43-
ImmutableMap.Builder<String, ColumnFamily> columnFamilies = ImmutableMap.builder();
44+
ImmutableList.Builder<ColumnFamily> columnFamilies = ImmutableList.builder();
4445

4546
for (Entry<String, com.google.bigtable.admin.v2.ColumnFamily> entry : proto.getColumnFamiliesMap().entrySet()) {
46-
columnFamilies.put(entry.getKey(), ColumnFamily.fromProto(entry.getKey(), entry.getValue()));
47+
columnFamilies.add(ColumnFamily.fromProto(entry.getKey(), entry.getValue()));
4748
}
4849

4950
return new Table(
5051
TableName.parse(proto.getName()),
51-
clusterStates.build(),
52+
replicationStates.build(),
5253
columnFamilies.build());
5354
}
5455

5556
private Table(TableName tableName,
56-
Map<String, ClusterState> clusterStates,
57-
Map<String, ColumnFamily> columnFamilies) {
58-
this.tableName = tableName;
59-
this.clusterStates = clusterStates;
57+
Map<String, ReplicationState> replicationStatesByClusterId,
58+
List<ColumnFamily> columnFamilies) {
59+
this.instanceId = tableName.getInstance();
60+
this.id = tableName.getTable();
61+
this.replicationStatesByClusterId = replicationStatesByClusterId;
6062
this.columnFamilies = columnFamilies;
6163
}
6264

63-
/**
64-
* Gets the unique name of the table in the format:
65-
* projects/{project}/instances/{instance}/tables/{tableId}
66-
*/
67-
public TableName getTableName() {
68-
return tableName;
65+
/** Gets the table's id. */
66+
public String getId() {
67+
return id;
6968
}
7069

71-
// TODO(igorbernstein2): don't expose the objects as both maps & lists
72-
/**
73-
* Returns state of the table by clusters in the instance as map of clusterId and {@link
74-
* ClusterState}
75-
*/
76-
public Map<String, ClusterState> getClusterStatesMap() {
77-
return clusterStates;
70+
/** Gets the id of the instance that owns this Table. */
71+
public String getInstanceId() {
72+
return instanceId;
7873
}
7974

80-
/**
81-
* Returns a map of columfamilies in the table keyed by columnfamily and name
82-
*/
83-
public Map<String, ColumnFamily> getColumnFamiliesMap() {
84-
return columnFamilies;
85-
}
86-
87-
/**
88-
* Returns state of the table by clusters in the instance as a Collection
89-
*/
90-
public Collection<ClusterState> getClusterStates() {
91-
return clusterStates.values();
75+
public Map<String, ReplicationState> getReplicationStatesByClusterId() {
76+
return replicationStatesByClusterId;
9277
}
9378

94-
/**
95-
* Returns all columnfamilies in the table as a Collection
96-
*/
97-
public Collection<ColumnFamily> getColumnFamiles() {
98-
return columnFamilies.values();
79+
public List<ColumnFamily> getColumnFamilies() {
80+
return columnFamilies;
9981
}
10082

10183
@Override
@@ -107,22 +89,15 @@ public boolean equals(Object o) {
10789
return false;
10890
}
10991
Table table = (Table) o;
110-
return Objects.equal(tableName, table.tableName) &&
111-
Objects.equal(clusterStates, table.clusterStates) &&
92+
return Objects.equal(id, table.id) &&
93+
Objects.equal(instanceId, table.instanceId) &&
94+
Objects
95+
.equal(replicationStatesByClusterId, table.replicationStatesByClusterId) &&
11296
Objects.equal(columnFamilies, table.columnFamilies);
11397
}
11498

11599
@Override
116100
public int hashCode() {
117-
return Objects.hashCode(tableName, clusterStates, columnFamilies);
118-
}
119-
120-
@Override
121-
public String toString() {
122-
return MoreObjects.toStringHelper(this)
123-
.add("tableName", tableName)
124-
.add("clusterStates", getClusterStates())
125-
.add("columnFamiles", getColumnFamiles())
126-
.toString();
101+
return Objects.hashCode(id, instanceId, replicationStatesByClusterId, columnFamilies);
127102
}
128103
}

google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableTableAdminClientIT.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.bigtable.admin.v2.InstanceName;
2525
import com.google.bigtable.admin.v2.TableName;
2626
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
27+
import com.google.cloud.bigtable.admin.v2.models.ColumnFamily;
2728
import com.google.cloud.bigtable.admin.v2.models.GCRules.DurationRule;
2829
import com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule;
2930
import com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule;
@@ -32,9 +33,11 @@
3233
import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest;
3334
import com.google.cloud.bigtable.admin.v2.models.ConsistencyToken;
3435
import com.google.cloud.bigtable.admin.v2.models.Table;
36+
import com.google.common.collect.Maps;
3537
import com.google.protobuf.ByteString;
3638
import java.io.IOException;
3739
import java.util.List;
40+
import java.util.Map;
3841
import org.junit.AfterClass;
3942
import org.junit.AssumptionViolatedException;
4043
import org.junit.Before;
@@ -88,13 +91,18 @@ public void createTable() {
8891
try {
8992
Table tableResponse = tableAdmin.createTable(createTableReq);
9093
assertNotNull(tableResponse);
91-
assertEquals(tableId, tableResponse.getTableName().getTable());
92-
assertEquals(2, tableResponse.getColumnFamiles().size());
93-
assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGCRule());
94-
assertTrue(tableResponse.getColumnFamiliesMap().get("cf2").hasGCRule());
94+
assertEquals(tableId, tableResponse.getId());
95+
96+
Map<String, ColumnFamily> columnFamilyById = Maps.newHashMap();
97+
for (ColumnFamily columnFamily : tableResponse.getColumnFamilies()) {
98+
columnFamilyById.put(columnFamily.getId(), columnFamily);
99+
}
100+
assertEquals(2, tableResponse.getColumnFamilies().size());
101+
assertFalse(columnFamilyById.get("cf1").hasGCRule());
102+
assertTrue(columnFamilyById.get("cf2").hasGCRule());
95103
assertEquals(
96104
10,
97-
((VersionRule) tableResponse.getColumnFamiliesMap().get("cf2").getGCRule())
105+
((VersionRule) columnFamilyById.get("cf2").getGCRule())
98106
.getMaxVersions());
99107
} finally {
100108
tableAdmin.deleteTable(tableId);
@@ -131,35 +139,40 @@ public void modifyFamilies() {
131139
try {
132140
tableAdmin.createTable(CreateTableRequest.of(tableId));
133141
Table tableResponse = tableAdmin.modifyFamilies(modifyFamiliesReq);
134-
assertEquals(5, tableResponse.getColumnFamiles().size());
135-
assertNotNull(tableResponse.getColumnFamiliesMap().get("mf1"));
136-
assertNotNull(tableResponse.getColumnFamiliesMap().get("mf2"));
142+
143+
Map<String, ColumnFamily> columnFamilyById = Maps.newHashMap();
144+
for (ColumnFamily columnFamily : tableResponse.getColumnFamilies()) {
145+
columnFamilyById.put(columnFamily.getId(), columnFamily);
146+
}
147+
assertEquals(5, columnFamilyById.size());
148+
assertNotNull(columnFamilyById.get("mf1"));
149+
assertNotNull(columnFamilyById.get("mf2"));
137150
assertEquals(
138151
2,
139-
((UnionRule) tableResponse.getColumnFamiliesMap().get("mf1").getGCRule())
152+
((UnionRule) columnFamilyById.get("mf1").getGCRule())
140153
.getRulesList()
141154
.size());
142155
assertEquals(
143156
1000,
144-
((DurationRule) tableResponse.getColumnFamiliesMap().get("mf2").getGCRule())
157+
((DurationRule) columnFamilyById.get("mf2").getGCRule())
145158
.getMaxAge()
146159
.getSeconds());
147160
assertEquals(
148161
20000,
149-
((DurationRule) tableResponse.getColumnFamiliesMap().get("mf2").getGCRule())
162+
((DurationRule) columnFamilyById.get("mf2").getGCRule())
150163
.getMaxAge()
151164
.getNano());
152165
assertEquals(
153166
2,
154-
((IntersectionRule) tableResponse.getColumnFamiliesMap().get("mf3").getGCRule())
167+
((IntersectionRule) columnFamilyById.get("mf3").getGCRule())
155168
.getRulesList()
156169
.size());
157170
assertEquals(
158171
360,
159-
((DurationRule) tableResponse.getColumnFamiliesMap().get("mf4").getGCRule())
172+
((DurationRule) columnFamilyById.get("mf4").getGCRule())
160173
.getMaxAge()
161174
.getSeconds());
162-
assertNotNull(tableResponse.getColumnFamiliesMap().get("mf7"));
175+
assertNotNull(columnFamilyById.get("mf7"));
163176
} finally {
164177
tableAdmin.deleteTable(tableId);
165178
}
@@ -180,7 +193,7 @@ public void getTable() {
180193
tableAdmin.createTable(CreateTableRequest.of(tableId));
181194
Table tableResponse = tableAdmin.getTable(tableId);
182195
assertNotNull(tableResponse);
183-
assertEquals(tableId, tableResponse.getTableName().getTable());
196+
assertEquals(tableId, tableResponse.getId());
184197
} finally {
185198
tableAdmin.deleteTable(tableId);
186199
}

0 commit comments

Comments
 (0)