google-cloud-spanner 2.0.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -0
  3. data/CONTRIBUTING.md +2 -2
  4. data/LOGGING.md +1 -1
  5. data/lib/google-cloud-spanner.rb +1 -0
  6. data/lib/google/cloud/spanner.rb +7 -5
  7. data/lib/google/cloud/spanner/backup.rb +10 -2
  8. data/lib/google/cloud/spanner/backup/job.rb +8 -8
  9. data/lib/google/cloud/spanner/backup/job/list.rb +2 -2
  10. data/lib/google/cloud/spanner/backup/list.rb +2 -2
  11. data/lib/google/cloud/spanner/batch_snapshot.rb +114 -15
  12. data/lib/google/cloud/spanner/batch_update.rb +3 -1
  13. data/lib/google/cloud/spanner/client.rb +413 -78
  14. data/lib/google/cloud/spanner/commit_response.rb +87 -0
  15. data/lib/google/cloud/spanner/commit_response/commit_stats.rb +51 -0
  16. data/lib/google/cloud/spanner/convert.rb +9 -0
  17. data/lib/google/cloud/spanner/data.rb +4 -5
  18. data/lib/google/cloud/spanner/database.rb +25 -3
  19. data/lib/google/cloud/spanner/database/backup_info.rb +12 -3
  20. data/lib/google/cloud/spanner/database/job/list.rb +2 -2
  21. data/lib/google/cloud/spanner/database/list.rb +4 -4
  22. data/lib/google/cloud/spanner/fields.rb +3 -2
  23. data/lib/google/cloud/spanner/instance/config/list.rb +4 -4
  24. data/lib/google/cloud/spanner/instance/list.rb +4 -4
  25. data/lib/google/cloud/spanner/partition.rb +4 -2
  26. data/lib/google/cloud/spanner/policy.rb +3 -2
  27. data/lib/google/cloud/spanner/pool.rb +10 -10
  28. data/lib/google/cloud/spanner/results.rb +105 -26
  29. data/lib/google/cloud/spanner/service.rb +218 -107
  30. data/lib/google/cloud/spanner/session.rb +361 -30
  31. data/lib/google/cloud/spanner/snapshot.rb +58 -4
  32. data/lib/google/cloud/spanner/status.rb +4 -1
  33. data/lib/google/cloud/spanner/transaction.rb +114 -8
  34. data/lib/google/cloud/spanner/version.rb +1 -1
  35. metadata +12 -10
@@ -142,7 +142,9 @@ module Google
142
142
 
143
143
  # @private
144
144
  class Statement
145
- attr_reader :sql, :params, :types
145
+ attr_reader :sql
146
+ attr_reader :params
147
+ attr_reader :types
146
148
 
147
149
  def initialize sql, params: nil, types: nil
148
150
  @sql = sql
@@ -23,6 +23,7 @@ require "google/cloud/spanner/snapshot"
23
23
  require "google/cloud/spanner/range"
24
24
  require "google/cloud/spanner/column_value"
25
25
  require "google/cloud/spanner/convert"
26
+ require "google/cloud/spanner/commit_response"
26
27
 
27
28
  module Google
28
29
  module Cloud
@@ -58,7 +59,7 @@ module Google
58
59
  @instance_id = instance_id
59
60
  @database_id = database_id
60
61
  @session_labels = session_labels
61
- @pool = Pool.new self, pool_opts
62
+ @pool = Pool.new self, **pool_opts
62
63
  @query_options = query_options
63
64
  end
64
65
 
@@ -222,6 +223,19 @@ module Google
222
223
  # * `:optimizer_version` (String) The version of optimizer to use.
223
224
  # Empty to use database default. "latest" to use the latest
224
225
  # available optimizer version.
226
+ # @param [Hash] call_options A hash of values to specify the custom
227
+ # call options, e.g., timeout, retries, etc. Call options are
228
+ # optional. The following settings can be provided:
229
+ #
230
+ # * `:timeout` (Numeric) A numeric value of custom timeout in seconds
231
+ # that overrides the default setting.
232
+ # * `:retry_policy` (Hash) A hash of values that overrides the default
233
+ # setting of retry policy with the following keys:
234
+ # * `:initial_delay` (`Numeric`) - The initial delay in seconds.
235
+ # * `:max_delay` (`Numeric`) - The max delay in seconds.
236
+ # * `:multiplier` (`Numeric`) - The incremental backoff multiplier.
237
+ # * `:retry_codes` (`Array<String>`) - The error codes that should
238
+ # trigger a retry.
225
239
  #
226
240
  # @return [Google::Cloud::Spanner::Results] The results of the query
227
241
  # execution.
@@ -335,8 +349,31 @@ module Google
335
349
  # puts "User #{row[:id]} is #{row[:name]}"
336
350
  # end
337
351
  #
352
+ # @example Query using custom timeout and retry policy:
353
+ # require "google/cloud/spanner"
354
+ #
355
+ # spanner = Google::Cloud::Spanner.new
356
+ #
357
+ # db = spanner.client "my-instance", "my-database"
358
+ #
359
+ # timeout = 30.0
360
+ # retry_policy = {
361
+ # initial_delay: 0.25,
362
+ # max_delay: 32.0,
363
+ # multiplier: 1.3,
364
+ # retry_codes: ["UNAVAILABLE"]
365
+ # }
366
+ # call_options = { timeout: timeout, retry_policy: retry_policy }
367
+ #
368
+ # results = db.execute_query \
369
+ # "SELECT * FROM users", call_options: call_options
370
+ #
371
+ # results.rows.each do |row|
372
+ # puts "User #{row[:id]} is #{row[:name]}"
373
+ # end
374
+ #
338
375
  def execute_query sql, params: nil, types: nil, single_use: nil,
339
- query_options: nil
376
+ query_options: nil, call_options: nil
340
377
  validate_single_use_args! single_use
341
378
  ensure_service!
342
379
 
@@ -347,7 +384,7 @@ module Google
347
384
  @pool.with_session do |session|
348
385
  results = session.execute_query \
349
386
  sql, params: params, types: types, transaction: single_use_tx,
350
- query_options: query_options
387
+ query_options: query_options, call_options: call_options
351
388
  end
352
389
  results
353
390
  end
@@ -474,14 +511,6 @@ module Google
474
511
  # value in `params`. In these cases, the `types` hash can be used to
475
512
  # specify the exact SQL type for some or all of the SQL query
476
513
  # parameters.
477
- # @param [Hash] query_options A hash of values to specify the custom
478
- # query options for executing SQL query. Query options are optional.
479
- # The following settings can be provided:
480
- #
481
- # * `:optimizer_version` (String) The version of optimizer to use.
482
- # Empty to use database default. "latest" to use the latest
483
- # available optimizer version.
484
- #
485
514
  #
486
515
  # The keys of the hash should be query string parameter placeholders,
487
516
  # minus the "@". The values of the hash should be Cloud Spanner type
@@ -499,6 +528,26 @@ module Google
499
528
  # `[:INT64]`.
500
529
  # * {Fields} - Nested Structs are specified by providing a Fields
501
530
  # object.
531
+ # @param [Hash] query_options A hash of values to specify the custom
532
+ # query options for executing SQL query. Query options are optional.
533
+ # The following settings can be provided:
534
+ #
535
+ # * `:optimizer_version` (String) The version of optimizer to use.
536
+ # Empty to use database default. "latest" to use the latest
537
+ # available optimizer version.
538
+ # @param [Hash] call_options A hash of values to specify the custom
539
+ # call options, e.g., timeout, retries, etc. Call options are
540
+ # optional. The following settings can be provided:
541
+ #
542
+ # * `:timeout` (Numeric) A numeric value of custom timeout in seconds
543
+ # that overrides the default setting.
544
+ # * `:retry_policy` (Hash) A hash of values that overrides the default
545
+ # setting of retry policy with the following keys:
546
+ # * `:initial_delay` (`Numeric`) - The initial delay in seconds.
547
+ # * `:max_delay` (`Numeric`) - The max delay in seconds.
548
+ # * `:multiplier` (`Numeric`) - The incremental backoff multiplier.
549
+ # * `:retry_codes` (`Array<String>`) - The error codes that should
550
+ # trigger a retry.
502
551
  # @return [Integer] The lower bound number of rows that were modified.
503
552
  #
504
553
  # @example
@@ -529,8 +578,28 @@ module Google
529
578
  # row_count = db.execute_partition_update \
530
579
  # "UPDATE users SET friends = NULL WHERE active = false",
531
580
  # query_options: { optimizer_version: "1" }
581
+ #
582
+ # @example Query using custom timeout and retry policy:
583
+ # require "google/cloud/spanner"
584
+ #
585
+ # spanner = Google::Cloud::Spanner.new
586
+ # db = spanner.client "my-instance", "my-database"
587
+ #
588
+ # timeout = 30.0
589
+ # retry_policy = {
590
+ # initial_delay: 0.25,
591
+ # max_delay: 32.0,
592
+ # multiplier: 1.3,
593
+ # retry_codes: ["UNAVAILABLE"]
594
+ # }
595
+ # call_options = { timeout: timeout, retry_policy: retry_policy }
596
+ #
597
+ # row_count = db.execute_partition_update \
598
+ # "UPDATE users SET friends = NULL WHERE active = false",
599
+ # call_options: call_options
600
+ #
532
601
  def execute_partition_update sql, params: nil, types: nil,
533
- query_options: nil
602
+ query_options: nil, call_options: nil
534
603
  ensure_service!
535
604
 
536
605
  params, types = Convert.to_input_params_and_types params, types
@@ -539,7 +608,7 @@ module Google
539
608
  results = session.execute_query \
540
609
  sql, params: params, types: types,
541
610
  transaction: pdml_transaction(session),
542
- query_options: query_options
611
+ query_options: query_options, call_options: call_options
543
612
  end
544
613
  # Stream all PartialResultSet to get ResultSetStats
545
614
  results.rows.to_a
@@ -620,6 +689,19 @@ module Google
620
689
  # Useful for reading the freshest data available at a nearby
621
690
  # replica, while bounding the possible staleness if the local
622
691
  # replica has fallen behind.
692
+ # @param [Hash] call_options A hash of values to specify the custom
693
+ # call options, e.g., timeout, retries, etc. Call options are
694
+ # optional. The following settings can be provided:
695
+ #
696
+ # * `:timeout` (Numeric) A numeric value of custom timeout in seconds
697
+ # that overrides the default setting.
698
+ # * `:retry_policy` (Hash) A hash of values that overrides the default
699
+ # setting of retry policy with the following keys:
700
+ # * `:initial_delay` (`Numeric`) - The initial delay in seconds.
701
+ # * `:max_delay` (`Numeric`) - The max delay in seconds.
702
+ # * `:multiplier` (`Numeric`) - The incremental backoff multiplier.
703
+ # * `:retry_codes` (`Array<String>`) - The error codes that should
704
+ # trigger a retry.
623
705
  #
624
706
  # @return [Google::Cloud::Spanner::Results] The results of the read.
625
707
  #
@@ -649,8 +731,30 @@ module Google
649
731
  # puts "User #{row[:id]} is #{row[:name]}"
650
732
  # end
651
733
  #
734
+ # @example Read using custom timeout and retry.
735
+ # require "google/cloud/spanner"
736
+ #
737
+ # spanner = Google::Cloud::Spanner.new
738
+ #
739
+ # db = spanner.client "my-instance", "my-database"
740
+ #
741
+ # timeout = 30.0
742
+ # retry_policy = {
743
+ # initial_delay: 0.25,
744
+ # max_delay: 32.0,
745
+ # multiplier: 1.3,
746
+ # retry_codes: ["UNAVAILABLE"]
747
+ # }
748
+ # call_options = { timeout: timeout, retry_policy: retry_policy }
749
+ #
750
+ # results = db.read "users", [:id, :name], call_options: call_options
751
+ #
752
+ # results.rows.each do |row|
753
+ # puts "User #{row[:id]} is #{row[:name]}"
754
+ # end
755
+ #
652
756
  def read table, columns, keys: nil, index: nil, limit: nil,
653
- single_use: nil
757
+ single_use: nil, call_options: nil
654
758
  validate_single_use_args! single_use
655
759
  ensure_service!
656
760
 
@@ -662,7 +766,8 @@ module Google
662
766
  @pool.with_session do |session|
663
767
  results = session.read \
664
768
  table, columns, keys: keys, index: index, limit: limit,
665
- transaction: single_use_tx
769
+ transaction: single_use_tx,
770
+ call_options: call_options
666
771
  end
667
772
  results
668
773
  end
@@ -705,7 +810,16 @@ module Google
705
810
  # See [Data
706
811
  # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
707
812
  #
708
- # @return [Time] The timestamp at which the operation committed.
813
+ # @param [Hash] commit_options A hash of commit options.
814
+ # e.g., return_commit_stats. Commit options are optional.
815
+ # The following options can be provided:
816
+ #
817
+ # * `:return_commit_stats` (Boolean) A boolean value. If `true`,
818
+ # then statistics related to the transaction will be included in
819
+ # {CommitResponse}. Default value is `false`
820
+ #
821
+ # @return [Time, CommitResponse] The timestamp at which the operation
822
+ # committed. If commit options are set it returns {CommitResponse}.
709
823
  #
710
824
  # @example
711
825
  # require "google/cloud/spanner"
@@ -717,9 +831,24 @@ module Google
717
831
  # db.upsert "users", [{ id: 1, name: "Charlie", active: false },
718
832
  # { id: 2, name: "Harvey", active: true }]
719
833
  #
720
- def upsert table, *rows
834
+ # @example Get commit stats
835
+ # require "google/cloud/spanner"
836
+ #
837
+ # spanner = Google::Cloud::Spanner.new
838
+ #
839
+ # db = spanner.client "my-instance", "my-database"
840
+ #
841
+ # records = [{ id: 1, name: "Charlie", active: false },
842
+ # { id: 2, name: "Harvey", active: true }]
843
+ # commit_options = { return_commit_stats: true }
844
+ # commit_resp = db.upsert "users", records, commit_options: commit_options
845
+ #
846
+ # puts commit_resp.timestamp
847
+ # puts commit_resp.stats.mutation_count
848
+ #
849
+ def upsert table, rows, commit_options: nil
721
850
  @pool.with_session do |session|
722
- session.upsert table, rows
851
+ session.upsert table, rows, commit_options: commit_options
723
852
  end
724
853
  end
725
854
  alias save upsert
@@ -761,7 +890,16 @@ module Google
761
890
  # See [Data
762
891
  # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
763
892
  #
764
- # @return [Time] The timestamp at which the operation committed.
893
+ # @param [Hash] commit_options A hash of commit options.
894
+ # e.g., return_commit_stats. Commit options are optional.
895
+ # The following options can be provided:
896
+ #
897
+ # * `:return_commit_stats` (Boolean) A boolean value. If `true`,
898
+ # then statistics related to the transaction will be included in
899
+ # {CommitResponse}. Default value is `false`
900
+ #
901
+ # @return [Time, CommitResponse] The timestamp at which the operation
902
+ # committed. If commit options are set it returns {CommitResponse}.
765
903
  #
766
904
  # @example
767
905
  # require "google/cloud/spanner"
@@ -773,9 +911,24 @@ module Google
773
911
  # db.insert "users", [{ id: 1, name: "Charlie", active: false },
774
912
  # { id: 2, name: "Harvey", active: true }]
775
913
  #
776
- def insert table, *rows
914
+ # @example Get commit stats
915
+ # require "google/cloud/spanner"
916
+ #
917
+ # spanner = Google::Cloud::Spanner.new
918
+ #
919
+ # db = spanner.client "my-instance", "my-database"
920
+ #
921
+ # records = [{ id: 1, name: "Charlie", active: false },
922
+ # { id: 2, name: "Harvey", active: true }]
923
+ # commit_options = { return_commit_stats: true }
924
+ # commit_resp = db.insert "users", records, commit_options: commit_options
925
+ #
926
+ # puts commit_resp.timestamp
927
+ # puts commit_resp.stats.mutation_count
928
+ #
929
+ def insert table, rows, commit_options: nil
777
930
  @pool.with_session do |session|
778
- session.insert table, rows
931
+ session.insert table, rows, commit_options: commit_options
779
932
  end
780
933
  end
781
934
 
@@ -816,7 +969,16 @@ module Google
816
969
  # See [Data
817
970
  # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
818
971
  #
819
- # @return [Time] The timestamp at which the operation committed.
972
+ # @param [Hash] commit_options A hash of commit options.
973
+ # e.g., return_commit_stats. Commit options are optional.
974
+ # The following options can be provided:
975
+ #
976
+ # * `:return_commit_stats` (Boolean) A boolean value. If `true`,
977
+ # then statistics related to the transaction will be included in
978
+ # {CommitResponse}. Default value is `false`
979
+ #
980
+ # @return [Time, CommitResponse] The timestamp at which the operation
981
+ # committed. If commit options are set it returns {CommitResponse}.
820
982
  #
821
983
  # @example
822
984
  # require "google/cloud/spanner"
@@ -828,9 +990,24 @@ module Google
828
990
  # db.update "users", [{ id: 1, name: "Charlie", active: false },
829
991
  # { id: 2, name: "Harvey", active: true }]
830
992
  #
831
- def update table, *rows
993
+ # @example Get commit stats
994
+ # require "google/cloud/spanner"
995
+ #
996
+ # spanner = Google::Cloud::Spanner.new
997
+ #
998
+ # db = spanner.client "my-instance", "my-database"
999
+ #
1000
+ # records = [{ id: 1, name: "Charlie", active: false },
1001
+ # { id: 2, name: "Harvey", active: true }]
1002
+ # commit_options = { return_commit_stats: true }
1003
+ # commit_resp = db.update "users", records, commit_options: commit_options
1004
+ #
1005
+ # puts commit_resp.timestamp
1006
+ # puts commit_resp.stats.mutation_count
1007
+ #
1008
+ def update table, rows, commit_options: nil
832
1009
  @pool.with_session do |session|
833
- session.update table, rows
1010
+ session.update table, rows, commit_options: commit_options
834
1011
  end
835
1012
  end
836
1013
 
@@ -873,7 +1050,16 @@ module Google
873
1050
  # See [Data
874
1051
  # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
875
1052
  #
876
- # @return [Time] The timestamp at which the operation committed.
1053
+ # @param [Hash] commit_options A hash of commit options.
1054
+ # e.g., return_commit_stats. Commit options are optional.
1055
+ # The following options can be provided:
1056
+ #
1057
+ # * `:return_commit_stats` (Boolean) A boolean value. If `true`,
1058
+ # then statistics related to the transaction will be included in
1059
+ # {CommitResponse}. Default value is `false`
1060
+ #
1061
+ # @return [Time, CommitResponse] The timestamp at which the operation
1062
+ # committed. If commit options are set it returns {CommitResponse}.
877
1063
  #
878
1064
  # @example
879
1065
  # require "google/cloud/spanner"
@@ -885,9 +1071,24 @@ module Google
885
1071
  # db.replace "users", [{ id: 1, name: "Charlie", active: false },
886
1072
  # { id: 2, name: "Harvey", active: true }]
887
1073
  #
888
- def replace table, *rows
1074
+ # @example Get commit stats
1075
+ # require "google/cloud/spanner"
1076
+ #
1077
+ # spanner = Google::Cloud::Spanner.new
1078
+ #
1079
+ # db = spanner.client "my-instance", "my-database"
1080
+ #
1081
+ # records = [{ id: 1, name: "Charlie", active: false },
1082
+ # { id: 2, name: "Harvey", active: true }]
1083
+ # commit_options = { return_commit_stats: true }
1084
+ # commit_resp = db.replace "users", records, commit_options: commit_options
1085
+ #
1086
+ # puts commit_resp.timestamp
1087
+ # puts commit_resp.stats.mutation_count
1088
+ #
1089
+ def replace table, rows, commit_options: nil
889
1090
  @pool.with_session do |session|
890
- session.replace table, rows
1091
+ session.replace table, rows, commit_options: commit_options
891
1092
  end
892
1093
  end
893
1094
 
@@ -911,8 +1112,30 @@ module Google
911
1112
  # @param [Object, Array<Object>] keys A single, or list of keys or key
912
1113
  # ranges to match returned data to. Values should have exactly as many
913
1114
  # elements as there are columns in the primary key.
914
- #
915
- # @return [Time] The timestamp at which the operation committed.
1115
+ # @param [Hash] commit_options A hash of commit options.
1116
+ # e.g., return_commit_stats. Commit options are optional.
1117
+ # The following options can be provided:
1118
+ #
1119
+ # * `:return_commit_stats` (Boolean) A boolean value. If `true`,
1120
+ # then statistics related to the transaction will be included in
1121
+ # {CommitResponse}. Default value is `false`
1122
+ #
1123
+ # @param [Hash] call_options A hash of values to specify the custom
1124
+ # call options, e.g., timeout, retries, etc. Call options are
1125
+ # optional. The following settings can be provided:
1126
+ #
1127
+ # * `:timeout` (Numeric) A numeric value of custom timeout in seconds
1128
+ # that overrides the default setting.
1129
+ # * `:retry_policy` (Hash) A hash of values that overrides the default
1130
+ # setting of retry policy with the following keys:
1131
+ # * `:initial_delay` (`Numeric`) - The initial delay in seconds.
1132
+ # * `:max_delay` (`Numeric`) - The max delay in seconds.
1133
+ # * `:multiplier` (`Numeric`) - The incremental backoff multiplier.
1134
+ # * `:retry_codes` (`Array<String>`) - The error codes that should
1135
+ # trigger a retry.
1136
+ #
1137
+ # @return [Time, CommitResponse] The timestamp at which the operation
1138
+ # committed. If commit options are set it returns {CommitResponse}.
916
1139
  #
917
1140
  # @example
918
1141
  # require "google/cloud/spanner"
@@ -923,9 +1146,23 @@ module Google
923
1146
  #
924
1147
  # db.delete "users", [1, 2, 3]
925
1148
  #
926
- def delete table, keys = []
1149
+ # @example Get commit stats
1150
+ # require "google/cloud/spanner"
1151
+ #
1152
+ # spanner = Google::Cloud::Spanner.new
1153
+ #
1154
+ # db = spanner.client "my-instance", "my-database"
1155
+ #
1156
+ # commit_options = { return_commit_stats: true }
1157
+ # commit_resp = db.delete "users", [1, 2, 3], commit_options: commit_options
1158
+ #
1159
+ # puts commit_resp.timestamp
1160
+ # puts commit_resp.stats.mutation_count
1161
+ #
1162
+ def delete table, keys = [], commit_options: nil, call_options: nil
927
1163
  @pool.with_session do |session|
928
- session.delete table, keys
1164
+ session.delete table, keys, commit_options: commit_options,
1165
+ call_options: call_options
929
1166
  end
930
1167
  end
931
1168
 
@@ -944,10 +1181,33 @@ module Google
944
1181
  # this method may be appropriate for latency sensitive and/or high
945
1182
  # throughput blind changes.
946
1183
  #
1184
+ # @param [Hash] commit_options A hash of commit options.
1185
+ # e.g., return_commit_stats. Commit options are optional.
1186
+ # The following options can be provided:
1187
+ #
1188
+ # * `:return_commit_stats` (Boolean) A boolean value. If `true`,
1189
+ # then statistics related to the transaction will be included in
1190
+ # {CommitResponse}. Default value is `false`
1191
+ #
1192
+ # @param [Hash] call_options A hash of values to specify the custom
1193
+ # call options, e.g., timeout, retries, etc. Call options are
1194
+ # optional. The following settings can be provided:
1195
+ #
1196
+ # * `:timeout` (Numeric) A numeric value of custom timeout in seconds
1197
+ # that overrides the default setting.
1198
+ # * `:retry_policy` (Hash) A hash of values that overrides the default
1199
+ # setting of retry policy with the following keys:
1200
+ # * `:initial_delay` (`Numeric`) - The initial delay in seconds.
1201
+ # * `:max_delay` (`Numeric`) - The max delay in seconds.
1202
+ # * `:multiplier` (`Numeric`) - The incremental backoff multiplier.
1203
+ # * `:retry_codes` (`Array<String>`) - The error codes that should
1204
+ # trigger a retry.
1205
+ #
947
1206
  # @yield [commit] The block for mutating the data.
948
1207
  # @yieldparam [Google::Cloud::Spanner::Commit] commit The Commit object.
949
1208
  #
950
- # @return [Time] The timestamp at which the operation committed.
1209
+ # @return [Time, CommitResponse] The timestamp at which the operation
1210
+ # committed. If commit options are set it returns {CommitResponse}.
951
1211
  #
952
1212
  # @example
953
1213
  # require "google/cloud/spanner"
@@ -961,11 +1221,29 @@ module Google
961
1221
  # c.insert "users", [{ id: 2, name: "Harvey", active: true }]
962
1222
  # end
963
1223
  #
964
- def commit &block
1224
+ # @example Get commit stats
1225
+ # require "google/cloud/spanner"
1226
+ #
1227
+ # spanner = Google::Cloud::Spanner.new
1228
+ #
1229
+ # db = spanner.client "my-instance", "my-database"
1230
+ #
1231
+ # commit_options = { return_commit_stats: true }
1232
+ # commit_resp = db.commit commit_options: commit_options do |c|
1233
+ # c.update "users", [{ id: 1, name: "Charlie", active: false }]
1234
+ # c.insert "users", [{ id: 2, name: "Harvey", active: true }]
1235
+ # end
1236
+ #
1237
+ # puts commit_resp.timestamp
1238
+ # puts commit_resp.stats.mutation_count
1239
+ #
1240
+ def commit commit_options: nil, call_options: nil, &block
965
1241
  raise ArgumentError, "Must provide a block" unless block_given?
966
1242
 
967
1243
  @pool.with_session do |session|
968
- session.commit(&block)
1244
+ session.commit(
1245
+ commit_options: commit_options, call_options: call_options, &block
1246
+ )
969
1247
  end
970
1248
  end
971
1249
 
@@ -988,12 +1266,34 @@ module Google
988
1266
  #
989
1267
  # @param [Numeric] deadline The total amount of time in seconds the
990
1268
  # transaction has to succeed. The default is `120`.
1269
+ # @param [Hash] commit_options A hash of commit options.
1270
+ # e.g., return_commit_stats. Commit options are optional.
1271
+ # The following options can be provided:
1272
+ #
1273
+ # * `:return_commit_stats` (Boolean) A boolean value. If `true`,
1274
+ # then statistics related to the transaction will be included in
1275
+ # {CommitResponse}. Default value is `false`
1276
+ #
1277
+ # @param [Hash] call_options A hash of values to specify the custom
1278
+ # call options, e.g., timeout, retries, etc. Call options are
1279
+ # optional. The following settings can be provided:
1280
+ #
1281
+ # * `:timeout` (Numeric) A numeric value of custom timeout in seconds
1282
+ # that overrides the default setting.
1283
+ # * `:retry_policy` (Hash) A hash of values that overrides the default
1284
+ # setting of retry policy with the following keys:
1285
+ # * `:initial_delay` (`Numeric`) - The initial delay in seconds.
1286
+ # * `:max_delay` (`Numeric`) - The max delay in seconds.
1287
+ # * `:multiplier` (`Numeric`) - The incremental backoff multiplier.
1288
+ # * `:retry_codes` (`Array<String>`) - The error codes that should
1289
+ # trigger a retry.
991
1290
  #
992
1291
  # @yield [transaction] The block for reading and writing data.
993
1292
  # @yieldparam [Google::Cloud::Spanner::Transaction] transaction The
994
1293
  # Transaction object.
995
1294
  #
996
- # @return [Time] The timestamp at which the transaction committed.
1295
+ # @return [Time, CommitResponse] The timestamp at which the operation
1296
+ # committed. If commit options are set it returns {CommitResponse}.
997
1297
  #
998
1298
  # @example
999
1299
  # require "google/cloud/spanner"
@@ -1029,7 +1329,28 @@ module Google
1029
1329
  # end
1030
1330
  # end
1031
1331
  #
1032
- def transaction deadline: 120
1332
+ # @example Get commit stats
1333
+ # require "google/cloud/spanner"
1334
+ #
1335
+ # spanner = Google::Cloud::Spanner.new
1336
+ # db = spanner.client "my-instance", "my-database"
1337
+ #
1338
+ # commit_options = { return_commit_stats: true }
1339
+ # commit_resp = db.transaction commit_options: commit_options do |tx|
1340
+ # results = tx.execute_query "SELECT * FROM users"
1341
+ #
1342
+ # results.rows.each do |row|
1343
+ # puts "User #{row[:id]} is #{row[:name]}"
1344
+ # end
1345
+ #
1346
+ # tx.update "users", [{ id: 1, name: "Charlie", active: false }]
1347
+ # tx.insert "users", [{ id: 2, name: "Harvey", active: true }]
1348
+ # end
1349
+ #
1350
+ # puts commit_resp.timestamp
1351
+ # puts commit_resp.stats.mutation_count
1352
+ #
1353
+ def transaction deadline: 120, commit_options: nil, call_options: nil
1033
1354
  ensure_service!
1034
1355
  unless Thread.current[:transaction_id].nil?
1035
1356
  raise "Nested transactions are not allowed"
@@ -1040,35 +1361,37 @@ module Google
1040
1361
  start_time = current_time
1041
1362
 
1042
1363
  @pool.with_transaction do |tx|
1043
- begin
1044
- Thread.current[:transaction_id] = tx.transaction_id
1045
- yield tx
1046
- commit_resp = @project.service.commit \
1047
- tx.session.path, tx.mutations, transaction_id: tx.transaction_id
1048
- return Convert.timestamp_to_time commit_resp.commit_timestamp
1049
- rescue GRPC::Aborted, Google::Cloud::AbortedError => err
1050
- # Re-raise if deadline has passed
1051
- if current_time - start_time > deadline
1052
- if err.is_a? GRPC::BadStatus
1053
- err = Google::Cloud::Error.from_error err
1054
- end
1055
- raise err
1364
+ Thread.current[:transaction_id] = tx.transaction_id
1365
+ yield tx
1366
+ commit_resp = @project.service.commit \
1367
+ tx.session.path, tx.mutations,
1368
+ transaction_id: tx.transaction_id,
1369
+ commit_options: commit_options,
1370
+ call_options: call_options
1371
+ resp = CommitResponse.from_grpc commit_resp
1372
+ commit_options ? resp : resp.timestamp
1373
+ rescue GRPC::Aborted, Google::Cloud::AbortedError => e
1374
+ # Re-raise if deadline has passed
1375
+ if current_time - start_time > deadline
1376
+ if e.is_a? GRPC::BadStatus
1377
+ e = Google::Cloud::Error.from_error e
1056
1378
  end
1057
- # Sleep the amount from RetryDelay, or incremental backoff
1058
- sleep(delay_from_aborted(err) || backoff *= 1.3)
1059
- # Create new transaction on the session and retry the block
1060
- tx = tx.session.create_transaction
1061
- retry
1062
- rescue StandardError => err
1063
- # Rollback transaction when handling unexpected error
1064
- tx.session.rollback tx.transaction_id
1065
- # Return nil if raised with rollback.
1066
- return nil if err.is_a? Rollback
1067
- # Re-raise error.
1068
- raise err
1069
- ensure
1070
- Thread.current[:transaction_id] = nil
1379
+ raise e
1071
1380
  end
1381
+ # Sleep the amount from RetryDelay, or incremental backoff
1382
+ sleep(delay_from_aborted(e) || backoff *= 1.3)
1383
+ # Create new transaction on the session and retry the block
1384
+ tx = tx.session.create_transaction
1385
+ retry
1386
+ rescue StandardError => e
1387
+ # Rollback transaction when handling unexpected error
1388
+ tx.session.rollback tx.transaction_id
1389
+ # Return nil if raised with rollback.
1390
+ return nil if e.is_a? Rollback
1391
+ # Re-raise error.
1392
+ raise e
1393
+ ensure
1394
+ Thread.current[:transaction_id] = nil
1072
1395
  end
1073
1396
  end
1074
1397
 
@@ -1109,6 +1432,19 @@ module Google
1109
1432
  # timestamp negotiation overhead of single-use `staleness`. (See
1110
1433
  # [TransactionOptions](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#transactionoptions).)
1111
1434
  # @param [Numeric] exact_staleness Same as `staleness`.
1435
+ # @param [Hash] call_options A hash of values to specify the custom
1436
+ # call options, e.g., timeout, retries, etc. Call options are
1437
+ # optional. The following settings can be provided:
1438
+ #
1439
+ # * `:timeout` (Numeric) A numeric value of custom timeout in seconds
1440
+ # that overrides the default setting.
1441
+ # * `:retry_policy` (Hash) A hash of values that overrides the default
1442
+ # setting of retry policy with the following keys:
1443
+ # * `:initial_delay` (`Numeric`) - The initial delay in seconds.
1444
+ # * `:max_delay` (`Numeric`) - The max delay in seconds.
1445
+ # * `:multiplier` (`Numeric`) - The incremental backoff multiplier.
1446
+ # * `:retry_codes` (`Array<String>`) - The error codes that should
1447
+ # trigger a retry.
1112
1448
  #
1113
1449
  # @yield [snapshot] The block for reading and writing data.
1114
1450
  # @yieldparam [Google::Cloud::Spanner::Snapshot] snapshot The Snapshot
@@ -1129,7 +1465,7 @@ module Google
1129
1465
  # end
1130
1466
  #
1131
1467
  def snapshot strong: nil, timestamp: nil, read_timestamp: nil,
1132
- staleness: nil, exact_staleness: nil
1468
+ staleness: nil, exact_staleness: nil, call_options: nil
1133
1469
  validate_snapshot_args! strong: strong, timestamp: timestamp,
1134
1470
  read_timestamp: read_timestamp,
1135
1471
  staleness: staleness,
@@ -1141,17 +1477,16 @@ module Google
1141
1477
  end
1142
1478
 
1143
1479
  @pool.with_session do |session|
1144
- begin
1145
- snp_grpc = @project.service.create_snapshot \
1146
- session.path, strong: strong,
1147
- timestamp: (timestamp || read_timestamp),
1148
- staleness: (staleness || exact_staleness)
1149
- Thread.current[:transaction_id] = snp_grpc.id
1150
- snp = Snapshot.from_grpc snp_grpc, session
1151
- yield snp if block_given?
1152
- ensure
1153
- Thread.current[:transaction_id] = nil
1154
- end
1480
+ snp_grpc = @project.service.create_snapshot \
1481
+ session.path, strong: strong,
1482
+ timestamp: (timestamp || read_timestamp),
1483
+ staleness: (staleness || exact_staleness),
1484
+ call_options: call_options
1485
+ Thread.current[:transaction_id] = snp_grpc.id
1486
+ snp = Snapshot.from_grpc snp_grpc, session
1487
+ yield snp if block_given?
1488
+ ensure
1489
+ Thread.current[:transaction_id] = nil
1155
1490
  end
1156
1491
  nil
1157
1492
  end
@@ -1350,7 +1685,7 @@ module Google
1350
1685
  def batch_create_new_sessions total
1351
1686
  sessions = []
1352
1687
  remaining = total
1353
- while remaining > 0
1688
+ while remaining.positive?
1354
1689
  sessions += batch_create_sessions remaining
1355
1690
  remaining = total - sessions.count
1356
1691
  end
@@ -1454,7 +1789,7 @@ module Google
1454
1789
 
1455
1790
  def validate_deadline deadline
1456
1791
  return 120 unless deadline.is_a? Numeric
1457
- return 120 if deadline < 0
1792
+ return 120 if deadline.negative?
1458
1793
  deadline
1459
1794
  end
1460
1795
 
@@ -1475,7 +1810,7 @@ module Google
1475
1810
  seconds = err.metadata["retryDelay"]["seconds"].to_i
1476
1811
  nanos = err.metadata["retryDelay"]["nanos"].to_i
1477
1812
  return seconds if nanos.zero?
1478
- return seconds + (nanos / 1000000000.0)
1813
+ return seconds + (nanos / 1_000_000_000.0)
1479
1814
  end
1480
1815
  # No metadata? Try the inner error
1481
1816
  delay_from_aborted err.cause