Google Cloud Bigtable C++ Client  1.33.0
A C++ Client Library for Google Cloud Bigtable
data_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/data_client.h"
16 #include "google/cloud/bigtable/internal/common_client.h"
17 #include "google/cloud/bigtable/internal/logging_data_client.h"
18 #include "google/cloud/internal/log_wrapper.h"
19 #include "google/cloud/log.h"
20 
21 namespace google {
22 namespace cloud {
23 namespace bigtable {
25 
26 namespace btproto = ::google::bigtable::v2;
27 
28 std::unique_ptr<
29  ::grpc::ClientAsyncReaderInterface<btproto::SampleRowKeysResponse>>
30 // NOLINTNEXTLINE(performance-unnecessary-value-param)
31 DataClient::PrepareAsyncSampleRowKeys(grpc::ClientContext*,
32  btproto::SampleRowKeysRequest const&,
33  grpc::CompletionQueue*) {
34  return nullptr;
35 }
36 
37 namespace {
38 
39 /**
40  * Implement a simple DataClient.
41  *
42  * This implementation does not support multiple threads, or refresh
43  * authorization tokens. In other words, it is extremely bare bones.
44  */
45 class DefaultDataClient : public DataClient {
46  public:
47  DefaultDataClient(std::string project, std::string instance,
48  Options options = {})
49  : project_(std::move(project)),
50  instance_(std::move(instance)),
51  impl_(std::move(options)) {}
52 
53  std::string const& project_id() const override { return project_; };
54  std::string const& instance_id() const override { return instance_; };
55 
56  std::shared_ptr<grpc::Channel> Channel() override { return impl_.Channel(); }
57  void reset() override { impl_.reset(); }
58 
59  grpc::Status MutateRow(grpc::ClientContext* context,
60  btproto::MutateRowRequest const& request,
61  btproto::MutateRowResponse* response) override {
62  return impl_.Stub()->MutateRow(context, request, response);
63  }
64 
65  std::unique_ptr<
66  grpc::ClientAsyncResponseReaderInterface<btproto::MutateRowResponse>>
67  AsyncMutateRow(grpc::ClientContext* context,
68  btproto::MutateRowRequest const& request,
69  grpc::CompletionQueue* cq) override {
70  return impl_.Stub()->AsyncMutateRow(context, request, cq);
71  }
72 
73  grpc::Status CheckAndMutateRow(
74  grpc::ClientContext* context,
75  btproto::CheckAndMutateRowRequest const& request,
76  btproto::CheckAndMutateRowResponse* response) override {
77  return impl_.Stub()->CheckAndMutateRow(context, request, response);
78  }
79 
80  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
81  btproto::CheckAndMutateRowResponse>>
82  AsyncCheckAndMutateRow(grpc::ClientContext* context,
83  btproto::CheckAndMutateRowRequest const& request,
84  grpc::CompletionQueue* cq) override {
85  return impl_.Stub()->AsyncCheckAndMutateRow(context, request, cq);
86  }
87 
88  grpc::Status ReadModifyWriteRow(
89  grpc::ClientContext* context,
90  btproto::ReadModifyWriteRowRequest const& request,
91  btproto::ReadModifyWriteRowResponse* response) override {
92  return impl_.Stub()->ReadModifyWriteRow(context, request, response);
93  }
94 
95  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
96  btproto::ReadModifyWriteRowResponse>>
97  AsyncReadModifyWriteRow(grpc::ClientContext* context,
98  btproto::ReadModifyWriteRowRequest const& request,
99  grpc::CompletionQueue* cq) override {
100  return impl_.Stub()->AsyncReadModifyWriteRow(context, request, cq);
101  }
102 
103  std::unique_ptr<grpc::ClientReaderInterface<btproto::ReadRowsResponse>>
104  ReadRows(grpc::ClientContext* context,
105  btproto::ReadRowsRequest const& request) override {
106  return impl_.Stub()->ReadRows(context, request);
107  }
108 
109  std::unique_ptr<grpc::ClientAsyncReaderInterface<btproto::ReadRowsResponse>>
110  AsyncReadRows(grpc::ClientContext* context,
111  btproto::ReadRowsRequest const& request,
112  grpc::CompletionQueue* cq, void* tag) override {
113  return impl_.Stub()->AsyncReadRows(context, request, cq, tag);
114  }
115 
116  std::unique_ptr<::grpc::ClientAsyncReaderInterface<btproto::ReadRowsResponse>>
117  PrepareAsyncReadRows(grpc::ClientContext* context,
118  btproto::ReadRowsRequest const& request,
119  grpc::CompletionQueue* cq) override {
120  return impl_.Stub()->PrepareAsyncReadRows(context, request, cq);
121  }
122 
123  std::unique_ptr<grpc::ClientReaderInterface<btproto::SampleRowKeysResponse>>
124  SampleRowKeys(grpc::ClientContext* context,
125  btproto::SampleRowKeysRequest const& request) override {
126  return impl_.Stub()->SampleRowKeys(context, request);
127  }
128 
129  std::unique_ptr<
130  ::grpc::ClientAsyncReaderInterface<btproto::SampleRowKeysResponse>>
131  AsyncSampleRowKeys(grpc::ClientContext* context,
132  btproto::SampleRowKeysRequest const& request,
133  grpc::CompletionQueue* cq, void* tag) override {
134  return impl_.Stub()->AsyncSampleRowKeys(context, request, cq, tag);
135  }
136 
137  std::unique_ptr<
138  ::grpc::ClientAsyncReaderInterface<btproto::SampleRowKeysResponse>>
139  PrepareAsyncSampleRowKeys(grpc::ClientContext* context,
140  btproto::SampleRowKeysRequest const& request,
141  grpc::CompletionQueue* cq) override {
142  return impl_.Stub()->PrepareAsyncSampleRowKeys(context, request, cq);
143  }
144 
145  std::unique_ptr<grpc::ClientReaderInterface<btproto::MutateRowsResponse>>
146  MutateRows(grpc::ClientContext* context,
147  btproto::MutateRowsRequest const& request) override {
148  return impl_.Stub()->MutateRows(context, request);
149  }
150 
151  std::unique_ptr<
152  ::grpc::ClientAsyncReaderInterface<btproto::MutateRowsResponse>>
153  AsyncMutateRows(grpc::ClientContext* context,
154  btproto::MutateRowsRequest const& request,
155  grpc::CompletionQueue* cq, void* tag) override {
156  return impl_.Stub()->AsyncMutateRows(context, request, cq, tag);
157  }
158 
159  std::unique_ptr<
160  ::grpc::ClientAsyncReaderInterface<btproto::MutateRowsResponse>>
161  PrepareAsyncMutateRows(grpc::ClientContext* context,
162  btproto::MutateRowsRequest const& request,
163  grpc::CompletionQueue* cq) override {
164  return impl_.Stub()->PrepareAsyncMutateRows(context, request, cq);
165  }
166 
167  private:
168  google::cloud::BackgroundThreadsFactory BackgroundThreadsFactory() override {
169  return impl_.BackgroundThreadsFactory();
170  }
171 
172  struct Traits {
173  static std::string const& Endpoint(Options const& options) {
174  return options.get<DataEndpointOption>();
175  }
176  };
177 
178  std::string project_;
179  std::string instance_;
180  internal::CommonClient<Traits, btproto::Bigtable> impl_;
181 };
182 
183 } // namespace
184 
185 std::shared_ptr<DataClient> MakeDataClient(std::string project_id,
186  std::string instance_id,
187  Options options) {
188  options = internal::DefaultOptions(std::move(options));
189  bool tracing_enabled = google::cloud::internal::Contains(
190  options.get<TracingComponentsOption>(), "rpc");
191  auto tracing_options = options.get<GrpcTracingOptionsOption>();
192 
193  std::shared_ptr<DataClient> client = std::make_shared<DefaultDataClient>(
194  std::move(project_id), std::move(instance_id), std::move(options));
195  if (tracing_enabled) {
196  GCP_LOG(INFO) << "Enabled logging for gRPC calls";
197  client = std::make_shared<internal::LoggingDataClient>(
198  std::move(client), std::move(tracing_options));
199  }
200  return client;
201 }
202 
203 std::shared_ptr<DataClient> CreateDefaultDataClient(std::string project_id,
204  std::string instance_id,
205  ClientOptions options) {
206  return MakeDataClient(std::move(project_id), std::move(instance_id),
207  internal::MakeOptions(std::move(options)));
208 }
209 
211 } // namespace bigtable
212 } // namespace cloud
213 } // namespace google