Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2019 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.core;

import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest;
import com.google.cloud.bigtable.admin.v2.models.Table;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;

/**
* Interface to wrap {@link com.google.cloud.bigtable.grpc.BigtableTableAdminClient} with
* Google-Cloud-java's models.
*/
public interface IBigtableTableAdminClient {

/**
* Creates a new table. The table can be created with a full set of initial column families,
* specified in the request.
* @param request a {@link CreateTableRequest} object.
*/
Table createTable(CreateTableRequest request);

/**
* Creates a new table asynchronously. The table can be created with a full set of initial column
* families, specified in the request.
*
* @param request a {@link CreateTableRequest} object.
*/
//TODO(rahulkql): Once it is adapted to v2.models, change the return type to ApiFuture.
ListenableFuture<Table> createTableAsync(CreateTableRequest request);

/**
* Gets the details of a table.
*
* @param tableId a String object.
* @return a {@link Table} object.
*/
Table getTable(String tableId);

/**
* Gets the details of a table asynchronously.
*
* @return a {@link ListenableFuture} that returns a {@link Table} object.
*/
//TODO(rahulkql): Once it is adapted to v2.models, change the return type to ApiFuture.
ListenableFuture<Table> getTableAsync(String tableId);

/**
* Lists the names of all tables in an instance.
*
* @return an immutable {@link List} object containing tableId.
*/
List<String> listTables();

/**
* Lists the names of all tables in an instance asynchronously.
*
* @return a {@link ListenableFuture} of type {@link Void} will be set when request is
* successful otherwise exception will be thrown.
*/
//TODO(rahulkql): Once it is adapted to v2.models, change the return type to ApiFuture.
ListenableFuture<List<String>> listTablesAsync();

/**
* Permanently deletes a specified table and all of its data.
*
*/
void deleteTable(String tableId);

/**
* Permanently deletes a specified table and all of its data.
*
* @return a {@link ListenableFuture} of type {@link Void} will be set when request is
* successful otherwise exception will be thrown.
*/
//TODO(rahulkql): Once it is adapted to v2.models, change the return type to ApiFuture.
ListenableFuture<Void> deleteTableAsync(String tableId);

/**
* Creates, modifies or deletes a new column family within a specified table.
*
* @param request a {@link ModifyColumnFamiliesRequest} object.
* @return return {@link Table} object that contains the updated table structure.
*/
Table modifyFamilies(ModifyColumnFamiliesRequest request);

/**
* Creates, modifies or deletes a new column family within a specified table.
*
* @param request a {@link ModifyColumnFamiliesRequest} object.
* @return a {@link ListenableFuture} that returns {@link Table} object that contains
* the updated table structure.
*/
//TODO(rahulkql): Once it is adapted to v2.models, change the return type to ApiFuture.
ListenableFuture<Table> modifyFamiliesAsync(ModifyColumnFamiliesRequest request);

/**
* Permanently deletes all rows in a range.
*
* @param tableId
* @param rowKeyPrefix
*/
void dropRowRange(String tableId, String rowKeyPrefix);

/**
* Permanently deletes all rows in a range.
*
* @param tableId
* @param rowKeyPrefix
* @return a {@link ListenableFuture} that returns {@link Void} object.
*/
//TODO(rahulkql): Once it is adapted to v2.models, change the return type to ApiFuture.
ListenableFuture<Void> dropRowRangeAsync(String tableId, String rowKeyPrefix);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
* Copyright 2019 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.grpc;

import com.google.bigtable.admin.v2.DeleteTableRequest;
import com.google.bigtable.admin.v2.DropRowRangeRequest;
import com.google.bigtable.admin.v2.GetTableRequest;
import com.google.bigtable.admin.v2.ListTablesRequest;
import com.google.bigtable.admin.v2.ListTablesResponse;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest;
import com.google.cloud.bigtable.admin.v2.models.Table;
import com.google.cloud.bigtable.config.BigtableOptions;
import com.google.cloud.bigtable.core.IBigtableTableAdminClient;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import java.util.List;
import javax.annotation.Nonnull;

/**
* This class implements the {@link IBigtableTableAdminClient} interface and wraps
* {@link BigtableTableAdminClient} with Google-cloud-java's models.
*/
public class BigtableTableAdminClientWrapper implements IBigtableTableAdminClient {

private final BigtableTableAdminClient adminClient;
private final BigtableInstanceName instanceName;

public BigtableTableAdminClientWrapper(@Nonnull BigtableTableAdminClient adminClient,
@Nonnull BigtableOptions options){
Preconditions.checkNotNull(adminClient);
Preconditions.checkNotNull(options);
this.adminClient = adminClient;
this.instanceName = new BigtableInstanceName(options.getProjectId(),
options.getInstanceId());
}

/** {@inheritDoc} */
@Override
public Table createTable(CreateTableRequest request) {
com.google.bigtable.admin.v2.CreateTableRequest requestProto =
request.toProto(instanceName.toAdminInstanceName());
adminClient.createTable(requestProto);

return getTable(requestProto.getTableId());
}

/** {@inheritDoc} */
@Override
public ListenableFuture<Table> createTableAsync(CreateTableRequest request) {
com.google.bigtable.admin.v2.CreateTableRequest requestProto =
request.toProto(instanceName.toAdminInstanceName());

return Futures.transform(adminClient.createTableAsync(requestProto),
new Function<com.google.bigtable.admin.v2.Table, Table>() {
@Override
public Table apply(com.google.bigtable.admin.v2.Table tableProto) {
return Table.fromProto(tableProto);
}
}, MoreExecutors.directExecutor());
}

/** {@inheritDoc} */
@Override
public Table getTable(String tableId) {
GetTableRequest requestProto = GetTableRequest.newBuilder()
.setName(instanceName.toTableNameStr(tableId))
.build();

return Table.fromProto(adminClient.getTable(requestProto));
}

/** {@inheritDoc} */
@Override
public ListenableFuture<Table> getTableAsync(String tableId) {
GetTableRequest requestProto = GetTableRequest.newBuilder()
.setName(instanceName.toTableNameStr(tableId))
.build();

return Futures.transform(adminClient.getTableAsync(requestProto),
new Function<com.google.bigtable.admin.v2.Table, Table>() {
@Override
public Table apply(com.google.bigtable.admin.v2.Table tableProto) {
return Table.fromProto(tableProto);
}
}, MoreExecutors.directExecutor());
}

/** {@inheritDoc} */
@Override
public List<String> listTables() {
ListTablesRequest requestProto = ListTablesRequest.newBuilder()
.setParent(instanceName.toString())
.build();

ListTablesResponse response = adminClient.listTables(requestProto);

ImmutableList.Builder<String> tableIdsBuilder =
ImmutableList.builderWithExpectedSize(response.getTablesList().size());
for(com.google.bigtable.admin.v2.Table tableProto : response.getTablesList()){
tableIdsBuilder.add(instanceName.toTableId(tableProto.getName()));
}

return tableIdsBuilder.build();
}

/** {@inheritDoc} */
@Override
public ListenableFuture<List<String>> listTablesAsync() {
ListTablesRequest request = ListTablesRequest.newBuilder()
.setParent(instanceName.toString())
.build();
ListenableFuture<ListTablesResponse> response = adminClient.listTablesAsync(request);

return Futures.transform(response, new Function<ListTablesResponse, List<String>>() {
@Override
public List<String> apply(ListTablesResponse input) {
ImmutableList.Builder<String> tableIdsBuilder =
ImmutableList.builderWithExpectedSize(input.getTablesList().size());
for(com.google.bigtable.admin.v2.Table tableProto : input.getTablesList()){
tableIdsBuilder.add(instanceName.toTableId(tableProto.getName()));
}

return tableIdsBuilder.build();
}
}, MoreExecutors.directExecutor());
}

/** {@inheritDoc} */
@Override
public void deleteTable(String tableId) {
DeleteTableRequest request = DeleteTableRequest.newBuilder()
.setName(instanceName.toTableNameStr(tableId))
.build();

adminClient.deleteTable(request);
}

/** {@inheritDoc} */
@Override
public ListenableFuture<Void> deleteTableAsync(String tableId) {
DeleteTableRequest request = DeleteTableRequest.newBuilder()
.setName(instanceName.toTableNameStr(tableId))
.build();

return Futures.transform(adminClient.deleteTableAsync(request), new Function<Empty, Void>() {
@Override
public Void apply(Empty empty) {
return null;
}
}, MoreExecutors.directExecutor());
}

/** {@inheritDoc} */
@Override
public Table modifyFamilies(ModifyColumnFamiliesRequest request) {
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest modifyColumnRequestProto =
request.toProto(instanceName.toAdminInstanceName());

return Table.fromProto(adminClient.modifyColumnFamily(modifyColumnRequestProto));
}

/** {@inheritDoc} */
@Override
public ListenableFuture<Table> modifyFamiliesAsync(ModifyColumnFamiliesRequest request) {
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest modifyColumnRequestProto =
request.toProto(instanceName.toAdminInstanceName());

return Futures.transform(adminClient.modifyColumnFamilyAsync(modifyColumnRequestProto),
new Function<com.google.bigtable.admin.v2.Table, Table>() {
@Override
public Table apply(com.google.bigtable.admin.v2.Table tableProto) {
return Table.fromProto(tableProto);
}
}, MoreExecutors.directExecutor());
}

/** {@inheritDoc} */
@Override
public void dropRowRange(String tableId, String rowKeyPrefix) {
DropRowRangeRequest requestProto = DropRowRangeRequest.newBuilder()
.setName(instanceName.toTableNameStr(tableId))
.setRowKeyPrefix(ByteString.copyFromUtf8(rowKeyPrefix))
.build();

adminClient.dropRowRange(requestProto);
}

/** {@inheritDoc} */
@Override
public ListenableFuture<Void> dropRowRangeAsync(String tableId, String rowKeyPrefix) {
DropRowRangeRequest requestProto = DropRowRangeRequest.newBuilder()
.setName(instanceName.toTableNameStr(tableId))
.setRowKeyPrefix(ByteString.copyFromUtf8(rowKeyPrefix))
.build();

return Futures.transform(adminClient.dropRowRangeAsync(requestProto), new Function<Empty, Void>() {
@Override
public Void apply(Empty empty) {
return null;
}
}, MoreExecutors.directExecutor());
}
}
Loading