|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +// https://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 | +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CLIENT_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CLIENT_H |
| 17 | + |
| 18 | +#include "google/cloud/bigtable/data_connection.h" |
| 19 | + |
| 20 | +namespace google { |
| 21 | +namespace cloud { |
| 22 | +namespace bigtable { |
| 23 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 24 | + |
| 25 | +/** |
| 26 | + * Connects to Cloud Bigtable's query preparation and execution APIs. |
| 27 | + * |
| 28 | + * A Bigtable query's lifecycle consists of two phases: |
| 29 | + * 1. Preparing a query: The service creates and caches a query execution plan. |
| 30 | + * 2. Executing a query: The client sends the plan ID and concrete parameters |
| 31 | + * to the service, which then executes the query. |
| 32 | + * |
| 33 | + * This class provides methods for both preparing and executing SQL queries. |
| 34 | + * |
| 35 | + * @par Cost |
| 36 | + * Creating a `Client` object is a relatively low-cost operation. It does not |
| 37 | + * require connecting to the Bigtable servers. However, each `Client` object |
| 38 | + * holds a `std::shared_ptr<DataConnection>`, and the first RPC made on this |
| 39 | + * connection may incur a higher latency as the connection is established. |
| 40 | + * For this reason, it is recommended to reuse `Client` objects when possible. |
| 41 | + * |
| 42 | + * @par Example |
| 43 | + * @code |
| 44 | + * #include "google/cloud/bigtable/client.h" |
| 45 | + * #include "google/cloud/project.h" |
| 46 | + * #include <iostream> |
| 47 | + * |
| 48 | + * int main() { |
| 49 | + * namespace cbt = google::cloud::bigtable; |
| 50 | + * cbt::Client client(cbt::MakeDataConnection()); |
| 51 | + * cbt::InstanceResource instance(google::cloud::Project("my-project"), |
| 52 | + * "my-instance"); |
| 53 | + * |
| 54 | + * // Declare a parameter with a type, but no value. |
| 55 | + * cbt::SqlStatement statement( |
| 56 | + * "SELECT _key, CAST(family['qual'] AS STRING) AS value " |
| 57 | + * "FROM my-table WHERE _key = @key", |
| 58 | + * {{"key", cbt::Value(cbt::Bytes())}}); |
| 59 | + * |
| 60 | + * google::cloud::StatusOr<cbt::PreparedQuery> prepared_query = |
| 61 | + * client.PrepareQuery(instance, statement); |
| 62 | + * if (!prepared_query) throw std::move(prepared_query).status(); |
| 63 | + * |
| 64 | + * auto bound_query = prepared_query->BindParameters( |
| 65 | + * {{"key", cbt::Value("row-key-2")}}); |
| 66 | + * |
| 67 | + * RowStream results = |
| 68 | + * client.ExecuteQuery(std::move(bound_query)); |
| 69 | + * |
| 70 | + * ... // process rows |
| 71 | + * } |
| 72 | + * @endcode |
| 73 | + */ |
| 74 | +class Client { |
| 75 | + public: |
| 76 | + /** |
| 77 | + * Creates a new Client object. |
| 78 | + * |
| 79 | + * @param conn The connection object to use for all RPCs. This is typically |
| 80 | + * created by `MakeDataConnection()`. |
| 81 | + * @param opts Unused for now |
| 82 | + */ |
| 83 | + explicit Client(std::shared_ptr<DataConnection> conn, Options opts = {}) |
| 84 | + : conn_(std::move(conn)), opts_(std::move(opts)) {} |
| 85 | + |
| 86 | + /** |
| 87 | + * Prepares a query for future execution. |
| 88 | + * |
| 89 | + * This sends the SQL statement to the service, which validates it and |
| 90 | + * creates an execution plan, returning a handle to this plan. |
| 91 | + * |
| 92 | + * @param instance The instance to prepare the query against. |
| 93 | + * @param statement The SQL statement to prepare. |
| 94 | + * @param opts Unused for now |
| 95 | + * @return A `StatusOr` containing the prepared query on success. On |
| 96 | + * failure, the `Status` contains error details. |
| 97 | + */ |
| 98 | + StatusOr<PreparedQuery> PrepareQuery(InstanceResource const& instance, |
| 99 | + SqlStatement const& statement, |
| 100 | + Options opts = {}); |
| 101 | + |
| 102 | + /** |
| 103 | + * Asynchronously prepares a query for future execution. |
| 104 | + * |
| 105 | + * This sends the SQL statement to the service, which validates it and |
| 106 | + * creates an execution plan, returning a handle to this plan. |
| 107 | + * |
| 108 | + * @param instance The instance to prepare the query against. |
| 109 | + * @param statement The SQL statement to prepare. |
| 110 | + * @param opts Unused for now |
| 111 | + * @return A `future` that will be satisfied with a `StatusOr` containing |
| 112 | + * the prepared query on success. On failure, the `Status` will contain |
| 113 | + * error details. |
| 114 | + */ |
| 115 | + future<StatusOr<PreparedQuery>> AsyncPrepareQuery( |
| 116 | + InstanceResource const& instance, SqlStatement const& statement, |
| 117 | + Options opts = {}); |
| 118 | + |
| 119 | + /** |
| 120 | + * Executes a bound query with concrete parameters. |
| 121 | + * |
| 122 | + * This returns a `RowStream`, which is a range of `StatusOr<QueryRow>`. |
| 123 | + * The `BoundQuery` is passed by rvalue-reference to promote thread safety, |
| 124 | + * as it is not safe to use a `BoundQuery` concurrently. |
| 125 | + * |
| 126 | + * @param bound_query The bound query to execute. |
| 127 | + * @param opts Overrides the client-level options for this call. |
| 128 | + * @return A `RowStream` that can be used to iterate over the result rows. |
| 129 | + */ |
| 130 | + RowStream ExecuteQuery(BoundQuery&& bound_query, Options opts = {}); |
| 131 | + |
| 132 | + private: |
| 133 | + std::shared_ptr<DataConnection> conn_; |
| 134 | + Options opts_; |
| 135 | +}; |
| 136 | + |
| 137 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 138 | +} // namespace bigtable |
| 139 | +} // namespace cloud |
| 140 | +} // namespace google |
| 141 | + |
| 142 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CLIENT_H |
0 commit comments