-
Notifications
You must be signed in to change notification settings - Fork 374
/
client.h
1225 lines (1173 loc) · 49.2 KB
/
client.h
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 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_SPANNER_CLIENT_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CLIENT_H
#include "google/cloud/spanner/batch_dml_result.h"
#include "google/cloud/spanner/client_options.h"
#include "google/cloud/spanner/commit_options.h"
#include "google/cloud/spanner/commit_result.h"
#include "google/cloud/spanner/connection.h"
#include "google/cloud/spanner/connection_options.h"
#include "google/cloud/spanner/database.h"
#include "google/cloud/spanner/internal/defaults.h"
#include "google/cloud/spanner/keys.h"
#include "google/cloud/spanner/mutations.h"
#include "google/cloud/spanner/partition_options.h"
#include "google/cloud/spanner/query_options.h"
#include "google/cloud/spanner/query_partition.h"
#include "google/cloud/spanner/read_options.h"
#include "google/cloud/spanner/read_partition.h"
#include "google/cloud/spanner/results.h"
#include "google/cloud/spanner/retry_policy.h"
#include "google/cloud/spanner/session_pool_options.h"
#include "google/cloud/spanner/sql_statement.h"
#include "google/cloud/spanner/transaction.h"
#include "google/cloud/spanner/version.h"
#include "google/cloud/backoff_policy.h"
#include "google/cloud/internal/non_constructible.h"
#include "google/cloud/options.h"
#include "google/cloud/status.h"
#include "google/cloud/status_or.h"
#include <google/spanner/v1/spanner.pb.h>
#include <grpcpp/grpcpp.h>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <vector>
namespace google {
namespace cloud {
namespace spanner {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
// clang-format off
/**
* Performs database client operations on Spanner.
*
* Applications use this class to perform operations on
* [Spanner Databases][spanner-doc-link].
*
* @par Performance
*
* `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.
*
* @par Thread Safety
*
* 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.
*
* @par Error Handling
*
* 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. For more information, see the
* [Error Handling Guide](#spanner-error-handling).
*
* @code
* namespace spanner = ::google::cloud::spanner;
*
* auto db = spanner::Database("my_project", "my_instance", "my_db_id"));
* auto conn = spanner::MakeConnection(db);
* auto client = spanner::Client(conn);
*
* auto rows = client.Read(...);
* using RowType = std::tuple<std::int64_t, std::string>;
* for (auto const& row : spanner::StreamOf<RowType>(rows)) {
* // ...
* }
* @endcode
*
* @par Query Options
*
* Most operations that take an `SqlStatement` may also be modified with
* query `Options`. These options can be set at various levels, with more
* specific levels taking precedence over broader ones. For example,
* `Options` 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
* query `Options` setting that they affect.
*
* Environment Variable | Options setting
* -------------------------------------- | --------------------
* `SPANNER_OPTIMIZER_VERSION` | `QueryOptimizerVersionOption`
* `SPANNER_OPTIMIZER_STATISTICS_PACKAGE` | `QueryOptimizerStatisticsPackageOption`
*
* @see https://cloud.google.com/spanner/docs/reference/rest/v1/QueryOptions
* @see http://cloud/spanner/docs/query-optimizer/manage-query-optimizer
*
* [spanner-doc-link]:
* https://cloud.google.com/spanner/docs/api-libraries-overview
*/
// clang-format on
class Client {
public:
/**
* Constructs a `Client` object using the specified @p conn and @p 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`.
*/
explicit Client(std::shared_ptr<Connection> conn, Options opts = {})
: conn_(std::move(conn)),
opts_(internal::MergeOptions(std::move(opts), conn_->options())) {}
/// No default construction.
Client() = delete;
///@{
/// @name Copy and move support
Client(Client const&) = default;
Client& operator=(Client const&) = default;
Client(Client&&) = default;
Client& operator=(Client&&) = default;
///@}
///@{
/// @name Equality
friend bool operator==(Client const& a, Client const& b) {
return a.conn_ == b.conn_;
}
friend bool operator!=(Client const& a, Client const& b) { return !(a == b); }
///@}
/**
* 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.
*
* @note No individual row in the `ReadResult` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*
* @par Example
* @snippet samples.cc read-data
*
* @param table The name of the table in the database to be read.
* @param 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.
* @param columns The columns of `table` to be returned for each row matching
* this request.
* @param opts `Options` used for this request.
*/
RowStream Read(std::string table, KeySet keys,
std::vector<std::string> columns, Options opts = {});
/**
* @copydoc Read
*
* @param transaction_options Execute this read in a single-use transaction
* with these options.
*/
RowStream Read(Transaction::SingleUseOptions transaction_options,
std::string table, KeySet keys,
std::vector<std::string> columns, Options opts = {});
/**
* @copydoc Read
*
* @param transaction Execute this read as part of an existing transaction.
*/
RowStream Read(Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns, Options opts = {});
/**
* 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.
* @note No individual row in the `ReadResult` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*
* @par Example
* @snippet samples.cc read-read-partition
*
* @param partition A `ReadPartition`, obtained by calling `PartitionRead`.
* @param opts `Options` used for this request.
*/
RowStream Read(ReadPartition const& partition, Options opts = {});
/**
* 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.
*
* @par Example
* @snippet samples.cc partition-read
*
* @param transaction The transaction to execute the operation in.
* **Must** be a read-only snapshot transaction.
* @param table The name of the table in the database to be read.
* @param 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.
* @param columns The columns of `table` to be returned for each row matching
* this request.
* @param opts `Options` used for this request.
*
* @return A `StatusOr` containing a vector of `ReadPartition` or error
* status on failure.
*/
StatusOr<std::vector<ReadPartition>> PartitionRead(
Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns, Options opts = {});
/**
* Executes a SQL query in a single-use transaction created with the
* default options.
*
* `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.
*
* @note No individual row in the `RowStream` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*
* @par Example
* Query with explicitly selected columns.
* @snippet samples.cc spanner-query-data
*
* @par Example
* Using `SELECT *`.
* @snippet samples.cc spanner-query-data-select-star
*
* @param statement The SQL statement to execute.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*/
RowStream ExecuteQuery(SqlStatement statement, Options opts = {});
/**
* Executes a SQL query in a single-use transaction created with the
* @p transaction_options.
*
* @param transaction_options Execute this query in a single-use transaction
* with these options.
* @param statement The SQL statement to execute.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*/
RowStream ExecuteQuery(Transaction::SingleUseOptions transaction_options,
SqlStatement statement, Options opts = {});
/**
* Executes a SQL query in an existing transaction.
*
* Operations inside read-write transactions might return `ABORTED`. If this
* occurs, the application should restart the transaction from the beginning.
*
* Can execute a DML statement with a returning clause in a read/write
* transaction.
*
* @par Example
* Using a DML statement with `THEN RETURN`.
* @snippet samples.cc spanner-update-dml-returning
*
* @param transaction Execute this query as part of an existing transaction.
* @param statement The SQL statement to execute.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*/
RowStream ExecuteQuery(Transaction transaction, SqlStatement statement,
Options 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.
*
* @note No individual row in the `RowStream` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*
* @par Example
* @snippet samples.cc execute-sql-query-partition
*
* @param partition A `QueryPartition`, obtained by calling `PartitionQuery`.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*/
RowStream ExecuteQuery(QueryPartition const& partition, Options 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.
*
* @note Callers must consume all rows from the result before execution
* statistics and `ExecutionPlan` are available.
*
* @note No individual row in the `ProfileQueryResult` can exceed 100 MiB, and
* no column value can exceed 10 MiB.
*
* @par Example
* @snippet samples.cc profile-query
*
* @param statement The SQL statement to execute.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*
*/
ProfileQueryResult ProfileQuery(SqlStatement statement, Options opts = {});
/**
* @copydoc ProfileQuery(SqlStatement, Options)
*
* @param transaction_options Execute this query in a single-use transaction
* with these options.
*/
ProfileQueryResult ProfileQuery(
Transaction::SingleUseOptions transaction_options, SqlStatement statement,
Options opts = {});
/**
* @copydoc ProfileQuery(SqlStatement, Options)
*
* @param transaction Execute this query as part of an existing transaction.
*/
ProfileQueryResult ProfileQuery(Transaction transaction,
SqlStatement statement, Options opts = {});
/**
* 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.
*
* @par Example
* @snippet samples.cc partition-query
*
* @param transaction The transaction to execute the operation in.
* **Must** be a read-only snapshot transaction.
* @param statement The SQL statement to execute.
* @param opts `Options` used for this request.
*
* @return A `StatusOr` containing a vector of `QueryPartition`s or error
* status on failure.
*/
StatusOr<std::vector<QueryPartition>> PartitionQuery(
Transaction transaction, SqlStatement statement,
Options opts = Options{});
/**
* 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.
*
* @note Single-use transactions are not supported with DML statements.
*
* @par Example
* @snippet samples.cc execute-dml
*
* @param transaction Execute this query as part of an existing transaction.
* @param statement The SQL statement to execute.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*/
StatusOr<DmlResult> ExecuteDml(Transaction transaction,
SqlStatement statement, Options 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.
*
* @note Single-use transactions are not supported with DML statements.
*
* @par Example
* @snippet samples.cc profile-dml
*
* @param transaction Execute this query as part of an existing transaction.
* @param statement The SQL statement to execute.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*/
StatusOr<ProfileDmlResult> ProfileDml(Transaction transaction,
SqlStatement statement,
Options 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.
*
* @note Single-use transactions are not supported with DML statements.
*
* @par Example
* @snippet samples.cc analyze-query
*
* @param transaction Execute this query as part of an existing transaction.
* @param statement The SQL statement to execute.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*/
StatusOr<ExecutionPlan> AnalyzeSql(Transaction transaction,
SqlStatement statement, 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.
*
* @warning A returned status of OK from this function does not imply that
* all the statements were executed successfully. For that, you need to
* inspect the `BatchDmlResult::status` field.
*
* @par Example
* @snippet samples.cc execute-batch-dml
*
* @param transaction The read-write transaction to execute the operation in.
* @param 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.
* @param opts (optional) The options to use for this call. Expected options
* are any of the types in the following option lists.
* - `google::cloud::RequestOptionList`
*/
StatusOr<BatchDmlResult> ExecuteBatchDml(Transaction transaction,
std::vector<SqlStatement> statements,
Options opts = {});
/**
* Commits a read-write transaction.
*
* Calls the @p mutator in the context of a new read-write transaction.
* The @p mutator can execute read/write operations using the transaction,
* and returns any additional `Mutations` to commit.
*
* If the @p mutator succeeds and the transaction commits, then `Commit()`
* returns the `CommitResult`.
*
* If the @p mutator returns a non-rerunnable status (according to the
* @p 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 @p rerun_policy and
* @p backoff_policy), by building a new transaction and re-running the
* @p 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 @p 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.
*
* @par Example
* @snippet samples.cc commit-with-policies
*
* @param mutator the function called to create mutations
* @param rerun_policy controls for how long (or how many times) the mutator
* will be rerun after the transaction aborts.
* @param backoff_policy controls how long `Commit` waits between reruns.
* @param opts (optional) The options to use for this call. Expected options
* include any of the following types:
* - `google::cloud::spanner::CommitReturnStatsOption`
* - `google::cloud::spanner::RequestPriorityOption`
* - `google::cloud::spanner::TransactionTagOption`
*
* @throw Rethrows any exception thrown by @p `mutator` (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> Commit(
std::function<StatusOr<Mutations>(Transaction)> const& mutator,
std::unique_ptr<TransactionRerunPolicy> rerun_policy,
std::unique_ptr<BackoffPolicy> backoff_policy, Options opts = {});
/**
* Commits a read-write transaction.
*
* Same as above, but uses the default rerun and backoff policies.
*
* @par Example
* @snippet samples.cc commit-with-mutator
*
* @param mutator the function called to create mutations
* @param opts (optional) The options to use for this call.
*/
StatusOr<CommitResult> Commit(
std::function<StatusOr<Mutations>(Transaction)> const& mutator,
Options opts = {});
/**
* Commits the @p mutations, using the @p options, atomically in order.
*
* @par Example
* @snippet samples.cc commit-with-mutations
*
* This function uses the re-run loop described above with the default
* policies.
*/
StatusOr<CommitResult> Commit(Mutations mutations, Options opts = {});
/**
* 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).
*
* @warning It is an error to call `Commit` with a read-only transaction.
*
* @note Prefer the previous `Commit` overloads if you want to simply reapply
* mutations after a `kAborted` error.
*
* @param transaction The transaction to commit.
* @param mutations The mutations to be executed when this transaction
* commits. All mutations are applied atomically, in the order they
* appear in this list.
* @param opts (optional) The options to use for this call.
*
* @return A `StatusOr` containing the result of the commit or error status
* on failure.
*/
StatusOr<CommitResult> Commit(Transaction transaction, Mutations mutations,
Options opts = {});
/**
* Commits a write transaction with at-least-once semantics.
*
* Apply the given mutations atomically, using a single RPC, and therefore
* without replay protection. That is, it is possible that the mutations
* will be applied more than once. If the mutations are not idempotent, this
* may lead to a failure (for example, an insert may fail with "already
* exists" even though the row did not exist before the call was made).
* Accordingly, this call may only be appropriate for idempotent, latency-
* sensitive and/or high-throughput blind writing.
*
* @note Prefer the `Commit` overloads if you want exactly-once semantics
* or want to reapply mutations after a `kAborted` error.
*
* @par Example
* @snippet samples.cc commit-at-least-once
*
* @param transaction_options Execute the commit in a temporary transaction
* with these options.
* @param mutations The mutations to be executed when this transaction
* commits. All mutations are applied atomically, in the order they
* appear in this list.
* @param opts (optional) The options to use for this call.
*
* @return A `StatusOr` containing the result of the commit or error status
* on failure.
*/
StatusOr<CommitResult> CommitAtLeastOnce(
Transaction::ReadWriteOptions transaction_options, Mutations mutations,
Options opts = {});
/**
* Commits the mutation groups, batched efficiently into transactions with
* at-least-once semantics, using a single RPC.
*
* All mutations within a group are committed atomically. There is no
* atomicity or ordering between groups however, so all groups must be
* independent of each other.
*
* Partial failure is possible. That is, some groups can commit successfully
* while others fail. The results of individual batches are returned via the
* response stream as their transactions complete.
*
* Mutation groups are not replay protected. That is, it is possible that
* each mutation group may be applied more than once. If the mutations are
* not idempotent, this may lead to a failure. For example, replays of an
* insert mutation might produce an "already exists" error, or, if you use
* generated or commit-timestamp-based keys, might result in additional
* rows being added to the mutation's table. We recommend structuring your
* mutation groups to be idempotent to avoid this issue.
*
* @note Prefer the `Commit` overloads if you want exactly-once semantics
* or want to automatically reapply mutations after a `kAborted` error.
*
* @par Example
* @snippet samples.cc commit-at-least-once-batched
*
* @param mutation_groups The mutation groups to be batched into temporary
* transactions and committed.
* @param opts (optional) The options to use for this call. Expected options
* include any of the following types:
* - `google::cloud::spanner::RequestPriorityOption`
* - `google::cloud::spanner::RequestTagOption`
* - `google::cloud::spanner::TransactionTagOption`
*/
BatchedCommitResultStream CommitAtLeastOnce(
std::vector<Mutations> mutation_groups, Options opts = {});
/**
* 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.
*
* @warning It is an error to call `Rollback` with a read-only transaction.
*
* @param transaction The transaction to roll back.
* @param opts (optional) The options to use for this call.
*
* @return The error status of the rollback.
*/
Status Rollback(Transaction transaction, Options opts = {});
/**
* Executes a Partitioned DML SQL query.
*
* @par Example
* @snippet samples.cc execute-sql-partitioned
*
* @param statement the SQL statement to execute. Please see the
* [spanner documentation][dml-partitioned] for the restrictions on the
* SQL statements supported by this function.
* @param opts (optional) The `Options` to use for this call. If given,
* these will take precedence over the options set at the client and
* environment levels.
*
* @see [Partitioned DML Transactions][txn-partitioned] for an overview of
* Partitioned DML transactions.
* @see [Partitioned DML][dml-partitioned] for a description of which SQL
* statements are supported in Partitioned DML transactions.
* [txn-partitioned]:
* https://cloud.google.com/spanner/docs/transactions#partitioned_dml_transactions
* [dml-partitioned]: https://cloud.google.com/spanner/docs/dml-partitioned
*/
StatusOr<PartitionedDmlResult> ExecutePartitionedDml(SqlStatement statement,
Options opts = {});
///@{
/// @name Backwards compatibility for ClientOptions.
explicit Client(std::shared_ptr<Connection> conn, ClientOptions const& opts)
: Client(std::move(conn), Options(opts)) {}
explicit Client(std::shared_ptr<Connection> conn,
std::initializer_list<internal::NonConstructible>)
: Client(std::move(conn)) {}
///@}
///@{
/// @name Backwards compatibility for ReadOptions.
/**
* @copybrief Read(std::string,KeySet,std::vector<std::string>,Options)
* @see Read(std::string,KeySet,std::vector<std::string>,Options)
*/
RowStream Read(std::string table, KeySet keys,
std::vector<std::string> columns,
ReadOptions const& read_options) {
return Read(std::move(table), std::move(keys), std::move(columns),
ToOptions(read_options));
}
/**
* @copybrief Read(std::string,KeySet,std::vector<std::string>,Options)
* @see Read(std::string,KeySet,std::vector<std::string>,Options)
*/
RowStream Read(std::string table, KeySet keys,
std::vector<std::string> columns,
std::initializer_list<internal::NonConstructible>) {
return Read(std::move(table), std::move(keys), std::move(columns));
}
/**
* @copybrief Read(Transaction::SingleUseOptions,std::string,KeySet,std::vector<std::string>,Options)
* @see Read(Transaction::SingleUseOptions,std::string,KeySet,std::vector<std::string>,Options)
*/
RowStream Read(Transaction::SingleUseOptions transaction_options,
std::string table, KeySet keys,
std::vector<std::string> columns,
ReadOptions const& read_options) {
return Read(std::move(transaction_options), std::move(table),
std::move(keys), std::move(columns), ToOptions(read_options));
}
/**
* @copybrief Read(Transaction::SingleUseOptions,std::string,KeySet,std::vector<std::string>,Options)
* @see Read(Transaction::SingleUseOptions,std::string,KeySet,std::vector<std::string>,Options)
*/
RowStream Read(Transaction::SingleUseOptions transaction_options,
std::string table, KeySet keys,
std::vector<std::string> columns,
std::initializer_list<internal::NonConstructible>) {
return Read(std::move(transaction_options), std::move(table),
std::move(keys), std::move(columns));
}
/**
* @copybrief Read(Transaction,std::string,KeySet,std::vector<std::string>,Options)
* @see Read(Transaction,std::string,KeySet,std::vector<std::string>,Options)
*/
RowStream Read(Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns,
ReadOptions const& read_options) {
return Read(std::move(transaction), std::move(table), std::move(keys),
std::move(columns), ToOptions(read_options));
}
/**
* @copybrief Read(Transaction,std::string,KeySet,std::vector<std::string>,Options)
* @see Read(Transaction,std::string,KeySet,std::vector<std::string>,Options)
*/
RowStream Read(Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns,
std::initializer_list<internal::NonConstructible>) {
return Read(std::move(transaction), std::move(table), std::move(keys),
std::move(columns));
}
///@}
///@{
/// @name Backwards compatibility for ReadOptions and PartitionOptions.
/**
* @copybrief PartitionRead(Transaction,std::string,KeySet,std::vector<std::string>,Options)
* @see PartitionRead(Transaction,std::string,KeySet,std::vector<std::string>,Options)
*/
StatusOr<std::vector<ReadPartition>> PartitionRead(
Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns, ReadOptions const& read_options,
PartitionOptions const& partition_options) {
return PartitionRead(std::move(transaction), std::move(table),
std::move(keys), std::move(columns),
internal::MergeOptions(ToOptions(read_options),
ToOptions(partition_options)));
}
/**
* @copybrief PartitionRead(Transaction,std::string,KeySet,std::vector<std::string>,Options)
* @see PartitionRead(Transaction,std::string,KeySet,std::vector<std::string>,Options)
*/
StatusOr<std::vector<ReadPartition>> PartitionRead(
Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns,
std::initializer_list<internal::NonConstructible>) {
return PartitionRead(std::move(transaction), std::move(table),
std::move(keys), std::move(columns));
}
///@}
///@{
/// @name Backwards compatibility for QueryOptions.
/**
* @copybrief ExecuteQuery(SqlStatement,Options)
* @see ExecuteQuery(SqlStatement,Options)
*/
RowStream ExecuteQuery(SqlStatement statement, QueryOptions const& opts) {
return ExecuteQuery(std::move(statement), Options(opts));
}
/**
* @copybrief ExecuteQuery(SqlStatement,Options)
* @see ExecuteQuery(SqlStatement,Options)
*/
RowStream ExecuteQuery(SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return ExecuteQuery(std::move(statement));
}
/**
* @copybrief ExecuteQuery(Transaction::SingleUseOptions,SqlStatement,Options)
* @see ExecuteQuery(Transaction::SingleUseOptions,SqlStatement,Options)
*/
RowStream ExecuteQuery(Transaction::SingleUseOptions transaction_options,
SqlStatement statement, QueryOptions const& opts) {
return ExecuteQuery(std::move(transaction_options), std::move(statement),
Options(opts));
}
/**
* @copybrief ExecuteQuery(Transaction::SingleUseOptions,SqlStatement,Options)
* @see ExecuteQuery(Transaction::SingleUseOptions,SqlStatement,Options)
*/
RowStream ExecuteQuery(Transaction::SingleUseOptions transaction_options,
SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return ExecuteQuery(std::move(transaction_options), std::move(statement));
}
/**
* @copybrief ExecuteQuery(Transaction,SqlStatement,Options)
* @see ExecuteQuery(Transaction,SqlStatement,Options)
*/
RowStream ExecuteQuery(Transaction transaction, SqlStatement statement,
QueryOptions const& opts) {
return ExecuteQuery(std::move(transaction), std::move(statement),
Options(opts));
}
/**
* @copybrief ExecuteQuery(Transaction,SqlStatement,Options)
* @see ExecuteQuery(Transaction,SqlStatement,Options)
*/
RowStream ExecuteQuery(Transaction transaction, SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return ExecuteQuery(std::move(transaction), std::move(statement));
}
/**
* @copybrief ExecuteQuery(QueryPartition const&,Options)
* @see ExecuteQuery(QueryPartition const&,Options)
*/
RowStream ExecuteQuery(QueryPartition const& partition,
QueryOptions const& opts) {
return ExecuteQuery(partition, Options(opts));
}
/**
* @copybrief ExecuteQuery(QueryPartition const&,Options)
* @see ExecuteQuery(QueryPartition const&,Options)
*/
RowStream ExecuteQuery(QueryPartition const& partition,
std::initializer_list<internal::NonConstructible>) {
return ExecuteQuery(partition);
}
///@}
///@{
/// @name Backwards compatibility for QueryOptions.
/**
* @copybrief ProfileQuery(SqlStatement,Options)
* @see ProfileQuery(SqlStatement,Options)
*/
ProfileQueryResult ProfileQuery(SqlStatement statement,
QueryOptions const& opts) {
return ProfileQuery(std::move(statement), Options(opts));
}
/**
* @copybrief ProfileQuery(SqlStatement,Options)
* @see ProfileQuery(SqlStatement,Options)
*/
ProfileQueryResult ProfileQuery(
SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return ProfileQuery(std::move(statement));
}
/**
* @copybrief ProfileQuery(Transaction::SingleUseOptions,SqlStatement,Options)
* @see ProfileQuery(Transaction::SingleUseOptions,SqlStatement,Options)
*/
ProfileQueryResult ProfileQuery(
Transaction::SingleUseOptions transaction_options, SqlStatement statement,
QueryOptions const& opts) {
return ProfileQuery(std::move(transaction_options), std::move(statement),
Options(opts));
}
/**
* @copybrief ProfileQuery(Transaction::SingleUseOptions,SqlStatement,Options)
* @see ProfileQuery(Transaction::SingleUseOptions,SqlStatement,Options)
*/
ProfileQueryResult ProfileQuery(
Transaction::SingleUseOptions transaction_options, SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return ProfileQuery(std::move(transaction_options), std::move(statement));
}
/**
* @copybrief ProfileQuery(Transaction,SqlStatement,Options)
* @see ProfileQuery(Transaction,SqlStatement,Options)
*/
ProfileQueryResult ProfileQuery(Transaction transaction,
SqlStatement statement,
QueryOptions const& opts) {
return ProfileQuery(std::move(transaction), std::move(statement),
Options(opts));
}
/**
* @copybrief ProfileQuery(Transaction,SqlStatement,Options)
* @see ProfileQuery(Transaction,SqlStatement,Options)
*/
ProfileQueryResult ProfileQuery(
Transaction transaction, SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return ProfileQuery(std::move(transaction), std::move(statement));
}
///@}
///@{
/// @name Backwards compatibility for PartitionOptions.
/**
* @copybrief PartitionQuery(Transaction,SqlStatement,Options)
* @see PartitionQuery(Transaction,SqlStatement,Options)
*/
StatusOr<std::vector<QueryPartition>> PartitionQuery(
Transaction transaction, SqlStatement statement,
PartitionOptions const& partition_options) {
return PartitionQuery(std::move(transaction), std::move(statement),
ToOptions(partition_options));
}
/**
* @copybrief PartitionQuery(Transaction,SqlStatement,Options)
* @see PartitionQuery(Transaction,SqlStatement,Options)
*/
StatusOr<std::vector<QueryPartition>> PartitionQuery(
Transaction transaction, SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return PartitionQuery(std::move(transaction), std::move(statement));
}
///@}
///@{
/// @name Backwards compatibility for QueryOptions.
/**
* @copybrief ExecuteDml(Transaction,SqlStatement,Options)
* @see ExecuteDml(Transaction,SqlStatement,Options)
*/
StatusOr<DmlResult> ExecuteDml(Transaction transaction,
SqlStatement statement,
QueryOptions const& opts) {
return ExecuteDml(std::move(transaction), std::move(statement),
Options(opts));
}
/**
* @copybrief ExecuteDml(Transaction,SqlStatement,Options)
* @see ExecuteDml(Transaction,SqlStatement,Options)
*/
StatusOr<DmlResult> ExecuteDml(
Transaction transaction, SqlStatement statement,
std::initializer_list<internal::NonConstructible>) {
return ExecuteDml(std::move(transaction), std::move(statement));
}
///@}
///@{
/// @name Backwards compatibility for QueryOptions.
StatusOr<ProfileDmlResult> ProfileDml(Transaction transaction,
SqlStatement statement,