1+ /*
2+ * Copyright 2018 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+ * in compliance with the License. You may obtain a copy of the License at
6+ *
7+ * https://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+ * or implied. See the License for the specific language governing permissions and limitations under
12+ * the License.
13+ */
114package com .google .cloud .bigtable .admin .v2 ;
215
316import java .io .IOException ;
2235import com .google .bigtable .admin .v2 .TableName ;
2336import com .google .cloud .bigtable .admin .v2 .models .TableAdminRequests .CreateTable ;
2437import com .google .cloud .bigtable .admin .v2 .models .TableAdminRequests .ModifyFamilies ;
38+ import com .google .cloud .bigtable .admin .v2 .models .TableAdminResponses ;
39+ import com .google .cloud .bigtable .admin .v2 .models .TableAdminResponses .TableResponse ;
2540import com .google .cloud .bigtable .admin .v2 .stub .BigtableTableAdminStub ;
2641import com .google .cloud .bigtable .admin .v2 .stub .BigtableTableAdminStubSettings ;
2742import com .google .common .annotations .VisibleForTesting ;
2843import com .google .common .base .Preconditions ;
2944import com .google .protobuf .ByteString ;
3045import com .google .protobuf .Empty ;
3146
47+ /** Table admin client for creating and configuring Bigtable tables. */
3248public class TableAdminClient implements AutoCloseable {
3349 private final BigtableTableAdminStub stub ;
3450 private final InstanceName instanceName ;
@@ -37,8 +53,8 @@ public static TableAdminClient create(InstanceName instanceName) throws IOExcept
3753 return new TableAdminClient (instanceName , BigtableTableAdminSettings .newBuilder ().build ());
3854 }
3955
40- public static TableAdminClient create (InstanceName instanceName ,
41- BigtableTableAdminSettings adminSettings ) throws IOException {
56+ public static TableAdminClient create (
57+ InstanceName instanceName , BigtableTableAdminSettings adminSettings ) throws IOException {
4258 return new TableAdminClient (instanceName , adminSettings );
4359 }
4460
@@ -47,25 +63,21 @@ public static TableAdminClient create(InstanceName instanceName, BigtableTableAd
4763 return new TableAdminClient (instanceName , stub );
4864 }
4965
50- public TableAdminClient (InstanceName instanceName , BigtableTableAdminSettings adminSettings )
66+ private TableAdminClient (InstanceName instanceName , BigtableTableAdminSettings adminSettings )
5167 throws IOException {
52- this (instanceName ,
53- ((BigtableTableAdminStubSettings ) adminSettings .getStubSettings ()).createStub ());
68+ this (
69+ instanceName ,
70+ ((BigtableTableAdminStubSettings ) adminSettings .getStubSettings ()).createStub ());
5471 }
5572
56- public TableAdminClient (InstanceName instanceName , BigtableTableAdminStub stub )
73+ private TableAdminClient (InstanceName instanceName , BigtableTableAdminStub stub )
5774 throws IOException {
5875 Preconditions .checkNotNull (instanceName );
5976 Preconditions .checkNotNull (stub );
6077 this .instanceName = instanceName ;
6178 this .stub = stub ;
6279 }
6380
64- @ SuppressWarnings ("unused" )
65- private BigtableTableAdminStub getTableAdminStub () {
66- return stub ;
67- }
68-
6981 public InstanceName getInstanceName () {
7082 return instanceName ;
7183 }
@@ -75,84 +87,112 @@ public void close() throws Exception {
7587 stub .close ();
7688 }
7789
78- public Table createTable (CreateTable createTable ) {
79- return this .stub .createTableCallable ().call (createTable .toProto (instanceName .toString ()));
90+ public TableResponse createTable (CreateTable createTable ) {
91+ Table table = this .stub .createTableCallable ().call (createTable .toProto (instanceName ));
92+ return TableAdminResponses .convertTable (table );
8093 }
8194
82- public ApiFuture <Table > createTableAsync (CreateTable createTable ) {
83- return this .stub .createTableCallable ().futureCall (createTable .toProto (instanceName .toString ()));
95+ public ApiFuture <TableResponse > createTableAsync (CreateTable createTable ) {
96+ return transformToTableResponse (
97+ this .stub .createTableCallable ().futureCall (createTable .toProto (instanceName )));
8498 }
8599
86- public Table modifyFamilies (ModifyFamilies modifyFamily ) {
100+ public TableResponse modifyFamilies (ModifyFamilies modifyFamily ) {
87101 ModifyColumnFamiliesRequest modReq =
88- modifyFamily .toProto (getUniqueTableName (modifyFamily .getTableId ()));
89- return this .stub .modifyColumnFamiliesCallable ().call (modReq );
102+ modifyFamily .toProto (getTableName (modifyFamily .getTableId ()));
103+ Table table = this .stub .modifyColumnFamiliesCallable ().call (modReq );
104+ return TableAdminResponses .convertTable (table );
90105 }
91106
92- public ApiFuture <Table > modifyFamiliesAsync (ModifyFamilies modifyFamily ) {
107+ public ApiFuture <TableResponse > modifyFamiliesAsync (ModifyFamilies modifyFamily ) {
93108 ModifyColumnFamiliesRequest modReq =
94- modifyFamily .toProto (getUniqueTableName (modifyFamily .getTableId ()));
95- return this .stub .modifyColumnFamiliesCallable ().futureCall (modReq );
109+ modifyFamily .toProto (getTableName (modifyFamily .getTableId ()));
110+ return transformToTableResponse ( this .stub .modifyColumnFamiliesCallable ().futureCall (modReq ) );
96111 }
97112
98113 public void deleteTable (String tableId ) {
99- this .stub .deleteTableCallable ().call (getDeleteTableRequest (tableId ));
114+ this .stub .deleteTableCallable ().call (composeDeleteTableRequest (tableId ));
100115 }
101116
102- public ApiFuture <Empty > deleteTableAsync (String tableId ) {
103- return this .stub .deleteTableCallable ().futureCall (getDeleteTableRequest (tableId ));
117+ public ApiFuture <Void > deleteTableAsync (String tableId ) {
118+ return transformToVoid (
119+ this .stub .deleteTableCallable ().futureCall (composeDeleteTableRequest (tableId )));
104120 }
105121
106- public Table getTable (String tableId ) {
107- return this .stub .getTableCallable ().call (getGetTableRequest (tableId ));
122+ public TableResponse getTable (String tableId ) {
123+ Table table = this .stub .getTableCallable ().call (composeGetTableRequest (tableId ));
124+ return TableAdminResponses .convertTable (table );
108125 }
109126
110- public ApiFuture <Table > getTableAsync (String tableId ) {
111- return this .stub .getTableCallable ().futureCall (getGetTableRequest (tableId ));
127+ public ApiFuture <TableResponse > getTableAsync (String tableId ) {
128+ return transformToTableResponse (
129+ this .stub .getTableCallable ().futureCall (composeGetTableRequest (tableId )));
112130 }
113131
114132 public List <TableName > listTables () {
115- ListTablesResponse listResp = this .stub .listTablesCallable ().call (getListTableRequest ());
116- return convert (listResp );
133+ ListTablesResponse listResp = this .stub .listTablesCallable ().call (composeListTableRequest ());
134+ return convertToTableNames (listResp );
117135 }
118136
119137 public ApiFuture <List <TableName >> listTablesAsync () {
120138 ApiFuture <ListTablesResponse > listResp =
121- this .stub .listTablesCallable ().futureCall (getListTableRequest ());
122-
123- return ApiFutures .transform (listResp , new ApiFunction <ListTablesResponse , List <TableName >>() {
124- @ Override
125- public List <TableName > apply (ListTablesResponse input ) {
126- return convert (input );
127- }
128- });
129- }
130-
131- /**
132- * Drops rows by prefix. If prefix is "*" drops all data
133- *
134- * @param tableId
135- * @param rowKeyPrefix
136- */
139+ this .stub .listTablesCallable ().futureCall (composeListTableRequest ());
140+
141+ return ApiFutures .transform (
142+ listResp ,
143+ new ApiFunction <ListTablesResponse , List <TableName >>() {
144+ @ Override
145+ public List <TableName > apply (ListTablesResponse input ) {
146+ return convertToTableNames (input );
147+ }
148+ });
149+ }
150+
137151 public void dropRowRange (String tableId , String rowKeyPrefix ) {
138- this .stub .dropRowRangeCallable ().call (getDropRowRangeRequest (tableId , rowKeyPrefix ));
152+ dropRowRange (tableId , ByteString .copyFromUtf8 (rowKeyPrefix ));
153+ }
154+
155+ public ApiFuture <Void > dropRowRangeAsync (String tableId , String rowKeyPrefix ) {
156+ return dropRowRangeAsync (tableId , ByteString .copyFromUtf8 (rowKeyPrefix ));
157+ }
158+
159+ public void dropAllData (String tableId ) {
160+ this .stub .dropRowRangeCallable ().call (composeDropRowRangeRequest (tableId , null , true ));
161+ }
162+
163+ public ApiFuture <Void > dropAllDataAsync (String tableId ) {
164+ return transformToVoid (
165+ this .stub
166+ .dropRowRangeCallable ()
167+ .futureCall (composeDropRowRangeRequest (tableId , null , true )));
139168 }
140169
141- public ApiFuture <Empty > dropRowRangeAsync (String tableId , String rowKeyPrefix ) {
142- return this .stub .dropRowRangeCallable ()
143- .futureCall (getDropRowRangeRequest (tableId , rowKeyPrefix ));
170+ public void dropRowRange (String tableId , ByteString rowKeyPrefix ) {
171+ this .stub .dropRowRangeCallable ().call (composeDropRowRangeRequest (tableId , rowKeyPrefix , false ));
144172 }
145173
146- public String GenerateConsistencyToken (String tableId ) {
147- return this .stub .generateConsistencyTokenCallable ().call (getGenConsistencyToken (tableId ))
148- .getConsistencyToken ();
174+ public ApiFuture <Void > dropRowRangeAsync (String tableId , ByteString rowKeyPrefix ) {
175+ return transformToVoid (
176+ this .stub
177+ .dropRowRangeCallable ()
178+ .futureCall (composeDropRowRangeRequest (tableId , rowKeyPrefix , false )));
179+ }
180+
181+ public String generateConsistencyToken (String tableId ) {
182+ return this .stub
183+ .generateConsistencyTokenCallable ()
184+ .call (composeGenerateConsistencyTokenRequest (tableId ))
185+ .getConsistencyToken ();
149186 }
150187
151188 public ApiFuture <String > generateConsistencyTokenAsync (String tableId ) {
152189 ApiFuture <GenerateConsistencyTokenResponse > tokenResp =
153- this .stub .generateConsistencyTokenCallable ().futureCall (getGenConsistencyToken (tableId ));
190+ this .stub
191+ .generateConsistencyTokenCallable ()
192+ .futureCall (composeGenerateConsistencyTokenRequest (tableId ));
154193
155- return ApiFutures .transform (tokenResp ,
194+ return ApiFutures .transform (
195+ tokenResp ,
156196 new ApiFunction <GenerateConsistencyTokenResponse , String >() {
157197 @ Override
158198 public String apply (GenerateConsistencyTokenResponse input ) {
@@ -162,15 +202,17 @@ public String apply(GenerateConsistencyTokenResponse input) {
162202 }
163203
164204 public boolean isConsistent (String tableId , String token ) {
165- return stub .checkConsistencyCallable ().call (getCheckConsistencyRequest (tableId , token ))
166- .getConsistent ();
205+ return stub .checkConsistencyCallable ()
206+ .call (composeCheckConsistencyRequest (tableId , token ))
207+ .getConsistent ();
167208 }
168209
169210 public ApiFuture <Boolean > isConsistentAsync (String tableId , String token ) {
170211 ApiFuture <CheckConsistencyResponse > CheckConsResp =
171- stub .checkConsistencyCallable ().futureCall (getCheckConsistencyRequest (tableId , token ));
212+ stub .checkConsistencyCallable ().futureCall (composeCheckConsistencyRequest (tableId , token ));
172213
173- return ApiFutures .transform (CheckConsResp ,
214+ return ApiFutures .transform (
215+ CheckConsResp ,
174216 new ApiFunction <CheckConsistencyResponse , Boolean >() {
175217 @ Override
176218 public Boolean apply (CheckConsistencyResponse input ) {
@@ -180,58 +222,82 @@ public Boolean apply(CheckConsistencyResponse input) {
180222 }
181223
182224 @ VisibleForTesting
183- String getUniqueTableName (String tableId ) {
225+ String getTableName (String tableId ) {
184226 return TableName .of (instanceName .getProject (), instanceName .getInstance (), tableId ).toString ();
185227 }
186228
187229 @ VisibleForTesting
188- ListTablesRequest getListTableRequest () {
230+ ListTablesRequest composeListTableRequest () {
189231 return ListTablesRequest .newBuilder ().setParent (instanceName .toString ()).build ();
190232 }
191233
192234 @ VisibleForTesting
193- GetTableRequest getGetTableRequest (String tableId ) {
194- return GetTableRequest .newBuilder ().setName (getUniqueTableName (tableId )).build ();
235+ GetTableRequest composeGetTableRequest (String tableId ) {
236+ return GetTableRequest .newBuilder ().setName (getTableName (tableId )).build ();
195237 }
196238
197239 @ VisibleForTesting
198- DeleteTableRequest getDeleteTableRequest (String tableId ) {
199- return DeleteTableRequest .newBuilder ().setName (getUniqueTableName (tableId )).build ();
240+ DeleteTableRequest composeDeleteTableRequest (String tableId ) {
241+ return DeleteTableRequest .newBuilder ().setName (getTableName (tableId )).build ();
200242 }
201243
202244 @ VisibleForTesting
203- DropRowRangeRequest getDropRowRangeRequest (String tableId , String rowKeyPrefix ) {
204- Builder dropRowReq = DropRowRangeRequest .newBuilder ().setName (getUniqueTableName (tableId ));
245+ DropRowRangeRequest composeDropRowRangeRequest (
246+ String tableId , ByteString rowKeyPrefix , boolean dropAll ) {
247+ Builder dropRowReq = DropRowRangeRequest .newBuilder ().setName (getTableName (tableId ));
205248
206- // TODO: verify if Utf8String and ByteString are equivalent
207- // and if using "*" prefix as special case to invoke deletAll approach is reasonable
208- if ("*" .equals (rowKeyPrefix )) {
249+ if (dropAll ) {
209250 dropRowReq .setDeleteAllDataFromTable (true );
210251 } else {
211- dropRowReq .setRowKeyPrefix (ByteString . copyFromUtf8 ( rowKeyPrefix ) );
252+ dropRowReq .setRowKeyPrefix (rowKeyPrefix );
212253 }
213254 return dropRowReq .build ();
214255 }
215256
216257 @ VisibleForTesting
217- GenerateConsistencyTokenRequest getGenConsistencyToken (String tableId ) {
218- return GenerateConsistencyTokenRequest .newBuilder ().setName (getUniqueTableName (tableId ))
219- .build ();
258+ GenerateConsistencyTokenRequest composeGenerateConsistencyTokenRequest (String tableId ) {
259+ return GenerateConsistencyTokenRequest .newBuilder ().setName (getTableName (tableId )).build ();
220260 }
221261
222262 @ VisibleForTesting
223- CheckConsistencyRequest getCheckConsistencyRequest (String tableId , String token ) {
224- return CheckConsistencyRequest .newBuilder ().setName (getUniqueTableName (tableId ))
225- .setConsistencyToken (token ).build ();
263+ CheckConsistencyRequest composeCheckConsistencyRequest (String tableId , String token ) {
264+ return CheckConsistencyRequest .newBuilder ()
265+ .setName (getTableName (tableId ))
266+ .setConsistencyToken (token )
267+ .build ();
226268 }
227269
228270 @ VisibleForTesting
229- static List <TableName > convert (ListTablesResponse listTablesResponse ) {
271+ static List <TableName > convertToTableNames (ListTablesResponse listTablesResponse ) {
230272 List <TableName > tableNames = new ArrayList <>();
231273
232274 for (Table table : listTablesResponse .getTablesList ()) {
233275 tableNames .add (TableName .parse (table .getName ()));
234276 }
235277 return tableNames ;
236278 }
279+
280+ @ VisibleForTesting
281+ static ApiFuture <TableResponse > transformToTableResponse (ApiFuture <Table > future ) {
282+ return ApiFutures .transform (
283+ future ,
284+ new ApiFunction <Table , TableResponse >() {
285+ @ Override
286+ public TableResponse apply (Table table ) {
287+ return TableAdminResponses .convertTable (table );
288+ }
289+ });
290+ }
291+
292+ @ VisibleForTesting
293+ static ApiFuture <Void > transformToVoid (ApiFuture <Empty > future ) {
294+ return ApiFutures .transform (
295+ future ,
296+ new ApiFunction <Empty , Void >() {
297+ @ Override
298+ public Void apply (Empty empty ) {
299+ return null ;
300+ }
301+ });
302+ }
237303}
0 commit comments