Google Cloud Bigtable C++ Client  1.1.0
A C++ Client Library for Google Cloud Bigtable
admin_client.cc
Go to the documentation of this file.
1 // Copyright 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/bigtable/admin_client.h"
16 #include "google/cloud/bigtable/internal/common_client.h"
17 #include <google/longrunning/operations.grpc.pb.h>
18 
19 namespace {
20 namespace btadmin = google::bigtable::admin::v2;
21 
22 /**
23  * An AdminClient for single-threaded programs that refreshes credentials on all
24  * gRPC errors.
25  *
26  * This class should not be used by multiple threads, it makes no attempt to
27  * protect its critical sections. While it is rare that the admin interface
28  * will be used by multiple threads, we should use the same approach here and in
29  * the regular client to support multi-threaded programs.
30  *
31  * The class also aggressively reconnects on any gRPC errors. A future version
32  * should only reconnect on those errors that indicate the credentials or
33  * connections need refreshing.
34  */
35 class DefaultAdminClient : public google::cloud::bigtable::AdminClient {
36  private:
37  // Introduce an early `private:` section because this type is used to define
38  // the public interface, it should not be part of the public interface.
39  struct AdminTraits {
40  static std::string const& Endpoint(
41  google::cloud::bigtable::ClientOptions& options) {
42  return options.admin_endpoint();
43  }
44  };
45 
46  using Impl = google::cloud::bigtable::internal::CommonClient<
47  AdminTraits, btadmin::BigtableTableAdmin>;
48 
49  public:
50  using AdminStubPtr = Impl::StubPtr;
51 
52  DefaultAdminClient(std::string project,
54  : project_(std::move(project)), impl_(std::move(options)) {}
55 
56  std::string const& project() const override { return project_; }
57  std::shared_ptr<grpc::Channel> Channel() override { return impl_.Channel(); }
58  void reset() override { return impl_.reset(); }
59 
60  grpc::Status CreateTable(grpc::ClientContext* context,
61  btadmin::CreateTableRequest const& request,
62  btadmin::Table* response) override {
63  return impl_.Stub()->CreateTable(context, request, response);
64  }
65 
66  grpc::Status ListTables(grpc::ClientContext* context,
67  btadmin::ListTablesRequest const& request,
68  btadmin::ListTablesResponse* response) override {
69  return impl_.Stub()->ListTables(context, request, response);
70  }
71 
72  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
73  google::bigtable::admin::v2::ListTablesResponse>>
74  AsyncListTables(grpc::ClientContext* context,
75  google::bigtable::admin::v2::ListTablesRequest const& request,
76  grpc::CompletionQueue* cq) override {
77  return impl_.Stub()->AsyncListTables(context, request, cq);
78  }
79 
80  grpc::Status GetTable(grpc::ClientContext* context,
81  btadmin::GetTableRequest const& request,
82  btadmin::Table* response) override {
83  return impl_.Stub()->GetTable(context, request, response);
84  }
85 
86  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
87  google::bigtable::admin::v2::Table>>
88  AsyncGetTable(grpc::ClientContext* context,
89  google::bigtable::admin::v2::GetTableRequest const& request,
90  grpc::CompletionQueue* cq) override {
91  return impl_.Stub()->AsyncGetTable(context, request, cq);
92  }
93 
94  grpc::Status DeleteTable(grpc::ClientContext* context,
95  btadmin::DeleteTableRequest const& request,
96  google::protobuf::Empty* response) override {
97  return impl_.Stub()->DeleteTable(context, request, response);
98  }
99 
100  grpc::Status ModifyColumnFamilies(
101  grpc::ClientContext* context,
102  btadmin::ModifyColumnFamiliesRequest const& request,
103  btadmin::Table* response) override {
104  return impl_.Stub()->ModifyColumnFamilies(context, request, response);
105  }
106 
107  grpc::Status DropRowRange(grpc::ClientContext* context,
108  btadmin::DropRowRangeRequest const& request,
109  google::protobuf::Empty* response) override {
110  return impl_.Stub()->DropRowRange(context, request, response);
111  }
112 
113  grpc::Status GenerateConsistencyToken(
114  grpc::ClientContext* context,
115  btadmin::GenerateConsistencyTokenRequest const& request,
116  btadmin::GenerateConsistencyTokenResponse* response) override {
117  return impl_.Stub()->GenerateConsistencyToken(context, request, response);
118  }
119 
120  grpc::Status CheckConsistency(
121  grpc::ClientContext* context,
122  btadmin::CheckConsistencyRequest const& request,
123  btadmin::CheckConsistencyResponse* response) override {
124  return impl_.Stub()->CheckConsistency(context, request, response);
125  }
126 
127  grpc::Status GetOperation(
128  grpc::ClientContext* context,
129  google::longrunning::GetOperationRequest const& request,
130  google::longrunning::Operation* response) override {
131  auto stub = google::longrunning::Operations::NewStub(Channel());
132  return stub->GetOperation(context, request, response);
133  }
134 
135  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
136  google::bigtable::admin::v2::Table>>
137  AsyncCreateTable(
138  grpc::ClientContext* context,
139  google::bigtable::admin::v2::CreateTableRequest const& request,
140  grpc::CompletionQueue* cq) override {
141  return impl_.Stub()->AsyncCreateTable(context, request, cq);
142  }
143 
144  std::unique_ptr<
145  ::grpc::ClientAsyncResponseReaderInterface<::google::protobuf::Empty>>
146  AsyncDeleteTable(
147  grpc::ClientContext* context,
148  google::bigtable::admin::v2::DeleteTableRequest const& request,
149  grpc::CompletionQueue* cq) override {
150  return impl_.Stub()->AsyncDeleteTable(context, request, cq);
151  }
152 
153  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
154  google::bigtable::admin::v2::Table>>
155  AsyncModifyColumnFamilies(
156  grpc::ClientContext* context,
157  google::bigtable::admin::v2::ModifyColumnFamiliesRequest const& request,
158  grpc::CompletionQueue* cq) override {
159  return impl_.Stub()->AsyncModifyColumnFamilies(context, request, cq);
160  }
161 
162  std::unique_ptr<
163  grpc::ClientAsyncResponseReaderInterface<google::protobuf::Empty>>
164  AsyncDropRowRange(
165  grpc::ClientContext* context,
166  google::bigtable::admin::v2::DropRowRangeRequest const& request,
167  grpc::CompletionQueue* cq) override {
168  return impl_.Stub()->AsyncDropRowRange(context, request, cq);
169  };
170 
171  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
172  google::bigtable::admin::v2::GenerateConsistencyTokenResponse>>
173  AsyncGenerateConsistencyToken(
174  grpc::ClientContext* context,
175  const google::bigtable::admin::v2::GenerateConsistencyTokenRequest&
176  request,
177  grpc::CompletionQueue* cq) override {
178  return impl_.Stub()->AsyncGenerateConsistencyToken(context, request, cq);
179  }
180 
181  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
182  google::bigtable::admin::v2::CheckConsistencyResponse>>
183  AsyncCheckConsistency(
184  grpc::ClientContext* context,
185  const google::bigtable::admin::v2::CheckConsistencyRequest& request,
186  grpc::CompletionQueue* cq) override {
187  return impl_.Stub()->AsyncCheckConsistency(context, request, cq);
188  }
189 
190  std::unique_ptr<
191  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
192  AsyncGetOperation(grpc::ClientContext* context,
193  const google::longrunning::GetOperationRequest& request,
194  grpc::CompletionQueue* cq) override {
195  auto stub = google::longrunning::Operations::NewStub(Channel());
196  return std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
197  google::longrunning::Operation>>(
198  stub->AsyncGetOperation(context, request, cq).release());
199  }
200 
201  private:
202  std::string project_;
203  Impl impl_;
204 };
205 } // anonymous namespace
206 
207 namespace google {
208 namespace cloud {
209 namespace bigtable {
210 inline namespace BIGTABLE_CLIENT_NS {
211 std::shared_ptr<AdminClient> CreateDefaultAdminClient(std::string project,
212  ClientOptions options) {
213  return std::make_shared<DefaultAdminClient>(std::move(project),
214  std::move(options));
215 }
216 
217 } // namespace BIGTABLE_CLIENT_NS
218 } // namespace bigtable
219 } // namespace cloud
220 } // namespace google
virtual std::unique_ptr< grpc::ClientAsyncResponseReaderInterface< google::longrunning::Operation > > AsyncGetOperation(grpc::ClientContext *context, const google::longrunning::GetOperationRequest &request, grpc::CompletionQueue *cq)=0
#define BIGTABLE_CLIENT_NS
Definition: version.h:22
std::shared_ptr< AdminClient > CreateDefaultAdminClient(std::string project, ClientOptions options)
Create a new admin client configured via options.
std::string const & admin_endpoint() const
Return the current endpoint for admin RPCs.
Configuration options for the Bigtable Client.
Contains all the Cloud Bigtable C++ client APIs.