Google Cloud Spanner C++ Client
1.32.0
A C++ Client Library for Google Cloud Spanner
|
Performs database client operations on Spanner. More...
#include <google/cloud/spanner/client.h>
Public Member Functions | |
Client (std::shared_ptr< Connection > conn, ClientOptions opts={}) | |
Constructs a Client object using the specified conn and opts . More... | |
Client ()=delete | |
No default construction. Use Client(std::shared_ptr<Connection>) More... | |
Client (Client const &)=default | |
Client & | operator= (Client const &)=default |
Client (Client &&)=default | |
Client & | operator= (Client &&)=default |
RowStream | Read (std::string table, KeySet keys, std::vector< std::string > columns, ReadOptions read_options={}) |
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteQuery() . More... | |
RowStream | Read (Transaction::SingleUseOptions transaction_options, std::string table, KeySet keys, std::vector< std::string > columns, ReadOptions read_options={}) |
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteQuery() . More... | |
RowStream | Read (Transaction transaction, std::string table, KeySet keys, std::vector< std::string > columns, ReadOptions read_options={}) |
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteQuery() . More... | |
RowStream | Read (ReadPartition const &partition) |
Reads rows from a subset of rows in a database. More... | |
StatusOr< std::vector< ReadPartition > > | PartitionRead (Transaction transaction, std::string table, KeySet keys, std::vector< std::string > columns, ReadOptions read_options={}, PartitionOptions const &partition_options=PartitionOptions{}) |
Creates a set of partitions that can be used to execute a read operation in parallel. More... | |
RowStream | ExecuteQuery (SqlStatement statement, QueryOptions const &opts={}) |
Executes a SQL query. More... | |
RowStream | ExecuteQuery (Transaction::SingleUseOptions transaction_options, SqlStatement statement, QueryOptions const &opts={}) |
Executes a SQL query. More... | |
RowStream | ExecuteQuery (Transaction transaction, SqlStatement statement, QueryOptions const &opts={}) |
Executes a SQL query. More... | |
RowStream | ExecuteQuery (QueryPartition const &partition, QueryOptions const &opts={}) |
Executes a SQL query on a subset of rows in a database. More... | |
ProfileQueryResult | ProfileQuery (SqlStatement statement, QueryOptions const &opts={}) |
Profiles a SQL query. More... | |
ProfileQueryResult | ProfileQuery (Transaction::SingleUseOptions transaction_options, SqlStatement statement, QueryOptions const &opts={}) |
Profiles a SQL query. More... | |
ProfileQueryResult | ProfileQuery (Transaction transaction, SqlStatement statement, QueryOptions const &opts={}) |
Profiles a SQL query. More... | |
StatusOr< std::vector< QueryPartition > > | PartitionQuery (Transaction transaction, SqlStatement statement, PartitionOptions const &partition_options=PartitionOptions{}) |
Creates a set of partitions that can be used to execute a query operation in parallel. More... | |
StatusOr< DmlResult > | ExecuteDml (Transaction transaction, SqlStatement statement, QueryOptions const &opts={}) |
Executes a SQL DML statement. More... | |
StatusOr< ProfileDmlResult > | ProfileDml (Transaction transaction, SqlStatement statement, QueryOptions const &opts={}) |
Profiles a SQL DML statement. More... | |
StatusOr< ExecutionPlan > | AnalyzeSql (Transaction transaction, SqlStatement statement, QueryOptions const &opts={}) |
Analyzes the execution plan of a SQL statement. More... | |
StatusOr< BatchDmlResult > | ExecuteBatchDml (Transaction transaction, std::vector< SqlStatement > statements, Options opts={}) |
Executes a batch of SQL DML statements. More... | |
StatusOr< CommitResult > | Commit (std::function< StatusOr< Mutations >(Transaction)> const &mutator, std::unique_ptr< TransactionRerunPolicy > rerun_policy, std::unique_ptr< BackoffPolicy > backoff_policy, CommitOptions const &options={}) |
Commits a read-write transaction. More... | |
StatusOr< CommitResult > | Commit (std::function< StatusOr< Mutations >(Transaction)> const &mutator, CommitOptions const &options={}) |
Commits a read-write transaction. More... | |
StatusOr< CommitResult > | Commit (Mutations mutations, CommitOptions const &options={}) |
Commits the mutations , using the options , atomically in order. More... | |
StatusOr< CommitResult > | Commit (Transaction transaction, Mutations mutations, CommitOptions const &options={}) |
Commits a read-write transaction. More... | |
Status | Rollback (Transaction transaction) |
Rolls back a read-write transaction, releasing any locks it holds. More... | |
StatusOr< PartitionedDmlResult > | ExecutePartitionedDml (SqlStatement statement, QueryOptions const &opts={}) |
Executes a Partitioned DML SQL query. More... | |
Friends | |
bool | operator== (Client const &a, Client const &b) |
bool | operator!= (Client const &a, Client const &b) |
Performs database client operations on Spanner.
Applications use this class to perform operations on Spanner Databases.
Client
objects are relatively cheap to create, copy, and move. However, each Client
object must be created with a std::shared_ptr<Connection>
, which itself is relatively expensive to create. Therefore, connection instances should be shared when possible. See the MakeConnection()
method and the Connection
interface for more details.
Instances of this class created via copy-construction or copy-assignment share the underlying pool of connections. Access to these copies via multiple threads is guaranteed to work. Two threads operating on the same instance of this class is not guaranteed to work.
This class uses StatusOr<T>
to report errors. When an operation fails to perform its work the returned StatusOr<T>
contains the error details. If the ok()
member function in the StatusOr<T>
returns true
then it contains the expected result. Please consult the StatusOr<T>
documentation for more details.
Most operations that take an SqlStatement
may also be modified with QueryOptions
. These options can be set at various levels, with more specific levels taking precedence over broader ones. For example, QueryOptions
that are passed directly to Client::ExecuteQuery()
will take precedence over the Client
-level defaults (if any), which will themselves take precedence over any environment variables. The following table shows the environment variables that may optionally be set and the QueryOptions
setting that they affect.
Environment Variable | QueryOptions setting |
---|---|
SPANNER_OPTIMIZER_VERSION | QueryOptions::optimizer_version() |
SPANNER_OPTIMIZER_STATISTICS_PACKAGE | QueryOptions::optimizer_statistics_package() |
|
inlineexplicit |
Constructs a Client
object using the specified conn
and opts
.
See MakeConnection()
for how to create a connection to Spanner. To help with unit testing, callers may create fake/mock Connection
objects that are injected into the Client
.
|
delete |
No default construction. Use Client(std::shared_ptr<Connection>)
|
default |
|
default |
StatusOr< ExecutionPlan > google::cloud::spanner::v1::Client::AnalyzeSql | ( | Transaction | transaction, |
SqlStatement | statement, | ||
QueryOptions const & | opts = {} |
||
) |
Analyzes the execution plan of a SQL statement.
Analyzing provides the ExecutionPlan
, but does not execute the SQL statement.
Operations inside read-write transactions might return ABORTED
. If this occurs, the application should restart the transaction from the beginning.
transaction | Execute this query as part of an existing transaction. |
statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
StatusOr< CommitResult > google::cloud::spanner::v1::Client::Commit | ( | Mutations | mutations, |
CommitOptions const & | options = {} |
||
) |
Commits the mutations
, using the options
, atomically in order.
This function uses the re-run loop described above with the default policies.
StatusOr< CommitResult > google::cloud::spanner::v1::Client::Commit | ( | std::function< StatusOr< Mutations >(Transaction)> const & | mutator, |
CommitOptions const & | options = {} |
||
) |
Commits a read-write transaction.
Same as above, but uses the default rerun and backoff policies.
mutator | the function called to create mutations |
options | to apply to the commit. |
StatusOr< CommitResult > google::cloud::spanner::v1::Client::Commit | ( | std::function< StatusOr< Mutations >(Transaction)> const & | mutator, |
std::unique_ptr< TransactionRerunPolicy > | rerun_policy, | ||
std::unique_ptr< BackoffPolicy > | backoff_policy, | ||
CommitOptions const & | options = {} |
||
) |
Commits a read-write transaction.
Calls the mutator
in the context of a new read-write transaction. The mutator
can execute read/write operations using the transaction, and returns any additional Mutations
to commit.
If the mutator
succeeds and the transaction commits, then Commit()
returns the CommitResult
.
If the mutator
returns a non-rerunnable status (according to the rerun_policy
), the transaction is rolled back and that status is returned. Similarly, if the transaction fails to commit with a non- rerunnable status, that status is returned.
Otherwise the whole process repeats (subject to rerun_policy
and backoff_policy
), by building a new transaction and re-running the mutator
. The lock priority of the operation increases after each rerun, meaning that the next attempt has a slightly better chance of success.
Note that the mutator
should only return a rerunnable status when the transaction is no longer usable (e.g., it was aborted). Otherwise the transaction will be leaked.
mutator | the function called to create mutations |
rerun_policy | controls for how long (or how many times) the mutator will be rerun after the transaction aborts. |
backoff_policy | controls how long Commit waits between reruns. |
options | to apply to the commit. |
Rethrows | any exception thrown by (after rolling back the transaction). However, a RuntimeStatusError exception is instead consumed and converted into a mutator return value of the enclosed Status . |
StatusOr< CommitResult > google::cloud::spanner::v1::Client::Commit | ( | Transaction | transaction, |
Mutations | mutations, | ||
CommitOptions const & | options = {} |
||
) |
Commits a read-write transaction.
The commit might return a kAborted
error. This can occur at any time. Commonly the cause is conflicts with concurrent transactions, however it can also happen for a variety of other reasons. If Commit
returns kAborted
, the caller may try to reapply the mutations within a new read-write transaction (which should share lock priority with the aborted transaction so that the new attempt has a slightly better chance of success).
Commit
overloads if you want to simply reapply mutations after a kAborted
error.Commit
with a read-only transaction.transaction | The transaction to commit. |
mutations | The mutations to be executed when this transaction commits. All mutations are applied atomically, in the order they appear in this list. |
options | to apply to the commit. |
StatusOr
containing the result of the commit or error status on failure. StatusOr< BatchDmlResult > google::cloud::spanner::v1::Client::ExecuteBatchDml | ( | Transaction | transaction, |
std::vector< SqlStatement > | statements, | ||
Options | opts = {} |
||
) |
Executes a batch of SQL DML statements.
This method allows many statements to be run with lower latency than submitting them sequentially with ExecuteDml
.
Statements are executed in order, sequentially. Execution will stop at the first failed statement; the remaining statements will not run.
As with all read-write transactions, the results will not be visible outside of the transaction until it is committed. For that reason, it is advisable to run this method from a Commit
mutator.
BatchDmlResult::status
field.transaction | The read-write transaction to execute the operation in. |
statements | The list of statements to execute in this batch. Statements are executed serially, such that the effects of statement i are visible to statement i+1. Each statement must be a DML statement. Execution will stop at the first failed statement; the remaining statements will not run. Must not be empty. |
opts | The options to use for this call. Expected options are any of the types in the following option lists.
|
StatusOr< DmlResult > google::cloud::spanner::v1::Client::ExecuteDml | ( | Transaction | transaction, |
SqlStatement | statement, | ||
QueryOptions const & | opts = {} |
||
) |
Executes a SQL DML statement.
Operations inside read-write transactions might return ABORTED
. If this occurs, the application should restart the transaction from the beginning.
transaction | Execute this query as part of an existing transaction. |
statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
StatusOr< PartitionedDmlResult > google::cloud::spanner::v1::Client::ExecutePartitionedDml | ( | SqlStatement | statement, |
QueryOptions const & | opts = {} |
||
) |
Executes a Partitioned DML SQL query.
statement | the SQL statement to execute. Please see the spanner documentation for the restrictions on the SQL statements supported by this function. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
RowStream google::cloud::spanner::v1::Client::ExecuteQuery | ( | QueryPartition const & | partition, |
QueryOptions const & | opts = {} |
||
) |
Executes a SQL query on a subset of rows in a database.
Requires a prior call to PartitionQuery
to obtain the partition information; see the documentation of that method for full details.
partition | A QueryPartition , obtained by calling PartitionQuery . |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
RowStream
can exceed 100 MiB, and no column value can exceed 10 MiB.RowStream google::cloud::spanner::v1::Client::ExecuteQuery | ( | SqlStatement | statement, |
QueryOptions const & | opts = {} |
||
) |
Executes a SQL query.
Operations inside read-write transactions might return ABORTED
. If this occurs, the application should restart the transaction from the beginning.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
SELECT * ...
queries are supported, but there's no guarantee about the order, nor number, of returned columns. Therefore, the caller must look up the wanted values in each row by column name. When the desired column names are known in advance, it is better to list them explicitly in the query's SELECT statement, so that unnecessary values are not returned/ignored, and the column order is known. This enables more efficient and simpler code.
statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
RowStream
can exceed 100 MiB, and no column value can exceed 10 MiB. RowStream google::cloud::spanner::v1::Client::ExecuteQuery | ( | Transaction | transaction, |
SqlStatement | statement, | ||
QueryOptions const & | opts = {} |
||
) |
Executes a SQL query.
Operations inside read-write transactions might return ABORTED
. If this occurs, the application should restart the transaction from the beginning.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
SELECT * ...
queries are supported, but there's no guarantee about the order, nor number, of returned columns. Therefore, the caller must look up the wanted values in each row by column name. When the desired column names are known in advance, it is better to list them explicitly in the query's SELECT statement, so that unnecessary values are not returned/ignored, and the column order is known. This enables more efficient and simpler code.
statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
RowStream
can exceed 100 MiB, and no column value can exceed 10 MiB.transaction | Execute this query as part of an existing transaction. |
RowStream google::cloud::spanner::v1::Client::ExecuteQuery | ( | Transaction::SingleUseOptions | transaction_options, |
SqlStatement | statement, | ||
QueryOptions const & | opts = {} |
||
) |
Executes a SQL query.
Operations inside read-write transactions might return ABORTED
. If this occurs, the application should restart the transaction from the beginning.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
SELECT * ...
queries are supported, but there's no guarantee about the order, nor number, of returned columns. Therefore, the caller must look up the wanted values in each row by column name. When the desired column names are known in advance, it is better to list them explicitly in the query's SELECT statement, so that unnecessary values are not returned/ignored, and the column order is known. This enables more efficient and simpler code.
statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
RowStream
can exceed 100 MiB, and no column value can exceed 10 MiB.transaction_options | Execute this query in a single-use transaction with these options. |
StatusOr< std::vector< QueryPartition > > google::cloud::spanner::v1::Client::PartitionQuery | ( | Transaction | transaction, |
SqlStatement | statement, | ||
PartitionOptions const & | partition_options = PartitionOptions{} |
||
) |
Creates a set of partitions that can be used to execute a query operation in parallel.
Each of the returned partitions can be passed to ExecuteQuery
to specify a subset of the query result to read.
Partitions become invalid when the session used to create them is deleted, is idle for too long, begins a new transaction, or becomes too old. When any of these happen, it is not possible to resume the query, and the whole operation must be restarted from the beginning.
transaction | The transaction to execute the operation in. Must be a read-only snapshot transaction. |
statement | The SQL statement to execute. |
partition_options | PartitionOptions used for this request. |
StatusOr
containing a vector of QueryPartition
s or error status on failure.StatusOr< std::vector< ReadPartition > > google::cloud::spanner::v1::Client::PartitionRead | ( | Transaction | transaction, |
std::string | table, | ||
KeySet | keys, | ||
std::vector< std::string > | columns, | ||
ReadOptions | read_options = {} , |
||
PartitionOptions const & | partition_options = PartitionOptions{} |
||
) |
Creates a set of partitions that can be used to execute a read operation in parallel.
Each of the returned partitions can be passed to Read
to specify a subset of the read result to read.
There are no ordering guarantees on rows returned among the returned partition, or even within each individual Read
call issued with a given partition.
Partitions become invalid when the session used to create them is deleted, is idle for too long, begins a new transaction, or becomes too old. When any of these happen, it is not possible to resume the read, and the whole operation must be restarted from the beginning.
transaction | The transaction to execute the operation in. Must be a read-only snapshot transaction. |
table | The name of the table in the database to be read. |
keys | Identifies the rows to be yielded. If read_options.index_name is set, names keys in that index; otherwise names keys in the primary index of table . It is not an error for keys to name rows that do not exist in the database; Read yields nothing for nonexistent rows. |
columns | The columns of table to be returned for each row matching this request. |
read_options | ReadOptions used for this request. |
partition_options | PartitionOptions used for this request. |
StatusOr
containing a vector of ReadPartition
or error status on failure.StatusOr< ProfileDmlResult > google::cloud::spanner::v1::Client::ProfileDml | ( | Transaction | transaction, |
SqlStatement | statement, | ||
QueryOptions const & | opts = {} |
||
) |
Profiles a SQL DML statement.
Profiling executes the DML statement, provides the modified row count, ExecutionPlan
, and execution statistics.
Operations inside read-write transactions might return ABORTED
. If this occurs, the application should restart the transaction from the beginning.
transaction | Execute this query as part of an existing transaction. |
statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
ProfileQueryResult google::cloud::spanner::v1::Client::ProfileQuery | ( | SqlStatement | statement, |
QueryOptions const & | opts = {} |
||
) |
Profiles a SQL query.
Profiling executes the query, provides the resulting rows, ExecutionPlan
, and execution statistics.
Operations inside read-write transactions might return kAborted
. If this occurs, the application should restart the transaction from the beginning.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
ExecutionPlan
are available.statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
ProfileQueryResult
can exceed 100 MiB, and no column value can exceed 10 MiB.ProfileQueryResult google::cloud::spanner::v1::Client::ProfileQuery | ( | Transaction | transaction, |
SqlStatement | statement, | ||
QueryOptions const & | opts = {} |
||
) |
Profiles a SQL query.
Profiling executes the query, provides the resulting rows, ExecutionPlan
, and execution statistics.
Operations inside read-write transactions might return kAborted
. If this occurs, the application should restart the transaction from the beginning.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
ExecutionPlan
are available.statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
ProfileQueryResult
can exceed 100 MiB, and no column value can exceed 10 MiB.transaction | Execute this query as part of an existing transaction. |
ProfileQueryResult google::cloud::spanner::v1::Client::ProfileQuery | ( | Transaction::SingleUseOptions | transaction_options, |
SqlStatement | statement, | ||
QueryOptions const & | opts = {} |
||
) |
Profiles a SQL query.
Profiling executes the query, provides the resulting rows, ExecutionPlan
, and execution statistics.
Operations inside read-write transactions might return kAborted
. If this occurs, the application should restart the transaction from the beginning.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
ExecutionPlan
are available.statement | The SQL statement to execute. |
opts | The QueryOptions to use for this call. If given, these will take precedence over the options set at the client and environment levels. |
ProfileQueryResult
can exceed 100 MiB, and no column value can exceed 10 MiB.transaction_options | Execute this query in a single-use transaction with these options. |
RowStream google::cloud::spanner::v1::Client::Read | ( | ReadPartition const & | partition | ) |
Reads rows from a subset of rows in a database.
Requires a prior call to PartitionRead
to obtain the partition information; see the documentation of that method for full details.
partition | A ReadPartition , obtained by calling PartitionRead . |
ReadResult
can exceed 100 MiB, and no column value can exceed 10 MiB.RowStream google::cloud::spanner::v1::Client::Read | ( | std::string | table, |
KeySet | keys, | ||
std::vector< std::string > | columns, | ||
ReadOptions | read_options = {} |
||
) |
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteQuery()
.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
table | The name of the table in the database to be read. |
keys | Identifies the rows to be yielded. If read_options.index_name is set, names keys in that index; otherwise names keys in the primary index of table . It is not an error for keys to name rows that do not exist in the database; Read yields nothing for nonexistent rows. |
columns | The columns of table to be returned for each row matching this request. |
read_options | ReadOptions used for this request. |
ReadResult
can exceed 100 MiB, and no column value can exceed 10 MiB. RowStream google::cloud::spanner::v1::Client::Read | ( | Transaction | transaction, |
std::string | table, | ||
KeySet | keys, | ||
std::vector< std::string > | columns, | ||
ReadOptions | read_options = {} |
||
) |
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteQuery()
.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
table | The name of the table in the database to be read. |
keys | Identifies the rows to be yielded. If read_options.index_name is set, names keys in that index; otherwise names keys in the primary index of table . It is not an error for keys to name rows that do not exist in the database; Read yields nothing for nonexistent rows. |
columns | The columns of table to be returned for each row matching this request. |
read_options | ReadOptions used for this request. |
ReadResult
can exceed 100 MiB, and no column value can exceed 10 MiB.transaction | Execute this read as part of an existing transaction. |
RowStream google::cloud::spanner::v1::Client::Read | ( | Transaction::SingleUseOptions | transaction_options, |
std::string | table, | ||
KeySet | keys, | ||
std::vector< std::string > | columns, | ||
ReadOptions | read_options = {} |
||
) |
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteQuery()
.
Callers can optionally supply a Transaction
or Transaction::SingleUseOptions
used to create a single-use transaction - or neither, in which case a single-use transaction with default options is used.
table | The name of the table in the database to be read. |
keys | Identifies the rows to be yielded. If read_options.index_name is set, names keys in that index; otherwise names keys in the primary index of table . It is not an error for keys to name rows that do not exist in the database; Read yields nothing for nonexistent rows. |
columns | The columns of table to be returned for each row matching this request. |
read_options | ReadOptions used for this request. |
ReadResult
can exceed 100 MiB, and no column value can exceed 10 MiB.transaction_options | Execute this read in a single-use transaction with these options. |
Status google::cloud::spanner::v1::Client::Rollback | ( | Transaction | transaction | ) |
Rolls back a read-write transaction, releasing any locks it holds.
At any time before Commit
, the client can call Rollback
to abort the transaction. It is a good idea to call this for any read-write transaction that includes one or more Read
, ExecuteQuery
, or ExecuteDml
requests and ultimately decides not to commit.
Rollback
with a read-only transaction.transaction | The transaction to roll back. |