-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathclient.h
More file actions
124 lines (114 loc) · 4.66 KB
/
client.h
File metadata and controls
124 lines (114 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2025 Google LLC
//
// 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
//
// https://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.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CLIENT_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CLIENT_H
#include "google/cloud/bigtable/bound_query.h"
#include "google/cloud/bigtable/data_connection.h"
#include "google/cloud/bigtable/instance_resource.h"
#include "google/cloud/bigtable/prepared_query.h"
#include "google/cloud/bigtable/sql_statement.h"
#include "google/cloud/bigtable/version.h"
#include "google/cloud/future.h"
#include "google/cloud/options.h"
#include "google/cloud/status_or.h"
#include <memory>
namespace google {
namespace cloud {
namespace bigtable {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
/**
* Connects to Cloud Bigtable's query preparation and execution APIs.
*
* A Bigtable query's lifecycle consists of two phases:
* 1. Preparing a query: The service creates and caches a query execution plan.
* 2. Executing a query: The client sends the plan ID and concrete parameters
* to the service, which then executes the query.
*
* This class provides methods for both preparing and executing SQL queries.
*
* @par Cost
* Creating a `Client` object is a relatively low-cost operation. It does not
* require connecting to the Bigtable servers. However, each `Client` object
* holds a `std::shared_ptr<DataConnection>`, and the first RPC made on this
* connection may incur a higher latency as the connection is established.
* For this reason, it is recommended to reuse `Client` objects when possible.
*
* @par Example
* @snippet data_snippets.cc prepare-and-execute-query
*/
class Client {
public:
/**
* Creates a new Client object.
*
* @param conn The connection object to use for all RPCs. This is typically
* created by `MakeDataConnection()`.
* @param opts Unused for now
*/
explicit Client(std::shared_ptr<DataConnection> conn, Options opts = {})
: conn_(std::move(conn)),
opts_(google::cloud::internal::MergeOptions(std::move(opts),
conn_->options())) {}
/**
* Prepares a query for future execution.
*
* This sends the SQL statement to the service, which validates it and
* creates an execution plan, returning a handle to this plan.
*
* @param instance The instance to prepare the query against.
* @param statement The SQL statement to prepare.
* @param opts Unused for now
* @return A `StatusOr` containing the prepared query on success. On
* failure, the `Status` contains error details.
*/
StatusOr<PreparedQuery> PrepareQuery(InstanceResource const& instance,
SqlStatement const& statement,
Options opts = {});
/**
* Asynchronously prepares a query for future execution.
*
* This sends the SQL statement to the service, which validates it and
* creates an execution plan, returning a handle to this plan.
*
* @param instance The instance to prepare the query against.
* @param statement The SQL statement to prepare.
* @param opts Unused for now
* @return A `future` that will be satisfied with a `StatusOr` containing
* the prepared query on success. On failure, the `Status` will contain
* error details.
*/
future<StatusOr<PreparedQuery>> AsyncPrepareQuery(
InstanceResource const& instance, SqlStatement const& statement,
Options opts = {});
/**
* Executes a bound query with concrete parameters.
*
* This returns a `RowStream`, which is a range of `StatusOr<QueryRow>`.
* The `BoundQuery` is passed by rvalue-reference to promote thread safety,
* as it is not safe to use a `BoundQuery` concurrently.
*
* @param bound_query The bound query to execute.
* @param opts Overrides the client-level options for this call.
* @return A `RowStream` that can be used to iterate over the result rows.
*/
RowStream ExecuteQuery(BoundQuery&& bound_query, Options opts = {});
private:
std::shared_ptr<DataConnection> conn_;
Options opts_;
};
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigtable
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CLIENT_H