forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.h
1085 lines (1029 loc) · 46.8 KB
/
table.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 2017 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_TABLE_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_TABLE_H
#include "google/cloud/bigtable/completion_queue.h"
#include "google/cloud/bigtable/data_client.h"
#include "google/cloud/bigtable/data_connection.h"
#include "google/cloud/bigtable/filters.h"
#include "google/cloud/bigtable/idempotent_mutation_policy.h"
#include "google/cloud/bigtable/internal/defaults.h"
#include "google/cloud/bigtable/internal/legacy_async_row_reader.h"
#include "google/cloud/bigtable/mutation_branch.h"
#include "google/cloud/bigtable/mutations.h"
#include "google/cloud/bigtable/options.h"
#include "google/cloud/bigtable/read_modify_write_rule.h"
#include "google/cloud/bigtable/resource_names.h"
#include "google/cloud/bigtable/row_key_sample.h"
#include "google/cloud/bigtable/row_reader.h"
#include "google/cloud/bigtable/row_set.h"
#include "google/cloud/bigtable/rpc_backoff_policy.h"
#include "google/cloud/bigtable/rpc_retry_policy.h"
#include "google/cloud/bigtable/table_resource.h"
#include "google/cloud/bigtable/version.h"
#include "google/cloud/future.h"
#include "google/cloud/grpc_error_delegate.h"
#include "google/cloud/internal/group_options.h"
#include "google/cloud/options.h"
#include "google/cloud/status.h"
#include "google/cloud/status_or.h"
#include "absl/meta/type_traits.h"
#include <string>
#include <vector>
namespace google {
namespace cloud {
namespace bigtable {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
class MutationBatcher;
/**
* Return the full table name.
*
* The full table name is:
*
* `projects/<PROJECT_ID>/instances/<INSTANCE_ID>/tables/<table_id>`
*
* Where the project id and instance id come from the @p client parameter.
*/
inline std::string TableName(std::shared_ptr<DataClient> const& client,
std::string const& table_id) {
return InstanceName(client) + "/tables/" + table_id;
}
/**
* The main interface to interact with data in a Cloud Bigtable table.
*
* This class provides member functions to:
* - read specific rows: `Table::ReadRow()`
* - scan a ranges of rows: `Table::ReadRows()`
* - update or create a single row: `Table::Apply()`
* - update or modify multiple rows: `Table::BulkApply()`
* - update a row based on previous values: `Table::CheckAndMutateRow()`
* - to atomically append data and/or increment multiple values in a row:
* `Table::ReadModifyWriteRow()`
* - to sample the row keys: `Table::SampleRows()`.
*
* The class deals with the most common transient failures, and retries the
* underlying RPC calls subject to the policies configured by the application.
* These policies are documented in `Table::Table()`.
*
* @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 concurrently on the same
* instance of this class is not guaranteed to work.
*
* @par Cost
* Creating a new object of type `Table` is comparable to creating a few objects
* of type `std::string` or a few objects of type `std::shared_ptr<int>`. The
* class represents a shallow handle to a remote object.
*
* @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. Operations that do not return a value simply
* return a `google::cloud::Status` indicating success or the details of the
* error Please consult the [`StatusOr<T>`
* documentation](#google::cloud::StatusOr) for more details.
*
* @code
* namespace cbt = google::cloud::bigtable;
* cbt::Table = ...;
* google::cloud::StatusOr<std::pair<bool, cbt::Row>> row = table.ReadRow(...);
*
* if (!row) {
* std::cerr << "Error reading row\n";
* return;
* }
*
* // Use "row" as a smart pointer here, e.g.:
* if (!row->first) {
* std::cout << "Contacting the server was successful, but the row does not"
* << " exist\n";
* return;
* }
* std::cout << "The row has " << row->second.cells().size() << " cells\n";
* @endcode
*
* In addition, the @ref index "main page" contains examples using `StatusOr<T>`
* to handle errors.
*
* @par Retry, Backoff, and Idempotency Policies
* The library automatically retries requests that fail with transient errors,
* and uses [truncated exponential backoff][backoff-link] to backoff between
* retries. The default policies are to continue retrying for up to 10 minutes.
* On each transient failure the backoff period is doubled, starting with an
* initial backoff of 100 milliseconds. The backoff period growth is truncated
* at 60 seconds. The default idempotency policy is to only retry idempotent
* operations. Note that most operations that change state are **not**
* idempotent.
*
* The application can override these policies when constructing objects of this
* class. The documentation for the constructors show examples of this in
* action.
*
* [backoff-link]: https://cloud.google.com/storage/docs/exponential-backoff
*
* @see https://cloud.google.com/bigtable/ for an overview of Cloud Bigtable.
*
* @see https://cloud.google.com/bigtable/docs/overview for an overview of the
* Cloud Bigtable data model.
*
* @see https://cloud.google.com/bigtable/docs/instances-clusters-nodes for an
* introduction of the main APIs into Cloud Bigtable.
*
* @see https://cloud.google.com/bigtable/docs/reference/service-apis-overview
* for an overview of the underlying Cloud Bigtable API.
*
* @see #google::cloud::StatusOr for a description of the error reporting class
* used by this library.
*
* @see `LimitedTimeRetryPolicy` and `LimitedErrorCountRetryPolicy` for
* alternative retry policies.
*
* @see `ExponentialBackoffPolicy` to configure different parameters for the
* exponential backoff policy.
*
* @see `SafeIdempotentMutationPolicy` and `AlwaysRetryMutationPolicy` for
* alternative idempotency policies.
*/
class Table {
private:
// We need to eliminate some function overloads from resolution, and that
// requires a bit of infrastructure in the private section.
/// A meta function to check if @p P is a valid Policy type.
template <typename P>
struct ValidPolicy
: absl::disjunction<std::is_base_of<RPCBackoffPolicy, P>,
std::is_base_of<RPCRetryPolicy, P>,
std::is_base_of<IdempotentMutationPolicy, P>> {};
/// A meta function to check if all the @p Policies are valid policy types.
template <typename... Policies>
struct ValidPolicies : absl::conjunction<ValidPolicy<Policies>...> {};
public:
/**
* Constructs a `Table` object.
*
* @param conn the connection to the Cloud Bigtable service. See
* `MakeDataConnection()` for how to create a connection. To mock the
* behavior of `Table` in your tests, use a
* `bigtable_mocks::MockDataConnection`.
* @param tr identifies the table resource by its project, instance, and table
* ids.
* @param options Configuration options for the table. Use
* `AppProfileIdOption` to supply an app profile for the `Table`
* operations. Or configure retry / backoff / idempotency policies with
* the options enumerated in `DataPolicyOptionList`.
*
* @par Example Using AppProfile
* @snippet bigtable_hello_app_profile.cc read with app profile
*
* @par Idempotency Policy Example
* @snippet data_snippets.cc apply relaxed idempotency
*
* @par Modified Retry Policy Example
* @snippet data_snippets.cc apply custom retry
*/
explicit Table(std::shared_ptr<bigtable::DataConnection> conn,
TableResource tr, Options options = {})
: table_(std::move(tr)),
table_name_(table_.FullName()),
connection_(std::move(conn)),
options_(google::cloud::internal::MergeOptions(std::move(options),
connection_->options())),
metadata_update_policy_(bigtable_internal::MakeMetadataUpdatePolicy(
table_name_, app_profile_id())) {}
std::string const& table_name() const { return table_name_; }
std::string const& app_profile_id() const {
return options_.get<AppProfileIdOption>();
}
std::string const& project_id() const {
return table_.instance().project_id();
}
std::string const& instance_id() const {
return table_.instance().instance_id();
}
std::string const& table_id() const { return table_.table_id(); }
/**
* Returns a Table that reuses the connection and configuration of this
* Table, but with a different resource name.
*
* @note The app profile id is copied from this Table.
*/
Table WithNewTarget(std::string project_id, std::string instance_id,
std::string table_id) const {
auto table = *this;
table.table_ = TableResource(std::move(project_id), std::move(instance_id),
std::move(table_id));
table.table_name_ = table.table_.FullName();
table.metadata_update_policy_ = bigtable_internal::MakeMetadataUpdatePolicy(
table.table_name_, table.app_profile_id());
return table;
}
/**
* Returns a Table that reuses the connection and configuration of this
* Table, but with a different resource name.
*/
Table WithNewTarget(std::string project_id, std::string instance_id,
std::string app_profile_id, std::string table_id) const {
auto table = *this;
table.table_ = TableResource(std::move(project_id), std::move(instance_id),
std::move(table_id));
table.table_name_ = table.table_.FullName();
table.options_.set<AppProfileIdOption>(std::move(app_profile_id));
table.metadata_update_policy_ = bigtable_internal::MakeMetadataUpdatePolicy(
table.table_name_, table.app_profile_id());
return table;
}
/**
* Attempts to apply the mutation to a row.
*
* @param mut the mutation. Note that this function takes ownership (and
* then discards) the data in the mutation. In general, a
* `SingleRowMutation` can be used to modify and/or delete multiple cells,
* across different columns and column families.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
*
* @return status of the operation.
*
* @par Idempotency
* This operation is idempotent if the provided mutations are idempotent. Note
* that `google::cloud::bigtable::SetCell()` without an explicit timestamp is
* **not** an idempotent operation.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work.
*
* @par Example
* @snippet data_snippets.cc apply
*/
Status Apply(SingleRowMutation mut, Options opts = {});
/**
* Makes asynchronous attempts to apply the mutation to a row.
*
* @param mut the mutation. Note that this function takes ownership
* (and then discards) the data in the mutation. In general, a
* `SingleRowMutation` can be used to modify and/or delete
* multiple cells, across different columns and column families.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
*
* @par Idempotency
* This operation is idempotent if the provided mutations are idempotent. Note
* that `google::cloud::bigtable::SetCell()` without an explicit timestamp is
* **not** an idempotent operation.
*
* @par Example
* @snippet data_async_snippets.cc async-apply
*/
future<Status> AsyncApply(SingleRowMutation mut, Options opts = {});
/**
* Attempts to apply mutations to multiple rows.
*
* These mutations are applied in bulk, in a single `MutateRowsRequest` RPC.
* Failures are handled on a per mutation basis. If the result of a mutation
* is a permanent (non-retryable) error, or if a non-idempotent mutation fails
* for any reason, the mutation will not be retried. Only idempotent mutations
* that encounter transient (retryable) errors can be retried. These mutations
* are collected and retried in bulk. This function will continue to retry any
* remaining errors until this class's retry policy is exhausted.
*
* It is possible that some mutations may not be attempted at all. These
* mutations are considered failing and will be returned.
*
* @note This function takes ownership (and then discards) the data in the
* mutation. In general, a `BulkMutation` can modify multiple rows, and
* the modifications for each row can change (or create) multiple cells,
* across different columns and column families.
*
* @param mut the mutations
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
* @returns a list of failed mutations
*
* @par Idempotency
* This operation is idempotent if the provided mutations are idempotent. Note
* that `google::cloud::bigtable::SetCell()` without an explicit timestamp is
* **not** an idempotent operation.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Example
* @snippet data_snippets.cc bulk apply
*/
std::vector<FailedMutation> BulkApply(BulkMutation mut, Options opts = {});
/**
* Makes asynchronous attempts to apply mutations to multiple rows.
*
* These mutations are applied in bulk, in a single `MutateRowsRequest` RPC.
* Failures are handled on a per mutation basis. If the result of a mutation
* is a permanent (non-retryable) error, or if a non-idempotent mutation fails
* for any reason, the mutation will not be retried. Only idempotent mutations
* that encounter transient (retryable) errors can be retried. These mutations
* are collected and retried in bulk. This function will continue to retry any
* remaining errors until this class's retry policy is exhausted.
*
* It is possible that some mutations may not be attempted at all. These
* mutations are considered failing and will be returned.
*
* @note This function takes ownership (and then discards) the data in the
* mutation. In general, a `BulkMutation` can modify multiple rows, and
* the modifications for each row can change (or create) multiple cells,
* across different columns and column families.
*
* @param mut the mutations
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
* @returns a future to be filled with a list of failed mutations, when the
* operation is complete.
*
* @par Idempotency
* This operation is idempotent if the provided mutations are idempotent. Note
* that `google::cloud::bigtable::SetCell()` without an explicit timestamp is
* **not** an idempotent operation.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Example
* @snippet data_async_snippets.cc bulk async-bulk-apply
*/
future<std::vector<FailedMutation>> AsyncBulkApply(BulkMutation mut,
Options opts = {});
/**
* Reads a set of rows from the table.
*
* @param row_set the rows to read from.
* @param filter is applied on the server-side to data in the rows.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
*
* @par Idempotency
* This is a read-only operation and therefore it is always idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread. The values returned by
* different calls are independent with respect to thread-safety, please see
* the `RowReader` documentation for more details.
*
* @par Example
* @snippet read_snippets.cc read rows
*/
RowReader ReadRows(RowSet row_set, Filter filter, Options opts = {});
/**
* Reads a limited set of rows from the table.
*
* @param row_set the rows to read from.
* @param rows_limit the maximum number of rows to read. Cannot be a negative
* number or zero. Use `ReadRows(RowSet, Filter)` to read all matching
* rows.
* @param filter is applied on the server-side to data in the rows.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
*
* @par Idempotency
* This is a read-only operation and therefore it is always idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread. The values returned by
* different calls are independent with respect to thread-safety, please see
* the `RowReader` documentation for more details.
*
* @par Example
* @snippet read_snippets.cc read rows with limit
*/
RowReader ReadRows(RowSet row_set, std::int64_t rows_limit, Filter filter,
Options opts = {});
/**
* Read and return a single row from the table.
*
* @param row_key the row to read.
* @param filter a filter expression, can be used to select a subset of the
* column families and columns in the row.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
* @returns a tuple, the first element is a boolean, with value `false` if the
* row does not exist. If the first element is `true` the second element
* has the contents of the Row. Note that the contents may be empty
* if the filter expression removes all column families and columns.
*
* @par Idempotency
* This is a read-only operation and therefore it is always idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Example
* @snippet read_snippets.cc read row
*/
StatusOr<std::pair<bool, Row>> ReadRow(std::string row_key, Filter filter,
Options opts = {});
/**
* Atomic test-and-set for a row using filter expressions.
*
* Atomically check the value of a row using a filter expression. If the
* expression passes (meaning at least one element is returned by it), one
* set of mutations is applied. If the filter does not pass, a different set
* of mutations is applied. The changes are atomically applied in the server.
*
* @param row_key the row to modify.
* @param filter the filter expression.
* @param true_mutations the mutations for the "filter passed" case.
* @param false_mutations the mutations for the "filter did not pass" case.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
* @returns true if the filter passed.
*
* @par Idempotency
* This operation is always treated as non-idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Check for Value Example
* @snippet data_snippets.cc check and mutate
*
* @par Check for Cell Presence Example
* @snippet data_snippets.cc check and mutate not present
*/
StatusOr<MutationBranch> CheckAndMutateRow(
std::string row_key, Filter filter, std::vector<Mutation> true_mutations,
std::vector<Mutation> false_mutations, Options opts = {});
/**
* Make an asynchronous request to conditionally mutate a row.
*
* @param row_key the row key on which the conditional mutation will be
* performed
* @param filter the condition, depending on which the mutation will be
* performed
* @param true_mutations the mutations which will be performed if @p filter is
* true
* @param false_mutations the mutations which will be performed if @p filter
* is false
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
*
* @par Idempotency
* This operation is always treated as non-idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Example
* @snippet data_async_snippets.cc async check and mutate
*/
future<StatusOr<MutationBranch>> AsyncCheckAndMutateRow(
std::string row_key, Filter filter, std::vector<Mutation> true_mutations,
std::vector<Mutation> false_mutations, Options opts = {});
/**
* Sample of the row keys in the table, including approximate data sizes.
*
* @note The sample may only include one element for small tables. In
* addition, the sample may include row keys that do not exist on the
* table, and may include the empty row key to indicate "end of table".
*
* @par Idempotency
* This operation is always treated as idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Examples
* @snippet data_snippets.cc sample row keys
*/
StatusOr<std::vector<bigtable::RowKeySample>> SampleRows(Options opts = {});
/**
* Asynchronously obtains a sample of the row keys in the table, including
* approximate data sizes.
*
* @returns a future, that becomes satisfied when the operation completes.
*
* @note The sample may only include one element for small tables. In
* addition, the sample may include row keys that do not exist on the
* table, and may include the empty row key to indicate "end of table".
*
* @par Idempotency
* This operation is always treated as idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Examples
* @snippet data_async_snippets.cc async sample row keys
*/
future<StatusOr<std::vector<bigtable::RowKeySample>>> AsyncSampleRows(
Options opts = {});
/**
* Atomically read and modify the row in the server, returning the
* resulting row
*
* @tparam Args this is zero or more ReadModifyWriteRules to apply on a row.
* Options to override the class-level options, such as retry, backoff,
* and idempotency policies are also be passed via this parameter pack.
* @param row_key the row to read
* @param rule to modify the row. Two types of rules are applied here
* AppendValue which will read the existing value and append the
* text provided to the value.
* IncrementAmount which will read the existing uint64 big-endian-int
* and add the value provided.
* Both rules accept the family and column identifier to modify.
* @param rules_and_options is the zero or more ReadModifyWriteRules to apply
* on a row. Options to override the class-level options, such as retry,
* backoff, and idempotency policies are also be passed via this parameter
* pack.
* @returns the new contents of all modified cells.
*
* @par Idempotency
* This operation is always treated as non-idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Example
* @snippet data_snippets.cc read modify write
*/
template <typename... Args>
StatusOr<Row> ReadModifyWriteRow(std::string row_key,
bigtable::ReadModifyWriteRule rule,
Args&&... rules_and_options) {
::google::bigtable::v2::ReadModifyWriteRowRequest request;
request.set_row_key(std::move(row_key));
// Generate a better compile time error message than the default one
// if the types do not match
static_assert(
absl::conjunction<absl::disjunction<
std::is_convertible<Args, bigtable::ReadModifyWriteRule>,
std::is_same<std::decay_t<Args>, Options>>...>::value,
"The arguments passed to ReadModifyWriteRow(row_key,...) must be "
"convertible to bigtable::ReadModifyWriteRule, or of type "
"google::cloud::Options");
*request.add_rules() = std::move(rule).as_proto();
AddRules(request, std::forward<Args>(rules_and_options)...);
auto opts = google::cloud::internal::GroupOptions(
std::forward<Args>(rules_and_options)...);
return ReadModifyWriteRowImpl(std::move(request), std::move(opts));
}
/**
* Make an asynchronous request to atomically read and modify a row.
*
* @tparam Args this is zero or more ReadModifyWriteRules to apply on a row.
* Options to override the class-level options, such as retry, backoff,
* and idempotency policies are also be passed via this parameter pack.
* @param row_key the row key on which modification will be performed
* @param rule to modify the row. Two types of rules are applied here
* AppendValue which will read the existing value and append the
* text provided to the value.
* IncrementAmount which will read the existing uint64 big-endian-int
* and add the value provided.
* Both rules accept the family and column identifier to modify.
* @param rules_and_options is the zero or more ReadModifyWriteRules to apply
* on a row. Options to override the class-level options, such as retry,
* backoff, and idempotency policies are also be passed via this parameter
* pack.
* @returns a future, that becomes satisfied when the operation completes,
* at that point the future has the contents of all modified cells.
*
* @par Idempotency
* This operation is always treated as non-idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work.
*
* @par Example
* @snippet data_async_snippets.cc async read modify write
*/
template <typename... Args>
future<StatusOr<Row>> AsyncReadModifyWriteRow(
std::string row_key, bigtable::ReadModifyWriteRule rule,
Args&&... rules_and_options) {
::google::bigtable::v2::ReadModifyWriteRowRequest request;
request.set_row_key(std::move(row_key));
// Generate a better compile time error message than the default one
// if the types do not match
static_assert(
absl::conjunction<absl::disjunction<
std::is_convertible<Args, bigtable::ReadModifyWriteRule>,
std::is_same<std::decay_t<Args>, Options>>...>::value,
"The arguments passed to AsyncReadModifyWriteRow(row_key,...) must be "
"convertible to bigtable::ReadModifyWriteRule, or of type "
"google::cloud::Options");
*request.add_rules() = std::move(rule).as_proto();
AddRules(request, std::forward<Args>(rules_and_options)...);
auto opts = google::cloud::internal::GroupOptions(
std::forward<Args>(rules_and_options)...);
return AsyncReadModifyWriteRowImpl(std::move(request), std::move(opts));
}
/**
* Asynchronously reads a set of rows from the table.
*
* @param on_row the callback to be invoked on each successfully read row; it
* should be invocable with `Row` and return a future<bool>; the returned
* `future<bool>` should be satisfied with `true` when the user is ready
* to receive the next callback and with `false` when the user doesn't
* want any more rows; if `on_row` throws, the results are undefined
* @param on_finish the callback to be invoked when the stream is closed; it
* should be invocable with `Status` and not return anything; it will
* always be called as the last callback; if `on_finish` throws, the
* results are undefined
* @param row_set the rows to read from.
* @param filter is applied on the server-side to data in the rows.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
*
* @tparam RowFunctor the type of the @p on_row callback.
* @tparam FinishFunctor the type of the @p on_finish callback.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Example
* @snippet data_async_snippets.cc async read rows
*/
template <typename RowFunctor, typename FinishFunctor>
void AsyncReadRows(RowFunctor on_row, FinishFunctor on_finish, RowSet row_set,
Filter filter, Options opts = {}) {
AsyncReadRows(std::move(on_row), std::move(on_finish), std::move(row_set),
RowReader::NO_ROWS_LIMIT, std::move(filter), std::move(opts));
}
/**
* Asynchronously reads a set of rows from the table.
*
* @param on_row the callback to be invoked on each successfully read row; it
* should be invocable with `Row` and return a future<bool>; the returned
* `future<bool>` should be satisfied with `true` when the user is ready
* to receive the next callback and with `false` when the user doesn't
* want any more rows; if `on_row` throws, the results are undefined
* @param on_finish the callback to be invoked when the stream is closed; it
* should be invocable with `Status` and not return anything; it will
* always be called as the last callback; if `on_finish` throws, the
* results are undefined
* @param row_set the rows to read from.
* @param rows_limit the maximum number of rows to read. Cannot be a negative
* number or zero. Use `AsyncReadRows(RowSet, Filter)` to
* read all matching rows.
* @param filter is applied on the server-side to data in the rows.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
*
* @tparam RowFunctor the type of the @p on_row callback.
* @tparam FinishFunctor the type of the @p on_finish callback.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread. The callbacks passed to this
* function may be executed on any thread running the provided completion
* queue.
*
* @par Example
* @snippet data_async_snippets.cc async read rows with limit
*/
template <typename RowFunctor, typename FinishFunctor>
void AsyncReadRows(RowFunctor on_row, FinishFunctor on_finish,
// NOLINTNEXTLINE(performance-unnecessary-value-param)
RowSet row_set, std::int64_t rows_limit, Filter filter,
Options opts = {}) {
static_assert(
google::cloud::internal::is_invocable<RowFunctor, bigtable::Row>::value,
"RowFunctor must be invocable with Row.");
static_assert(
google::cloud::internal::is_invocable<FinishFunctor, Status>::value,
"FinishFunctor must be invocable with Status.");
static_assert(
std::is_same<
google::cloud::internal::invoke_result_t<RowFunctor, bigtable::Row>,
future<bool>>::value,
"RowFunctor should return a future<bool>.");
auto on_row_ptr = std::make_shared<RowFunctor>(std::move(on_row));
// NOLINTNEXTLINE(performance-unnecessary-value-param)
auto on_row_fn = [on_row_ptr](Row row) {
return (*on_row_ptr)(std::move(row));
};
auto on_finish_ptr = std::make_shared<FinishFunctor>(std::move(on_finish));
// NOLINTNEXTLINE(performance-unnecessary-value-param)
auto on_finish_fn = [on_finish_ptr](Status status) {
return (*on_finish_ptr)(std::move(status));
};
if (connection_) {
google::cloud::internal::OptionsSpan span(
google::cloud::internal::MergeOptions(std::move(opts), options_));
connection_->AsyncReadRows(table_name_, std::move(on_row_fn),
std::move(on_finish_fn), std::move(row_set),
rows_limit, std::move(filter));
return;
}
if (!google::cloud::internal::IsEmpty(opts)) {
on_finish_fn(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."));
return;
}
bigtable_internal::LegacyAsyncRowReader::Create(
background_threads_->cq(), client_, app_profile_id(), table_name_,
std::move(on_row_fn), std::move(on_finish_fn), std::move(row_set),
rows_limit, std::move(filter), clone_rpc_retry_policy(),
clone_rpc_backoff_policy(), metadata_update_policy_,
std::make_unique<bigtable::internal::ReadRowsParserFactory>());
}
/**
* Asynchronously read and return a single row from the table.
*
* @param row_key the row to read.
* @param filter a filter expression, can be used to select a subset of the
* column families and columns in the row.
* @param opts (Optional) Override the class-level options, such as retry,
* backoff, and idempotency policies.
* @returns a future satisfied when the operation completes, fails
* permanently or keeps failing transiently, but the retry policy has been
* exhausted. The future will return a tuple. The first element is a
* boolean, with value `false` if the row does not exist. If the first
* element is `true` the second element has the contents of the Row. Note
* that the contents may be empty if the filter expression removes all
* column families and columns.
*
* @par Idempotency
* This is a read-only operation and therefore it is always idempotent.
*
* @par Thread-safety
* Two threads concurrently calling this member function on the same instance
* of this class are **not** guaranteed to work. Consider copying the object
* and using different copies in each thread.
*
* @par Example
* @snippet data_async_snippets.cc async read row
*/
future<StatusOr<std::pair<bool, Row>>> AsyncReadRow(std::string row_key,
Filter filter,
Options opts = {});
/**
* Constructor with default policies.
*
* @param client how to communicate with Cloud Bigtable, including
* credentials, the project id, and the instance id.
* @param table_id the table id within the instance defined by client. The
* full table name is `client->instance_name() + "/tables/" + table_id`.
*
* @deprecated #google::cloud::bigtable::DataConnection is the preferred way
* to communicate with the Bigtable Data API. To migrate existing code,
* see @ref migrating-from-dataclient "Migrating from DataClient".
*/
Table(std::shared_ptr<DataClient> client, std::string const& table_id)
: Table(std::move(client), std::string{}, table_id) {}
/**
* Constructor with default policies.
*
* @param client how to communicate with Cloud Bigtable, including
* credentials, the project id, and the instance id.
* @param app_profile_id the app_profile_id needed for using the replication
* API.
* @param table_id the table id within the instance defined by client. The
* full table name is `client->instance_name() + "/tables/" + table_id`.
*
* @deprecated #google::cloud::bigtable::DataConnection is the preferred way
* to communicate with the Bigtable Data API. To migrate existing code,
* see @ref migrating-from-dataclient "Migrating from DataClient".
*/
Table(std::shared_ptr<DataClient> client, std::string app_profile_id,
std::string const& table_id)
: client_(std::move(client)),
table_(client_->project_id(), client_->instance_id(), table_id),
table_name_(table_.FullName()),
rpc_retry_policy_prototype_(
bigtable::DefaultRPCRetryPolicy(internal::kBigtableLimits)),
rpc_backoff_policy_prototype_(
bigtable::DefaultRPCBackoffPolicy(internal::kBigtableLimits)),
idempotent_mutation_policy_(
bigtable::DefaultIdempotentMutationPolicy()),
background_threads_(client_->BackgroundThreadsFactory()()),
options_(Options{}.set<AppProfileIdOption>(std::move(app_profile_id))),
metadata_update_policy_(bigtable_internal::MakeMetadataUpdatePolicy(
table_name_, this->app_profile_id())) {}
/**
* Constructor with explicit policies.
*
* The policies are passed by value, because this makes it easy for
* applications to create them.
*
* @par Example
* @code
* using namespace std::chrono_literals; // assuming C++14.
* auto client = bigtable::MakeClient(...); // details omitted
* bigtable::Table table(client, "my-table",
* // Allow up to 20 minutes to retry operations
* bigtable::LimitedTimeRetryPolicy(20min),
* // Start with 50 milliseconds backoff, grow
* // exponentially to 5 minutes.
* bigtable::ExponentialBackoffPolicy(50ms, 5min),
* // Only retry idempotent mutations.
* bigtable::SafeIdempotentMutationPolicy());
* @endcode
*
* @param client how to communicate with Cloud Bigtable, including
* credentials, the project id, and the instance id.
* @param table_id the table id within the instance defined by client. The
* full table name is `client->instance_name() + "/tables/" + table_id`.
* @param policies the set of policy overrides for this object.
* @tparam Policies the types of the policies to override, the types must
* derive from one of the following types:
*
* - `IdempotentMutationPolicy` which mutations are retried. Use
* `SafeIdempotentMutationPolicy` to only retry idempotent operations,
* use `AlwaysRetryMutationPolicy` to retry all operations. Read the
* caveats in the class definition to understand the downsides of the
* latter. You can also create your own policies that decide which
* mutations to retry.
* - `RPCBackoffPolicy` how to backoff from a failed RPC. Currently only
* `ExponentialBackoffPolicy` is implemented. You can also create your
* own policies that backoff using a different algorithm.
* - `RPCRetryPolicy` for how long to retry failed RPCs. Use
* `LimitedErrorCountRetryPolicy` to limit the number of failures
* allowed. Use `LimitedTimeRetryPolicy` to bound the time for any
* request. You can also create your own policies that combine time and
* error counts.
*
* @see SafeIdempotentMutationPolicy, AlwaysRetryMutationPolicy,
* ExponentialBackoffPolicy, LimitedErrorCountRetryPolicy,
* LimitedTimeRetryPolicy.
*
* @deprecated #google::cloud::bigtable::DataConnection is the preferred way
* to communicate with the Bigtable Data API. To migrate existing code,
* see @ref migrating-from-dataclient "Migrating from DataClient".
*/
template <typename... Policies,
/// @cond implementation_details
std::enable_if_t<ValidPolicies<Policies...>::value, int> = 0
/// @endcond
>
Table(std::shared_ptr<DataClient> client, std::string const& table_id,
Policies&&... policies)
: Table(std::move(client), table_id) {
ChangePolicies(std::forward<Policies>(policies)...);
}
/**
* Constructor with explicit policies.
*
* The policies are passed by value, because this makes it easy for
* applications to create them.
*
* @par Example
* @code
* using namespace std::chrono_literals; // assuming C++14.
* auto client = bigtable::MakeClient(...); // details omitted
* bigtable::Table table(client, "app_id", "my-table",
* // Allow up to 20 minutes to retry operations
* bigtable::LimitedTimeRetryPolicy(20min),
* // Start with 50 milliseconds backoff, grow
* // exponentially to 5 minutes.
* bigtable::ExponentialBackoffPolicy(50ms, 5min),
* // Only retry idempotent mutations.
* bigtable::SafeIdempotentMutationPolicy());
* @endcode
*
* @param client how to communicate with Cloud Bigtable, including
* credentials, the project id, and the instance id.
* @param app_profile_id the app_profile_id needed for using the replication
* API.
* @param table_id the table id within the instance defined by client. The
* full table name is `client->instance_name() + "/tables/" + table_id`.
* @param policies the set of policy overrides for this object.
* @tparam Policies the types of the policies to override, the types must
* derive from one of the following types:
* - `IdempotentMutationPolicy` which mutations are retried. Use
* `SafeIdempotentMutationPolicy` to only retry idempotent operations,
* use `AlwaysRetryMutationPolicy` to retry all operations. Read the
* caveats in the class definition to understand the downsides of the
* latter. You can also create your own policies that decide which
* mutations to retry.
* - `RPCBackoffPolicy` how to backoff from a failed RPC. Currently only
* `ExponentialBackoffPolicy` is implemented. You can also create your
* own policies that backoff using a different algorithm.
* - `RPCRetryPolicy` for how long to retry failed RPCs. Use
* `LimitedErrorCountRetryPolicy` to limit the number of failures
* allowed. Use `LimitedTimeRetryPolicy` to bound the time for any
* request. You can also create your own policies that combine time and
* error counts.
*
* @see SafeIdempotentMutationPolicy, AlwaysRetryMutationPolicy,
* ExponentialBackoffPolicy, LimitedErrorCountRetryPolicy,
* LimitedTimeRetryPolicy.
*
* @deprecated #google::cloud::bigtable::DataConnection is the preferred way
* to communicate with the Bigtable Data API. To migrate existing code,
* see @ref migrating-from-dataclient "Migrating from DataClient".
*/
template <typename... Policies,
/// @cond implementation_details
std::enable_if_t<ValidPolicies<Policies...>::value, int> = 0
/// @endcond
>
Table(std::shared_ptr<DataClient> client, std::string app_profile_id,
std::string const& table_id, Policies&&... policies)
: Table(std::move(client), std::move(app_profile_id), table_id) {
ChangePolicies(std::forward<Policies>(policies)...);
}